From f93369b84e4dd53e1d084d322a5e534c899fd38b Mon Sep 17 00:00:00 2001
From: Ravi Kumar <rkumar@gitlab.com>
Date: Tue, 26 Mar 2024 17:57:41 +0000
Subject: [PATCH] Add missing attributes in the search/count endpoint

Added the payload in the ELastic Kibana for the search/count endpoint.
This data can be used for analyzing the count endpoint.

MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/147796
Changelog: other
---
 app/controllers/search_controller.rb       | 23 ++++++++++-------
 spec/controllers/search_controller_spec.rb | 29 ++++++++++++++++++++++
 2 files changed, 43 insertions(+), 9 deletions(-)

diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb
index 896b71d282227..f120237697952 100644
--- a/app/controllers/search_controller.rb
+++ b/app/controllers/search_controller.rb
@@ -100,18 +100,23 @@ def count
 
     scope = search_service.scope
 
+    @search_level = search_service.level
+    @search_type = search_type
+
     count = 0
-    ApplicationRecord.with_fast_read_statement_timeout do
-      count = search_service.search_results.formatted_count(scope)
-    end
+    @global_search_duration_s = Benchmark.realtime do
+      ApplicationRecord.with_fast_read_statement_timeout do
+        count = search_service.search_results.formatted_count(scope)
+      end
 
-    # Users switching tabs will keep fetching the same tab counts so it's a
-    # good idea to cache in their browser just for a short time. They can still
-    # clear cache if they are seeing an incorrect count but inaccurate count is
-    # not such a bad thing.
-    expires_in 1.minute
+      # Users switching tabs will keep fetching the same tab counts so it's a
+      # good idea to cache in their browser just for a short time. They can still
+      # clear cache if they are seeing an incorrect count but inaccurate count is
+      # not such a bad thing.
+      expires_in 1.minute
 
-    render json: { count: count }
+      render json: { count: count }
+    end
   end
 
   def autocomplete
diff --git a/spec/controllers/search_controller_spec.rb b/spec/controllers/search_controller_spec.rb
index 10fe15558c51d..7f5b2653fa5c0 100644
--- a/spec/controllers/search_controller_spec.rb
+++ b/spec/controllers/search_controller_spec.rb
@@ -26,6 +26,33 @@
       end
     end
 
+    shared_examples_for 'metadata is set' do |action|
+      it 'renders a 408 when a timeout occurs' do
+        expect(controller).to receive(:append_info_to_payload).and_wrap_original do |method, payload|
+          method.call(payload)
+
+          expect(payload[:metadata]['meta.search.group_id']).to eq('123')
+          expect(payload[:metadata]['meta.search.project_id']).to eq('456')
+          expect(payload[:metadata]).not_to have_key('meta.search.search')
+          expect(payload[:metadata]['meta.search.scope']).to eq('issues')
+          expect(payload[:metadata]['meta.search.force_search_results']).to eq('true')
+          expect(payload[:metadata]['meta.search.filters.confidential']).to eq('true')
+          expect(payload[:metadata]['meta.search.filters.state']).to eq('true')
+          expect(payload[:metadata]['meta.search.project_ids']).to eq(%w[456 789])
+          expect(payload[:metadata]['meta.search.type']).to eq('basic')
+          expect(payload[:metadata]['meta.search.level']).to eq('global')
+          expect(payload[:metadata]['meta.search.filters.language']).to eq('ruby')
+          expect(payload[:metadata]['meta.search.page']).to eq('2')
+          expect(payload[:metadata][:global_search_duration_s]).to be_a_kind_of(Numeric)
+        end
+        params = {
+          scope: 'issues', search: 'hello world', group_id: '123', page: '2', project_id: '456', language: 'ruby',
+          project_ids: %w[456 789], confidential: true, include_archived: true, state: true, force_search_results: true
+        }
+        get action, params: params
+      end
+    end
+
     describe 'GET #show', :snowplow do
       it_behaves_like 'when the user cannot read cross project', :show, { search: 'hello' } do
         it 'still allows accessing the search page' do
@@ -37,6 +64,7 @@
 
       it_behaves_like 'with external authorization service enabled', :show, { search: 'hello' }
       it_behaves_like 'support for active record query timeouts', :show, { search: 'hello' }, :search_objects, :html
+      it_behaves_like 'metadata is set', :show
 
       describe 'rate limit scope' do
         it 'uses current_user and search scope' do
@@ -387,6 +415,7 @@ def request
       it_behaves_like 'when the user cannot read cross project', :count, { search: 'hello', scope: 'projects' }
       it_behaves_like 'with external authorization service enabled', :count, { search: 'hello', scope: 'projects' }
       it_behaves_like 'support for active record query timeouts', :count, { search: 'hello', scope: 'projects' }, :search_results, :json
+      it_behaves_like 'metadata is set', :count
 
       it 'returns the result count for the given term and scope' do
         create(:project, :public, name: 'hello world')
-- 
GitLab