debian-mirror-gitlab/spec/lib/gitlab/usage/metrics/aggregates/aggregate_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

139 lines
4.4 KiB
Ruby
Raw Normal View History

2021-03-11 19:13:27 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Usage::Metrics::Aggregates::Aggregate, :clean_gitlab_redis_shared_state do
let(:end_date) { Date.current }
2021-04-17 20:07:23 +05:30
let(:namespace) { described_class.to_s.deconstantize.constantize }
2023-01-13 00:05:48 +05:30
let(:sources) { Gitlab::Usage::Metrics::Aggregates::Sources }
2021-03-11 19:13:27 +05:30
let_it_be(:recorded_at) { Time.current.to_i }
2023-01-13 00:05:48 +05:30
describe '.calculate_count_for_aggregation' do
using RSpec::Parameterized::TableSyntax
context 'with valid configuration' do
where(:number_of_days, :operator, :datasource, :expected_method) do
28 | 'AND' | 'redis_hll' | :calculate_metrics_intersections
7 | 'AND' | 'redis_hll' | :calculate_metrics_intersections
28 | 'AND' | 'database' | :calculate_metrics_intersections
7 | 'AND' | 'database' | :calculate_metrics_intersections
28 | 'OR' | 'redis_hll' | :calculate_metrics_union
7 | 'OR' | 'redis_hll' | :calculate_metrics_union
28 | 'OR' | 'database' | :calculate_metrics_union
7 | 'OR' | 'database' | :calculate_metrics_union
2021-03-11 19:13:27 +05:30
end
2023-01-13 00:05:48 +05:30
with_them do
let(:time_frame) { "#{number_of_days}d" }
let(:start_date) { number_of_days.days.ago.to_date }
2021-06-08 01:23:25 +05:30
let(:params) { { start_date: start_date, end_date: end_date, recorded_at: recorded_at } }
2023-01-13 00:05:48 +05:30
let(:aggregate) do
{
source: datasource,
operator: operator,
events: %w[event1 event2]
}
2021-03-11 19:13:27 +05:30
end
2023-01-13 00:05:48 +05:30
subject(:calculate_count_for_aggregation) do
described_class
.new(recorded_at)
.calculate_count_for_aggregation(aggregation: aggregate, time_frame: time_frame)
2021-03-11 19:13:27 +05:30
end
2023-01-13 00:05:48 +05:30
it 'returns the number of unique events for aggregation', :aggregate_failures do
expect(namespace::SOURCES[datasource])
.to receive(expected_method)
.with(params.merge(metric_names: %w[event1 event2]))
.and_return(5)
expect(calculate_count_for_aggregation).to eq(5)
2021-04-17 20:07:23 +05:30
end
end
end
2023-01-13 00:05:48 +05:30
context 'with invalid configuration' do
where(:time_frame, :operator, :datasource, :expected_error) do
'28d' | 'SUM' | 'redis_hll' | namespace::UnknownAggregationOperator
'7d' | 'AND' | 'mongodb' | namespace::UnknownAggregationSource
'all' | 'AND' | 'redis_hll' | namespace::DisallowedAggregationTimeFrame
2021-04-17 20:07:23 +05:30
end
2023-01-13 00:05:48 +05:30
with_them do
let(:aggregate) do
{
source: datasource,
operator: operator,
events: %w[event1 event2]
}
end
2021-04-17 20:07:23 +05:30
2023-01-13 00:05:48 +05:30
subject(:calculate_count_for_aggregation) do
described_class
.new(recorded_at)
.calculate_count_for_aggregation(aggregation: aggregate, time_frame: time_frame)
2021-04-17 20:07:23 +05:30
end
2023-01-13 00:05:48 +05:30
context 'with non prod environment' do
it 'raises error' do
expect { calculate_count_for_aggregation }.to raise_error expected_error
2021-04-17 20:07:23 +05:30
end
end
2023-01-13 00:05:48 +05:30
context 'with prod environment' do
before do
2021-04-17 20:07:23 +05:30
stub_rails_env('production')
2023-01-13 00:05:48 +05:30
end
2021-03-11 19:13:27 +05:30
2023-01-13 00:05:48 +05:30
it 'returns fallback value' do
expect(calculate_count_for_aggregation).to be(-1)
2021-03-11 19:13:27 +05:30
end
end
end
end
2023-01-13 00:05:48 +05:30
context 'when union data is not available' do
subject(:calculate_count_for_aggregation) do
described_class
.new(recorded_at)
.calculate_count_for_aggregation(aggregation: aggregate, time_frame: time_frame)
2022-11-25 23:54:43 +05:30
end
2021-03-11 19:13:27 +05:30
2023-01-13 00:05:48 +05:30
where(:time_frame, :operator, :datasource) do
'28d' | 'OR' | 'redis_hll'
'7d' | 'OR' | 'database'
2022-11-25 23:54:43 +05:30
end
2021-03-11 19:13:27 +05:30
2023-01-13 00:05:48 +05:30
with_them do
before do
allow(namespace::SOURCES[datasource]).to receive(:calculate_metrics_union).and_raise(sources::UnionNotAvailable)
2022-11-25 23:54:43 +05:30
end
2023-01-13 00:05:48 +05:30
let(:aggregate) do
{
source: datasource,
operator: operator,
events: %w[event1 event2]
}
2022-11-25 23:54:43 +05:30
end
2023-01-13 00:05:48 +05:30
context 'with non prod environment' do
it 'raises error' do
expect { calculate_count_for_aggregation }.to raise_error sources::UnionNotAvailable
2022-11-25 23:54:43 +05:30
end
end
2023-01-13 00:05:48 +05:30
context 'with prod environment' do
2022-11-25 23:54:43 +05:30
before do
2023-01-13 00:05:48 +05:30
stub_rails_env('production')
2022-11-25 23:54:43 +05:30
end
2023-01-13 00:05:48 +05:30
it 'returns fallback value' do
expect(calculate_count_for_aggregation).to be(-1)
2022-11-25 23:54:43 +05:30
end
end
end
end
2021-03-11 19:13:27 +05:30
end
end