fix the 99th percentile in the analytics

This commit is contained in:
Irevoire 2021-11-10 16:52:24 +01:00
parent e9b6a05b75
commit cc5e884b34
No known key found for this signature in database
GPG Key ID: 7A6A970C96104F1B

View File

@ -1,4 +1,4 @@
use std::collections::{HashMap, HashSet};
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::fs;
use std::path::Path;
use std::sync::Arc;
@ -286,7 +286,7 @@ pub struct SearchAggregator {
// requests
total_received: usize,
total_succeeded: usize,
time_spent: Vec<usize>,
time_spent: BinaryHeap<usize>,
// sort
sort_with_geo_point: bool,
@ -398,17 +398,21 @@ impl SearchAggregator {
self.max_offset = self.max_offset.max(other.max_offset);
}
pub fn into_event(mut self, user: &User, event_name: &str) -> Option<Track> {
pub fn into_event(self, user: &User, event_name: &str) -> Option<Track> {
if self.total_received == 0 {
None
} else {
// the index of the 99th percentage of value
let percentile_99th = 0.99 * (self.total_succeeded as f64 - 1.) + 1.;
self.time_spent.drain(percentile_99th as usize..);
// we get all the values in a sorted manner
let time_spent = self.time_spent.into_sorted_vec();
// We are only intersted by the slowest value of the 99th fastest results
let time_spent = time_spent[percentile_99th as usize];
let properties = json!({
"user-agent": self.user_agents,
"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}", time_spent),
"total_succeeded": self.total_succeeded,
"total_failed": self.total_received.saturating_sub(self.total_succeeded), // just to be sure we never panics
"total_received": self.total_received,