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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Analytics::InternalEventsGenerator, :silence_stdout, feature_category: :service_ping do
include UsageDataHelpers
let(:temp_dir) { Dir.mktmpdir }
let(:ee_temp_dir) { Dir.mktmpdir }
let(:tmpfile) { Tempfile.new('test-metadata') }
let(:existing_key_paths) { {} }
let(:description) { "This metric counts unique users viewing analytics metrics dashboard section" }
let(:group) { "group::analytics instrumentation" }
let(:stage) { "analytics" }
let(:section) { "analytics" }
let(:mr) { "https://gitlab.com/some-group/some-project/-/merge_requests/123" }
let(:event) { "view_analytics_dashboard" }
let(:unique) { "user_id" }
let(:time_frames) { %w[7d] }
let(:include_default_identifiers) { 'yes' }
let(:options) do
{
time_frames: time_frames,
free: true,
mr: mr,
group: group,
stage: stage,
section: section,
event: event,
unique: unique
}.stringify_keys
end
let(:key_path_7d) { "count_distinct_#{unique}_from_#{event}_7d" }
let(:metric_definition_path_7d) { Dir.glob(File.join(temp_dir, "metrics/counts_7d/#{key_path_7d}.yml")).first }
let(:metric_definition_7d) do
{
"key_path" => key_path_7d,
"description" => description,
"product_section" => section,
"product_stage" => stage,
"product_group" => group,
"performance_indicator_type" => [],
"value_type" => "number",
"status" => "active",
"milestone" => "13.9",
"introduced_by_url" => mr,
"time_frame" => "7d",
"data_source" => "internal_events",
"data_category" => "optional",
"instrumentation_class" => "RedisHLLMetric",
"distribution" => %w[ce ee],
"tier" => %w[free premium ultimate]
}
end
before do
stub_const("#{described_class}::TOP_LEVEL_DIR_EE", ee_temp_dir)
stub_const("#{described_class}::TOP_LEVEL_DIR", temp_dir)
stub_const("#{described_class}::KNOWN_EVENTS_PATH", tmpfile.path)
stub_const("#{described_class}::KNOWN_EVENTS_PATH_EE", tmpfile.path)
# Stub version so that `milestone` key remains constant between releases to prevent flakiness.
stub_const('Gitlab::VERSION', '13.9.0')
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:ask)
.with(/Please describe in at least 50 characters/)
.and_return(description)
end
allow(Gitlab::TaskHelpers).to receive(:prompt).and_return(include_default_identifiers)
allow(Gitlab::Usage::MetricDefinition).to receive(:definitions).and_return(existing_key_paths)
end
after do
FileUtils.rm_rf(temp_dir)
FileUtils.rm_rf(ee_temp_dir)
FileUtils.rm_rf(tmpfile.path)
end
describe 'Creating event definition file' do
let(:event_definition_path) { Dir.glob(File.join(temp_dir, "events/#{event}.yml")).first }
let(:identifiers) { %w[project user namespace] }
let(:event_definition) do
{
"category" => "InternalEventTracking",
"action" => event,
"description" => description,
"product_section" => section,
"product_stage" => stage,
"product_group" => group,
"label_description" => nil,
"property_description" => nil,
"value_description" => nil,
"extra_properties" => nil,
"identifiers" => identifiers,
"milestone" => "13.9",
"introduced_by_url" => mr,
"distributions" => %w[ce ee],
"tiers" => %w[free premium ultimate]
}
end
it 'creates an event definition file using the template' do
described_class.new([], options).invoke_all
expect(YAML.safe_load(File.read(event_definition_path))).to eq(event_definition)
end
context 'for ultimate only feature' do
let(:event_definition_path) do
Dir.glob(File.join(ee_temp_dir, temp_dir, "events/#{event}.yml")).first
end
it 'creates an event definition file using the template' do
described_class.new([], options.merge(tiers: %w[ultimate])).invoke_all
expect(YAML.safe_load(File.read(event_definition_path)))
.to eq(event_definition.merge("tiers" => ["ultimate"], "distributions" => ["ee"]))
end
end
context 'without default identifiers' do
let(:include_default_identifiers) { 'no' }
it 'creates an event definition file using the template' do
described_class.new([], options).invoke_all
expect(YAML.safe_load(File.read(event_definition_path)))
.to eq(event_definition.merge("identifiers" => nil))
end
end
context 'with duplicated event' do
context 'in known_events files' do
before do
allow(::Gitlab::UsageDataCounters::HLLRedisCounter)
.to receive(:known_event?).with(event).and_return(true)
end
it 'raises error' do
expect { described_class.new([], options).invoke_all }.to raise_error(RuntimeError)
end
end
context 'in event definition files' do
before do
Dir.mkdir(File.join(temp_dir, "events"))
File.write(File.join(temp_dir, "events", "#{event}.yml"), { action: event }.to_yaml)
end
it 'raises error' do
expect { described_class.new([], options).invoke_all }.to raise_error(RuntimeError)
end
end
end
end
describe 'Creating metric definition file' do
context 'for single time frame' do
let(:time_frames) { %w[7d] }
it 'creates a metric definition file using the template' do
described_class.new([], options).invoke_all
expect(YAML.safe_load(File.read(metric_definition_path_7d))).to eq(metric_definition_7d)
end
context 'for ultimate only feature' do
let(:metric_definition_path_7d) do
Dir.glob(File.join(ee_temp_dir, temp_dir, "metrics/counts_7d/#{key_path_7d}.yml")).first
end
it 'creates a metric definition file using the template' do
described_class.new([], options.merge(tiers: %w[ultimate])).invoke_all
expect(YAML.safe_load(File.read(metric_definition_path_7d)))
.to eq(metric_definition_7d.merge("tier" => ["ultimate"], "distribution" => ["ee"]))
end
end
context 'with invalid time frame' do
let(:time_frames) { %w[14d] }
it 'raises error' do
expect { described_class.new([], options).invoke_all }.to raise_error(RuntimeError)
end
end
context 'with duplicated key path' do
let(:existing_key_paths) { { key_path_7d => true } }
it 'raises error' do
expect { described_class.new([], options).invoke_all }.to raise_error(RuntimeError)
end
end
context 'with unique value passed with a dot' do
it 'creates a metric definition file using the template' do
described_class.new([], options.merge(unique: 'user.id')).invoke_all
expect(YAML.safe_load(File.read(metric_definition_path_7d))).to eq(metric_definition_7d)
end
end
context 'without at least one tier available' do
it 'raises error' do
expect { described_class.new([], options.merge(tiers: [])).invoke_all }
.to raise_error(RuntimeError)
end
end
context 'with unknown tier' do
it 'raises error' do
expect { described_class.new([], options.merge(tiers: %w[superb])).invoke_all }
.to raise_error(RuntimeError)
end
end
context 'without obligatory parameter' do
it 'raises error', :aggregate_failures do
%w[unique event mr section stage group].each do |option|
expect { described_class.new([], options.without(option)).invoke_all }
.to raise_error(RuntimeError)
end
end
end
context 'with to short description' do
it 'asks again for description' do
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:ask)
.with(/By convention all events automatically include the following properties/)
.and_return(include_default_identifiers)
allow(instance).to receive(:ask).twice
.with(/Please describe in at least 50 characters/)
.and_return("I am to short")
expect(instance).to receive(:ask).twice
.with(/Please provide description that is 50 characters long/)
.and_return(description)
end
described_class.new([], options).invoke_all
end
end
end
context 'for multiple time frames' do
let(:time_frames) { %w[7d 28d] }
let(:key_path_28d) { "count_distinct_#{unique}_from_#{event}_28d" }
let(:metric_definition_path_28d) { Dir.glob(File.join(temp_dir, "metrics/counts_28d/#{key_path_28d}.yml")).first }
let(:metric_definition_28d) do
metric_definition_7d.merge(
"key_path" => key_path_28d,
"time_frame" => "28d"
)
end
it 'creates a metric definition file using the template' do
described_class.new([], options).invoke_all
expect(YAML.safe_load(File.read(metric_definition_path_7d))).to eq(metric_definition_7d)
expect(YAML.safe_load(File.read(metric_definition_path_28d))).to eq(metric_definition_28d)
end
end
context 'with default time frames' do
let(:time_frames) { nil }
let(:key_path_28d) { "count_distinct_#{unique}_from_#{event}_28d" }
let(:metric_definition_path_28d) { Dir.glob(File.join(temp_dir, "metrics/counts_28d/#{key_path_28d}.yml")).first }
let(:metric_definition_28d) do
metric_definition_7d.merge(
"key_path" => key_path_28d,
"time_frame" => "28d"
)
end
it 'creates a metric definition file using the template' do
described_class.new([], options.without('time_frames')).invoke_all
expect(YAML.safe_load(File.read(metric_definition_path_7d))).to eq(metric_definition_7d)
expect(YAML.safe_load(File.read(metric_definition_path_28d))).to eq(metric_definition_28d)
end
end
end
describe 'Creating known event entry' do
let(:time_frames) { %w[7d 28d] }
let(:expected_known_events) { [{ "name" => event }] }
it 'creates a metric definition file using the template' do
described_class.new([], options).invoke_all
expect(YAML.safe_load(File.read(tmpfile.path))).to match_array(expected_known_events)
end
context 'for ultimate only feature' do
let(:ee_tmpfile) { Tempfile.new('test-metadata') }
after do
FileUtils.rm_rf(ee_tmpfile)
end
it 'creates a metric definition file using the template' do
stub_const("#{described_class}::KNOWN_EVENTS_PATH_EE", ee_tmpfile.path)
described_class.new([], options.merge(tiers: %w[ultimate])).invoke_all
expect(YAML.safe_load(File.read(tmpfile.path))).to be nil
expect(YAML.safe_load(File.read(ee_tmpfile.path))).to match_array(expected_known_events)
end
end
end
end
|