Commit e7da9a43 authored by Marc Shaw's avatar Marc Shaw

Code Review: Tidy up specs and change redis hash key

Issue: gitlab.com/gitlab-org/gitlab/-/issues/219956
Merge Request: gitlab.com/gitlab-org/gitlab/-/merge_requests/35580
parent 2913c5cd
...@@ -176,14 +176,18 @@ class EventCreateService ...@@ -176,14 +176,18 @@ class EventCreateService
action = Event.actions[status] action = Event.actions[status]
raise IllegalActionError, "#{status} is not a valid status" if action.nil? raise IllegalActionError, "#{status} is not a valid status" if action.nil?
Gitlab::UsageDataCounters::TrackUniqueActions.track_action(event_action: status, event_target: record.class, author_id: current_user.id)
parent_attrs(record.resource_parent) parent_attrs(record.resource_parent)
.merge(base_attrs) .merge(base_attrs)
.merge(action: action, target_id: record.id, target_type: record.class.name) .merge(action: action, target_id: record.id, target_type: record.class.name)
end end
Event.insert_all(attribute_sets, returning: %w[id]) result = Event.insert_all(attribute_sets, returning: %w[id])
pairs.each do |record, status|
Gitlab::UsageDataCounters::TrackUniqueActions.track_action(event_action: status, event_target: record.class, author_id: current_user.id)
end
result
end end
def create_push_event(service_class, project, current_user, push_data) def create_push_event(service_class, project, current_user, push_data)
......
...@@ -49,11 +49,9 @@ module Gitlab ...@@ -49,11 +49,9 @@ module Gitlab
keys = (date_from.to_date..date_to.to_date).map { |date| key(event_action, date) } keys = (date_from.to_date..date_to.to_date).map { |date| key(event_action, date) }
Gitlab::Redis::SharedState.with do |redis| Gitlab::Redis::SharedState.with do |redis|
Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do
redis.pfcount(*keys) redis.pfcount(*keys)
end end
end end
end
private private
...@@ -75,7 +73,7 @@ module Gitlab ...@@ -75,7 +73,7 @@ module Gitlab
def key(event_action, date) def key(event_action, date)
year_day = date.strftime('%G-%j') year_day = date.strftime('%G-%j')
"#{event_action}-#{year_day}" "{#{event_action}}-#{year_day}"
end end
def add_event(event_action, author_id, date) def add_event(event_action, author_id, date)
......
...@@ -5,6 +5,8 @@ require 'spec_helper' ...@@ -5,6 +5,8 @@ require 'spec_helper'
RSpec.describe Gitlab::UsageDataCounters::TrackUniqueActions, :clean_gitlab_redis_shared_state do RSpec.describe Gitlab::UsageDataCounters::TrackUniqueActions, :clean_gitlab_redis_shared_state do
subject(:track_unique_events) { described_class.new } subject(:track_unique_events) { described_class.new }
let(:time) { Time.zone.now }
def track_action(params) def track_action(params)
track_unique_events.track_action(params) track_unique_events.track_action(params)
end end
...@@ -13,91 +15,67 @@ RSpec.describe Gitlab::UsageDataCounters::TrackUniqueActions, :clean_gitlab_redi ...@@ -13,91 +15,67 @@ RSpec.describe Gitlab::UsageDataCounters::TrackUniqueActions, :clean_gitlab_redi
track_unique_events.count_unique_events(params) track_unique_events.count_unique_events(params)
end end
describe '#tracking_event' do context 'tracking an event' do
context 'when usage pings are enabled' do context 'when tracking successfully' do
before do context 'when the feature flag and the application setting is enabled' do
Gitlab::CurrentSettings.update!(usage_ping_enabled: true) context 'when the target and the action is valid' do
end
context 'when the feature flag is enabled ' do
let(:time) { Time.zone.now }
before do before do
stub_feature_flags(cache_diff_stats_merge_request: true) stub_feature_flags(cache_diff_stats_merge_request: true)
stub_application_setting(usage_ping_enabled: true)
end end
context 'when target is valid' do
context 'when action is valid' do
it 'tracks and counts the events as expected' do it 'tracks and counts the events as expected' do
project = Event::TARGET_TYPES[:project] project = Event::TARGET_TYPES[:project]
design = Event::TARGET_TYPES[:design] design = Event::TARGET_TYPES[:design]
wiki = Event::TARGET_TYPES[:wiki] wiki = Event::TARGET_TYPES[:wiki]
track_action(event_action: :pushed, event_target: project, author_id: 1) expect(track_action(event_action: :pushed, event_target: project, author_id: 1)).to be_truthy
track_action(event_action: :pushed, event_target: project, author_id: 1) expect(track_action(event_action: :pushed, event_target: project, author_id: 1)).to be_truthy
track_action(event_action: :pushed, event_target: project, author_id: 2) expect(track_action(event_action: :pushed, event_target: project, author_id: 2)).to be_truthy
track_action(event_action: :pushed, event_target: project, author_id: 3) expect(track_action(event_action: :pushed, event_target: project, author_id: 3)).to be_truthy
track_action(event_action: :pushed, event_target: project, author_id: 4, time: time - 3.days) expect(track_action(event_action: :pushed, event_target: project, author_id: 4, time: time - 3.days)).to be_truthy
track_action(event_action: :created, event_target: project, author_id: 5, time: time - 3.days) expect(track_action(event_action: :created, event_target: project, author_id: 5, time: time - 3.days)).to be_truthy
track_action(event_action: :destroyed, event_target: design, author_id: 3) expect(track_action(event_action: :destroyed, event_target: design, author_id: 3)).to be_truthy
track_action(event_action: :created, event_target: design, author_id: 4) expect(track_action(event_action: :created, event_target: design, author_id: 4)).to be_truthy
track_action(event_action: :updated, event_target: design, author_id: 5) expect(track_action(event_action: :updated, event_target: design, author_id: 5)).to be_truthy
track_action(event_action: :pushed, event_target: design, author_id: 6) expect(track_action(event_action: :pushed, event_target: design, author_id: 6)).to be_truthy
track_action(event_action: :destroyed, event_target: wiki, author_id: 5) expect(track_action(event_action: :destroyed, event_target: wiki, author_id: 5)).to be_truthy
track_action(event_action: :created, event_target: wiki, author_id: 3) expect(track_action(event_action: :created, event_target: wiki, author_id: 3)).to be_truthy
track_action(event_action: :updated, event_target: wiki, author_id: 4) expect(track_action(event_action: :updated, event_target: wiki, author_id: 4)).to be_truthy
track_action(event_action: :pushed, event_target: wiki, author_id: 6) expect(track_action(event_action: :pushed, event_target: wiki, author_id: 6)).to be_truthy
expect(count_unique_events(event_action: described_class::PUSH_ACTION, date_from: time, date_to: Date.tomorrow)).to eq(3) expect(count_unique_events(event_action: described_class::PUSH_ACTION, date_from: time, date_to: Date.today)).to eq(3)
expect(count_unique_events(event_action: described_class::PUSH_ACTION, date_from: time - 5.days, date_to: Date.tomorrow)).to eq(4) expect(count_unique_events(event_action: described_class::PUSH_ACTION, date_from: time - 5.days, date_to: Date.tomorrow)).to eq(4)
expect(count_unique_events(event_action: described_class::DESIGN_ACTION, date_from: time - 5.days, date_to: Date.tomorrow)).to eq(3) expect(count_unique_events(event_action: described_class::DESIGN_ACTION, date_from: time - 5.days, date_to: Date.today)).to eq(3)
expect(count_unique_events(event_action: described_class::WIKI_ACTION, date_from: time - 5.days, date_to: Date.tomorrow)).to eq(3) expect(count_unique_events(event_action: described_class::WIKI_ACTION, date_from: time - 5.days, date_to: Date.today)).to eq(3)
expect(count_unique_events(event_action: described_class::PUSH_ACTION, date_from: time - 5.days, date_to: time - 2.days)).to eq(1) expect(count_unique_events(event_action: described_class::PUSH_ACTION, date_from: time - 5.days, date_to: time - 2.days)).to eq(1)
end end
end end
context 'when action is invalid' do
it 'does not add any event' do
expect(Gitlab::Redis::SharedState).not_to receive(:with)
track_action(event_action: :test, event_target: :wiki, author_id: 2)
end
end end
end end
context 'when target is invalid' do context 'when tracking unsuccessfully' do
it 'does not add any event' do using RSpec::Parameterized::TableSyntax
expect(Gitlab::Redis::SharedState).not_to receive(:with)
track_action(event_action: :pushed, event_target: :test, author_id: 2) where(:feature_flag, :application_setting, :target, :action) do
end true | true | :project | :invalid_action
end false | true | :project | :pushed
true | false | :project | :pushed
true | true | :invalid_target | :pushed
end end
context 'when the feature flag is disabled' do with_them do
before do before do
stub_feature_flags(cache_diff_stats_merge_request: true) stub_application_setting(usage_ping_enabled: application_setting)
end stub_feature_flags(cache_diff_stats_merge_request: feature_flag)
it 'does not add any event' do
expect(Gitlab::Redis::SharedState).not_to receive(:with)
track_action(event_action: :pushed, event_target: :project, author_id: 2)
end
end
end end
context 'when usage pings are disabled' do it 'returns the expected values' do
before do expect(track_action(event_action: action, event_target: target, author_id: 2)).to be_nil
Gitlab::CurrentSettings.update!(usage_ping_enabled: false) expect(count_unique_events(event_action: described_class::PUSH_ACTION, date_from: time, date_to: Date.today)).to eq(0)
end end
it 'does not add any event' do
expect(Gitlab::Redis::SharedState).not_to receive(:with)
track_action(event_action: :pushed, event_target: :project, author_id: 2)
end 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