Welcome to mirror list, hosted at ThFree Co, Russian Federation.

aggregate_spec.rb « aggregates « metrics « usage « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3060cd4927489a44ef4a16f84b254aa8ca47c4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# 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 }
  let(:namespace) { described_class.to_s.deconstantize.constantize }
  let(:sources) { Gitlab::Usage::Metrics::Aggregates::Sources }

  let_it_be(:recorded_at) { Time.current.to_i }

  describe '.calculate_count_for_aggregation' do
    using RSpec::Parameterized::TableSyntax

    before do
      %w[event1 event2].each do |event_name|
        allow(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:known_event?).with(event_name).and_return(true)
      end
    end

    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
        28 | 'AND' | 'internal_events'    | :calculate_metrics_intersections
        7  | 'AND' | 'internal_events'    | :calculate_metrics_intersections
        28 | 'OR'  | 'internal_events'    | :calculate_metrics_union
        7  | 'OR'  | 'internal_events'    | :calculate_metrics_union
      end

      with_them do
        let(:time_frame) { "#{number_of_days}d" }
        let(:start_date) { number_of_days.days.ago.to_date }
        let(:params) { { start_date: start_date, end_date: end_date, recorded_at: recorded_at } }
        let(:aggregate) do
          {
            source: datasource,
            operator: operator,
            events: %w[event1 event2]
          }
        end

        subject(:calculate_count_for_aggregation) do
          described_class
            .new(recorded_at)
            .calculate_count_for_aggregation(aggregation: aggregate, time_frame: time_frame)
        end

        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)
        end
      end
    end

    # EE version has validation that doesn't allow undefined events
    # On CE, we detect EE events as undefined
    context 'when configuration includes undefined events', unless: Gitlab.ee? do
      let(:number_of_days) { 28 }

      before do
        allow(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:known_event?).with('event3').and_return(false)
      end

      where(:operator, :datasource, :expected_method, :expected_events) do
        'AND' | 'redis_hll' | :calculate_metrics_intersections | %w[event1 event2]
        'AND' | 'database'  | :calculate_metrics_intersections | %w[event1 event2 event3]
        'OR'  | 'redis_hll' | :calculate_metrics_union         | %w[event1 event2]
        'OR'  | 'database'  | :calculate_metrics_union         | %w[event1 event2 event3]
      end

      with_them do
        let(:time_frame) { "#{number_of_days}d" }
        let(:start_date) { number_of_days.days.ago.to_date }
        let(:params) { { start_date: start_date, end_date: end_date, recorded_at: recorded_at } }
        let(:aggregate) do
          {
            source: datasource,
            operator: operator,
            events: %w[event1 event2 event3]
          }
        end

        subject(:calculate_count_for_aggregation) do
          described_class
            .new(recorded_at)
            .calculate_count_for_aggregation(aggregation: aggregate, time_frame: time_frame)
        end

        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: expected_events))
                  .and_return(5)
          expect(calculate_count_for_aggregation).to eq(5)
        end
      end
    end

    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
      end

      with_them do
        let(:aggregate) do
          {
            source: datasource,
            operator: operator,
            events: %w[event1 event2]
          }
        end

        subject(:calculate_count_for_aggregation) do
          described_class
            .new(recorded_at)
            .calculate_count_for_aggregation(aggregation: aggregate, time_frame: time_frame)
        end

        context 'with non prod environment' do
          it 'raises error' do
            expect { calculate_count_for_aggregation }.to raise_error expected_error
          end
        end

        context 'with prod environment' do
          before do
            stub_rails_env('production')
          end

          it 'returns fallback value' do
            expect(calculate_count_for_aggregation).to be(-1)
          end
        end
      end
    end

    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)
      end

      where(:time_frame, :operator, :datasource) do
        '28d' | 'OR' | 'redis_hll'
        '7d'  | 'OR' | 'database'
        '28d' | 'OR' | 'internal_events'
      end

      with_them do
        before do
          allow(namespace::SOURCES[datasource]).to receive(:calculate_metrics_union).and_raise(sources::UnionNotAvailable)
        end

        let(:aggregate) do
          {
            source: datasource,
            operator: operator,
            events: %w[event1 event2]
          }
        end

        context 'with non prod environment' do
          it 'raises error' do
            expect { calculate_count_for_aggregation }.to raise_error sources::UnionNotAvailable
          end
        end

        context 'with prod environment' do
          before do
            stub_rails_env('production')
          end

          it 'returns fallback value' do
            expect(calculate_count_for_aggregation).to be(-1)
          end
        end
      end
    end
  end
end