ruby-grafana-reporter 0.2.0 → 0.4.1

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.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +105 -86
  3. data/bin/ruby-grafana-reporter +5 -5
  4. data/lib/VERSION.rb +3 -2
  5. data/lib/grafana/abstract_datasource.rb +136 -0
  6. data/lib/grafana/dashboard.rb +21 -23
  7. data/lib/grafana/errors.rb +8 -1
  8. data/lib/grafana/grafana.rb +61 -65
  9. data/lib/grafana/grafana_alerts_datasource.rb +57 -0
  10. data/lib/grafana/grafana_annotations_datasource.rb +56 -0
  11. data/lib/grafana/grafana_property_datasource.rb +25 -0
  12. data/lib/grafana/graphite_datasource.rb +50 -0
  13. data/lib/grafana/image_rendering_datasource.rb +44 -0
  14. data/lib/grafana/panel.rb +9 -3
  15. data/lib/grafana/prometheus_datasource.rb +45 -0
  16. data/lib/grafana/sql_datasource.rb +71 -0
  17. data/lib/grafana/unsupported_datasource.rb +7 -0
  18. data/lib/grafana/variable.rb +3 -2
  19. data/lib/grafana/webrequest.rb +71 -0
  20. data/lib/grafana_reporter/abstract_query.rb +359 -0
  21. data/lib/grafana_reporter/abstract_report.rb +119 -17
  22. data/lib/grafana_reporter/alerts_table_query.rb +44 -0
  23. data/lib/grafana_reporter/annotations_table_query.rb +43 -0
  24. data/lib/grafana_reporter/application/application.rb +49 -297
  25. data/lib/grafana_reporter/application/webservice.rb +49 -14
  26. data/lib/grafana_reporter/asciidoctor/alerts_table_include_processor.rb +90 -0
  27. data/lib/grafana_reporter/asciidoctor/annotations_table_include_processor.rb +89 -0
  28. data/lib/grafana_reporter/asciidoctor/panel_image_block_macro.rb +77 -0
  29. data/lib/grafana_reporter/asciidoctor/panel_image_inline_macro.rb +79 -0
  30. data/lib/grafana_reporter/asciidoctor/panel_property_inline_macro.rb +73 -0
  31. data/lib/grafana_reporter/asciidoctor/panel_query_table_include_processor.rb +99 -0
  32. data/lib/grafana_reporter/asciidoctor/panel_query_value_inline_macro.rb +93 -0
  33. data/lib/grafana_reporter/asciidoctor/processor_mixin.rb +64 -0
  34. data/lib/grafana_reporter/asciidoctor/report.rb +47 -76
  35. data/lib/grafana_reporter/asciidoctor/show_environment_include_processor.rb +46 -0
  36. data/lib/grafana_reporter/asciidoctor/show_help_include_processor.rb +35 -0
  37. data/lib/grafana_reporter/asciidoctor/sql_table_include_processor.rb +92 -0
  38. data/lib/grafana_reporter/asciidoctor/sql_value_inline_macro.rb +88 -0
  39. data/lib/grafana_reporter/asciidoctor/value_as_variable_include_processor.rb +90 -0
  40. data/lib/grafana_reporter/configuration.rb +108 -43
  41. data/lib/grafana_reporter/console_configuration_wizard.rb +311 -0
  42. data/lib/grafana_reporter/demo_report_wizard.rb +101 -0
  43. data/lib/grafana_reporter/erb/report.rb +43 -0
  44. data/lib/grafana_reporter/errors.rb +41 -0
  45. data/lib/grafana_reporter/help.rb +443 -0
  46. data/lib/grafana_reporter/logger/{two_way_logger.rb → two_way_delegate_logger.rb} +1 -1
  47. data/lib/grafana_reporter/panel_image_query.rb +29 -0
  48. data/lib/grafana_reporter/panel_property_query.rb +22 -0
  49. data/lib/grafana_reporter/query_value_query.rb +79 -0
  50. data/lib/grafana_reporter/report_webhook.rb +35 -0
  51. data/lib/ruby_grafana_extension.rb +8 -0
  52. data/lib/{ruby-grafana-reporter.rb → ruby_grafana_reporter.rb} +13 -0
  53. metadata +47 -43
  54. data/lib/grafana/abstract_panel_query.rb +0 -22
  55. data/lib/grafana/abstract_query.rb +0 -132
  56. data/lib/grafana/abstract_sql_query.rb +0 -51
  57. data/lib/grafana/panel_image_query.rb +0 -52
  58. data/lib/grafana_reporter/asciidoctor/alerts_table_query.rb +0 -104
  59. data/lib/grafana_reporter/asciidoctor/annotations_table_query.rb +0 -99
  60. data/lib/grafana_reporter/asciidoctor/errors.rb +0 -40
  61. data/lib/grafana_reporter/asciidoctor/extensions/alerts_table_include_processor.rb +0 -92
  62. data/lib/grafana_reporter/asciidoctor/extensions/annotations_table_include_processor.rb +0 -91
  63. data/lib/grafana_reporter/asciidoctor/extensions/panel_image_block_macro.rb +0 -69
  64. data/lib/grafana_reporter/asciidoctor/extensions/panel_image_inline_macro.rb +0 -68
  65. data/lib/grafana_reporter/asciidoctor/extensions/panel_property_inline_macro.rb +0 -61
  66. data/lib/grafana_reporter/asciidoctor/extensions/panel_query_table_include_processor.rb +0 -78
  67. data/lib/grafana_reporter/asciidoctor/extensions/panel_query_value_inline_macro.rb +0 -73
  68. data/lib/grafana_reporter/asciidoctor/extensions/processor_mixin.rb +0 -20
  69. data/lib/grafana_reporter/asciidoctor/extensions/show_environment_include_processor.rb +0 -43
  70. data/lib/grafana_reporter/asciidoctor/extensions/show_help_include_processor.rb +0 -202
  71. data/lib/grafana_reporter/asciidoctor/extensions/sql_table_include_processor.rb +0 -70
  72. data/lib/grafana_reporter/asciidoctor/extensions/sql_value_inline_macro.rb +0 -66
  73. data/lib/grafana_reporter/asciidoctor/extensions/value_as_variable_include_processor.rb +0 -61
  74. data/lib/grafana_reporter/asciidoctor/panel_first_value_query.rb +0 -34
  75. data/lib/grafana_reporter/asciidoctor/panel_image_query.rb +0 -25
  76. data/lib/grafana_reporter/asciidoctor/panel_property_query.rb +0 -44
  77. data/lib/grafana_reporter/asciidoctor/panel_table_query.rb +0 -38
  78. data/lib/grafana_reporter/asciidoctor/query_mixin.rb +0 -310
  79. data/lib/grafana_reporter/asciidoctor/sql_first_value_query.rb +0 -37
  80. data/lib/grafana_reporter/asciidoctor/sql_table_query.rb +0 -39
@@ -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 {Logger::Severity}
28
+ # @param severity one of Logger::Severity
29
29
  def level=(severity)
30
30
  @additional_logger.level = severity
31
31
  end
@@ -0,0 +1,29 @@
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
+ @from = translate_date(@from, @variables['grafana_report_timestamp'], false, @variables['from_timezone'] ||
9
+ @variables['grafana_default_from_timezone'])
10
+ @to = translate_date(@to, @variables['grafana_report_timestamp'], true, @variables['to_timezone'] ||
11
+ @variables['grafana_default_to_timezone'])
12
+ # TODO: ensure that in case of timezones are specified, that they are also forwarded to the image renderer
13
+ # rename "render-" variables
14
+ @variables = @variables.each_with_object({}) { |(k, v), h| h[k.gsub(/^render-/, '')] = v }
15
+ @datasource = Grafana::ImageRenderingDatasource.new(nil)
16
+ end
17
+
18
+ # Returns the body of the http query, which contains the raw image.
19
+ def post_process
20
+ @result = @result[:content].first
21
+ raise ImageCouldNotBeRenderedError, @panel if @result.include?('<html')
22
+ end
23
+
24
+ # @see AbstractQuery#raw_query
25
+ def raw_query
26
+ { panel: @panel }
27
+ end
28
+ end
29
+ 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,79 @@
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
+ # Translates the from and to times.
7
+ # @see Grafana::AbstractQuery#pre_process
8
+ def pre_process
9
+ @datasource = @panel.datasource if @panel
10
+
11
+ @from = translate_date(@from, @variables['grafana_report_timestamp'], false, @variables['from_timezone'] ||
12
+ @variables['grafana_default_from_timezone'])
13
+ @to = translate_date(@to, @variables['grafana_report_timestamp'], true, @variables['to_timezone'] ||
14
+ @variables['grafana_default_to_timezone'])
15
+ @variables['result_type'] ||= Variable.new('')
16
+ end
17
+
18
+ # Executes {AbstractQuery#format_columns}, {AbstractQuery#replace_values} and
19
+ # {AbstractQuery#filter_columns} on the query results.
20
+ #
21
+ # Finally the results are formatted as a asciidoctor table.
22
+ # @see Grafana::AbstractQuery#post_process
23
+ def post_process
24
+ modify_results
25
+
26
+ case @variables['result_type'].raw_value
27
+ when /(?:panel_table|sql_table)/
28
+ result_to_table
29
+
30
+ when /(?:panel_value|sql_value)/
31
+ tmp = @result[:content] || []
32
+ @result = tmp.flatten.first
33
+
34
+ else
35
+ raise StandardError, "Unsupported 'result_type' received: '#{@variables['result_type'].raw_value}'"
36
+
37
+ end
38
+ end
39
+
40
+ # @see Grafana::AbstractQuery#raw_query
41
+ def raw_query
42
+ return @raw_query if @raw_query
43
+
44
+ case @variables['result_type'].raw_value
45
+ when /(?:panel_table|panel_value)/
46
+ @variables['query'] ? @panel.query(@variables['query'].raw_value) : @panel.query(nil)
47
+
48
+ when /(?:sql_table|sql_value)/
49
+ nil
50
+
51
+ else
52
+ raise StandardError, "Unsupported 'result_type' received: '#{@variables['result_type'].raw_value}'"
53
+
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def result_to_table
60
+ row_div = '| '
61
+ row_div = @variables['row_divider'].raw_value if @variables['row_divider'].is_a?(Grafana::Variable)
62
+ col_div = ' | '
63
+ col_div = @variables['column_divider'].raw_value if @variables['column_divider'].is_a?(Grafana::Variable)
64
+
65
+ @result = @result[:content].map do |row|
66
+ row_div + row.map do |item|
67
+ col_div == ' | ' ? item.to_s.gsub('|', '\\|') : item.to_s
68
+ end.join(col_div)
69
+ end
70
+ end
71
+
72
+ def modify_results
73
+ @result = format_columns(@result, @variables['format'])
74
+ @result = replace_values(@result, @variables.select { |k, _v| k =~ /^replace_values_\d+/ })
75
+ @result = filter_columns(@result, @variables['filter_columns'])
76
+ @result = transpose(@result, @variables['transpose'])
77
+ end
78
+ end
79
+ 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
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'ruby_grafana_reporter'
4
+
5
+ config = GrafanaReporter::Configuration.new
6
+ config.config = GrafanaReporter::Application::Application.load_config
7
+
8
+ GrafanaReporter::Asciidoctor::Report.new(config).register
@@ -18,12 +18,25 @@ require 'asciidoctor-pdf'
18
18
  require 'zip'
19
19
  require_relative 'VERSION'
20
20
 
21
+ # TODO: show error in document, if grafana could not be accessed
22
+ # TODO: show backtrace in extension error rescues
23
+ # TODO: improve ERB calls
24
+ # TODO: add test case for proper image generation
25
+ # TODO: allow to choose report-class during execution, not mandatory by config file
26
+ # TODO: add docu for ERB reports
27
+ # TODO: add docu for custom plugins
28
+ # TODO: add docu for webhook
29
+
30
+ # TODO: enable as asciidoctor extension
31
+ # TODO: build ADOC template as a result with resolved grafana content for further editing
32
+
21
33
  folders = [
22
34
  %w[grafana],
23
35
  %w[grafana_reporter logger],
24
36
  %w[grafana_reporter],
25
37
  %w[grafana_reporter asciidoctor extensions],
26
38
  %w[grafana_reporter asciidoctor],
39
+ %w[grafana_reporter erb],
27
40
  %w[grafana_reporter application]
28
41
  ]
29
42
  folders.each { |folder| Dir[File.join(__dir__, *folder, '*.rb')].sort.each { |file| require_relative file } }
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.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Kohlmeyer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-30 00:00:00.000000000 Z
11
+ date: 2021-05-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.5'
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.5'
40
+ version: '1.6'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rubyzip
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -47,7 +47,7 @@ dependencies:
47
47
  version: 1.1.1
48
48
  - - "<"
49
49
  - !ruby/object:Gem::Version
50
- version: '2.3'
50
+ version: '2.4'
51
51
  type: :runtime
52
52
  prerelease: false
53
53
  version_requirements: !ruby/object:Gem::Requirement
@@ -57,7 +57,7 @@ dependencies:
57
57
  version: 1.1.1
58
58
  - - "<"
59
59
  - !ruby/object:Gem::Version
60
- version: '2.3'
60
+ version: '2.4'
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: rspec
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -100,13 +100,9 @@ dependencies:
100
100
  - - "~>"
101
101
  - !ruby/object:Gem::Version
102
102
  version: '3.9'
103
- description: |
104
- Provides a standalone and a webservice frontend for creating reports
105
- based on asciidoctor, including interfaces to integrate dynamic content
106
- captured from grafana.
107
-
108
- By default the reports will be converted to PDF documents, whereas other
109
- target formats 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.
110
106
  email: kohly@gmx.de
111
107
  executables:
112
108
  - ruby-grafana-reporter
@@ -116,47 +112,55 @@ extra_rdoc_files:
116
112
  - LICENSE
117
113
  files:
118
114
  - "./lib/VERSION.rb"
119
- - "./lib/grafana/abstract_panel_query.rb"
120
- - "./lib/grafana/abstract_query.rb"
121
- - "./lib/grafana/abstract_sql_query.rb"
115
+ - "./lib/grafana/abstract_datasource.rb"
122
116
  - "./lib/grafana/dashboard.rb"
123
117
  - "./lib/grafana/errors.rb"
124
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"
125
124
  - "./lib/grafana/panel.rb"
126
- - "./lib/grafana/panel_image_query.rb"
125
+ - "./lib/grafana/prometheus_datasource.rb"
126
+ - "./lib/grafana/sql_datasource.rb"
127
+ - "./lib/grafana/unsupported_datasource.rb"
127
128
  - "./lib/grafana/variable.rb"
129
+ - "./lib/grafana/webrequest.rb"
130
+ - "./lib/grafana_reporter/abstract_query.rb"
128
131
  - "./lib/grafana_reporter/abstract_report.rb"
132
+ - "./lib/grafana_reporter/alerts_table_query.rb"
133
+ - "./lib/grafana_reporter/annotations_table_query.rb"
129
134
  - "./lib/grafana_reporter/application/application.rb"
130
135
  - "./lib/grafana_reporter/application/errors.rb"
131
136
  - "./lib/grafana_reporter/application/webservice.rb"
132
- - "./lib/grafana_reporter/asciidoctor/alerts_table_query.rb"
133
- - "./lib/grafana_reporter/asciidoctor/annotations_table_query.rb"
134
- - "./lib/grafana_reporter/asciidoctor/errors.rb"
135
- - "./lib/grafana_reporter/asciidoctor/extensions/alerts_table_include_processor.rb"
136
- - "./lib/grafana_reporter/asciidoctor/extensions/annotations_table_include_processor.rb"
137
- - "./lib/grafana_reporter/asciidoctor/extensions/panel_image_block_macro.rb"
138
- - "./lib/grafana_reporter/asciidoctor/extensions/panel_image_inline_macro.rb"
139
- - "./lib/grafana_reporter/asciidoctor/extensions/panel_property_inline_macro.rb"
140
- - "./lib/grafana_reporter/asciidoctor/extensions/panel_query_table_include_processor.rb"
141
- - "./lib/grafana_reporter/asciidoctor/extensions/panel_query_value_inline_macro.rb"
142
- - "./lib/grafana_reporter/asciidoctor/extensions/processor_mixin.rb"
143
- - "./lib/grafana_reporter/asciidoctor/extensions/show_environment_include_processor.rb"
144
- - "./lib/grafana_reporter/asciidoctor/extensions/show_help_include_processor.rb"
145
- - "./lib/grafana_reporter/asciidoctor/extensions/sql_table_include_processor.rb"
146
- - "./lib/grafana_reporter/asciidoctor/extensions/sql_value_inline_macro.rb"
147
- - "./lib/grafana_reporter/asciidoctor/extensions/value_as_variable_include_processor.rb"
148
- - "./lib/grafana_reporter/asciidoctor/panel_first_value_query.rb"
149
- - "./lib/grafana_reporter/asciidoctor/panel_image_query.rb"
150
- - "./lib/grafana_reporter/asciidoctor/panel_property_query.rb"
151
- - "./lib/grafana_reporter/asciidoctor/panel_table_query.rb"
152
- - "./lib/grafana_reporter/asciidoctor/query_mixin.rb"
137
+ - "./lib/grafana_reporter/asciidoctor/alerts_table_include_processor.rb"
138
+ - "./lib/grafana_reporter/asciidoctor/annotations_table_include_processor.rb"
139
+ - "./lib/grafana_reporter/asciidoctor/panel_image_block_macro.rb"
140
+ - "./lib/grafana_reporter/asciidoctor/panel_image_inline_macro.rb"
141
+ - "./lib/grafana_reporter/asciidoctor/panel_property_inline_macro.rb"
142
+ - "./lib/grafana_reporter/asciidoctor/panel_query_table_include_processor.rb"
143
+ - "./lib/grafana_reporter/asciidoctor/panel_query_value_inline_macro.rb"
144
+ - "./lib/grafana_reporter/asciidoctor/processor_mixin.rb"
153
145
  - "./lib/grafana_reporter/asciidoctor/report.rb"
154
- - "./lib/grafana_reporter/asciidoctor/sql_first_value_query.rb"
155
- - "./lib/grafana_reporter/asciidoctor/sql_table_query.rb"
146
+ - "./lib/grafana_reporter/asciidoctor/show_environment_include_processor.rb"
147
+ - "./lib/grafana_reporter/asciidoctor/show_help_include_processor.rb"
148
+ - "./lib/grafana_reporter/asciidoctor/sql_table_include_processor.rb"
149
+ - "./lib/grafana_reporter/asciidoctor/sql_value_inline_macro.rb"
150
+ - "./lib/grafana_reporter/asciidoctor/value_as_variable_include_processor.rb"
156
151
  - "./lib/grafana_reporter/configuration.rb"
152
+ - "./lib/grafana_reporter/console_configuration_wizard.rb"
153
+ - "./lib/grafana_reporter/demo_report_wizard.rb"
154
+ - "./lib/grafana_reporter/erb/report.rb"
157
155
  - "./lib/grafana_reporter/errors.rb"
158
- - "./lib/grafana_reporter/logger/two_way_logger.rb"
159
- - "./lib/ruby-grafana-reporter.rb"
156
+ - "./lib/grafana_reporter/help.rb"
157
+ - "./lib/grafana_reporter/logger/two_way_delegate_logger.rb"
158
+ - "./lib/grafana_reporter/panel_image_query.rb"
159
+ - "./lib/grafana_reporter/panel_property_query.rb"
160
+ - "./lib/grafana_reporter/query_value_query.rb"
161
+ - "./lib/grafana_reporter/report_webhook.rb"
162
+ - "./lib/ruby_grafana_extension.rb"
163
+ - "./lib/ruby_grafana_reporter.rb"
160
164
  - LICENSE
161
165
  - README.md
162
166
  - bin/ruby-grafana-reporter
@@ -174,7 +178,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
174
178
  requirements:
175
179
  - - ">="
176
180
  - !ruby/object:Gem::Version
177
- version: 1.9.3
181
+ version: '2.5'
178
182
  required_rubygems_version: !ruby/object:Gem::Requirement
179
183
  requirements:
180
184
  - - ">="
@@ -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[:format])
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