Commit 036a0518 authored by charlie ablett's avatar charlie ablett

Merge branch '214250-usage-ping-counts-for-all-search' into 'master'

Resolve "Usage Ping counts for all Search"

See merge request gitlab-org/gitlab!32111
parents 5f46aca7 17ad1372
...@@ -33,7 +33,7 @@ class SearchController < ApplicationController ...@@ -33,7 +33,7 @@ class SearchController < ApplicationController
render_commits if @scope == 'commits' render_commits if @scope == 'commits'
eager_load_user_status if @scope == 'users' eager_load_user_status if @scope == 'users'
increment_navbar_searches_counter increment_search_counters
check_single_commit_result check_single_commit_result
end end
...@@ -98,9 +98,11 @@ class SearchController < ApplicationController ...@@ -98,9 +98,11 @@ class SearchController < ApplicationController
end end
end end
def increment_navbar_searches_counter def increment_search_counters
Gitlab::UsageDataCounters::SearchCounter.count(:all_searches)
return if params[:nav_source] != 'navbar' return if params[:nav_source] != 'navbar'
Gitlab::UsageDataCounters::SearchCounter.increment_navbar_searches_count Gitlab::UsageDataCounters::SearchCounter.count(:navbar_searches)
end end
end end
---
title: Add Usage Ping count for all searches
merge_request: 32111
author:
type: changed
...@@ -36,6 +36,8 @@ module API ...@@ -36,6 +36,8 @@ module API
results = SearchService.new(current_user, search_params).search_objects(preload_method) results = SearchService.new(current_user, search_params).search_objects(preload_method)
Gitlab::UsageDataCounters::SearchCounter.count(:all_searches)
paginate(results) paginate(results)
end end
......
...@@ -8,7 +8,7 @@ module Gitlab::UsageDataCounters ...@@ -8,7 +8,7 @@ module Gitlab::UsageDataCounters
class << self class << self
def redis_key(event) def redis_key(event)
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(UnknownEvent.new, event: event) unless known_events.include?(event.to_s) require_known_event(event)
"USAGE_#{prefix}_#{event}".upcase "USAGE_#{prefix}_#{event}".upcase
end end
...@@ -31,6 +31,10 @@ module Gitlab::UsageDataCounters ...@@ -31,6 +31,10 @@ module Gitlab::UsageDataCounters
private private
def require_known_event(event)
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(UnknownEvent.new, event: event) unless known_events.include?(event.to_s)
end
def counter_key(event) def counter_key(event)
"#{prefix}_#{event}".to_sym "#{prefix}_#{event}".to_sym
end end
......
...@@ -4,7 +4,7 @@ module Gitlab::UsageDataCounters ...@@ -4,7 +4,7 @@ module Gitlab::UsageDataCounters
class DesignsCounter class DesignsCounter
extend Gitlab::UsageDataCounters::RedisCounter extend Gitlab::UsageDataCounters::RedisCounter
KNOWN_EVENTS = %w[create update delete].map(&:freeze).freeze KNOWN_EVENTS = %w[create update delete].freeze
UnknownEvent = Class.new(StandardError) UnknownEvent = Class.new(StandardError)
......
...@@ -2,28 +2,20 @@ ...@@ -2,28 +2,20 @@
module Gitlab module Gitlab
module UsageDataCounters module UsageDataCounters
class SearchCounter class SearchCounter < BaseCounter
extend RedisCounter KNOWN_EVENTS = %w[all_searches navbar_searches].freeze
NAVBAR_SEARCHES_COUNT_KEY = 'NAVBAR_SEARCHES_COUNT'
class << self class << self
def increment_navbar_searches_count def redis_key(event)
increment(NAVBAR_SEARCHES_COUNT_KEY) require_known_event(event)
end
def total_navbar_searches_count "#{event}_COUNT".upcase
total_count(NAVBAR_SEARCHES_COUNT_KEY)
end end
def totals private
{
navbar_searches: total_navbar_searches_count
}
end
def fallback_totals def counter_key(event)
{ navbar_searches: -1 } "#{event}".to_sym
end end
end end
end end
......
...@@ -14,7 +14,8 @@ describe 'Global search' do ...@@ -14,7 +14,8 @@ describe 'Global search' do
end end
it 'increases usage ping searches counter' do it 'increases usage ping searches counter' do
expect(Gitlab::UsageDataCounters::SearchCounter).to receive(:increment_navbar_searches_count) expect(Gitlab::UsageDataCounters::SearchCounter).to receive(:count).with(:navbar_searches)
expect(Gitlab::UsageDataCounters::SearchCounter).to receive(:count).with(:all_searches)
submit_search('foobar') submit_search('foobar')
end end
......
...@@ -3,11 +3,21 @@ ...@@ -3,11 +3,21 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::UsageDataCounters::SearchCounter, :clean_gitlab_redis_shared_state do describe Gitlab::UsageDataCounters::SearchCounter, :clean_gitlab_redis_shared_state do
it 'increments counter and return the total count' do shared_examples_for 'usage counter with totals' do |counter|
expect(described_class.total_navbar_searches_count).to eq(0) it 'increments counter and returns total count' do
expect(described_class.read(counter)).to eq(0)
2.times { described_class.increment_navbar_searches_count } 2.times { described_class.count(counter) }
expect(described_class.total_navbar_searches_count).to eq(2) expect(described_class.read(counter)).to eq(2)
end
end
context 'all_searches counter' do
it_behaves_like 'usage counter with totals', :all_searches
end
context 'navbar_searches counter' do
it_behaves_like 'usage counter with totals', :navbar_searches
end end
end end
...@@ -15,6 +15,14 @@ describe API::Search do ...@@ -15,6 +15,14 @@ describe API::Search do
it { expect(json_response.size).to eq(size) } it { expect(json_response.size).to eq(size) }
end end
shared_examples 'ping counters' do |scope:, search: ''|
it 'increases usage ping searches counter' do
expect(Gitlab::UsageDataCounters::SearchCounter).to receive(:count).with(:all_searches)
get api(endpoint, user), params: { scope: scope, search: search }
end
end
shared_examples 'pagination' do |scope:, search: ''| shared_examples 'pagination' do |scope:, search: ''|
it 'returns a different result for each page' do it 'returns a different result for each page' do
get api(endpoint, user), params: { scope: scope, search: search, page: 1, per_page: 1 } get api(endpoint, user), params: { scope: scope, search: search, page: 1, per_page: 1 }
...@@ -75,6 +83,8 @@ describe API::Search do ...@@ -75,6 +83,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/projects' it_behaves_like 'response is correct', schema: 'public_api/v4/projects'
it_behaves_like 'pagination', scope: :projects it_behaves_like 'pagination', scope: :projects
it_behaves_like 'ping counters', scope: :projects
end end
context 'for issues scope' do context 'for issues scope' do
...@@ -86,6 +96,8 @@ describe API::Search do ...@@ -86,6 +96,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/issues' it_behaves_like 'response is correct', schema: 'public_api/v4/issues'
it_behaves_like 'ping counters', scope: :issues
describe 'pagination' do describe 'pagination' do
before do before do
create(:issue, project: project, title: 'another issue') create(:issue, project: project, title: 'another issue')
...@@ -104,6 +116,8 @@ describe API::Search do ...@@ -104,6 +116,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/merge_requests' it_behaves_like 'response is correct', schema: 'public_api/v4/merge_requests'
it_behaves_like 'ping counters', scope: :merge_requests
describe 'pagination' do describe 'pagination' do
before do before do
create(:merge_request, source_project: repo_project, title: 'another mr', target_branch: 'another_branch') create(:merge_request, source_project: repo_project, title: 'another mr', target_branch: 'another_branch')
...@@ -125,6 +139,8 @@ describe API::Search do ...@@ -125,6 +139,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/milestones' it_behaves_like 'response is correct', schema: 'public_api/v4/milestones'
it_behaves_like 'ping counters', scope: :milestones
describe 'pagination' do describe 'pagination' do
before do before do
create(:milestone, project: project, title: 'another milestone') create(:milestone, project: project, title: 'another milestone')
...@@ -161,6 +177,8 @@ describe API::Search do ...@@ -161,6 +177,8 @@ describe API::Search do
it_behaves_like 'pagination', scope: :users it_behaves_like 'pagination', scope: :users
it_behaves_like 'ping counters', scope: :users
context 'when users search feature is disabled' do context 'when users search feature is disabled' do
before do before do
stub_feature_flags(users_search: false) stub_feature_flags(users_search: false)
...@@ -183,6 +201,8 @@ describe API::Search do ...@@ -183,6 +201,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/snippets' it_behaves_like 'response is correct', schema: 'public_api/v4/snippets'
it_behaves_like 'ping counters', scope: :snippet_titles
describe 'pagination' do describe 'pagination' do
before do before do
create(:snippet, :public, title: 'another snippet', content: 'snippet content') create(:snippet, :public, title: 'another snippet', content: 'snippet content')
...@@ -248,6 +268,8 @@ describe API::Search do ...@@ -248,6 +268,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/projects' it_behaves_like 'response is correct', schema: 'public_api/v4/projects'
it_behaves_like 'pagination', scope: :projects it_behaves_like 'pagination', scope: :projects
it_behaves_like 'ping counters', scope: :projects
end end
context 'for issues scope' do context 'for issues scope' do
...@@ -259,6 +281,8 @@ describe API::Search do ...@@ -259,6 +281,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/issues' it_behaves_like 'response is correct', schema: 'public_api/v4/issues'
it_behaves_like 'ping counters', scope: :issues
describe 'pagination' do describe 'pagination' do
before do before do
create(:issue, project: project, title: 'another issue') create(:issue, project: project, title: 'another issue')
...@@ -277,6 +301,8 @@ describe API::Search do ...@@ -277,6 +301,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/merge_requests' it_behaves_like 'response is correct', schema: 'public_api/v4/merge_requests'
it_behaves_like 'ping counters', scope: :merge_requests
describe 'pagination' do describe 'pagination' do
before do before do
create(:merge_request, source_project: repo_project, title: 'another mr', target_branch: 'another_branch') create(:merge_request, source_project: repo_project, title: 'another mr', target_branch: 'another_branch')
...@@ -295,6 +321,8 @@ describe API::Search do ...@@ -295,6 +321,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/milestones' it_behaves_like 'response is correct', schema: 'public_api/v4/milestones'
it_behaves_like 'ping counters', scope: :milestones
describe 'pagination' do describe 'pagination' do
before do before do
create(:milestone, project: project, title: 'another milestone') create(:milestone, project: project, title: 'another milestone')
...@@ -326,6 +354,8 @@ describe API::Search do ...@@ -326,6 +354,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/user/basics' it_behaves_like 'response is correct', schema: 'public_api/v4/user/basics'
it_behaves_like 'ping counters', scope: :users
describe 'pagination' do describe 'pagination' do
before do before do
create(:group_member, :developer, group: group) create(:group_member, :developer, group: group)
...@@ -395,7 +425,7 @@ describe API::Search do ...@@ -395,7 +425,7 @@ describe API::Search do
end end
end end
context 'when user does can not see the project' do context 'when user can not see the project' do
it 'returns 404 error' do it 'returns 404 error' do
project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE) project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
...@@ -415,6 +445,8 @@ describe API::Search do ...@@ -415,6 +445,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/issues' it_behaves_like 'response is correct', schema: 'public_api/v4/issues'
it_behaves_like 'ping counters', scope: :issues
describe 'pagination' do describe 'pagination' do
before do before do
create(:issue, project: project, title: 'another issue') create(:issue, project: project, title: 'another issue')
...@@ -435,6 +467,8 @@ describe API::Search do ...@@ -435,6 +467,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/merge_requests' it_behaves_like 'response is correct', schema: 'public_api/v4/merge_requests'
it_behaves_like 'ping counters', scope: :merge_requests
describe 'pagination' do describe 'pagination' do
before do before do
create(:merge_request, source_project: repo_project, title: 'another mr', target_branch: 'another_branch') create(:merge_request, source_project: repo_project, title: 'another mr', target_branch: 'another_branch')
...@@ -456,6 +490,8 @@ describe API::Search do ...@@ -456,6 +490,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/milestones' it_behaves_like 'response is correct', schema: 'public_api/v4/milestones'
it_behaves_like 'ping counters', scope: :milestones
describe 'pagination' do describe 'pagination' do
before do before do
create(:milestone, project: project, title: 'another milestone') create(:milestone, project: project, title: 'another milestone')
...@@ -491,6 +527,8 @@ describe API::Search do ...@@ -491,6 +527,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/user/basics' it_behaves_like 'response is correct', schema: 'public_api/v4/user/basics'
it_behaves_like 'ping counters', scope: :users
describe 'pagination' do describe 'pagination' do
before do before do
create(:project_member, :developer, project: project) create(:project_member, :developer, project: project)
...@@ -521,6 +559,8 @@ describe API::Search do ...@@ -521,6 +559,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/notes' it_behaves_like 'response is correct', schema: 'public_api/v4/notes'
it_behaves_like 'ping counters', scope: :notes
describe 'pagination' do describe 'pagination' do
before do before do
mr = create(:merge_request, source_project: project, target_branch: 'another_branch') mr = create(:merge_request, source_project: project, target_branch: 'another_branch')
...@@ -542,6 +582,8 @@ describe API::Search do ...@@ -542,6 +582,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/blobs' it_behaves_like 'response is correct', schema: 'public_api/v4/blobs'
it_behaves_like 'ping counters', scope: :wiki_blobs
describe 'pagination' do describe 'pagination' do
before do before do
create(:wiki_page, wiki: wiki, title: 'home 2', content: 'Another page') create(:wiki_page, wiki: wiki, title: 'home 2', content: 'Another page')
...@@ -561,6 +603,8 @@ describe API::Search do ...@@ -561,6 +603,8 @@ describe API::Search do
it_behaves_like 'response is correct', schema: 'public_api/v4/commits_details' it_behaves_like 'response is correct', schema: 'public_api/v4/commits_details'
it_behaves_like 'pagination', scope: :commits, search: 'merge' it_behaves_like 'pagination', scope: :commits, search: 'merge'
it_behaves_like 'ping counters', scope: :commits
end end
context 'for commits scope with project path as id' do context 'for commits scope with project path as id' do
...@@ -582,6 +626,8 @@ describe API::Search do ...@@ -582,6 +626,8 @@ describe API::Search do
it_behaves_like 'pagination', scope: :blobs, search: 'monitors' it_behaves_like 'pagination', scope: :blobs, search: 'monitors'
it_behaves_like 'ping counters', scope: :blobs
context 'filters' do context 'filters' do
it 'by filename' do it 'by filename' do
get api(endpoint, user), params: { scope: 'blobs', search: 'mon filename:PROCESS.md' } get api(endpoint, user), params: { scope: 'blobs', search: 'mon filename:PROCESS.md' }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment