ruby-grafana-reporter 0.2.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +126 -85
- data/bin/ruby-grafana-reporter +2 -2
- data/lib/VERSION.rb +3 -2
- data/lib/grafana/abstract_datasource.rb +146 -0
- data/lib/grafana/dashboard.rb +21 -23
- data/lib/grafana/errors.rb +18 -3
- data/lib/grafana/grafana.rb +64 -66
- data/lib/grafana/grafana_alerts_datasource.rb +57 -0
- data/lib/grafana/grafana_annotations_datasource.rb +56 -0
- data/lib/grafana/grafana_property_datasource.rb +30 -0
- data/lib/grafana/graphite_datasource.rb +72 -0
- data/lib/grafana/image_rendering_datasource.rb +44 -0
- data/lib/grafana/influxdb_datasource.rb +70 -0
- data/lib/grafana/panel.rb +9 -3
- data/lib/grafana/prometheus_datasource.rb +67 -0
- data/lib/grafana/sql_datasource.rb +78 -0
- data/lib/grafana/unsupported_datasource.rb +7 -0
- data/lib/grafana/variable.rb +1 -1
- data/lib/grafana/webrequest.rb +71 -0
- data/lib/grafana_reporter/abstract_query.rb +460 -0
- data/lib/grafana_reporter/abstract_report.rb +139 -18
- data/lib/grafana_reporter/alerts_table_query.rb +39 -0
- data/lib/grafana_reporter/annotations_table_query.rb +38 -0
- data/lib/grafana_reporter/application/application.rb +34 -286
- data/lib/grafana_reporter/application/webservice.rb +50 -15
- data/lib/grafana_reporter/asciidoctor/alerts_table_include_processor.rb +91 -0
- data/lib/grafana_reporter/asciidoctor/annotations_table_include_processor.rb +90 -0
- data/lib/grafana_reporter/asciidoctor/panel_image_block_macro.rb +74 -0
- data/lib/grafana_reporter/asciidoctor/panel_image_inline_macro.rb +76 -0
- data/lib/grafana_reporter/asciidoctor/panel_property_inline_macro.rb +70 -0
- data/lib/grafana_reporter/asciidoctor/panel_query_table_include_processor.rb +95 -0
- data/lib/grafana_reporter/asciidoctor/panel_query_value_inline_macro.rb +90 -0
- data/lib/grafana_reporter/asciidoctor/processor_mixin.rb +49 -0
- data/lib/grafana_reporter/asciidoctor/report.rb +40 -81
- data/lib/grafana_reporter/asciidoctor/show_environment_include_processor.rb +46 -0
- data/lib/grafana_reporter/asciidoctor/show_help_include_processor.rb +35 -0
- data/lib/grafana_reporter/asciidoctor/sql_table_include_processor.rb +90 -0
- data/lib/grafana_reporter/asciidoctor/sql_value_inline_macro.rb +86 -0
- data/lib/grafana_reporter/asciidoctor/value_as_variable_include_processor.rb +90 -0
- data/lib/grafana_reporter/configuration.rb +59 -52
- data/lib/grafana_reporter/console_configuration_wizard.rb +311 -0
- data/lib/grafana_reporter/demo_report_wizard.rb +105 -0
- data/lib/grafana_reporter/erb/report.rb +30 -0
- data/lib/grafana_reporter/erb/report_jail.rb +21 -0
- data/lib/grafana_reporter/errors.rb +55 -0
- data/lib/grafana_reporter/help.rb +443 -0
- data/lib/grafana_reporter/logger/{two_way_logger.rb → two_way_delegate_logger.rb} +1 -1
- data/lib/grafana_reporter/panel_image_query.rb +25 -0
- data/lib/grafana_reporter/panel_property_query.rb +22 -0
- data/lib/grafana_reporter/query_value_query.rb +61 -0
- data/lib/grafana_reporter/report_webhook.rb +35 -0
- data/lib/ruby_grafana_extension.rb +8 -0
- data/lib/{ruby-grafana-reporter.rb → ruby_grafana_reporter.rb} +1 -2
- metadata +47 -38
- data/lib/grafana/abstract_panel_query.rb +0 -22
- data/lib/grafana/abstract_query.rb +0 -132
- data/lib/grafana/abstract_sql_query.rb +0 -51
- data/lib/grafana/panel_image_query.rb +0 -52
- data/lib/grafana_reporter/asciidoctor/alerts_table_query.rb +0 -104
- data/lib/grafana_reporter/asciidoctor/annotations_table_query.rb +0 -99
- data/lib/grafana_reporter/asciidoctor/errors.rb +0 -40
- data/lib/grafana_reporter/asciidoctor/extensions/alerts_table_include_processor.rb +0 -92
- data/lib/grafana_reporter/asciidoctor/extensions/annotations_table_include_processor.rb +0 -91
- data/lib/grafana_reporter/asciidoctor/extensions/panel_image_block_macro.rb +0 -69
- data/lib/grafana_reporter/asciidoctor/extensions/panel_image_inline_macro.rb +0 -68
- data/lib/grafana_reporter/asciidoctor/extensions/panel_property_inline_macro.rb +0 -61
- data/lib/grafana_reporter/asciidoctor/extensions/panel_query_table_include_processor.rb +0 -78
- data/lib/grafana_reporter/asciidoctor/extensions/panel_query_value_inline_macro.rb +0 -73
- data/lib/grafana_reporter/asciidoctor/extensions/processor_mixin.rb +0 -20
- data/lib/grafana_reporter/asciidoctor/extensions/show_environment_include_processor.rb +0 -43
- data/lib/grafana_reporter/asciidoctor/extensions/show_help_include_processor.rb +0 -228
- data/lib/grafana_reporter/asciidoctor/extensions/sql_table_include_processor.rb +0 -70
- data/lib/grafana_reporter/asciidoctor/extensions/sql_value_inline_macro.rb +0 -66
- data/lib/grafana_reporter/asciidoctor/extensions/value_as_variable_include_processor.rb +0 -86
- data/lib/grafana_reporter/asciidoctor/panel_first_value_query.rb +0 -34
- data/lib/grafana_reporter/asciidoctor/panel_image_query.rb +0 -25
- data/lib/grafana_reporter/asciidoctor/panel_property_query.rb +0 -44
- data/lib/grafana_reporter/asciidoctor/panel_table_query.rb +0 -38
- data/lib/grafana_reporter/asciidoctor/query_mixin.rb +0 -294
- data/lib/grafana_reporter/asciidoctor/sql_first_value_query.rb +0 -32
- data/lib/grafana_reporter/asciidoctor/sql_table_query.rb +0 -34
@@ -25,7 +25,7 @@ module GrafanaReporter
|
|
25
25
|
end
|
26
26
|
|
27
27
|
# Sets the severity level of the additional logger to the given severity.
|
28
|
-
# @param severity one of
|
28
|
+
# @param severity one of Logger::Severity
|
29
29
|
def level=(severity)
|
30
30
|
@additional_logger.level = severity
|
31
31
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrafanaReporter
|
4
|
+
# This class is used to create an image out of a {Grafana::Panel}.
|
5
|
+
class PanelImageQuery < AbstractQuery
|
6
|
+
# Sets the proper render variables.
|
7
|
+
def pre_process
|
8
|
+
# TODO: ensure that in case of timezones are specified, that they are also forwarded to the image renderer
|
9
|
+
# rename "render-" variables
|
10
|
+
@variables = @variables.each_with_object({}) { |(k, v), h| h[k.gsub(/^render-/, '')] = v }
|
11
|
+
@datasource = Grafana::ImageRenderingDatasource.new(nil)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns the body of the http query, which contains the raw image.
|
15
|
+
def post_process
|
16
|
+
@result = @result[:content].first
|
17
|
+
raise ::Grafana::ImageCouldNotBeRenderedError, @panel if @result.include?('<html')
|
18
|
+
end
|
19
|
+
|
20
|
+
# @see AbstractQuery#raw_query
|
21
|
+
def raw_query
|
22
|
+
{ panel: @panel }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrafanaReporter
|
4
|
+
# This class is used to query properties from a {Grafana::Panel}, such as +description+,
|
5
|
+
# +title+ etc.
|
6
|
+
class PanelPropertyQuery < AbstractQuery
|
7
|
+
# @see Grafana::AbstractQuery#pre_process
|
8
|
+
def pre_process
|
9
|
+
@datasource = Grafana::GrafanaPropertyDatasource.new(nil)
|
10
|
+
end
|
11
|
+
|
12
|
+
# @see Grafana::AbstractQuery#post_process
|
13
|
+
def post_process
|
14
|
+
@result = @result[:content].first
|
15
|
+
end
|
16
|
+
|
17
|
+
# @see Grafana::AbstractQuery#raw_query
|
18
|
+
def raw_query
|
19
|
+
@raw_query.merge({ panel: @panel })
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrafanaReporter
|
4
|
+
# This class provides a general query implementation for any kind of single value and table queries.
|
5
|
+
class QueryValueQuery < AbstractQuery
|
6
|
+
# @see Grafana::AbstractQuery#pre_process
|
7
|
+
def pre_process
|
8
|
+
@datasource = @panel.datasource if @panel
|
9
|
+
|
10
|
+
@variables['result_type'] ||= Variable.new('')
|
11
|
+
end
|
12
|
+
|
13
|
+
# Executes {AbstractQuery#format_columns}, {AbstractQuery#replace_values} and
|
14
|
+
# {AbstractQuery#filter_columns} on the query results.
|
15
|
+
#
|
16
|
+
# Finally the results are formatted as a asciidoctor table.
|
17
|
+
# @see Grafana::AbstractQuery#post_process
|
18
|
+
def post_process
|
19
|
+
modify_results
|
20
|
+
|
21
|
+
case @variables['result_type'].raw_value
|
22
|
+
when /(?:panel_table|sql_table)/
|
23
|
+
@result = format_table_output(@result, row_divider: @variables['row_divider'], column_divider: @variables['column_divider'])
|
24
|
+
|
25
|
+
when /(?:panel_value|sql_value)/
|
26
|
+
tmp = @result[:content] || []
|
27
|
+
@result = tmp.flatten.first
|
28
|
+
|
29
|
+
else
|
30
|
+
raise StandardError, "Unsupported 'result_type' received: '#{@variables['result_type'].raw_value}'"
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# @see Grafana::AbstractQuery#raw_query
|
36
|
+
def raw_query
|
37
|
+
return @raw_query if @raw_query
|
38
|
+
|
39
|
+
case @variables['result_type'].raw_value
|
40
|
+
when /(?:panel_table|panel_value)/
|
41
|
+
@variables['query'] ? @panel.query(@variables['query'].raw_value) : @panel.query(nil)
|
42
|
+
|
43
|
+
when /(?:sql_table|sql_value)/
|
44
|
+
nil
|
45
|
+
|
46
|
+
else
|
47
|
+
raise StandardError, "Unsupported 'result_type' received: '#{@variables['result_type'].raw_value}'"
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def modify_results
|
55
|
+
@result = format_columns(@result, @variables['format'])
|
56
|
+
@result = replace_values(@result, @variables.select { |k, _v| k =~ /^replace_values_\d+/ })
|
57
|
+
@result = filter_columns(@result, @variables['filter_columns'])
|
58
|
+
@result = transpose(@result, @variables['transpose'])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrafanaReporter
|
4
|
+
# This class provides a default webhook implementation for report events. It sends out
|
5
|
+
# a webrequest to the configured +callback_url+ with all necessary information about the
|
6
|
+
# event and the report.
|
7
|
+
class ReportWebhook
|
8
|
+
def initialize(callback_url)
|
9
|
+
@callback_url = callback_url
|
10
|
+
end
|
11
|
+
|
12
|
+
# Implements the call of the configured webhook.
|
13
|
+
# Provides the following report information in JSON format:
|
14
|
+
#
|
15
|
+
# +object_id+ - id of the current report
|
16
|
+
# +path+ - file path to the report
|
17
|
+
# +status+ - report status as string, e.g. +cancelled+, +finished+ or +in progress+
|
18
|
+
# +execution_time+ - execution time of the report
|
19
|
+
# +template+ - name of the used template
|
20
|
+
# +start_time+ - time when the report creation started
|
21
|
+
# +end_time+ - time when the report creation ended
|
22
|
+
# +event+ - event, which has happened
|
23
|
+
def callback(event, report)
|
24
|
+
# build report information as JSON
|
25
|
+
data = { object_id: report.object_id, path: report.path, status: report.status,
|
26
|
+
execution_time: report.execution_time, template: report.template,
|
27
|
+
start_time: report.start_time, end_time: report.end_time, event: event }
|
28
|
+
|
29
|
+
request = { body: JSON.generate(data), accept: nil, content_type: nil }
|
30
|
+
res = ::Grafana::WebRequest.new(@callback_url, request).execute
|
31
|
+
|
32
|
+
"#{res} - Body: #{res.body}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -24,8 +24,7 @@ folders = [
|
|
24
24
|
%w[grafana_reporter],
|
25
25
|
%w[grafana_reporter asciidoctor extensions],
|
26
26
|
%w[grafana_reporter asciidoctor],
|
27
|
+
%w[grafana_reporter erb],
|
27
28
|
%w[grafana_reporter application]
|
28
29
|
]
|
29
30
|
folders.each { |folder| Dir[File.join(__dir__, *folder, '*.rb')].sort.each { |file| require_relative file } }
|
30
|
-
|
31
|
-
# TODO check if panel with ID exists before trying to render image
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-grafana-reporter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christian Kohlmeyer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: asciidoctor
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '1.
|
33
|
+
version: '1.6'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1.
|
40
|
+
version: '1.6'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rubyzip
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -100,10 +100,9 @@ dependencies:
|
|
100
100
|
- - "~>"
|
101
101
|
- !ruby/object:Gem::Version
|
102
102
|
version: '3.9'
|
103
|
-
description:
|
104
|
-
|
105
|
-
|
106
|
-
can be used as well.
|
103
|
+
description: Build reports based on grafana dashboards in asciidoctor syntax. Runs
|
104
|
+
as webservice for easy integration with grafana, or as a standalone, command line
|
105
|
+
utility.
|
107
106
|
email: kohly@gmx.de
|
108
107
|
executables:
|
109
108
|
- ruby-grafana-reporter
|
@@ -113,47 +112,57 @@ extra_rdoc_files:
|
|
113
112
|
- LICENSE
|
114
113
|
files:
|
115
114
|
- "./lib/VERSION.rb"
|
116
|
-
- "./lib/grafana/
|
117
|
-
- "./lib/grafana/abstract_query.rb"
|
118
|
-
- "./lib/grafana/abstract_sql_query.rb"
|
115
|
+
- "./lib/grafana/abstract_datasource.rb"
|
119
116
|
- "./lib/grafana/dashboard.rb"
|
120
117
|
- "./lib/grafana/errors.rb"
|
121
118
|
- "./lib/grafana/grafana.rb"
|
119
|
+
- "./lib/grafana/grafana_alerts_datasource.rb"
|
120
|
+
- "./lib/grafana/grafana_annotations_datasource.rb"
|
121
|
+
- "./lib/grafana/grafana_property_datasource.rb"
|
122
|
+
- "./lib/grafana/graphite_datasource.rb"
|
123
|
+
- "./lib/grafana/image_rendering_datasource.rb"
|
124
|
+
- "./lib/grafana/influxdb_datasource.rb"
|
122
125
|
- "./lib/grafana/panel.rb"
|
123
|
-
- "./lib/grafana/
|
126
|
+
- "./lib/grafana/prometheus_datasource.rb"
|
127
|
+
- "./lib/grafana/sql_datasource.rb"
|
128
|
+
- "./lib/grafana/unsupported_datasource.rb"
|
124
129
|
- "./lib/grafana/variable.rb"
|
130
|
+
- "./lib/grafana/webrequest.rb"
|
131
|
+
- "./lib/grafana_reporter/abstract_query.rb"
|
125
132
|
- "./lib/grafana_reporter/abstract_report.rb"
|
133
|
+
- "./lib/grafana_reporter/alerts_table_query.rb"
|
134
|
+
- "./lib/grafana_reporter/annotations_table_query.rb"
|
126
135
|
- "./lib/grafana_reporter/application/application.rb"
|
127
136
|
- "./lib/grafana_reporter/application/errors.rb"
|
128
137
|
- "./lib/grafana_reporter/application/webservice.rb"
|
129
|
-
- "./lib/grafana_reporter/asciidoctor/
|
130
|
-
- "./lib/grafana_reporter/asciidoctor/
|
131
|
-
- "./lib/grafana_reporter/asciidoctor/
|
132
|
-
- "./lib/grafana_reporter/asciidoctor/
|
133
|
-
- "./lib/grafana_reporter/asciidoctor/
|
134
|
-
- "./lib/grafana_reporter/asciidoctor/
|
135
|
-
- "./lib/grafana_reporter/asciidoctor/
|
136
|
-
- "./lib/grafana_reporter/asciidoctor/
|
137
|
-
- "./lib/grafana_reporter/asciidoctor/extensions/panel_query_table_include_processor.rb"
|
138
|
-
- "./lib/grafana_reporter/asciidoctor/extensions/panel_query_value_inline_macro.rb"
|
139
|
-
- "./lib/grafana_reporter/asciidoctor/extensions/processor_mixin.rb"
|
140
|
-
- "./lib/grafana_reporter/asciidoctor/extensions/show_environment_include_processor.rb"
|
141
|
-
- "./lib/grafana_reporter/asciidoctor/extensions/show_help_include_processor.rb"
|
142
|
-
- "./lib/grafana_reporter/asciidoctor/extensions/sql_table_include_processor.rb"
|
143
|
-
- "./lib/grafana_reporter/asciidoctor/extensions/sql_value_inline_macro.rb"
|
144
|
-
- "./lib/grafana_reporter/asciidoctor/extensions/value_as_variable_include_processor.rb"
|
145
|
-
- "./lib/grafana_reporter/asciidoctor/panel_first_value_query.rb"
|
146
|
-
- "./lib/grafana_reporter/asciidoctor/panel_image_query.rb"
|
147
|
-
- "./lib/grafana_reporter/asciidoctor/panel_property_query.rb"
|
148
|
-
- "./lib/grafana_reporter/asciidoctor/panel_table_query.rb"
|
149
|
-
- "./lib/grafana_reporter/asciidoctor/query_mixin.rb"
|
138
|
+
- "./lib/grafana_reporter/asciidoctor/alerts_table_include_processor.rb"
|
139
|
+
- "./lib/grafana_reporter/asciidoctor/annotations_table_include_processor.rb"
|
140
|
+
- "./lib/grafana_reporter/asciidoctor/panel_image_block_macro.rb"
|
141
|
+
- "./lib/grafana_reporter/asciidoctor/panel_image_inline_macro.rb"
|
142
|
+
- "./lib/grafana_reporter/asciidoctor/panel_property_inline_macro.rb"
|
143
|
+
- "./lib/grafana_reporter/asciidoctor/panel_query_table_include_processor.rb"
|
144
|
+
- "./lib/grafana_reporter/asciidoctor/panel_query_value_inline_macro.rb"
|
145
|
+
- "./lib/grafana_reporter/asciidoctor/processor_mixin.rb"
|
150
146
|
- "./lib/grafana_reporter/asciidoctor/report.rb"
|
151
|
-
- "./lib/grafana_reporter/asciidoctor/
|
152
|
-
- "./lib/grafana_reporter/asciidoctor/
|
147
|
+
- "./lib/grafana_reporter/asciidoctor/show_environment_include_processor.rb"
|
148
|
+
- "./lib/grafana_reporter/asciidoctor/show_help_include_processor.rb"
|
149
|
+
- "./lib/grafana_reporter/asciidoctor/sql_table_include_processor.rb"
|
150
|
+
- "./lib/grafana_reporter/asciidoctor/sql_value_inline_macro.rb"
|
151
|
+
- "./lib/grafana_reporter/asciidoctor/value_as_variable_include_processor.rb"
|
153
152
|
- "./lib/grafana_reporter/configuration.rb"
|
153
|
+
- "./lib/grafana_reporter/console_configuration_wizard.rb"
|
154
|
+
- "./lib/grafana_reporter/demo_report_wizard.rb"
|
155
|
+
- "./lib/grafana_reporter/erb/report.rb"
|
156
|
+
- "./lib/grafana_reporter/erb/report_jail.rb"
|
154
157
|
- "./lib/grafana_reporter/errors.rb"
|
155
|
-
- "./lib/grafana_reporter/
|
156
|
-
- "./lib/
|
158
|
+
- "./lib/grafana_reporter/help.rb"
|
159
|
+
- "./lib/grafana_reporter/logger/two_way_delegate_logger.rb"
|
160
|
+
- "./lib/grafana_reporter/panel_image_query.rb"
|
161
|
+
- "./lib/grafana_reporter/panel_property_query.rb"
|
162
|
+
- "./lib/grafana_reporter/query_value_query.rb"
|
163
|
+
- "./lib/grafana_reporter/report_webhook.rb"
|
164
|
+
- "./lib/ruby_grafana_extension.rb"
|
165
|
+
- "./lib/ruby_grafana_reporter.rb"
|
157
166
|
- LICENSE
|
158
167
|
- README.md
|
159
168
|
- bin/ruby-grafana-reporter
|
@@ -171,7 +180,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
171
180
|
requirements:
|
172
181
|
- - ">="
|
173
182
|
- !ruby/object:Gem::Version
|
174
|
-
version: 2.
|
183
|
+
version: '2.5'
|
175
184
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
176
185
|
requirements:
|
177
186
|
- - ">="
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'abstract_query'
|
4
|
-
|
5
|
-
module Grafana
|
6
|
-
# @abstract
|
7
|
-
#
|
8
|
-
# Used as a superclass for all queries, which rely on a {Panel} object.
|
9
|
-
#
|
10
|
-
# @see AbstractQuery
|
11
|
-
class AbstractPanelQuery < AbstractQuery
|
12
|
-
attr_reader :panel
|
13
|
-
|
14
|
-
# Initializes the variables of the query using {AbstractQuery#extract_dashboard_variables}.
|
15
|
-
# @param panel [Panel] panel for which the query shall be executed
|
16
|
-
def initialize(panel)
|
17
|
-
super()
|
18
|
-
@panel = panel
|
19
|
-
extract_dashboard_variables(@panel.dashboard)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,132 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Grafana
|
4
|
-
# @abstract Override {#url}, #{#request}, {#pre_process} and {#post_process} in subclass.
|
5
|
-
#
|
6
|
-
# Superclass containing everything for all queries towards grafana.
|
7
|
-
class AbstractQuery
|
8
|
-
attr_accessor :from, :to, :timeout, :result
|
9
|
-
attr_reader :variables
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
@variables = {}
|
13
|
-
end
|
14
|
-
|
15
|
-
# Runs the whole process to receive values properly from this query:
|
16
|
-
# - calls {#pre_process}
|
17
|
-
# - executes this query against the given {Grafana} instance
|
18
|
-
# - calls {#post_process}
|
19
|
-
# - returns the result
|
20
|
-
#
|
21
|
-
# @param grafana [Grafana] {Grafana} object, against which the query is executed
|
22
|
-
# @return [Object] result of the query
|
23
|
-
def execute(grafana)
|
24
|
-
return @result unless @result.nil?
|
25
|
-
|
26
|
-
pre_process(grafana)
|
27
|
-
@result = grafana.execute_http_request(url, request, timeout)
|
28
|
-
post_process
|
29
|
-
@result
|
30
|
-
end
|
31
|
-
|
32
|
-
# Used to retrieve default configurations from the given {Dashboard} and store them as settings in the query.
|
33
|
-
#
|
34
|
-
# Following data is extracted:
|
35
|
-
# - +from+, by {Dashboard#from_time}
|
36
|
-
# - +to+, by {Dashboard#to_time}
|
37
|
-
# - and all variables as {Variable}, prefixed with +var-+, as grafana also does it
|
38
|
-
def extract_dashboard_variables(dashboard)
|
39
|
-
@from = dashboard.from_time
|
40
|
-
@to = dashboard.to_time
|
41
|
-
dashboard.variables.each { |item| merge_variables({ "var-#{item.name}": item }) }
|
42
|
-
self
|
43
|
-
end
|
44
|
-
|
45
|
-
# Merges the given Hash with the stored variables.
|
46
|
-
#
|
47
|
-
# Can be used to easily set many values at once in the local variables hash.
|
48
|
-
#
|
49
|
-
# Please note, that the values of the Hash need to be of type {Variable}.
|
50
|
-
#
|
51
|
-
# @param hash [Hash<String,Variable>] Hash containing variable name as key and {Variable} as value
|
52
|
-
# @return [AbstractQuery] this object
|
53
|
-
def merge_variables(hash)
|
54
|
-
hash.each do |k, v|
|
55
|
-
if @variables[k.to_s].nil?
|
56
|
-
@variables[k.to_s] = v
|
57
|
-
else
|
58
|
-
@variables[k.to_s].raw_value = v.raw_value
|
59
|
-
end
|
60
|
-
end
|
61
|
-
self
|
62
|
-
end
|
63
|
-
|
64
|
-
# @return [Hash<String, Variable>] all grafana variables stored in this query, i.e. the variable name
|
65
|
-
# is prefixed with +var-+
|
66
|
-
def grafana_variables
|
67
|
-
@variables.select { |k, _v| k =~ /^var-.+/ }
|
68
|
-
end
|
69
|
-
|
70
|
-
# Replaces the grafana variables in the given string with their replacement value.
|
71
|
-
#
|
72
|
-
# @param string [String] string in which the variables shall be replaced
|
73
|
-
# @param variables [Hash<String,Variable>] Hash containing the variables, which shall be replaced in the
|
74
|
-
# given string
|
75
|
-
# @return [String] string in which all variables are properly replaced
|
76
|
-
def replace_variables(string, variables = {})
|
77
|
-
res = string
|
78
|
-
repeat = true
|
79
|
-
repeat_count = 0
|
80
|
-
|
81
|
-
# TODO: find a proper way to replace variables recursively instead of over and over again
|
82
|
-
# TODO: add tests for recursive replacement of variable
|
83
|
-
while repeat && (repeat_count < 3)
|
84
|
-
repeat = false
|
85
|
-
repeat_count += 1
|
86
|
-
variables.each do |var_name, obj|
|
87
|
-
# only set ticks if value is string
|
88
|
-
variable = var_name.gsub(/^var-/, '')
|
89
|
-
res = res.gsub(/(?:\$\{#{variable}(?::(?<format>\w+))?\}|(?<!\.)\$#{variable}(?!\.))/) do
|
90
|
-
obj.value_formatted($LAST_MATCH_INFO ? $LAST_MATCH_INFO[:format] : nil)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
repeat = true if res.include?('$')
|
94
|
-
end
|
95
|
-
|
96
|
-
res
|
97
|
-
end
|
98
|
-
|
99
|
-
# @abstract
|
100
|
-
#
|
101
|
-
# @return [String] String containing the relative URL to execute the query
|
102
|
-
def uri
|
103
|
-
raise NotImplementedError
|
104
|
-
end
|
105
|
-
|
106
|
-
# @abstract
|
107
|
-
#
|
108
|
-
# @return [Hash] Hash containing the request parameters, which shall be overwritten or extended in
|
109
|
-
# {Grafana#execute_http_request}
|
110
|
-
def request
|
111
|
-
raise NotImplementedError
|
112
|
-
end
|
113
|
-
|
114
|
-
# @abstract
|
115
|
-
#
|
116
|
-
# Use this function to perform all necessary actions, before the query is actually executed.
|
117
|
-
# Here you can e.g. set values of variables or similar.
|
118
|
-
#
|
119
|
-
# @param grafana [Grafana] {Grafana} object, against which the query shall be executed
|
120
|
-
def pre_process(grafana)
|
121
|
-
raise NotImplementedError
|
122
|
-
end
|
123
|
-
|
124
|
-
# @abstract
|
125
|
-
#
|
126
|
-
# Use this function to format the raw result of the @result variable to conform to the expected return value.
|
127
|
-
# You might also want to {#replace_variables} in the @result or similar.
|
128
|
-
def post_process
|
129
|
-
raise NotImplementedError
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|