Skip to content

Commit 6841e36

Browse files
committed
fix(analytics): use feature name instead of feature id
1 parent a2b9e2b commit 6841e36

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/flagsmith/analytics.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ static ANALYTICS_TIMER_IN_MILLI: u64 = 10 * 1000;
1010

1111
#[derive(Clone, Debug)]
1212
pub struct AnalyticsProcessor {
13-
pub tx: Sender<u32>,
14-
_analytics_data: Arc<RwLock<HashMap<u32, u32>>>,
13+
pub tx: Sender<String>,
14+
_analytics_data: Arc<RwLock<HashMap<String, u32>>>,
1515
}
1616

1717
impl AnalyticsProcessor {
@@ -21,7 +21,7 @@ impl AnalyticsProcessor {
2121
timeout: std::time::Duration,
2222
timer: Option<u64>,
2323
) -> Self {
24-
let (tx, rx) = mpsc::channel::<u32>();
24+
let (tx, rx) = mpsc::channel::<String>();
2525
let client = reqwest::blocking::Client::builder()
2626
.default_headers(headers)
2727
.timeout(timeout)
@@ -30,7 +30,7 @@ impl AnalyticsProcessor {
3030
let analytics_endpoint = format!("{}analytics/flags/", api_url);
3131
let timer = timer.unwrap_or(ANALYTICS_TIMER_IN_MILLI);
3232

33-
let analytics_data_arc: Arc<RwLock<HashMap<u32, u32>>> =
33+
let analytics_data_arc: Arc<RwLock<HashMap<String, u32>>> =
3434
Arc::new(RwLock::new(HashMap::new()));
3535

3636
let analytics_data_locked = Arc::clone(&analytics_data_arc);
@@ -43,9 +43,9 @@ impl AnalyticsProcessor {
4343
let mut analytics_data = analytics_data_locked.write().unwrap();
4444
match data {
4545
// Update the analytics data with feature_id received
46-
Ok(feature_id) => {
46+
Ok(feature_name) => {
4747
analytics_data
48-
.entry(feature_id)
48+
.entry(feature_name)
4949
.and_modify(|e| *e += 1)
5050
.or_insert(1);
5151
}
@@ -69,14 +69,14 @@ impl AnalyticsProcessor {
6969
_analytics_data: Arc::clone(&analytics_data_arc),
7070
};
7171
}
72-
pub fn track_feature(&self, feature_id: u32) {
73-
self.tx.send(feature_id).unwrap();
72+
pub fn track_feature(&self, feature_name: &str) {
73+
self.tx.send(feature_name.to_string()).unwrap();
7474
}
7575
}
7676

7777
fn flush(
7878
client: &reqwest::blocking::Client,
79-
analytics_data: &HashMap<u32, u32>,
79+
analytics_data: &HashMap<String, u32>,
8080
analytics_endpoint: &str,
8181
) {
8282
if analytics_data.len() == 0 {
@@ -98,7 +98,7 @@ mod tests {
9898
#[test]
9999
fn track_feature_updates_analytics_data() {
100100
// Given
101-
let feature_1 = 1;
101+
let feature_1 = "feature_1";
102102
let processor = AnalyticsProcessor::new(
103103
"http://localhost".to_string(),
104104
header::HeaderMap::new(),
@@ -112,20 +112,20 @@ mod tests {
112112
thread::sleep(std::time::Duration::from_millis(50));
113113
let analytics_data = processor._analytics_data.read().unwrap();
114114
// Then, verify that analytics_data was updated correctly
115-
assert_eq!(analytics_data[&feature_1], 2);
115+
assert_eq!(analytics_data[feature_1], 2);
116116
}
117117

118118
#[test]
119119
fn test_analytics_processor() {
120120
// Given
121-
let feature_1 = 1;
122-
let feature_2 = 2;
121+
let feature_1 = "feature_1";
122+
let feature_2 = "feature_2";
123123
let server = MockServer::start();
124124
let first_invocation_mock = server.mock(|when, then| {
125125
when.method(POST)
126126
.path("/api/v1/analytics/flags/")
127127
.header("X-Environment-Key", "ser.UiYoRr6zUjiFBUXaRwo7b5")
128-
.json_body(serde_json::json!({feature_1.to_string():10, feature_2.to_string():10}));
128+
.json_body(serde_json::json!({feature_1:10, feature_2:10}));
129129
then.status(200).header("content-type", "application/json");
130130
});
131131
let mut headers = header::HeaderMap::new();
@@ -143,8 +143,8 @@ mod tests {
143143
);
144144
// Now, let's update the analytics data
145145
let mut analytics_data = processor._analytics_data.write().unwrap();
146-
analytics_data.insert(1, 10);
147-
analytics_data.insert(2, 10);
146+
analytics_data.insert(feature_1.to_string(), 10);
147+
analytics_data.insert(feature_2.to_string(), 10);
148148
// drop the analytics data to release the lock
149149
drop(analytics_data);
150150
// Next, let's sleep a little to let the processor flush the data

src/flagsmith/models.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl Flags {
141141
.as_ref()
142142
.unwrap()
143143
.tx
144-
.send(flag.feature_id);
144+
.send(flag.feature_name.clone());
145145
};
146146
return Ok(flag.clone());
147147
}

0 commit comments

Comments
 (0)