From 904bae98f83e7fa0a2ceb888ac5fd5f1d76a8ab2 Mon Sep 17 00:00:00 2001
From: Tamo <tamo@meilisearch.com>
Date: Tue, 2 Nov 2021 12:38:01 +0100
Subject: [PATCH] send the analytics even when the search fail

---
 .../src/analytics/mock_analytics.rs           |  2 +-
 .../src/analytics/segment_analytics.rs        |  2 +-
 meilisearch-http/src/routes/indexes/search.rs | 26 ++++++++++---------
 3 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/meilisearch-http/src/analytics/mock_analytics.rs b/meilisearch-http/src/analytics/mock_analytics.rs
index eb26add26..01838f223 100644
--- a/meilisearch-http/src/analytics/mock_analytics.rs
+++ b/meilisearch-http/src/analytics/mock_analytics.rs
@@ -18,7 +18,7 @@ impl SearchAggregator {
         Self::default()
     }
 
-    pub fn finish(&mut self, _: &dyn Any) {}
+    pub fn succeed(&mut self, _: &dyn Any) {}
 }
 
 impl MockAnalytics {
diff --git a/meilisearch-http/src/analytics/segment_analytics.rs b/meilisearch-http/src/analytics/segment_analytics.rs
index 8fbea3eb8..8d81bfacf 100644
--- a/meilisearch-http/src/analytics/segment_analytics.rs
+++ b/meilisearch-http/src/analytics/segment_analytics.rs
@@ -364,7 +364,7 @@ impl SearchAggregator {
         ret
     }
 
-    pub fn finish(&mut self, result: &SearchResult) {
+    pub fn succeed(&mut self, result: &SearchResult) {
         self.total_succeeded += 1;
         self.time_spent.push(result.processing_time_ms as usize);
     }
diff --git a/meilisearch-http/src/routes/indexes/search.rs b/meilisearch-http/src/routes/indexes/search.rs
index 5f7a91673..ea115efdd 100644
--- a/meilisearch-http/src/routes/indexes/search.rs
+++ b/meilisearch-http/src/routes/indexes/search.rs
@@ -118,17 +118,18 @@ pub async fn search_with_url_query(
 
     let mut aggregate = SearchAggregator::from_query(&query, &req);
 
-    let search_result = meilisearch
-        .search(path.into_inner().index_uid, query)
-        .await?;
+    let search_result = meilisearch.search(path.into_inner().index_uid, query).await;
+    if let Ok(ref search_result) = search_result {
+        aggregate.succeed(search_result);
+    }
+    analytics.get_search(aggregate);
+
+    let search_result = search_result?;
 
     // Tests that the nb_hits is always set to false
     #[cfg(test)]
     assert!(!search_result.exhaustive_nb_hits);
 
-    aggregate.finish(&search_result);
-    analytics.get_search(aggregate);
-
     debug!("returns: {:?}", search_result);
     Ok(HttpResponse::Ok().json(search_result))
 }
@@ -145,17 +146,18 @@ pub async fn search_with_post(
 
     let mut aggregate = SearchAggregator::from_query(&query, &req);
 
-    let search_result = meilisearch
-        .search(path.into_inner().index_uid, query)
-        .await?;
+    let search_result = meilisearch.search(path.into_inner().index_uid, query).await;
+    if let Ok(ref search_result) = search_result {
+        aggregate.succeed(search_result);
+    }
+    analytics.get_search(aggregate);
+
+    let search_result = search_result?;
 
     // Tests that the nb_hits is always set to false
     #[cfg(test)]
     assert!(!search_result.exhaustive_nb_hits);
 
-    aggregate.finish(&search_result);
-    analytics.post_search(aggregate);
-
     debug!("returns: {:?}", search_result);
     Ok(HttpResponse::Ok().json(search_result))
 }