simplify the into_events methods

This commit is contained in:
Tamo 2021-10-25 16:52:15 +02:00 committed by marin postma
parent 392ee86714
commit a9523146a3
No known key found for this signature in database
GPG Key ID: 6088B7721C3E39F9

View File

@ -153,28 +153,18 @@ mod segment {
}) })
.await; .await;
} }
println!("ANALYTICS: taking the lock on the search batcher"); println!("ANALYTICS: taking the lock on the search batchers");
let get_search = std::mem::take(&mut *self.get_search_batcher.lock().await); let get_search = std::mem::take(&mut *self.get_search_batcher.lock().await)
let get_search = (get_search.total_received != 0).then(|| { .into_event(&self.user, "Document Searched GET");
get_search let post_search = std::mem::take(&mut *self.post_search_batcher.lock().await)
.into_event(self.user.clone(), "Document Searched GET".to_string()) .into_event(&self.user, "Document Searched POST");
}); println!("ANALYTICS: taking the lock on the documents batchers");
let post_search = std::mem::take(&mut *self.post_search_batcher.lock().await);
let post_search = (post_search.total_received != 0).then(|| {
post_search
.into_event(self.user.clone(), "Document Searched POST".to_string())
});
let add_documents = let add_documents =
std::mem::take(&mut *self.documents_added_batcher.lock().await); std::mem::take(&mut *self.documents_added_batcher.lock().await)
let add_documents = (add_documents.updated).then(|| { .into_event(&self.user, "Documents Added");
add_documents.into_event(self.user.clone(), "Documents Added".to_string())
});
let update_documents = let update_documents =
std::mem::take(&mut *self.documents_updated_batcher.lock().await); std::mem::take(&mut *self.documents_updated_batcher.lock().await)
let update_documents = (update_documents.updated).then(|| { .into_event(&self.user, "Documents Updated");
update_documents
.into_event(self.user.clone(), "Documents Updated".to_string())
});
// keep the lock on the batcher just for these three operations // keep the lock on the batcher just for these three operations
{ {
println!("ANALYTICS: taking the lock on the batcher"); println!("ANALYTICS: taking the lock on the batcher");
@ -458,7 +448,10 @@ mod segment {
.collect() .collect()
} }
pub fn into_event(mut self, user: User, event_name: String) -> Track { pub fn into_event(mut self, user: &User, event_name: &str) -> Option<Track> {
if self.total_received == 0 {
None
} else {
let context = Some(json!({ "user-agent": self.user_agents})); let context = Some(json!({ "user-agent": self.user_agents}));
let percentile_99th = 0.99 * (self.total_succeeded as f64 - 1.) + 1.; let percentile_99th = 0.99 * (self.total_succeeded as f64 - 1.) + 1.;
self.time_spent.drain(percentile_99th as usize..); self.time_spent.drain(percentile_99th as usize..);
@ -488,12 +481,13 @@ mod segment {
}, },
}); });
Track { Some(Track {
user, user: user.clone(),
event: event_name, event: event_name.to_string(),
context, context,
properties, properties,
..Default::default() ..Default::default()
})
} }
} }
} }
@ -512,7 +506,10 @@ mod segment {
} }
impl DocumentsBatcher { impl DocumentsBatcher {
pub fn into_event(mut self, user: User, event_name: String) -> Track { pub fn into_event(mut self, user: &User, event_name: &str) -> Option<Track> {
if self.updated {
None
} else {
let context = Some(json!({ "user-agent": self.user_agents})); let context = Some(json!({ "user-agent": self.user_agents}));
let properties = json!({ let properties = json!({
@ -521,12 +518,13 @@ mod segment {
"index_creation": self.index_creation, "index_creation": self.index_creation,
}); });
Track { Some(Track {
user, user: user.clone(),
event: event_name, event: event_name.to_string(),
context, context,
properties, properties,
..Default::default() ..Default::default()
})
} }
} }
} }