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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Usage::Metrics::Instrumentations::DatabaseMetric do
subject do
described_class.tap do |metric_class|
metric_class.relation { Issue }
metric_class.operation :count
metric_class.start { metric_class.relation.minimum(:id) }
metric_class.finish { metric_class.relation.maximum(:id) }
end.new(time_frame: 'all')
end
describe '#value' do
let_it_be(:issue_1) { create(:issue) }
let_it_be(:issue_2) { create(:issue) }
let_it_be(:issue_3) { create(:issue) }
let_it_be(:issues) { Issue.all }
before do
allow(Issue.connection).to receive(:transaction_open?).and_return(false)
end
it 'calculates a correct result' do
expect(subject.value).to eq(3)
end
it 'does not cache the result of start and finish', :request_store, :use_clean_rails_redis_caching do
expect(Gitlab::Cache).not_to receive(:fetch_once)
expect(subject).to receive(:count).with(any_args, hash_including(start: issues.min_by(&:id).id, finish: issues.max_by(&:id).id)).and_call_original
subject.value
expect(Rails.cache.read('metric_instrumentation/special_issue_count_minimum_id')).to eq(nil)
expect(Rails.cache.read('metric_instrumentation/special_issue_count_maximum_id')).to eq(nil)
end
context 'with metric options specified with custom batch_size' do
subject do
described_class.tap do |metric_class|
metric_class.relation { Issue }
metric_class.operation :count
metric_class.start { metric_class.relation.minimum(:id) }
metric_class.finish { metric_class.relation.maximum(:id) }
metric_class.metric_options { { batch_size: 12345 } }
end.new(time_frame: 'all')
end
it 'calls metric with customized batch_size' do
expect(subject).to receive(:count).with(any_args, hash_including(batch_size: 12345, start: issues.min_by(&:id).id, finish: issues.max_by(&:id).id)).and_call_original
subject.value
end
it 'calculates a correct result' do
expect(subject.value).to eq(3)
end
end
context 'with start and finish not called' do
subject do
described_class.tap do |metric_class|
metric_class.relation { Issue }
metric_class.operation :count
end.new(time_frame: 'all')
end
it 'calculates a correct result' do
expect(subject.value).to eq(3)
end
end
context 'with availability defined' do
subject do
described_class.tap do |metric_class|
metric_class.relation { Issue }
metric_class.operation :count
metric_class.available? { false }
end.new(time_frame: 'all')
end
it 'responds to #available? properly' do
expect(subject.available?).to eq(false)
end
end
context 'with availability not defined' do
subject do
Class.new(described_class) do
relation { Issue }
operation :count
end.new(time_frame: 'all')
end
it 'responds to #available? properly' do
expect(subject.available?).to eq(true)
end
end
context 'with cache_start_and_finish_as called' do
subject do
described_class.tap do |metric_class|
metric_class.relation { Issue }
metric_class.operation :count
metric_class.start { metric_class.relation.minimum(:id) }
metric_class.finish { metric_class.relation.maximum(:id) }
metric_class.cache_start_and_finish_as :special_issue_count
end.new(time_frame: 'all')
end
it 'caches using the key name passed', :request_store, :use_clean_rails_redis_caching do
expect(Gitlab::Cache).to receive(:fetch_once).with('metric_instrumentation/special_issue_count_minimum_id', any_args).and_call_original
expect(Gitlab::Cache).to receive(:fetch_once).with('metric_instrumentation/special_issue_count_maximum_id', any_args).and_call_original
expect(subject).to receive(:count).with(any_args, hash_including(start: issues.min_by(&:id).id, finish: issues.max_by(&:id).id)).and_call_original
subject.value
expect(Rails.cache.read('metric_instrumentation/special_issue_count_minimum_id')).to eq(issues.min_by(&:id).id)
expect(Rails.cache.read('metric_instrumentation/special_issue_count_maximum_id')).to eq(issues.max_by(&:id).id)
end
end
context 'with estimate_batch_distinct_count' do
subject do
described_class.tap do |metric_class|
metric_class.relation { Issue }
metric_class.operation(:estimate_batch_distinct_count)
metric_class.start { metric_class.relation.minimum(:id) }
metric_class.finish { metric_class.relation.maximum(:id) }
end.new(time_frame: 'all')
end
it 'calculates a correct result', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/349762' do
expect(subject.value).to be_within(Gitlab::Database::PostgresHll::BatchDistinctCounter::ERROR_RATE).percent_of(3)
end
context 'with block passed to operation' do
let(:buckets) { double('Buckets').as_null_object }
subject do
described_class.tap do |metric_class|
metric_class.relation { Issue }
metric_class.operation(:estimate_batch_distinct_count) do |result|
result.foo
end
metric_class.start { metric_class.relation.minimum(:id) }
metric_class.finish { metric_class.relation.maximum(:id) }
end.new(time_frame: 'all')
end
before do
allow(Gitlab::Database::PostgresHll::Buckets).to receive(:new).and_return(buckets)
end
it 'calls the block passing HLL buckets as an argument' do
expect(buckets).to receive(:foo)
subject.value
end
end
end
context 'with custom timestamp column' do
subject do
described_class.tap do |metric_class|
metric_class.relation { Issue }
metric_class.operation :count
metric_class.timestamp_column :last_edited_at
end.new(time_frame: '28d')
end
it 'calculates a correct result' do
create(:issue, last_edited_at: 5.days.ago)
expect(subject.value).to eq(1)
end
end
context 'with default timestamp column' do
subject do
described_class.tap do |metric_class|
metric_class.relation { Issue }
metric_class.operation :count
end.new(time_frame: '28d')
end
it 'calculates a correct result' do
create(:issue, last_edited_at: 5.days.ago)
create(:issue, created_at: 5.days.ago)
expect(subject.value).to eq(1)
end
end
end
context 'with unimplemented operation method used' do
subject do
described_class.tap do |metric_class|
metric_class.relation { Issue }
metric_class.operation :invalid_operation
end.new(time_frame: 'all')
end
it 'raises an error' do
expect { subject }.to raise_error(described_class::UnimplementedOperationError)
end
end
end
|