diff options
Diffstat (limited to 'spec/lib/gitlab/usage/metrics/instrumentations')
7 files changed, 239 insertions, 4 deletions
diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/aggregated_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/aggregated_metric_spec.rb new file mode 100644 index 00000000000..3e7b13e21c1 --- /dev/null +++ b/spec/lib/gitlab/usage/metrics/instrumentations/aggregated_metric_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Usage::Metrics::Instrumentations::AggregatedMetric, :clean_gitlab_redis_shared_state do + using RSpec::Parameterized::TableSyntax + before do + # weekly AND 1 weekly OR 2 + Gitlab::UsageDataCounters::HLLRedisCounter.track_event(:i_quickactions_approve, values: 1, time: 1.week.ago) + Gitlab::UsageDataCounters::HLLRedisCounter.track_event(:i_quickactions_unapprove, values: 1, time: 1.week.ago) + Gitlab::UsageDataCounters::HLLRedisCounter.track_event(:i_quickactions_unapprove, values: 2, time: 1.week.ago) + + # monthly AND 2 weekly OR 3 + Gitlab::UsageDataCounters::HLLRedisCounter.track_event(:i_quickactions_approve, values: 2, time: 2.weeks.ago) + Gitlab::UsageDataCounters::HLLRedisCounter.track_event(:i_quickactions_unapprove, values: 3, time: 2.weeks.ago) + + # out of date range + Gitlab::UsageDataCounters::HLLRedisCounter.track_event(:i_quickactions_approve, values: 3, time: 2.months.ago) + + # database events + Gitlab::Usage::Metrics::Aggregates::Sources::PostgresHll + .save_aggregated_metrics( + metric_name: :i_quickactions_approve, + time_period: { created_at: (1.week.ago..Date.current) }, + recorded_at_timestamp: Time.current, + data: ::Gitlab::Database::PostgresHll::Buckets.new(141 => 1, 56 => 1) + ) + Gitlab::Usage::Metrics::Aggregates::Sources::PostgresHll + .save_aggregated_metrics( + metric_name: :i_quickactions_unapprove, + time_period: { created_at: (1.week.ago..Date.current) }, + recorded_at_timestamp: Time.current, + data: ::Gitlab::Database::PostgresHll::Buckets.new(10 => 1, 56 => 1) + ) + end + + where(:data_source, :time_frame, :operator, :expected_value) do + 'redis_hll' | '28d' | 'AND' | 2 + 'redis_hll' | '28d' | 'OR' | 3 + 'redis_hll' | '7d' | 'AND' | 1 + 'redis_hll' | '7d' | 'OR' | 2 + 'database' | '7d' | 'OR' | 3.0 + 'database' | '7d' | 'AND' | 1.0 + end + + with_them do + let(:error_rate) { Gitlab::Database::PostgresHll::BatchDistinctCounter::ERROR_RATE } + let(:metric_definition) do + { + data_source: data_source, + time_frame: time_frame, + options: { + aggregate: { + operator: operator + }, + events: %w[ + i_quickactions_approve + i_quickactions_unapprove + ] + } + } + end + + around do |example| + freeze_time { example.run } + end + + it 'has correct value' do + expect(described_class.new(metric_definition).value).to be_within(error_rate).percent_of(expected_value) + end + end +end diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/distinct_count_projects_with_expiration_policy_disabled_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/distinct_count_projects_with_expiration_policy_disabled_metric_spec.rb new file mode 100644 index 00000000000..757adee6117 --- /dev/null +++ b/spec/lib/gitlab/usage/metrics/instrumentations/distinct_count_projects_with_expiration_policy_disabled_metric_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Usage::Metrics::Instrumentations::DistinctCountProjectsWithExpirationPolicyDisabledMetric do + before_all do + create(:container_expiration_policy, enabled: false) + create(:container_expiration_policy, enabled: false, created_at: 29.days.ago) + create(:container_expiration_policy, enabled: true) + end + + it_behaves_like 'a correct instrumented metric value', { time_frame: '28d' } do + let(:expected_value) { 1 } + end + + it_behaves_like 'a correct instrumented metric value', { time_frame: 'all' } do + let(:expected_value) { 2 } + end +end diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/gitlab_for_jira_app_direct_installations_count_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/gitlab_for_jira_app_direct_installations_count_metric_spec.rb new file mode 100644 index 00000000000..061558085a1 --- /dev/null +++ b/spec/lib/gitlab/usage/metrics/instrumentations/gitlab_for_jira_app_direct_installations_count_metric_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Usage::Metrics::Instrumentations::GitlabForJiraAppDirectInstallationsCountMetric do + before do + create(:jira_connect_subscription) + end + + let(:expected_value) { 1 } + let(:expected_query) do + 'SELECT COUNT("jira_connect_installations"."id") FROM "jira_connect_installations"'\ + ' INNER JOIN "jira_connect_subscriptions" ON "jira_connect_subscriptions"."jira_connect_installation_id"'\ + ' = "jira_connect_installations"."id"' + end + + it_behaves_like 'a correct instrumented metric value and query', { time_frame: 'all' } +end diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/gitlab_for_jira_app_proxy_installations_count_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/gitlab_for_jira_app_proxy_installations_count_metric_spec.rb new file mode 100644 index 00000000000..4535bab7702 --- /dev/null +++ b/spec/lib/gitlab/usage/metrics/instrumentations/gitlab_for_jira_app_proxy_installations_count_metric_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Usage::Metrics::Instrumentations::GitlabForJiraAppProxyInstallationsCountMetric do + let_it_be(:installation) { create(:jira_connect_installation, instance_url: 'http://self-managed-gitlab.com') } + + before do + create(:jira_connect_subscription, installation: installation) + end + + let(:expected_value) { 1 } + let(:expected_query) do + 'SELECT COUNT("jira_connect_installations"."id") FROM "jira_connect_installations"'\ + ' WHERE "jira_connect_installations"."instance_url" IS NOT NULL' + end + + it_behaves_like 'a correct instrumented metric value and query', { time_frame: 'all' } +end diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/merge_request_widget_extension_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/merge_request_widget_extension_metric_spec.rb new file mode 100644 index 00000000000..c0ac00c9cdd --- /dev/null +++ b/spec/lib/gitlab/usage/metrics/instrumentations/merge_request_widget_extension_metric_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Usage::Metrics::Instrumentations::MergeRequestWidgetExtensionMetric, + :clean_gitlab_redis_shared_state do + before do + 4.times do + Gitlab::UsageDataCounters::MergeRequestWidgetExtensionCounter.count(:terraform_count_expand) + end + end + + let(:expected_value) { 4 } + + it_behaves_like 'a correct instrumented metric value', { + options: { event: 'expand', widget: 'terraform' }, + time_frame: 'all' + } + + it 'raises an exception if widget option is not present' do + expect do + described_class.new(options: { event: 'expand' }, time_frame: 'all') + end.to raise_error(ArgumentError, /'widget' option is required/) + end +end diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/redis_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/redis_metric_spec.rb index 80ae5c6fd21..c4d6edd43e1 100644 --- a/spec/lib/gitlab/usage/metrics/instrumentations/redis_metric_spec.rb +++ b/spec/lib/gitlab/usage/metrics/instrumentations/redis_metric_spec.rb @@ -11,14 +11,21 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::RedisMetric, :clean_git let(:expected_value) { 4 } - it_behaves_like 'a correct instrumented metric value', { options: { event: 'pushes', prefix: 'source_code' } } + it_behaves_like 'a correct instrumented metric value', { + options: { event: 'pushes', prefix: 'source_code' }, + time_frame: 'all' + } it 'raises an exception if event option is not present' do - expect { described_class.new(prefix: 'source_code') }.to raise_error(ArgumentError) + expect do + described_class.new(options: { prefix: 'source_code' }, time_frame: 'all') + end.to raise_error(ArgumentError, /'event' option is required/) end it 'raises an exception if prefix option is not present' do - expect { described_class.new(event: 'pushes') }.to raise_error(ArgumentError) + expect do + described_class.new(options: { event: 'pushes' }, time_frame: 'all') + end.to raise_error(ArgumentError, /'prefix' option is required/) end describe 'children classes' do @@ -55,7 +62,22 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::RedisMetric, :clean_git end it_behaves_like 'a correct instrumented metric value', { - options: { event: 'merge_requests_count', prefix: 'web_ide', include_usage_prefix: false } + options: { event: 'merge_requests_count', prefix: 'web_ide', include_usage_prefix: false }, + time_frame: 'all' + } + end + + context "with prefix disabled" do + let(:expected_value) { 3 } + + before do + 3.times do + Gitlab::UsageDataCounters::SearchCounter.count(:all_searches) + end + end + + it_behaves_like 'a correct instrumented metric value', { + options: { event: 'all_searches_count', prefix: nil, include_usage_prefix: false }, time_frame: 'all' } end end diff --git a/spec/lib/gitlab/usage/metrics/instrumentations/work_items_activity_aggregated_metric_spec.rb b/spec/lib/gitlab/usage/metrics/instrumentations/work_items_activity_aggregated_metric_spec.rb new file mode 100644 index 00000000000..3e315692d0a --- /dev/null +++ b/spec/lib/gitlab/usage/metrics/instrumentations/work_items_activity_aggregated_metric_spec.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Usage::Metrics::Instrumentations::WorkItemsActivityAggregatedMetric do + let(:metric_definition) do + { + data_source: 'redis_hll', + time_frame: time_frame, + options: { + aggregate: { + operator: 'OR' + }, + events: %w[ + users_creating_work_items + users_updating_work_item_title + users_updating_work_item_dates + users_updating_work_item_iteration + ] + } + } + end + + around do |example| + freeze_time { example.run } + end + + where(:time_frame) { [['28d'], ['7d']] } + + with_them do + describe '#available?' do + it 'returns false without track_work_items_activity feature' do + stub_feature_flags(track_work_items_activity: false) + + expect(described_class.new(metric_definition).available?).to eq(false) + end + + it 'returns true with track_work_items_activity feature' do + stub_feature_flags(track_work_items_activity: true) + + expect(described_class.new(metric_definition).available?).to eq(true) + end + end + + describe '#value', :clean_gitlab_redis_shared_state do + let(:counter) { Gitlab::UsageDataCounters::HLLRedisCounter } + + before do + counter.track_event(:users_creating_work_items, values: 1, time: 1.week.ago) + counter.track_event(:users_updating_work_item_title, values: 1, time: 1.week.ago) + counter.track_event(:users_updating_work_item_dates, values: 2, time: 1.week.ago) + counter.track_event(:users_updating_work_item_iteration, values: 2, time: 1.week.ago) + end + + it 'has correct value' do + expect(described_class.new(metric_definition).value).to eq 2 + end + end + end +end |