ruby-grafana-reporter 0.1.6 → 0.3.0

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 (49) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +0 -0
  3. data/README.md +95 -173
  4. data/bin/ruby-grafana-reporter +5 -0
  5. data/lib/VERSION.rb +5 -3
  6. data/lib/grafana/abstract_panel_query.rb +22 -20
  7. data/lib/grafana/abstract_query.rb +132 -127
  8. data/lib/grafana/abstract_sql_query.rb +51 -42
  9. data/lib/grafana/dashboard.rb +77 -66
  10. data/lib/grafana/errors.rb +66 -61
  11. data/lib/grafana/grafana.rb +133 -131
  12. data/lib/grafana/panel.rb +41 -39
  13. data/lib/grafana/panel_image_query.rb +52 -49
  14. data/lib/grafana/variable.rb +217 -259
  15. data/lib/grafana_reporter/abstract_report.rb +112 -109
  16. data/lib/grafana_reporter/application/application.rb +158 -229
  17. data/lib/grafana_reporter/application/errors.rb +33 -30
  18. data/lib/grafana_reporter/application/webservice.rb +230 -0
  19. data/lib/grafana_reporter/asciidoctor/alerts_table_query.rb +101 -99
  20. data/lib/grafana_reporter/asciidoctor/annotations_table_query.rb +96 -96
  21. data/lib/grafana_reporter/asciidoctor/errors.rb +40 -37
  22. data/lib/grafana_reporter/asciidoctor/extensions/alerts_table_include_processor.rb +92 -86
  23. data/lib/grafana_reporter/asciidoctor/extensions/annotations_table_include_processor.rb +91 -86
  24. data/lib/grafana_reporter/asciidoctor/extensions/panel_image_block_macro.rb +69 -67
  25. data/lib/grafana_reporter/asciidoctor/extensions/panel_image_inline_macro.rb +68 -65
  26. data/lib/grafana_reporter/asciidoctor/extensions/panel_property_inline_macro.rb +61 -58
  27. data/lib/grafana_reporter/asciidoctor/extensions/panel_query_table_include_processor.rb +78 -75
  28. data/lib/grafana_reporter/asciidoctor/extensions/panel_query_value_inline_macro.rb +73 -70
  29. data/lib/grafana_reporter/asciidoctor/extensions/processor_mixin.rb +20 -18
  30. data/lib/grafana_reporter/asciidoctor/extensions/show_environment_include_processor.rb +43 -41
  31. data/lib/grafana_reporter/asciidoctor/extensions/show_help_include_processor.rb +30 -202
  32. data/lib/grafana_reporter/asciidoctor/extensions/sql_table_include_processor.rb +70 -67
  33. data/lib/grafana_reporter/asciidoctor/extensions/sql_value_inline_macro.rb +66 -65
  34. data/lib/grafana_reporter/asciidoctor/extensions/value_as_variable_include_processor.rb +88 -57
  35. data/lib/grafana_reporter/asciidoctor/help.rb +435 -0
  36. data/lib/grafana_reporter/asciidoctor/panel_first_value_query.rb +36 -32
  37. data/lib/grafana_reporter/asciidoctor/panel_image_query.rb +28 -23
  38. data/lib/grafana_reporter/asciidoctor/panel_property_query.rb +44 -43
  39. data/lib/grafana_reporter/asciidoctor/panel_table_query.rb +40 -36
  40. data/lib/grafana_reporter/asciidoctor/query_mixin.rb +312 -309
  41. data/lib/grafana_reporter/asciidoctor/report.rb +179 -159
  42. data/lib/grafana_reporter/asciidoctor/sql_first_value_query.rb +42 -34
  43. data/lib/grafana_reporter/asciidoctor/sql_table_query.rb +44 -32
  44. data/lib/grafana_reporter/configuration.rb +304 -326
  45. data/lib/grafana_reporter/console_configuration_wizard.rb +269 -0
  46. data/lib/grafana_reporter/errors.rb +48 -38
  47. data/lib/grafana_reporter/logger/two_way_logger.rb +58 -52
  48. data/lib/ruby-grafana-reporter.rb +32 -27
  49. metadata +116 -16
@@ -1,326 +1,304 @@
1
- # In this namespace all objects needed for the grafana reporter are collected.
2
- module GrafanaReporter
3
- # Used to store the whole settings, which are necessary to run the reporter.
4
- # It can read configuration files, but might also be configured programmatically.
5
- #
6
- # This class also contains a function {#validate}, which ensures that the
7
- # provided settings are set properly.
8
- #
9
- # Using this class is embedded in the {Application::Application#configure_and_run}.
10
- #
11
- # TODO add config example
12
- class Configuration
13
- # @return [AbstractReport] specific report class, which should be used.
14
- attr_accessor :report_class
15
-
16
- # Returned by {#mode} if only a connection test shall be executed.
17
- MODE_CONNECTION_TEST = 'test'
18
- # Returned by {#mode} if only one configured report shall be rendered.
19
- MODE_SINGLE_RENDER = 'single-render'
20
- # Returned by {#mode} if the default webservice shall be started.
21
- MODE_SERVICE = 'webservice'
22
-
23
- def initialize
24
- @config = {}
25
- @logger = ::Logger.new(STDERR, level: :unknown)
26
- # TODO: set report class somewhere else, but make it known here
27
- self.report_class = Asciidoctor::Report
28
- end
29
-
30
- attr_reader :logger
31
-
32
- # @return [String] mode, in which the reporting shall be executed. One of {MODE_CONNECTION_TEST}, {MODE_SINGLE_RENDER} and {MODE_SERVICE}.
33
- def mode
34
- return MODE_SERVICE if get_config('grafana-reporter:run-mode') != MODE_CONNECTION_TEST and get_config('grafana-reporter:run-mode') != MODE_SINGLE_RENDER
35
-
36
- return get_config('grafana-reporter:run-mode')
37
- end
38
-
39
- # @return [String] configured report template. Only needed in {MODE_SINGLE_RENDER}.
40
- def template
41
- get_config('default-document-attributes:var-template')
42
- end
43
-
44
- # @return [String] destination filename for the report in {MODE_SINGLE_RENDER}.
45
- def to_file
46
- return get_config('to_file') || true if mode == MODE_SINGLE_RENDER
47
-
48
- get_config('to_file')
49
- end
50
-
51
- # @return [Array<String>] names of the configured grafana_instances.
52
- def grafana_instances
53
- instances = get_config('grafana')
54
- instances.keys
55
- end
56
-
57
- # @param instance [String] grafana instance name, for which the value shall be retrieved.
58
- # @return [String] configured 'host' for the requested grafana instance.
59
- def grafana_host(instance = 'default')
60
- host = get_config("grafana:#{instance}:host")
61
- raise GrafanaInstanceWithoutHostError, instance if host.nil?
62
-
63
- host
64
- end
65
-
66
- # @param instance [String] grafana instance name, for which the value shall be retrieved.
67
- # @return [String] configured 'api_key' for the requested grafana instance.
68
- def grafana_api_key(instance = 'default')
69
- get_config("grafana:#{instance}:api_key")
70
- end
71
-
72
- # @param instance [String] grafana instance name, for which the value shall be retrieved.
73
- # @return [Hash<String,Integer>] configured datasources for the requested grafana instance. Name as key, ID as value.
74
- def grafana_datasources(instance = 'default')
75
- hash = get_config("grafana:#{instance}:datasources")
76
- return nil if hash.nil?
77
-
78
- hash.map { |k, v| [k, v] }.to_h
79
- end
80
-
81
- # @return [String] configured folder, in which the report templates are stored including trailing slash. By default: current folder.
82
- def templates_folder
83
- result = get_config('grafana-reporter:templates-folder') || '.'
84
- result.sub!(%r{[/]*$}, '/') unless result.empty?
85
- result
86
- end
87
-
88
- # Returns configured folder, in which temporary images during report generation
89
- # shall be stored including trailing slash. Folder has to be a subfolder of
90
- # {#templates_folder}. By default: current folder.
91
- # @return [String] configured folder, in which temporary images shall be stored.
92
- def images_folder
93
- img_path = templates_folder
94
- img_path = img_path.empty? ? get_config('default-document-attributes:imagesdir').to_s : img_path + get_config('default-document-attributes:imagesdir').to_s
95
- img_path.empty? ? './' : img_path.sub(%r{[/]*$}, '/')
96
- end
97
-
98
- # @return [String] name of grafana instance, against which a test shall be executed
99
- def test_instance
100
- get_config('grafana-reporter:test-instance')
101
- end
102
-
103
- # @return [String] configured folder, in which the reports shall be stored including trailing slash. By default: current folder.
104
- def reports_folder
105
- result = get_config('grafana-reporter:reports-folder') || '.'
106
- result.sub!(%r{[/]*$}, '/') unless result.empty?
107
- result
108
- end
109
-
110
- # @return [Integer] how many hours a generated report shall be retained, before it shall be deleted. By default: 24.
111
- def report_retention
112
- get_config('grafana-reporter:report-retention') || 24
113
- end
114
-
115
- # @return [Integer] port, on which the webserver shall run. By default: 8815.
116
- def webserver_port
117
- get_config('grafana-reporter:webservice-port') || 8815
118
- end
119
-
120
- # The configuration made with the setting 'default-document-attributes' will
121
- # be passed 1:1 to the asciidoctor report service. It can be used to preconfigure
122
- # whatever is essential for the needed report renderings.
123
- # @return [Hash] configured document attributes
124
- def default_document_attributes
125
- get_config('default-document-attributes') || {}
126
- end
127
-
128
- # Used to load the configuration of a file or a manually created Hash to this
129
- # object. To make sure, that the configuration is valid, call {#validate}.
130
- #
131
- # NOTE: This function overwrites all existing configurations
132
- # @param hash [Hash] configuration settings
133
- # @return [void]
134
- def load_config(hash)
135
- @config = hash
136
- end
137
-
138
- # Used to do the configuration by a command line call. Therefore also help will
139
- # be shown, in case no parameter has been given.
140
- # @param params [Array<String>] command line parameters, mainly ARGV can be used.
141
- # @return [Integer] 0 if everything is fine, -1 if execution shall be aborted.
142
- def configure_by_command_line(params = [])
143
- params << '--help' if params.empty?
144
-
145
- parser = OptionParser.new do |opts|
146
- opts.banner = 'Usage: ruby ruby-grafana-reporter.rb CONFIG_FILE [options]'
147
-
148
- opts.on('-d', '--debug LEVEL', 'Specify detail level: FATAL, ERROR, WARN, INFO, DEBUG.') do |level|
149
- @logger.level = Object.const_get("::Logger::Severity::#{level}") if level =~ /(?:FATAL|ERROR|WARN|INFO|DEBUG)/
150
- end
151
-
152
- opts.on('--test GRAFANA_INSTANCE', 'test current configuration against given GRAFANA_INSTANCE') do |instance|
153
- if get_config('grafana-reporter')
154
- @config['grafana-reporter']['run-mode'] = 'test'
155
- else
156
- @config.merge!({'grafana-reporter' => {'run-mode' => 'test'} })
157
- end
158
- @config['grafana-reporter']['test-instance'] = instance
159
- end
160
-
161
- opts.on('-t', '--template TEMPLATE', 'Render a single ASCIIDOC template to PDF and exit') do |template|
162
- if get_config('grafana-reporter')
163
- @config['grafana-reporter']['run-mode'] = 'single-render'
164
- else
165
- @config.merge!({'grafana-reporter' => {'run-mode' => 'single-render'} })
166
- end
167
- @config['default-document-attributes']['var-template'] = template
168
- end
169
-
170
- opts.on('-o', '--output FILE', 'Output filename if only a single file is rendered') do |file|
171
- @config.merge!({ 'to_file' => file })
172
- end
173
-
174
- opts.on('-v', '--version', 'Version information') do
175
- puts GRAFANA_REPORTER_VERSION.join('.')
176
- return -1
177
- end
178
-
179
- opts.on('-h', '--help', 'Show this message') do
180
- puts opts
181
- return -1
182
- end
183
- end
184
-
185
- unless params.empty?
186
- if File.exist?(params[0])
187
- config_file = params.slice!(0)
188
- begin
189
- load_config(YAML.load_file(config_file))
190
- rescue StandardError => e
191
- raise ConfigurationError, "Could not read CONFIG_FILE '#{config_file}' (Error: #{e.message})"
192
- end
193
- end
194
- end
195
- parser.parse!(params)
196
-
197
- 0
198
- end
199
-
200
- # This function shall be called, before the configuration object is used in the
201
- # {Application::Application#run}. It ensures, that everything is setup properly
202
- # and all necessary folders exist. Appropriate errors are raised in case of errors.
203
- # @return [void]
204
- def validate
205
- validate_schema(schema, @config)
206
-
207
- # check if set folders exist
208
- raise FolderDoesNotExistError.new(reports_folder, 'reports-folder') unless File.directory?(reports_folder)
209
- raise FolderDoesNotExistError.new(templates - folder, 'templates-folder') unless File.directory?(templates_folder)
210
- raise FolderDoesNotExistError.new(images - folder, 'images-folder') unless File.directory?(images_folder)
211
- end
212
-
213
- private
214
-
215
- def get_config(path)
216
- return if path.nil?
217
-
218
- cur_pos = @config
219
- path.split(':').each do |subpath|
220
- cur_pos = cur_pos[subpath] if cur_pos
221
- end
222
- cur_pos
223
- end
224
-
225
- def validate_schema(schema, subject)
226
- return nil if subject.nil?
227
-
228
- schema.each do |key, config|
229
- type, min_occurence, next_level = config
230
-
231
- validate_schema(next_level, subject[key]) if next_level
232
-
233
- if key.nil?
234
- # apply to all on this level
235
- if subject.is_a?(Hash)
236
- if subject.length < min_occurence
237
- raise ConfigurationDoesNotMatchSchemaError.new(key, 'occur', min_occurence, subject.length)
238
- end
239
-
240
- subject.each do |k, _v|
241
- sub_scheme = {}
242
- sub_scheme[k] = schema[nil]
243
- validate_schema(sub_scheme, subject)
244
- end
245
-
246
- elsif subject.is_a?(Array)
247
- if subject.length < min_occurence
248
- raise ConfigurationDoesNotMatchSchemaError.new(key, 'occur', min_occurence, subject.length)
249
- end
250
-
251
- subject.each_index do |i|
252
- sub_scheme = {}
253
- sub_scheme[i] = schema[nil]
254
- validate_schema(sub_scheme, subject)
255
- end
256
-
257
- else
258
- raise ConfigurationError, "Unhandled configuration data type '#{subject.class}'."
259
- end
260
- else
261
- # apply to single item
262
- if subject.is_a?(Hash)
263
- if !subject.key?(key) && (min_occurence > 0)
264
- raise ConfigurationDoesNotMatchSchemaError.new(key, 'occur', min_occurence, 0)
265
- end
266
- if !subject[key].is_a?(type) && subject.key?(key)
267
- raise ConfigurationDoesNotMatchSchemaError.new(key, 'be a', type, subject[key].class)
268
- end
269
-
270
- elsif subject.is_a?(Array)
271
- if (subject.length < key) && (min_occurence > subject.length)
272
- raise ConfigurationDoesNotMatchSchemaError.new(key, 'occur', min_occurence, subject.length)
273
- end
274
- if !subject[key].is_a?(type) && (subject.length >= key)
275
- raise ConfigurationDoesNotMatchSchemaError.new(key, 'be a', type, subject[key].class)
276
- end
277
-
278
- else
279
- raise ConfigurationError, "Unhandled configuration data type '#{subject.class}'."
280
- end
281
- end
282
- end
283
-
284
- # validate also if subject has further configurations, which are not known by the reporter
285
- subject.each do |item, subitems|
286
- schema_config = schema[item] || schema[nil]
287
- if schema_config.nil?
288
- logger.warn("Item '#{item}' in configuration is unknown to the reporter and will be ignored")
289
- end
290
- end
291
- end
292
-
293
- def schema
294
- {
295
- 'grafana' =>
296
- [
297
- Hash, 1,
298
- {
299
- nil =>
300
- [
301
- Hash, 1,
302
- {
303
- 'host' => [String, 1],
304
- 'api_key' => [String, 0],
305
- 'datasources' => [Hash, 0, { nil => [Integer, 1] }]
306
- }
307
- ]
308
- }
309
- ],
310
- 'default-document-attributes' => [Hash, 0],
311
- 'grafana-reporter' =>
312
- [
313
- Hash, 0,
314
- {
315
- 'run-mode' => [String, 0],
316
- 'test-instance' => [String, 0],
317
- 'templates-folder' => [String, 0],
318
- 'reports-folder' => [String, 0],
319
- 'report-retention' => [Integer, 0],
320
- 'webservice-port' => [Integer, 0]
321
- }
322
- ]
323
- }
324
- end
325
- end
326
- end
1
+ # frozen_string_literal: true
2
+
3
+ # In this namespace all objects needed for the grafana reporter are collected.
4
+ module GrafanaReporter
5
+ # Used to store the whole settings, which are necessary to run the reporter.
6
+ # It can read configuration files, but might also be configured programmatically.
7
+ #
8
+ # This class also contains a function {#validate}, which ensures that the
9
+ # provided settings are set properly.
10
+ #
11
+ # Using this class is embedded in the {Application::Application#configure_and_run}.
12
+ #
13
+ class Configuration
14
+ # @return [AbstractReport] specific report class, which should be used.
15
+ attr_accessor :report_class
16
+
17
+ # Returned by {#mode} if only a connection test shall be executed.
18
+ MODE_CONNECTION_TEST = 'test'
19
+ # Returned by {#mode} if only one configured report shall be rendered.
20
+ MODE_SINGLE_RENDER = 'single-render'
21
+ # Returned by {#mode} if the default webservice shall be started.
22
+ MODE_SERVICE = 'webservice'
23
+
24
+ # Used to access the configuration hash. To make sure, that the configuration is
25
+ # valid, call {#validate}.
26
+ attr_reader :config
27
+
28
+ def initialize
29
+ @config = {}
30
+ @logger = ::Logger.new($stderr, level: :info)
31
+ end
32
+
33
+ attr_accessor :logger
34
+
35
+ # Used to overwrite the current configuration.
36
+ def config=(new_config)
37
+ @config = new_config
38
+ update_configuration
39
+ end
40
+
41
+ # @return [String] mode, in which the reporting shall be executed. One of {MODE_CONNECTION_TEST},
42
+ # {MODE_SINGLE_RENDER} and {MODE_SERVICE}.
43
+ def mode
44
+ if (get_config('grafana-reporter:run-mode') != MODE_CONNECTION_TEST) &&
45
+ (get_config('grafana-reporter:run-mode') != MODE_SINGLE_RENDER)
46
+ return MODE_SERVICE
47
+ end
48
+
49
+ get_config('grafana-reporter:run-mode')
50
+ end
51
+
52
+ # @return [String] full path of configured report template. Only needed in {MODE_SINGLE_RENDER}.
53
+ def template
54
+ return nil if get_config('default-document-attributes:var-template').nil?
55
+
56
+ "#{templates_folder}#{get_config('default-document-attributes:var-template')}.adoc"
57
+ end
58
+
59
+ # @return [String] path to ssl certificate file, if manually specified, or nil
60
+ def ssl_cert
61
+ get_config('grafana-reporter:ssl-cert')
62
+ end
63
+
64
+ # @return [String] destination filename for the report in {MODE_SINGLE_RENDER}.
65
+ def to_file
66
+ return get_config('to_file') || true if mode == MODE_SINGLE_RENDER
67
+
68
+ get_config('to_file')
69
+ end
70
+
71
+ # @return [Array<String>] names of the configured grafana_instances.
72
+ def grafana_instances
73
+ instances = get_config('grafana')
74
+ instances.keys
75
+ end
76
+
77
+ # @param instance [String] grafana instance name, for which the value shall be retrieved.
78
+ # @return [String] configured 'host' for the requested grafana instance.
79
+ def grafana_host(instance = 'default')
80
+ host = get_config("grafana:#{instance}:host")
81
+ raise GrafanaInstanceWithoutHostError, instance if host.nil?
82
+
83
+ host
84
+ end
85
+
86
+ # @param instance [String] grafana instance name, for which the value shall be retrieved.
87
+ # @return [String] configured 'api_key' for the requested grafana instance.
88
+ def grafana_api_key(instance = 'default')
89
+ get_config("grafana:#{instance}:api_key")
90
+ end
91
+
92
+ # @return [String] configured folder, in which the report templates are stored including trailing slash.
93
+ # By default: current folder.
94
+ def templates_folder
95
+ result = get_config('grafana-reporter:templates-folder') || '.'
96
+ return result.sub(%r{/*$}, '/') unless result.empty?
97
+
98
+ result
99
+ end
100
+
101
+ # Returns configured folder, in which temporary images during report generation
102
+ # shall be stored including trailing slash. Folder has to be a subfolder of
103
+ # {#templates_folder}. By default: current folder.
104
+ # @return [String] configured folder, in which temporary images shall be stored.
105
+ def images_folder
106
+ img_path = templates_folder
107
+ img_path = if img_path.empty?
108
+ get_config('default-document-attributes:imagesdir').to_s
109
+ else
110
+ img_path + get_config('default-document-attributes:imagesdir').to_s
111
+ end
112
+ img_path.empty? ? './' : img_path.sub(%r{/*$}, '/')
113
+ end
114
+
115
+ # @return [String] name of grafana instance, against which a test shall be executed
116
+ def test_instance
117
+ get_config('grafana-reporter:test-instance')
118
+ end
119
+
120
+ # @return [String] configured folder, in which the reports shall be stored including trailing slash.
121
+ # By default: current folder.
122
+ def reports_folder
123
+ result = get_config('grafana-reporter:reports-folder') || '.'
124
+ return result.sub(%r{/*$}, '/') unless result.empty?
125
+
126
+ result
127
+ end
128
+
129
+ # @return [Integer] how many hours a generated report shall be retained, before it shall be deleted.
130
+ # By default: 24.
131
+ def report_retention
132
+ get_config('grafana-reporter:report-retention') || 24
133
+ end
134
+
135
+ # @return [Integer] port, on which the webserver shall run. By default: 8815.
136
+ def webserver_port
137
+ get_config('grafana-reporter:webservice-port') || 8815
138
+ end
139
+
140
+ # The configuration made with the setting 'default-document-attributes' will
141
+ # be passed 1:1 to the asciidoctor report service. It can be used to preconfigure
142
+ # whatever is essential for the needed report renderings.
143
+ # @return [Hash] configured document attributes
144
+ def default_document_attributes
145
+ get_config('default-document-attributes') || {}
146
+ end
147
+
148
+ # This function shall be called, before the configuration object is used in the
149
+ # {Application::Application#run}. It ensures, that everything is setup properly
150
+ # and all necessary folders exist. Appropriate errors are raised in case of errors.
151
+ # @param explicit [Boolean] true, if validation shall expect explicit (wizard) configuration file
152
+ # @return [void]
153
+ def validate(explicit = false)
154
+ check_deprecation
155
+ validate_schema(schema(explicit), @config)
156
+
157
+ # check if set folders exist
158
+ raise FolderDoesNotExistError.new(reports_folder, 'reports-folder') unless File.directory?(reports_folder)
159
+ raise FolderDoesNotExistError.new(templates_folder, 'templates-folder') unless File.directory?(templates_folder)
160
+ raise FolderDoesNotExistError.new(images_folder, 'images-folder') unless File.directory?(images_folder)
161
+ end
162
+
163
+ # Can be used to configure or overwrite single parameters.
164
+ #
165
+ # @param path [String] path of the paramter to set, e.g. +grafana-reporter:webservice-port+
166
+ # @param value [Object] value to set
167
+ def set_param(path, value)
168
+ return if path.nil?
169
+
170
+ levels = path.split(':')
171
+ last_level = levels.pop
172
+
173
+ cur_pos = @config
174
+ levels.each do |subpath|
175
+ cur_pos[subpath] = {} unless cur_pos[subpath]
176
+ cur_pos = cur_pos[subpath]
177
+ end
178
+
179
+ cur_pos[last_level] = value
180
+ update_configuration
181
+ end
182
+
183
+ # Merge the given configuration object settings with the current config, i.e. overwrite and add all
184
+ # settings from the given config, but keep the not specified configs from the current object.
185
+ #
186
+ # param other_config [Configuration] other configuration object
187
+ def merge!(other_config)
188
+ config.merge!(other_config.config) { |_key, v1, v2| v1.is_a?(Hash) && v2.is_a?(Hash) ? v1.merge(v2) : v2 }
189
+ update_configuration
190
+ end
191
+
192
+ private
193
+
194
+ def check_deprecation
195
+ return if report_class
196
+
197
+ logger.warn('DEPRECATION WARNING: Your configuration explicitly needs to specify the '\
198
+ '\'grafana-reporter:report-class\' value. Currently this defaults to '\
199
+ '\'GrafanaReporter::Asciidoctor::Report\'. You can get rid of this warning, if you '\
200
+ 'explicitly set this configuration in your configuration file. Setting this default will be '\
201
+ 'removed in a future version.')
202
+ set_param('grafana-reporter:report-class', 'GrafanaReporter::Asciidoctor::Report')
203
+ end
204
+
205
+ def update_configuration
206
+ debug_level = get_config('grafana-reporter:debug-level')
207
+ rep_class = get_config('grafana-reporter:report-class')
208
+
209
+ @logger.level = Object.const_get("::Logger::Severity::#{debug_level}") if debug_level =~ /DEBUG|INFO|WARN|
210
+ ERROR|FATAL|UNKNOWN/x
211
+ self.report_class = Object.const_get(rep_class) if rep_class
212
+ end
213
+
214
+ def get_config(path)
215
+ return if path.nil?
216
+
217
+ cur_pos = @config
218
+ path.split(':').each do |subpath|
219
+ cur_pos = cur_pos[subpath] if cur_pos
220
+ end
221
+ cur_pos
222
+ end
223
+
224
+ def validate_schema(schema, subject)
225
+ return nil if subject.nil?
226
+
227
+ schema.each do |key, config|
228
+ type, min_occurence, next_level = config
229
+
230
+ validate_schema(next_level, subject[key]) if next_level
231
+
232
+ if key.nil?
233
+ # apply to all on this level
234
+ raise ConfigurationError, "Unhandled configuration data type '#{subject.class}'." unless subject.is_a?(Hash)
235
+
236
+ if subject.length < min_occurence
237
+ raise ConfigurationDoesNotMatchSchemaError.new(key, 'occur', min_occurence, subject.length)
238
+ end
239
+
240
+ subject.each do |k, _v|
241
+ sub_scheme = {}
242
+ sub_scheme[k] = schema[nil]
243
+ validate_schema(sub_scheme, subject)
244
+ end
245
+
246
+ # apply to single item
247
+ elsif subject.is_a?(Hash)
248
+ if !subject.key?(key) && min_occurence.positive?
249
+ raise ConfigurationDoesNotMatchSchemaError.new(key, 'occur', min_occurence, 0)
250
+ end
251
+ if !subject[key].is_a?(type) && subject.key?(key)
252
+ raise ConfigurationDoesNotMatchSchemaError.new(key, 'be a', type, subject[key].class)
253
+ end
254
+
255
+ else
256
+ raise ConfigurationError, "Unhandled configuration data type '#{subject.class}'."
257
+ end
258
+ end
259
+
260
+ # validate also if subject has further configurations, which are not known by the reporter
261
+ subject.each do |item, _subitems|
262
+ schema_config = schema[item] || schema[nil]
263
+ if schema_config.nil?
264
+ logger.warn("Item '#{item}' in configuration is unknown to the reporter and will be ignored")
265
+ end
266
+ end
267
+ end
268
+
269
+ def schema(explicit)
270
+ {
271
+ 'grafana' =>
272
+ [
273
+ Hash, 1,
274
+ {
275
+ nil =>
276
+ [
277
+ Hash, 1,
278
+ {
279
+ 'host' => [String, 1],
280
+ 'api_key' => [String, 0]
281
+ }
282
+ ]
283
+ }
284
+ ],
285
+ 'default-document-attributes' => [Hash, explicit ? 1 : 0],
286
+ 'grafana-reporter' =>
287
+ [
288
+ Hash, 1,
289
+ {
290
+ 'debug-level' => [String, 0],
291
+ 'run-mode' => [String, 0],
292
+ 'test-instance' => [String, 0],
293
+ 'templates-folder' => [String, explicit ? 1 : 0],
294
+ 'report-class' => [String, 1],
295
+ 'reports-folder' => [String, explicit ? 1 : 0],
296
+ 'report-retention' => [Integer, explicit ? 1 : 0],
297
+ 'ssl-cert' => [String, 0],
298
+ 'webservice-port' => [Integer, explicit ? 1 : 0]
299
+ }
300
+ ]
301
+ }
302
+ end
303
+ end
304
+ end