diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-20 23:50:22 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-20 23:50:22 +0000 |
commit | 9dc93a4519d9d5d7be48ff274127136236a3adb3 (patch) | |
tree | 70467ae3692a0e35e5ea56bcb803eb512a10bedb /spec/lib/gitlab/usage_data_counters | |
parent | 4b0f34b6d759d6299322b3a54453e930c6121ff0 (diff) | |
download | gitlab-ce-9dc93a4519d9d5d7be48ff274127136236a3adb3.tar.gz |
Add latest changes from gitlab-org/gitlab@13-11-stable-eev13.11.0-rc43
Diffstat (limited to 'spec/lib/gitlab/usage_data_counters')
5 files changed, 65 insertions, 106 deletions
diff --git a/spec/lib/gitlab/usage_data_counters/aggregated_metrics_spec.rb b/spec/lib/gitlab/usage_data_counters/aggregated_metrics_spec.rb deleted file mode 100644 index 9aba86cdaf2..00000000000 --- a/spec/lib/gitlab/usage_data_counters/aggregated_metrics_spec.rb +++ /dev/null @@ -1,95 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe 'aggregated metrics' do - RSpec::Matchers.define :be_known_event do - match do |event| - Gitlab::UsageDataCounters::HLLRedisCounter.known_event?(event) - end - - failure_message do |event| - "Event with name: `#{event}` can not be found within `#{Gitlab::UsageDataCounters::HLLRedisCounter::KNOWN_EVENTS_PATH}`" - end - end - - RSpec::Matchers.define :has_known_source do - match do |aggregate| - Gitlab::Usage::Metrics::Aggregates::SOURCES.include?(aggregate[:source]) - end - - failure_message do |aggregate| - "Aggregate with name: `#{aggregate[:name]}` uses not allowed source `#{aggregate[:source]}`" - end - end - - RSpec::Matchers.define :have_known_time_frame do - allowed_time_frames = [ - Gitlab::Utils::UsageData::ALL_TIME_TIME_FRAME_NAME, - Gitlab::Utils::UsageData::TWENTY_EIGHT_DAYS_TIME_FRAME_NAME, - Gitlab::Utils::UsageData::SEVEN_DAYS_TIME_FRAME_NAME - ] - - match do |aggregate| - (aggregate[:time_frame] - allowed_time_frames).empty? - end - - failure_message do |aggregate| - "Aggregate with name: `#{aggregate[:name]}` uses not allowed time_frame`#{aggregate[:time_frame] - allowed_time_frames}`" - end - end - - let_it_be(:known_events) do - Gitlab::UsageDataCounters::HLLRedisCounter.known_events - end - - Gitlab::Usage::Metrics::Aggregates::Aggregate.new(Time.current).send(:aggregated_metrics).tap do |aggregated_metrics| - it 'all events has unique name' do - event_names = aggregated_metrics&.map { |event| event[:name] } - - expect(event_names).to eq(event_names&.uniq) - end - - it 'all aggregated metrics has known source' do - expect(aggregated_metrics).to all has_known_source - end - - it 'all aggregated metrics has known source' do - expect(aggregated_metrics).to all have_known_time_frame - end - - aggregated_metrics&.select { |agg| agg[:source] == Gitlab::Usage::Metrics::Aggregates::REDIS_SOURCE }&.each do |aggregate| - context "for #{aggregate[:name]} aggregate of #{aggregate[:events].join(' ')}" do - let_it_be(:events_records) { known_events.select { |event| aggregate[:events].include?(event[:name]) } } - - it "does not include 'all' time frame for Redis sourced aggregate" do - expect(aggregate[:time_frame]).not_to include(Gitlab::Utils::UsageData::ALL_TIME_TIME_FRAME_NAME) - end - - it "only refers to known events" do - expect(aggregate[:events]).to all be_known_event - end - - it "has expected structure" do - expect(aggregate.keys).to include(*%w[name operator events]) - end - - it "uses allowed aggregation operators" do - expect(Gitlab::Usage::Metrics::Aggregates::ALLOWED_METRICS_AGGREGATIONS).to include aggregate[:operator] - end - - it "uses events from the same Redis slot" do - event_slots = events_records.map { |event| event[:redis_slot] }.uniq - - expect(event_slots).to contain_exactly(be_present) - end - - it "uses events with the same aggregation period" do - event_slots = events_records.map { |event| event[:aggregation] }.uniq - - expect(event_slots).to contain_exactly(be_present) - end - end - end - end -end diff --git a/spec/lib/gitlab/usage_data_counters/code_review_events_spec.rb b/spec/lib/gitlab/usage_data_counters/code_review_events_spec.rb index 664e7938a7e..a1dee442131 100644 --- a/spec/lib/gitlab/usage_data_counters/code_review_events_spec.rb +++ b/spec/lib/gitlab/usage_data_counters/code_review_events_spec.rb @@ -5,7 +5,7 @@ require 'spec_helper' # If this spec fails, we need to add the new code review event to the correct aggregated metric RSpec.describe 'Code review events' do it 'the aggregated metrics contain all the code review metrics' do - path = Rails.root.join('lib/gitlab/usage_data_counters/aggregated_metrics/code_review.yml') + path = Rails.root.join('config/metrics/aggregates/code_review.yml') aggregated_events = YAML.safe_load(File.read(path), aliases: true)&.map(&:with_indifferent_access) code_review_aggregated_events = aggregated_events 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 index d12dcdae955..9fc28f6c4ec 100644 --- a/spec/lib/gitlab/usage_data_counters/hll_redis_counter_spec.rb +++ b/spec/lib/gitlab/usage_data_counters/hll_redis_counter_spec.rb @@ -34,6 +34,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s 'source_code', 'incident_management', 'incident_management_alerts', + 'incident_management_oncall', 'testing', 'issues_edit', 'ci_secrets_management', @@ -43,7 +44,8 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s 'ci_templates', 'quickactions', 'pipeline_authoring', - 'epics_usage' + 'epics_usage', + 'secure' ) end end @@ -93,7 +95,25 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s end describe '.track_event' do - context 'with feature flag set' do + context 'with redis_hll_tracking' do + it 'tracks the event when feature enabled' do + stub_feature_flags(redis_hll_tracking: true) + + expect(Gitlab::Redis::HLL).to receive(:add) + + described_class.track_event(weekly_event, values: 1) + end + + it 'does not track the event with feature flag disabled' do + stub_feature_flags(redis_hll_tracking: false) + + expect(Gitlab::Redis::HLL).not_to receive(:add) + + described_class.track_event(weekly_event, values: 1) + end + end + + context 'with event feature flag set' do it 'tracks the event when feature enabled' do stub_feature_flags(feature => true) @@ -111,7 +131,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s end end - context 'with no feature flag set' do + context 'with no event feature flag set' do it 'tracks the event' do expect(Gitlab::Redis::HLL).to receive(:add) @@ -289,6 +309,11 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s described_class.track_event(daily_event, values: entity4, time: 29.days.ago) end + it 'returns 0 if there are no keys for the given events' do + expect(Gitlab::Redis::HLL).not_to receive(:count) + expect(described_class.unique_events(event_names: [weekly_event], start_date: Date.current, end_date: 4.weeks.ago)).to eq(-1) + end + it 'raise error if metrics are not in the same slot' do expect do described_class.unique_events(event_names: [compliance_slot_event, analytics_slot_event], start_date: 4.weeks.ago, end_date: Date.current) @@ -508,6 +533,11 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s expect { described_class.calculate_events_union(**time_range.merge(event_names: %w[event1_slot event4])) }.to raise_error described_class::SlotMismatch expect { described_class.calculate_events_union(**time_range.merge(event_names: %w[event5_slot event3_slot])) }.to raise_error described_class::AggregationMismatch end + + it 'returns 0 if there are no keys for given events' do + expect(Gitlab::Redis::HLL).not_to receive(:count) + expect(described_class.calculate_events_union(event_names: %w[event1_slot event2_slot event3_slot], start_date: Date.current, end_date: 4.weeks.ago)).to eq(-1) + end end describe '.weekly_time_range' do diff --git a/spec/lib/gitlab/usage_data_counters/issue_activity_unique_counter_spec.rb b/spec/lib/gitlab/usage_data_counters/issue_activity_unique_counter_spec.rb index f8f6494b92e..1b73e5269d7 100644 --- a/spec/lib/gitlab/usage_data_counters/issue_activity_unique_counter_spec.rb +++ b/spec/lib/gitlab/usage_data_counters/issue_activity_unique_counter_spec.rb @@ -3,9 +3,10 @@ require 'spec_helper' RSpec.describe Gitlab::UsageDataCounters::IssueActivityUniqueCounter, :clean_gitlab_redis_shared_state do - let(:user1) { build(:user, id: 1) } - let(:user2) { build(:user, id: 2) } - let(:user3) { build(:user, id: 3) } + let_it_be(:user1) { build(:user, id: 1) } + let_it_be(:user2) { build(:user, id: 2) } + let_it_be(:user3) { build(:user, id: 3) } + let(:time) { Time.zone.now } context 'for Issue title edit actions' do @@ -272,10 +273,13 @@ RSpec.describe Gitlab::UsageDataCounters::IssueActivityUniqueCounter, :clean_git described_class.track_issue_title_changed_action(author: user1) described_class.track_issue_description_changed_action(author: user1) described_class.track_issue_assignee_changed_action(author: user1) - described_class.track_issue_title_changed_action(author: user2, time: time - 2.days) - described_class.track_issue_title_changed_action(author: user3, time: time - 3.days) - described_class.track_issue_description_changed_action(author: user3, time: time - 3.days) - described_class.track_issue_assignee_changed_action(author: user3, time: time - 3.days) + + travel_to(2.days.ago) do + described_class.track_issue_title_changed_action(author: user2) + described_class.track_issue_title_changed_action(author: user3) + described_class.track_issue_description_changed_action(author: user3) + described_class.track_issue_assignee_changed_action(author: user3) + end events = Gitlab::UsageDataCounters::HLLRedisCounter.events_for_category(described_class::ISSUE_CATEGORY) today_count = Gitlab::UsageDataCounters::HLLRedisCounter.unique_events(event_names: events, start_date: time, end_date: time) diff --git a/spec/lib/gitlab/usage_data_counters/quick_action_activity_unique_counter_spec.rb b/spec/lib/gitlab/usage_data_counters/quick_action_activity_unique_counter_spec.rb index 2df0f331f73..1940442d2ad 100644 --- a/spec/lib/gitlab/usage_data_counters/quick_action_activity_unique_counter_spec.rb +++ b/spec/lib/gitlab/usage_data_counters/quick_action_activity_unique_counter_spec.rb @@ -115,6 +115,26 @@ RSpec.describe Gitlab::UsageDataCounters::QuickActionActivityUniqueCounter, :cle end end + context 'tracking spent' do + let(:quickaction_name) { 'spent' } + + context 'adding time' do + let(:args) { '1d' } + + it_behaves_like 'a tracked quick action unique event' do + let(:action) { 'i_quickactions_spend_add' } + end + end + + context 'removing time' do + let(:args) { '-1d' } + + it_behaves_like 'a tracked quick action unique event' do + let(:action) { 'i_quickactions_spend_subtract' } + end + end + end + context 'tracking unassign' do let(:quickaction_name) { 'unassign' } |