Skip to main content

statsig_rust/event_logging/event_queue/
queued_secondary_expo.rs

1use crate::{
2    event_logging::{
3        exposure_sampling::{EvtSamplingDecision, ExposureSamplingKey},
4        exposure_utils::get_statsig_metadata_with_sampling_decision,
5        statsig_event::StatsigEvent,
6        statsig_event_internal::{StatsigEventInternal, GATE_EXPOSURE_EVENT_NAME},
7    },
8    user::StatsigUserLoggable,
9    SecondaryExposure,
10};
11
12use super::queued_event::{EnqueueOperation, QueuedEvent, QueuedExposure};
13use crate::event_logging::statsig_event::string_metadata_to_value_metadata;
14use std::collections::HashMap;
15
16pub struct EnqueueSecondaryExposureAsPrimaryOp {
17    pub user: StatsigUserLoggable,
18    pub secondary_exposure: SecondaryExposure,
19    pub exposure_time: u64,
20}
21
22impl EnqueueOperation for EnqueueSecondaryExposureAsPrimaryOp {
23    fn as_exposure(&self) -> Option<&impl QueuedExposure<'_>> {
24        Some(self)
25    }
26
27    fn into_queued_event(self, sampling_decision: EvtSamplingDecision) -> QueuedEvent {
28        QueuedEvent::SecondaryExposureAsPrimary(QueuedSecondaryExposureAsPrimaryEvent {
29            user: self.user,
30            secondary_exposure: self.secondary_exposure,
31            exposure_time: self.exposure_time,
32            sampling_decision,
33        })
34    }
35}
36
37pub struct QueuedSecondaryExposureAsPrimaryEvent {
38    pub user: StatsigUserLoggable,
39    pub secondary_exposure: SecondaryExposure,
40    pub exposure_time: u64,
41    pub sampling_decision: EvtSamplingDecision,
42}
43
44impl QueuedSecondaryExposureAsPrimaryEvent {
45    pub fn into_statsig_event_internal(self) -> StatsigEventInternal {
46        let metadata = HashMap::from([
47            (
48                "gate".into(),
49                self.secondary_exposure.gate.unperformant_to_string(),
50            ),
51            (
52                "gateValue".into(),
53                self.secondary_exposure.gate_value.unperformant_to_string(),
54            ),
55            (
56                "ruleID".into(),
57                self.secondary_exposure.rule_id.unperformant_to_string(),
58            ),
59        ]);
60
61        let event = StatsigEvent {
62            event_name: GATE_EXPOSURE_EVENT_NAME.into(),
63            value: None,
64            metadata: Some(string_metadata_to_value_metadata(metadata)),
65            statsig_metadata: Some(get_statsig_metadata_with_sampling_decision(
66                self.sampling_decision,
67            )),
68        };
69
70        StatsigEventInternal::new(self.exposure_time, self.user, event, Some(Vec::new()))
71    }
72}
73
74impl<'a> QueuedExposure<'a> for EnqueueSecondaryExposureAsPrimaryOp {
75    fn create_exposure_sampling_key(&self) -> ExposureSamplingKey {
76        ExposureSamplingKey {
77            spec_name_hash: self.secondary_exposure.gate.hash,
78            rule_id_hash: self.secondary_exposure.rule_id.hash,
79            user_values_hash: self.user.data.create_exposure_dedupe_user_hash(None),
80            additional_hash: (self.secondary_exposure.gate_value.as_str() == "true") as u64,
81        }
82    }
83
84    fn get_rule_id_ref(&'a self) -> &'a str {
85        self.secondary_exposure.rule_id.as_str()
86    }
87
88    fn get_extra_exposure_info_ref(
89        &'a self,
90    ) -> Option<&'a crate::evaluation::evaluation_types::ExtraExposureInfo> {
91        None
92    }
93}