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
|
# frozen_string_literal: true
module Importers
module CommonMetrics
class Importer
MissingQueryId = Class.new(StandardError)
attr_reader :content
def initialize(filename = 'common_metrics.yml')
@content = YAML.load_file(Rails.root.join('config', 'prometheus', filename))
end
def execute
PrometheusMetric.reset_column_information
process_content do |id, attributes|
find_or_build_metric!(id)
.update!(**attributes)
end
end
private
def process_content(&blk)
content['panel_groups'].map do |group|
process_group(group, &blk)
end
end
def process_group(group, &blk)
attributes = {
group: find_group_title_key(group['group'])
}
group['panels'].map do |panel|
process_panel(panel, attributes, &blk)
end
end
def process_panel(panel, attributes, &blk)
attributes = attributes.merge(
title: panel['title'],
y_label: panel['y_label'])
panel['metrics'].map do |metric_details|
process_metric_details(metric_details, attributes, &blk)
end
end
def process_metric_details(metric_details, attributes, &blk)
attributes = attributes.merge(
legend: metric_details['label'],
query: metric_details['query_range'],
unit: metric_details['unit'])
yield(metric_details['id'], attributes)
end
def find_or_build_metric!(id)
raise MissingQueryId unless id
PrometheusMetric.common.find_by(identifier: id) ||
PrometheusMetric.new(common: true, identifier: id)
end
def find_group_title_key(title)
PrometheusMetricEnums.groups[find_group_title(title)]
end
def find_group_title(title)
PrometheusMetricEnums.group_titles.invert[title]
end
end
end
end
|