feat(analytics): Set the timestamp of the aggregated event as the first aggregate

This commit is contained in:
Tamo 2022-01-20 19:01:37 +01:00
parent f9f075bca2
commit 40bf98711c
No known key found for this signature in database
GPG Key ID: 20CD8020AFA88D69

View File

@ -6,6 +6,7 @@ use std::time::{Duration, Instant};
use actix_web::http::header::USER_AGENT; use actix_web::http::header::USER_AGENT;
use actix_web::HttpRequest; use actix_web::HttpRequest;
use chrono::{DateTime, Utc};
use http::header::CONTENT_TYPE; use http::header::CONTENT_TYPE;
use meilisearch_lib::index::{SearchQuery, SearchResult}; use meilisearch_lib::index::{SearchQuery, SearchResult};
use meilisearch_lib::index_controller::Stats; use meilisearch_lib::index_controller::Stats;
@ -301,6 +302,8 @@ impl Segment {
#[derive(Default)] #[derive(Default)]
pub struct SearchAggregator { pub struct SearchAggregator {
timestamp: Option<DateTime<Utc>>,
// context // context
user_agents: HashSet<String>, user_agents: HashSet<String>,
@ -336,6 +339,8 @@ pub struct SearchAggregator {
impl SearchAggregator { impl SearchAggregator {
pub fn from_query(query: &SearchQuery, request: &HttpRequest) -> Self { pub fn from_query(query: &SearchQuery, request: &HttpRequest) -> Self {
let mut ret = Self::default(); let mut ret = Self::default();
ret.timestamp = Some(chrono::offset::Utc::now());
ret.total_received = 1; ret.total_received = 1;
ret.user_agents = extract_user_agents(request).into_iter().collect(); ret.user_agents = extract_user_agents(request).into_iter().collect();
@ -389,6 +394,10 @@ impl SearchAggregator {
/// Aggregate one [SearchAggregator] into another. /// Aggregate one [SearchAggregator] into another.
pub fn aggregate(&mut self, mut other: Self) { pub fn aggregate(&mut self, mut other: Self) {
if self.timestamp.is_none() {
self.timestamp = other.timestamp;
}
// context // context
for user_agent in other.user_agents.into_iter() { for user_agent in other.user_agents.into_iter() {
self.user_agents.insert(user_agent); self.user_agents.insert(user_agent);
@ -462,6 +471,7 @@ impl SearchAggregator {
}); });
Some(Track { Some(Track {
timestamp: self.timestamp,
user: user.clone(), user: user.clone(),
event: event_name.to_string(), event: event_name.to_string(),
properties, properties,
@ -473,6 +483,8 @@ impl SearchAggregator {
#[derive(Default)] #[derive(Default)]
pub struct DocumentsAggregator { pub struct DocumentsAggregator {
timestamp: Option<DateTime<Utc>>,
// set to true when at least one request was received // set to true when at least one request was received
updated: bool, updated: bool,
@ -491,6 +503,7 @@ impl DocumentsAggregator {
request: &HttpRequest, request: &HttpRequest,
) -> Self { ) -> Self {
let mut ret = Self::default(); let mut ret = Self::default();
ret.timestamp = Some(chrono::offset::Utc::now());
ret.updated = true; ret.updated = true;
ret.user_agents = extract_user_agents(request).into_iter().collect(); ret.user_agents = extract_user_agents(request).into_iter().collect();
@ -511,6 +524,10 @@ impl DocumentsAggregator {
/// Aggregate one [DocumentsAggregator] into another. /// Aggregate one [DocumentsAggregator] into another.
pub fn aggregate(&mut self, other: Self) { pub fn aggregate(&mut self, other: Self) {
if self.timestamp.is_none() {
self.timestamp = other.timestamp;
}
self.updated |= other.updated; self.updated |= other.updated;
// we can't create a union because there is no `into_union` method // we can't create a union because there is no `into_union` method
for user_agent in other.user_agents.into_iter() { for user_agent in other.user_agents.into_iter() {
@ -537,6 +554,7 @@ impl DocumentsAggregator {
}); });
Some(Track { Some(Track {
timestamp: self.timestamp,
user: user.clone(), user: user.clone(),
event: event_name.to_string(), event: event_name.to_string(),
properties, properties,