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,42 +448,46 @@ 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> {
let context = Some(json!({ "user-agent": self.user_agents})); if self.total_received == 0 {
let percentile_99th = 0.99 * (self.total_succeeded as f64 - 1.) + 1.; None
self.time_spent.drain(percentile_99th as usize..); } else {
let context = Some(json!({ "user-agent": self.user_agents}));
let percentile_99th = 0.99 * (self.total_succeeded as f64 - 1.) + 1.;
self.time_spent.drain(percentile_99th as usize..);
let properties = json!({ let properties = json!({
"requests": { "requests": {
"99th_response_time": format!("{:.2}", self.time_spent.iter().sum::<usize>() as f64 / self.time_spent.len() as f64), "99th_response_time": format!("{:.2}", self.time_spent.iter().sum::<usize>() as f64 / self.time_spent.len() as f64),
"total_succeeded": self.total_succeeded, "total_succeeded": self.total_succeeded,
"total_failed": self.total_received.saturating_sub(self.total_succeeded), // just to be sure we never panics "total_failed": self.total_received.saturating_sub(self.total_succeeded), // just to be sure we never panics
"total_received": self.total_received, "total_received": self.total_received,
}, },
"sort": { "sort": {
"with_geoPoint": self.sort_with_geo_point, "with_geoPoint": self.sort_with_geo_point,
"avg_criteria_number": format!("{:.2}", self.sort_sum_of_criteria_terms as f64 / self.sort_total_number_of_criteria as f64), "avg_criteria_number": format!("{:.2}", self.sort_sum_of_criteria_terms as f64 / self.sort_total_number_of_criteria as f64),
}, },
"filter": { "filter": {
"with_geoRadius": self.filter_with_geo_radius, "with_geoRadius": self.filter_with_geo_radius,
"avg_criteria_number": format!("{:.2}", self.filter_sum_of_criteria_terms as f64 / self.filter_total_number_of_criteria as f64), "avg_criteria_number": format!("{:.2}", self.filter_sum_of_criteria_terms as f64 / self.filter_total_number_of_criteria as f64),
"most_used_syntax": self.used_syntax.iter().max_by_key(|(_, v)| *v).map(|(k, _)| json!(k)).unwrap_or_else(|| json!(null)), "most_used_syntax": self.used_syntax.iter().max_by_key(|(_, v)| *v).map(|(k, _)| json!(k)).unwrap_or_else(|| json!(null)),
}, },
"q": { "q": {
"avg_terms_number": format!("{:.2}", self.sum_of_terms_count as f64 / self.total_number_of_q as f64), "avg_terms_number": format!("{:.2}", self.sum_of_terms_count as f64 / self.total_number_of_q as f64),
}, },
"pagination": { "pagination": {
"max_limit": self.max_limit, "max_limit": self.max_limit,
"max_offset": self.max_offset, "max_offset": self.max_offset,
}, },
}); });
Track { Some(Track {
user, user: user.clone(),
event: event_name, event: event_name.to_string(),
context, context,
properties, properties,
..Default::default() ..Default::default()
})
} }
} }
} }
@ -512,21 +506,25 @@ 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> {
let context = Some(json!({ "user-agent": self.user_agents})); if self.updated {
None
} else {
let context = Some(json!({ "user-agent": self.user_agents}));
let properties = json!({ let properties = json!({
"payload_type": self.content_types, "payload_type": self.content_types,
"primary_key": self.primary_keys, "primary_key": self.primary_keys,
"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()
})
} }
} }
} }