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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Redis::HLL, :clean_gitlab_redis_shared_state do
using RSpec::Parameterized::TableSyntax
let(:expiry) { 1.day }
describe '.add' do
context 'when checking key format' do
context 'for invalid keys' do
where(:metric_key, :value) do
'test' | 1
'test-{metric' | 1
'test-{metric}}' | 1
end
with_them do
it 'raise an error when using an invalid key format' do
expect { described_class.add(key: metric_key, value: value, expiry: expiry) }.to raise_error(Gitlab::Redis::HLL::KeyFormatError)
end
end
end
context 'for valid keys' do
where(:metric_key, :value) do
'test-{metric}' | 1
'test-{metric}-1' | 1
'test:{metric}-1' | 1
'2020-216-{project_action}' | 1
'i_{analytics}_dev_ops_score-2020-32' | 1
end
with_them do
it "doesn't raise error when having correct format" do
expect { described_class.add(key: metric_key, value: value, expiry: expiry) }.not_to raise_error
end
end
end
end
context 'when adding entries' do
let(:metric) { 'test-{metric}' }
it 'supports single value' do
track_event(metric, 1)
expect(count_unique_events([metric])).to eq(1)
end
it 'supports multiple values' do
stub_const("#{described_class.name}::HLL_BATCH_SIZE", 2)
track_event(metric, [1, 2, 3, 4, 5])
expect(count_unique_events([metric])).to eq(5)
end
end
end
describe '.count' do
let(:event_2020_32) { '2020-32-{expand_vulnerabilities}' }
let(:event_2020_33) { '2020-33-{expand_vulnerabilities}' }
let(:event_2020_34) { '2020-34-{expand_vulnerabilities}' }
let(:entity1) { 'user_id_1'}
let(:entity2) { 'user_id_2'}
let(:entity3) { 'user_id_3'}
let(:entity4) { 'user_id_4'}
before do
track_event(event_2020_32, entity1)
track_event(event_2020_32, entity1)
track_event(event_2020_32, entity2)
track_event(event_2020_32, entity3)
track_event(event_2020_33, entity3)
track_event(event_2020_33, entity3)
track_event(event_2020_34, entity3)
track_event(event_2020_34, entity2)
end
it 'has 3 distinct users for weeks 32, 33, 34' do
unique_counts = count_unique_events([event_2020_32, event_2020_33, event_2020_34])
expect(unique_counts).to eq(3)
end
it 'has 3 distinct users for weeks 32, 33' do
unique_counts = count_unique_events([event_2020_32, event_2020_33])
expect(unique_counts).to eq(3)
end
it 'has 2 distinct users for weeks 33, 34' do
unique_counts = count_unique_events([event_2020_33, event_2020_34])
expect(unique_counts).to eq(2)
end
it 'has one distinct user for week 33' do
unique_counts = count_unique_events([event_2020_33])
expect(unique_counts).to eq(1)
end
it 'has 4 distinct users when one different user has an action on week 34' do
track_event(event_2020_34, entity4, 29.days)
unique_counts = count_unique_events([event_2020_32, event_2020_33, event_2020_34])
expect(unique_counts).to eq(4)
end
end
def track_event(key, value, expiry = 1.day)
described_class.add(key: key, value: value, expiry: expiry)
end
def count_unique_events(keys)
described_class.count(keys: keys)
end
end
|