From 40bf98711c3eb09ee39e4ad01fd0666768853306 Mon Sep 17 00:00:00 2001 From: Tamo Date: Thu, 20 Jan 2022 19:01:37 +0100 Subject: [PATCH] feat(analytics): Set the timestamp of the aggregated event as the first aggregate --- .../src/analytics/segment_analytics.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/meilisearch-http/src/analytics/segment_analytics.rs b/meilisearch-http/src/analytics/segment_analytics.rs index 86be0f432..597da7523 100644 --- a/meilisearch-http/src/analytics/segment_analytics.rs +++ b/meilisearch-http/src/analytics/segment_analytics.rs @@ -6,6 +6,7 @@ use std::time::{Duration, Instant}; use actix_web::http::header::USER_AGENT; use actix_web::HttpRequest; +use chrono::{DateTime, Utc}; use http::header::CONTENT_TYPE; use meilisearch_lib::index::{SearchQuery, SearchResult}; use meilisearch_lib::index_controller::Stats; @@ -301,6 +302,8 @@ impl Segment { #[derive(Default)] pub struct SearchAggregator { + timestamp: Option>, + // context user_agents: HashSet, @@ -336,6 +339,8 @@ pub struct SearchAggregator { impl SearchAggregator { pub fn from_query(query: &SearchQuery, request: &HttpRequest) -> Self { let mut ret = Self::default(); + ret.timestamp = Some(chrono::offset::Utc::now()); + ret.total_received = 1; ret.user_agents = extract_user_agents(request).into_iter().collect(); @@ -389,6 +394,10 @@ impl SearchAggregator { /// Aggregate one [SearchAggregator] into another. pub fn aggregate(&mut self, mut other: Self) { + if self.timestamp.is_none() { + self.timestamp = other.timestamp; + } + // context for user_agent in other.user_agents.into_iter() { self.user_agents.insert(user_agent); @@ -462,6 +471,7 @@ impl SearchAggregator { }); Some(Track { + timestamp: self.timestamp, user: user.clone(), event: event_name.to_string(), properties, @@ -473,6 +483,8 @@ impl SearchAggregator { #[derive(Default)] pub struct DocumentsAggregator { + timestamp: Option>, + // set to true when at least one request was received updated: bool, @@ -491,6 +503,7 @@ impl DocumentsAggregator { request: &HttpRequest, ) -> Self { let mut ret = Self::default(); + ret.timestamp = Some(chrono::offset::Utc::now()); ret.updated = true; ret.user_agents = extract_user_agents(request).into_iter().collect(); @@ -511,6 +524,10 @@ impl DocumentsAggregator { /// Aggregate one [DocumentsAggregator] into another. pub fn aggregate(&mut self, other: Self) { + if self.timestamp.is_none() { + self.timestamp = other.timestamp; + } + self.updated |= other.updated; // we can't create a union because there is no `into_union` method for user_agent in other.user_agents.into_iter() { @@ -537,6 +554,7 @@ impl DocumentsAggregator { }); Some(Track { + timestamp: self.timestamp, user: user.clone(), event: event_name.to_string(), properties,