Track SFE actions in BlobController

In this commit we track Single File Editor actions
when a blob is created or updated.
parent 146c43c9
...@@ -10,6 +10,8 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -10,6 +10,8 @@ class Projects::BlobController < Projects::ApplicationController
include RedirectsForMissingPathOnTree include RedirectsForMissingPathOnTree
include SourcegraphDecorator include SourcegraphDecorator
include DiffHelper include DiffHelper
include RedisTracking
extend ::Gitlab::Utils::Override
prepend_before_action :authenticate_user!, only: [:edit] prepend_before_action :authenticate_user!, only: [:edit]
...@@ -35,6 +37,8 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -35,6 +37,8 @@ class Projects::BlobController < Projects::ApplicationController
push_frontend_feature_flag(:suggest_pipeline) if experiment_enabled?(:suggest_pipeline) push_frontend_feature_flag(:suggest_pipeline) if experiment_enabled?(:suggest_pipeline)
end end
track_redis_hll_event :create, :update, name: 'g_edit_by_sfe', feature: :track_editor_edit_actions
def new def new
commit unless @repository.empty? commit unless @repository.empty?
end end
...@@ -256,4 +260,9 @@ class Projects::BlobController < Projects::ApplicationController ...@@ -256,4 +260,9 @@ class Projects::BlobController < Projects::ApplicationController
def diff_params def diff_params
params.permit(:full, :since, :to, :bottom, :unfold, :offset, :indent) params.permit(:full, :since, :to, :bottom, :unfold, :offset, :indent)
end end
override :visitor_id
def visitor_id
current_user&.id
end
end end
---
title: Track SFE actions in BlobController
merge_request: 40846
author:
type: changed
...@@ -17,16 +17,11 @@ RSpec.describe SearchController do ...@@ -17,16 +17,11 @@ RSpec.describe SearchController do
end end
context 'i_search_advanced' do context 'i_search_advanced' do
let(:target_id) { 'i_search_advanced' } it_behaves_like 'tracking unique hll events', :search_track_unique_users do
let(:request_params) { { scope: 'projects', search: 'term' } } subject { get :show, params: { scope: 'projects', search: 'term' }, format: format }
it_behaves_like 'tracking unique hll events', :show
it 'does not track if feature flag is disabled' do
stub_feature_flags(search_track_unique_users: false)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event).with(instance_of(String), target_id)
get :show, params: request_params, format: :html let(:target_id) { 'i_search_advanced' }
let(:expected_type) { instance_of(String) }
end end
end end
...@@ -42,13 +37,10 @@ RSpec.describe SearchController do ...@@ -42,13 +37,10 @@ RSpec.describe SearchController do
stub_ee_application_setting(elasticsearch_search: true, elasticsearch_indexing: true) stub_ee_application_setting(elasticsearch_search: true, elasticsearch_indexing: true)
end end
it_behaves_like 'tracking unique hll events', :show it_behaves_like 'tracking unique hll events', :search_track_unique_users do
subject { get :show, params: request_params, format: format }
it 'does not track if feature flag is disabled' do let(:expected_type) { instance_of(String) }
stub_feature_flags(search_track_unique_users: false)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event).with(instance_of(String), target_id)
get :show, params: request_params, format: :html
end end
end end
...@@ -62,13 +54,10 @@ RSpec.describe SearchController do ...@@ -62,13 +54,10 @@ RSpec.describe SearchController do
stub_licensed_features(elastic_search: true) stub_licensed_features(elastic_search: true)
end end
it_behaves_like 'tracking unique hll events', :show it_behaves_like 'tracking unique hll events', :search_track_unique_users do
subject { get :show, params: request_params, format: format }
it 'does not track if feature flag is disabled' do let(:expected_type) { instance_of(String) }
stub_feature_flags(search_track_unique_users: false)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event).with(instance_of(String), target_id)
get :show, params: request_params, format: :html
end end
end end
......
...@@ -347,6 +347,13 @@ RSpec.describe Projects::BlobController do ...@@ -347,6 +347,13 @@ RSpec.describe Projects::BlobController do
end end
end end
end end
it_behaves_like 'tracking unique hll events', :track_editor_edit_actions do
subject { put :update, params: default_params, format: format }
let(:target_id) { 'g_edit_by_sfe' }
let(:expected_type) { instance_of(Integer) }
end
end end
describe 'DELETE destroy' do describe 'DELETE destroy' do
...@@ -436,4 +443,32 @@ RSpec.describe Projects::BlobController do ...@@ -436,4 +443,32 @@ RSpec.describe Projects::BlobController do
end end
end end
end end
describe 'POST create' do
let(:user) { create(:user) }
let(:default_params) do
{
namespace_id: project.namespace,
project_id: project,
id: 'master',
branch_name: 'master',
file_name: 'docs/EXAMPLE_FILE',
content: 'Added changes',
commit_message: 'Create CHANGELOG'
}
end
before do
project.add_developer(user)
sign_in(user)
end
it_behaves_like 'tracking unique hll events', :track_editor_edit_actions do
subject { post :create, params: default_params, format: format }
let(:target_id) { 'g_edit_by_sfe' }
let(:expected_type) { instance_of(Integer) }
end
end
end end
...@@ -154,9 +154,11 @@ RSpec.describe SearchController do ...@@ -154,9 +154,11 @@ RSpec.describe SearchController do
allow(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event) allow(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
end end
it_behaves_like 'tracking unique hll events', :show do it_behaves_like 'tracking unique hll events', :search_track_unique_users do
let(:request_params) { { scope: 'projects', search: 'term' } } subject { get :show, params: { scope: 'projects', search: 'term' }, format: format }
let(:target_id) { 'i_search_total' } let(:target_id) { 'i_search_total' }
let(:expected_type) { instance_of(String) }
end end
end end
......
# frozen_string_literal: true # frozen_string_literal: true
RSpec.shared_examples 'tracking unique hll events' do |method| RSpec.shared_examples 'tracking unique hll events' do |feature_flag|
it 'tracks unique event if the format is HTML' do context 'when format is HTML' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(instance_of(String), target_id) let(:format) { :html }
get method, params: request_params, format: :html it 'tracks unique event' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(expected_type, target_id)
subject
end end
it 'tracks unique event if DNT is not enabled' do it 'tracks unique event if DNT is not enabled' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(instance_of(String), target_id) expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(expected_type, target_id)
request.headers['DNT'] = '0' request.headers['DNT'] = '0'
get method, params: request_params, format: :html subject
end end
it 'does not track unique event if DNT is enabled' do it 'does not track unique event if DNT is enabled' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event).with(instance_of(String), target_id) expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event).with(expected_type, target_id)
request.headers['DNT'] = '1' request.headers['DNT'] = '1'
get method, params: request_params, format: :html subject
end end
context 'when feature flag is disabled' do
it 'does not track unique event' do
stub_feature_flags(feature_flag => false)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event).with(expected_type, target_id)
subject
end
end
end
context 'when format is JSON' do
let(:format) { :json }
it 'does not track unique event if the format is JSON' do it 'does not track unique event if the format is JSON' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event).with(instance_of(String), target_id) expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event).with(expected_type, target_id)
get method, params: request_params, format: :json subject
end
end end
end end
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