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
|
# frozen_string_literal: true
module InternalEventsCli
NEW_METRIC_FIELDS = [
:key_path,
:description,
:product_section,
:product_stage,
:product_group,
:performance_indicator_type,
:value_type,
:status,
:milestone,
:introduced_by_url,
:time_frame,
:data_source,
:data_category,
:product_category,
:instrumentation_class,
:distribution,
:tier,
:options,
:events
].freeze
ADDITIONAL_METRIC_FIELDS = [
:milestone_removed,
:removed_by_url,
:removed_by,
:repair_issue_url,
:value_json_schema,
:name
].freeze
METRIC_DEFAULTS = {
product_section: nil,
product_stage: nil,
product_group: nil,
introduced_by_url: 'TODO',
value_type: 'number',
status: 'active',
data_source: 'internal_events',
data_category: 'optional',
performance_indicator_type: []
}.freeze
Metric = Struct.new(*NEW_METRIC_FIELDS, *ADDITIONAL_METRIC_FIELDS, :identifier, keyword_init: true) do
def formatted_output
METRIC_DEFAULTS
.merge(to_h.compact)
.merge(
key_path: key_path,
instrumentation_class: instrumentation_class,
events: events)
.slice(*NEW_METRIC_FIELDS)
.transform_keys(&:to_s)
.to_yaml(line_width: 150)
end
def file_path
File.join(
*[
('ee' unless distribution.include?('ce')),
'config',
'metrics',
"counts_#{time_frame}",
"#{key}.yml"
].compact
)
end
def key
[
'count',
(identifier ? "distinct_#{identifier}_id_from" : 'total'),
actions.join('_and_'),
(time_frame_prefix&.downcase if time_frame != 'all')
].compact.join('_')
end
def key_path
self[:key_path] ||= "#{key_path_prefix}.#{key}"
end
def instrumentation_class
self[:instrumentation_class] ||= identifier ? 'RedisHLLMetric' : 'TotalCountMetric'
end
def events
self[:events] ||= actions.map do |action|
if identifier
{
'name' => action,
'unique' => "#{identifier}.id"
}
else
{ 'name' => action }
end
end
end
def key_path_prefix
case instrumentation_class
when 'RedisHLLMetric'
'redis_hll_counters'
when 'TotalCountMetric'
'counts'
end
end
def actions
options&.dig('events')&.sort || []
end
def identifier_prefix
if identifier
"count of unique #{identifier}s"
else
"count of"
end
end
def time_frame_prefix
case time_frame
when '7d'
'Weekly'
when '28d'
'Monthly'
when 'all'
'Total'
end
end
def prefix
[time_frame_prefix, identifier_prefix].join(' ')
end
def technical_description
simple_event_list = actions.join(' or ')
case identifier
when 'user'
"#{prefix} who triggered #{simple_event_list}"
when 'project', 'namespace'
"#{prefix} where #{simple_event_list} occurred"
else
"#{prefix} #{simple_event_list} occurrences"
end
end
def bulk_assign(key_value_pairs)
key_value_pairs.each { |key, value| self[key] = value }
end
end
end
|