diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 18:42:06 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 18:42:06 +0000 |
commit | 6e4e1050d9dba2b7b2523fdd1768823ab85feef4 (patch) | |
tree | 78be5963ec075d80116a932011d695dd33910b4e /spec/lib/gitlab/usage_data_counters | |
parent | 1ce776de4ae122aba3f349c02c17cebeaa8ecf07 (diff) | |
download | gitlab-ce-6e4e1050d9dba2b7b2523fdd1768823ab85feef4.tar.gz |
Add latest changes from gitlab-org/gitlab@13-3-stable-ee
Diffstat (limited to 'spec/lib/gitlab/usage_data_counters')
3 files changed, 145 insertions, 33 deletions
diff --git a/spec/lib/gitlab/usage_data_counters/hll_redis_counter_spec.rb b/spec/lib/gitlab/usage_data_counters/hll_redis_counter_spec.rb new file mode 100644 index 00000000000..2ab349a67d9 --- /dev/null +++ b/spec/lib/gitlab/usage_data_counters/hll_redis_counter_spec.rb @@ -0,0 +1,113 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_shared_state do + let(:entity1) { 'dfb9d2d2-f56c-4c77-8aeb-6cddc4a1f857' } + let(:entity2) { '1dd9afb2-a3ee-4de1-8ae3-a405579c8584' } + let(:entity3) { '34rfjuuy-ce56-sa35-ds34-dfer567dfrf2' } + let(:entity4) { '8b9a2671-2abf-4bec-a682-22f6a8f7bf31' } + + let(:weekly_event) { 'g_analytics_contribution' } + let(:daily_event) { 'g_search' } + let(:different_aggregation) { 'different_aggregation' } + + let(:known_events) do + [ + { name: "g_analytics_contribution", redis_slot: "analytics", category: "analytics", expiry: 84, aggregation: "weekly" }, + { name: "g_analytics_valuestream", redis_slot: "analytics", category: "analytics", expiry: 84, aggregation: "daily" }, + { name: "g_analytics_productivity", redis_slot: "analytics", category: "productivity", expiry: 84, aggregation: "weekly" }, + { name: "g_compliance_dashboard", redis_slot: "compliance", category: "compliance", aggregation: "weekly" }, + { name: "g_search", category: "global", aggregation: "daily" }, + { name: "different_aggregation", category: "global", aggregation: "monthly" } + ].map(&:with_indifferent_access) + end + + before do + allow(described_class).to receive(:known_events).and_return(known_events) + end + + around do |example| + # We need to freeze to a reference time + # because visits are grouped by the week number in the year + # Without freezing the time, the test may behave inconsistently + # depending on which day of the week test is run. + # Monday 6th of June + reference_time = Time.utc(2020, 6, 1) + Timecop.freeze(reference_time) { example.run } + end + + describe '.track_event' do + it "raise error if metrics don't have same aggregation" do + expect { described_class.track_event(entity1, different_aggregation, Date.current) } .to raise_error(Gitlab::UsageDataCounters::HLLRedisCounter::UnknownAggregation) + end + + it 'raise error if metrics of unknown aggregation' do + expect { described_class.track_event(entity1, 'unknown', Date.current) } .to raise_error(Gitlab::UsageDataCounters::HLLRedisCounter::UnknownEvent) + end + end + + describe '.unique_events' do + before do + # events in current week, should not be counted as week is not complete + described_class.track_event(entity1, weekly_event, Date.current) + described_class.track_event(entity2, weekly_event, Date.current) + + # Events last week + described_class.track_event(entity1, weekly_event, 2.days.ago) + described_class.track_event(entity1, weekly_event, 2.days.ago) + + # Events 2 weeks ago + described_class.track_event(entity1, weekly_event, 2.weeks.ago) + + # Events 4 weeks ago + described_class.track_event(entity3, weekly_event, 4.weeks.ago) + described_class.track_event(entity4, weekly_event, 29.days.ago) + + # events in current day should be counted in daily aggregation + described_class.track_event(entity1, daily_event, Date.current) + described_class.track_event(entity2, daily_event, Date.current) + + # Events last week + described_class.track_event(entity1, daily_event, 2.days.ago) + described_class.track_event(entity1, daily_event, 2.days.ago) + + # Events 2 weeks ago + described_class.track_event(entity1, daily_event, 14.days.ago) + + # Events 4 weeks ago + described_class.track_event(entity3, daily_event, 28.days.ago) + described_class.track_event(entity4, daily_event, 29.days.ago) + end + + it 'raise error if metrics are not in the same slot' do + expect { described_class.unique_events(event_names: %w(g_analytics_contribution g_compliance_dashboard), start_date: 4.weeks.ago, end_date: Date.current) }.to raise_error('Events should be in same slot') + end + + it 'raise error if metrics are not in the same category' do + expect { described_class.unique_events(event_names: %w(g_analytics_contribution g_analytics_productivity), start_date: 4.weeks.ago, end_date: Date.current) }.to raise_error('Events should be in same category') + end + + it "raise error if metrics don't have same aggregation" do + expect { described_class.unique_events(event_names: %w(g_analytics_contribution g_analytics_valuestream), start_date: 4.weeks.ago, end_date: Date.current) }.to raise_error('Events should have same aggregation level') + end + + context 'when data for the last complete week' do + it { expect(described_class.unique_events(event_names: weekly_event, start_date: 1.week.ago, end_date: Date.current)).to eq(1) } + end + + context 'when data for the last 4 complete weeks' do + it { expect(described_class.unique_events(event_names: weekly_event, start_date: 4.weeks.ago, end_date: Date.current)).to eq(2) } + end + + context 'when data for the week 4 weeks ago' do + it { expect(described_class.unique_events(event_names: weekly_event, start_date: 4.weeks.ago, end_date: 3.weeks.ago)).to eq(1) } + end + + context 'when using daily aggregation' do + it { expect(described_class.unique_events(event_names: daily_event, start_date: 7.days.ago, end_date: Date.current)).to eq(2) } + it { expect(described_class.unique_events(event_names: daily_event, start_date: 28.days.ago, end_date: Date.current)).to eq(3) } + it { expect(described_class.unique_events(event_names: daily_event, start_date: 28.days.ago, end_date: 21.days.ago)).to eq(1) } + end + end +end diff --git a/spec/lib/gitlab/usage_data_counters/track_unique_actions_spec.rb b/spec/lib/gitlab/usage_data_counters/track_unique_actions_spec.rb index 584d8407e79..bd348666729 100644 --- a/spec/lib/gitlab/usage_data_counters/track_unique_actions_spec.rb +++ b/spec/lib/gitlab/usage_data_counters/track_unique_actions_spec.rb @@ -7,20 +7,19 @@ RSpec.describe Gitlab::UsageDataCounters::TrackUniqueActions, :clean_gitlab_redi let(:time) { Time.zone.now } - def track_action(params) - track_unique_events.track_action(params) + def track_event(params) + track_unique_events.track_event(params) end - def count_unique_events(params) - track_unique_events.count_unique_events(params) + def count_unique(params) + track_unique_events.count_unique(params) end context 'tracking an event' do context 'when tracking successfully' do - context 'when the feature flag and the application setting is enabled' do + context 'when the application setting is enabled' do context 'when the target and the action is valid' do before do - stub_feature_flags(described_class::FEATURE_FLAG => true) stub_application_setting(usage_ping_enabled: true) end @@ -29,28 +28,28 @@ RSpec.describe Gitlab::UsageDataCounters::TrackUniqueActions, :clean_gitlab_redi design = Event::TARGET_TYPES[:design] wiki = Event::TARGET_TYPES[:wiki] - expect(track_action(event_action: :pushed, event_target: project, author_id: 1)).to be_truthy - expect(track_action(event_action: :pushed, event_target: project, author_id: 1)).to be_truthy - expect(track_action(event_action: :pushed, event_target: project, author_id: 2)).to be_truthy - expect(track_action(event_action: :pushed, event_target: project, author_id: 3)).to be_truthy - expect(track_action(event_action: :pushed, event_target: project, author_id: 4, time: time - 3.days)).to be_truthy - expect(track_action(event_action: :created, event_target: project, author_id: 5, time: time - 3.days)).to be_truthy + expect(track_event(event_action: :pushed, event_target: project, author_id: 1)).to be_truthy + expect(track_event(event_action: :pushed, event_target: project, author_id: 1)).to be_truthy + expect(track_event(event_action: :pushed, event_target: project, author_id: 2)).to be_truthy + expect(track_event(event_action: :pushed, event_target: project, author_id: 3)).to be_truthy + expect(track_event(event_action: :pushed, event_target: project, author_id: 4, time: time - 3.days)).to be_truthy + expect(track_event(event_action: :created, event_target: project, author_id: 5, time: time - 3.days)).to be_truthy - expect(track_action(event_action: :destroyed, event_target: design, author_id: 3)).to be_truthy - expect(track_action(event_action: :created, event_target: design, author_id: 4)).to be_truthy - expect(track_action(event_action: :updated, event_target: design, author_id: 5)).to be_truthy - expect(track_action(event_action: :pushed, event_target: design, author_id: 6)).to be_truthy + expect(track_event(event_action: :destroyed, event_target: design, author_id: 3)).to be_truthy + expect(track_event(event_action: :created, event_target: design, author_id: 4)).to be_truthy + expect(track_event(event_action: :updated, event_target: design, author_id: 5)).to be_truthy + expect(track_event(event_action: :pushed, event_target: design, author_id: 6)).to be_truthy - expect(track_action(event_action: :destroyed, event_target: wiki, author_id: 5)).to be_truthy - expect(track_action(event_action: :created, event_target: wiki, author_id: 3)).to be_truthy - expect(track_action(event_action: :updated, event_target: wiki, author_id: 4)).to be_truthy - expect(track_action(event_action: :pushed, event_target: wiki, author_id: 6)).to be_truthy + expect(track_event(event_action: :destroyed, event_target: wiki, author_id: 5)).to be_truthy + expect(track_event(event_action: :created, event_target: wiki, author_id: 3)).to be_truthy + expect(track_event(event_action: :updated, event_target: wiki, author_id: 4)).to be_truthy + expect(track_event(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.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::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.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(event_action: described_class::PUSH_ACTION, date_from: time, date_to: Date.today)).to eq(3) + expect(count_unique(event_action: described_class::PUSH_ACTION, date_from: time - 5.days, date_to: Date.tomorrow)).to eq(4) + expect(count_unique(event_action: described_class::DESIGN_ACTION, date_from: time - 5.days, date_to: Date.today)).to eq(3) + expect(count_unique(event_action: described_class::WIKI_ACTION, date_from: time - 5.days, date_to: Date.today)).to eq(3) + expect(count_unique(event_action: described_class::PUSH_ACTION, date_from: time - 5.days, date_to: time - 2.days)).to eq(1) end end end @@ -59,22 +58,20 @@ RSpec.describe Gitlab::UsageDataCounters::TrackUniqueActions, :clean_gitlab_redi context 'when tracking unsuccessfully' do using RSpec::Parameterized::TableSyntax - where(:feature_flag, :application_setting, :target, :action) do - true | true | Project | :invalid_action - false | true | Project | :pushed - true | false | Project | :pushed - true | true | :invalid_target | :pushed + where(:application_setting, :target, :action) do + true | Project | :invalid_action + false | Project | :pushed + true | :invalid_target | :pushed end with_them do before do stub_application_setting(usage_ping_enabled: application_setting) - stub_feature_flags(described_class::FEATURE_FLAG => feature_flag) end it 'returns the expected values' do - expect(track_action(event_action: action, event_target: target, author_id: 2)).to be_nil - expect(count_unique_events(event_action: described_class::PUSH_ACTION, date_from: time, date_to: Date.today)).to eq(0) + expect(track_event(event_action: action, event_target: target, author_id: 2)).to be_nil + expect(count_unique(event_action: described_class::PUSH_ACTION, date_from: time, date_to: Date.today)).to eq(0) end end end diff --git a/spec/lib/gitlab/usage_data_counters/wiki_page_counter_spec.rb b/spec/lib/gitlab/usage_data_counters/wiki_page_counter_spec.rb index 685f6ea0a85..0c8f89fdf07 100644 --- a/spec/lib/gitlab/usage_data_counters/wiki_page_counter_spec.rb +++ b/spec/lib/gitlab/usage_data_counters/wiki_page_counter_spec.rb @@ -3,11 +3,13 @@ require 'spec_helper' RSpec.describe Gitlab::UsageDataCounters::WikiPageCounter do + it_behaves_like 'a redis usage counter', 'Wiki Page', :view it_behaves_like 'a redis usage counter', 'Wiki Page', :create it_behaves_like 'a redis usage counter', 'Wiki Page', :update it_behaves_like 'a redis usage counter', 'Wiki Page', :delete it_behaves_like 'a redis usage counter with totals', :wiki_pages, + view: 8, create: 5, update: 3, delete: 2 |