From 3cdb3e4eaf6589a6d1621769e793f5dbcc045fca Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Mon, 11 Sep 2023 23:29:52 +0530 Subject: [PATCH] struct destructuring for DocumentsAggregator --- .../src/analytics/segment_analytics.rs | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/meilisearch/src/analytics/segment_analytics.rs b/meilisearch/src/analytics/segment_analytics.rs index 0208b8d77..23337a4fe 100644 --- a/meilisearch/src/analytics/segment_analytics.rs +++ b/meilisearch/src/analytics/segment_analytics.rs @@ -1282,14 +1282,17 @@ impl DocumentsAggregator { index_creation: bool, request: &HttpRequest, ) -> Self { + let UpdateDocumentsQuery { primary_key, csv_delimiter: _ } = documents_query; + let mut ret = Self::default(); ret.timestamp = Some(OffsetDateTime::now_utc()); ret.updated = true; ret.user_agents = extract_user_agents(request).into_iter().collect(); - if let Some(primary_key) = documents_query.primary_key.clone() { + if let Some(primary_key) = primary_key.clone() { ret.primary_keys.insert(primary_key); } + let content_type = request .headers() .get(CONTENT_TYPE) @@ -1304,37 +1307,43 @@ impl DocumentsAggregator { /// Aggregate one [DocumentsAggregator] into another. pub fn aggregate(&mut self, other: Self) { + let Self { timestamp, user_agents, primary_keys, content_types, index_creation, updated } = + other; + if self.timestamp.is_none() { - self.timestamp = other.timestamp; + self.timestamp = timestamp; } - self.updated |= other.updated; + self.updated |= updated; // we can't create a union because there is no `into_union` method - for user_agent in other.user_agents { + for user_agent in user_agents { self.user_agents.insert(user_agent); } - for primary_key in other.primary_keys { + for primary_key in primary_keys { self.primary_keys.insert(primary_key); } - for content_type in other.content_types { + for content_type in content_types { self.content_types.insert(content_type); } - self.index_creation |= other.index_creation; + self.index_creation |= index_creation; } pub fn into_event(self, user: &User, event_name: &str) -> Option { - if !self.updated { + let Self { timestamp, user_agents, primary_keys, content_types, index_creation, updated } = + self; + + if !updated { None } else { let properties = json!({ - "user-agent": self.user_agents, - "payload_type": self.content_types, - "primary_key": self.primary_keys, - "index_creation": self.index_creation, + "user-agent": user_agents, + "payload_type": content_types, + "primary_key": primary_keys, + "index_creation": index_creation, }); Some(Track { - timestamp: self.timestamp, + timestamp: timestamp, user: user.clone(), event: event_name.to_string(), properties,