ruby-grafana-reporter 0.3.0 → 0.4.4
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 +337 -170
- data/bin/ruby-grafana-reporter +5 -5
- data/lib/VERSION.rb +3 -2
- data/lib/grafana/abstract_datasource.rb +149 -0
- data/lib/grafana/dashboard.rb +1 -3
- data/lib/grafana/errors.rb +20 -5
- data/lib/grafana/grafana.rb +52 -57
- 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 +37 -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 +10 -4
- data/lib/grafana/prometheus_datasource.rb +67 -0
- data/lib/grafana/sql_datasource.rb +70 -0
- data/lib/grafana/unsupported_datasource.rb +7 -0
- data/lib/grafana/variable.rb +27 -21
- data/lib/grafana/webrequest.rb +71 -0
- data/lib/grafana_reporter/abstract_query.rb +478 -0
- data/lib/grafana_reporter/abstract_report.rb +152 -18
- data/lib/grafana_reporter/abstract_table_format_strategy.rb +34 -0
- data/lib/grafana_reporter/alerts_table_query.rb +43 -0
- data/lib/grafana_reporter/annotations_table_query.rb +42 -0
- data/lib/grafana_reporter/application/application.rb +28 -25
- data/lib/grafana_reporter/application/webservice.rb +80 -39
- data/lib/grafana_reporter/asciidoctor/adoc_plain_table_format_strategy.rb +25 -0
- data/lib/grafana_reporter/asciidoctor/alerts_table_include_processor.rb +92 -0
- data/lib/grafana_reporter/asciidoctor/annotations_table_include_processor.rb +91 -0
- data/lib/grafana_reporter/asciidoctor/help.rb +336 -313
- data/lib/grafana_reporter/asciidoctor/panel_image_block_macro.rb +78 -0
- data/lib/grafana_reporter/asciidoctor/panel_image_inline_macro.rb +80 -0
- data/lib/grafana_reporter/asciidoctor/panel_property_inline_macro.rb +74 -0
- data/lib/grafana_reporter/asciidoctor/panel_query_table_include_processor.rb +99 -0
- data/lib/grafana_reporter/asciidoctor/panel_query_value_inline_macro.rb +93 -0
- data/lib/grafana_reporter/asciidoctor/processor_mixin.rb +50 -0
- data/lib/grafana_reporter/asciidoctor/report.rb +41 -82
- 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 +94 -0
- data/lib/grafana_reporter/asciidoctor/sql_value_inline_macro.rb +90 -0
- data/lib/grafana_reporter/asciidoctor/value_as_variable_include_processor.rb +90 -0
- data/lib/grafana_reporter/configuration.rb +26 -8
- data/lib/grafana_reporter/console_configuration_wizard.rb +109 -67
- data/lib/grafana_reporter/csv_table_format_strategy.rb +23 -0
- data/lib/grafana_reporter/demo_report_wizard.rb +104 -0
- data/lib/grafana_reporter/erb/demo_report_builder.rb +46 -0
- data/lib/grafana_reporter/erb/report.rb +36 -0
- data/lib/grafana_reporter/erb/report_jail.rb +21 -0
- data/lib/grafana_reporter/errors.rb +57 -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 +39 -0
- data/lib/ruby_grafana_extension.rb +8 -0
- data/lib/{ruby-grafana-reporter.rb → ruby_grafana_reporter.rb} +1 -3
- metadata +49 -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 -101
- data/lib/grafana_reporter/asciidoctor/annotations_table_query.rb +0 -96
- 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 -30
- 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 -88
- data/lib/grafana_reporter/asciidoctor/panel_first_value_query.rb +0 -36
- data/lib/grafana_reporter/asciidoctor/panel_image_query.rb +0 -28
- data/lib/grafana_reporter/asciidoctor/panel_property_query.rb +0 -44
- data/lib/grafana_reporter/asciidoctor/panel_table_query.rb +0 -40
- data/lib/grafana_reporter/asciidoctor/query_mixin.rb +0 -312
- data/lib/grafana_reporter/asciidoctor/sql_first_value_query.rb +0 -42
- data/lib/grafana_reporter/asciidoctor/sql_table_query.rb +0 -44
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module GrafanaReporter
|
4
|
-
# @abstract Override {#
|
4
|
+
# @abstract Override {#build} and {#progress}.
|
5
5
|
#
|
6
6
|
# This class is used to build a report on basis of a given configuration and
|
7
7
|
# template.
|
@@ -9,6 +9,13 @@ module GrafanaReporter
|
|
9
9
|
# Objects of this class are also stored in {Application::Application}, unless
|
10
10
|
# the retention time is over.
|
11
11
|
class AbstractReport
|
12
|
+
# Array of supported event callback symbols
|
13
|
+
EVENT_CALLBACKS = %i[all on_before_create on_after_cancel on_after_finish].freeze
|
14
|
+
|
15
|
+
# Class variable for storing event listeners
|
16
|
+
@@event_listeners = {}
|
17
|
+
@@event_listeners.default = []
|
18
|
+
|
12
19
|
# @return [String] path to the template
|
13
20
|
attr_reader :template
|
14
21
|
|
@@ -28,21 +35,38 @@ module GrafanaReporter
|
|
28
35
|
attr_reader :done
|
29
36
|
|
30
37
|
# @param config [Configuration] configuration object
|
31
|
-
|
32
|
-
# @param destination_file_or_path [String or File] path to the destination report or file object to use
|
33
|
-
# @param custom_attributes [Hash] custom attributes, which shall be merged with priority over the configuration
|
34
|
-
def initialize(config, template, destination_file_or_path = nil, custom_attributes = {})
|
38
|
+
def initialize(config)
|
35
39
|
@config = config
|
36
40
|
@logger = Logger::TwoWayDelegateLogger.new
|
37
41
|
@logger.additional_logger = @config.logger
|
38
|
-
@
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
@grafana_instances = {}
|
43
|
+
|
44
|
+
init_before_create
|
45
|
+
end
|
46
|
+
|
47
|
+
# Registers a new event listener object.
|
48
|
+
# @param event [Symbol] one of EVENT_CALLBACKS
|
49
|
+
# @param listener [Object] object responding to #callback(event_symbol, object)
|
50
|
+
def self.add_event_listener(event, listener)
|
51
|
+
@@event_listeners[event] = [] if @@event_listeners[event] == []
|
52
|
+
@@event_listeners[event].push(listener)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Removes all registeres event listener objects
|
56
|
+
def self.clear_event_listeners
|
57
|
+
@@event_listeners = {}
|
58
|
+
@@event_listeners.default = []
|
59
|
+
end
|
60
|
+
|
61
|
+
# @param instance [String] requested grafana instance
|
62
|
+
# @return [Grafana::Grafana] the requested grafana instance.
|
63
|
+
def grafana(instance)
|
64
|
+
unless @grafana_instances[instance]
|
65
|
+
@grafana_instances[instance] = ::Grafana::Grafana.new(@config.grafana_host(instance),
|
66
|
+
@config.grafana_api_key(instance),
|
67
|
+
logger: @logger)
|
68
|
+
end
|
69
|
+
@grafana_instances[instance]
|
46
70
|
end
|
47
71
|
|
48
72
|
# Call to request cancelling the report generation.
|
@@ -50,6 +74,7 @@ module GrafanaReporter
|
|
50
74
|
def cancel!
|
51
75
|
@cancel = true
|
52
76
|
logger.info('Cancelling report generation invoked.')
|
77
|
+
notify(:on_after_cancel)
|
53
78
|
end
|
54
79
|
|
55
80
|
# @return [String] path to the report destination file
|
@@ -81,9 +106,12 @@ module GrafanaReporter
|
|
81
106
|
@error || []
|
82
107
|
end
|
83
108
|
|
84
|
-
# @return [String] status of the report,
|
109
|
+
# @return [String] status of the report as string, either 'not started', 'in progress', 'cancelling',
|
110
|
+
# 'cancelled', 'died' or 'finished'.
|
85
111
|
def status
|
112
|
+
return 'not started' unless @start_time
|
86
113
|
return 'cancelled' if done && cancel
|
114
|
+
return 'cancelling' if !done && cancel
|
87
115
|
return 'finished' if done && error.empty?
|
88
116
|
return 'died' if done && !error.empty?
|
89
117
|
|
@@ -96,17 +124,123 @@ module GrafanaReporter
|
|
96
124
|
logger.internal_messages
|
97
125
|
end
|
98
126
|
|
99
|
-
#
|
100
|
-
#
|
127
|
+
# Is being called to start the report generation. To execute the specific report generation, this function
|
128
|
+
# calls the abstract {#build} method with the given parameters.
|
129
|
+
# @param template [String] path to the template to be used, trailing extension may be omitted, whereas {#default_template_extension} will be appended
|
130
|
+
# @param destination_file_or_path [String or File] path to the destination report or file object to use
|
131
|
+
# @param custom_attributes [Hash] custom attributes, which shall be merged with priority over the configuration
|
101
132
|
# @return [void]
|
102
|
-
def create_report
|
103
|
-
|
133
|
+
def create_report(template, destination_file_or_path = nil, custom_attributes = {})
|
134
|
+
init_before_create
|
135
|
+
@template = template
|
136
|
+
@destination_file_or_path = destination_file_or_path
|
137
|
+
@custom_attributes = custom_attributes
|
138
|
+
|
139
|
+
# automatically add extension, if a file with default template extension exists
|
140
|
+
@template = "#{@template}.#{self.class.default_template_extension}" if File.file?("#{@template}.#{self.class.default_template_extension}") && !File.file?(@template.to_s)
|
141
|
+
raise MissingTemplateError, @template.to_s unless File.file?(@template.to_s)
|
142
|
+
|
143
|
+
notify(:on_before_create)
|
144
|
+
@start_time = Time.new
|
145
|
+
logger.info("Report started at #{@start_time}")
|
146
|
+
build
|
147
|
+
rescue MissingTemplateError => e
|
148
|
+
@logger.error(e.message)
|
149
|
+
@error = [e.message]
|
150
|
+
done!
|
151
|
+
raise e
|
152
|
+
rescue StandardError => e
|
153
|
+
# catch all errors during execution
|
154
|
+
died_with_error(e)
|
155
|
+
raise e
|
156
|
+
ensure
|
157
|
+
done!
|
104
158
|
end
|
105
159
|
|
106
160
|
# @abstract
|
161
|
+
# Needs to be overridden by the report implementation.
|
162
|
+
def build(template, destination_file_or_path, custom_attributes)
|
163
|
+
raise NotImplementedError
|
164
|
+
end
|
165
|
+
|
166
|
+
# Used to calculate the progress of a report. By default expects +@total_steps+ to contain the total
|
167
|
+
# number of steps, which will be processed with each call of {#next_step}.
|
107
168
|
# @return [Integer] number between 0 and 100, representing the current progress of the report creation.
|
108
169
|
def progress
|
170
|
+
return @current_pos.to_i if @total_steps.to_i.zero?
|
171
|
+
|
172
|
+
@current_pos.to_f / @total_steps
|
173
|
+
end
|
174
|
+
|
175
|
+
# Increments the progress.
|
176
|
+
# @return [Integer] number of the current progress position.
|
177
|
+
def next_step
|
178
|
+
@current_pos += 1
|
179
|
+
@current_pos
|
180
|
+
end
|
181
|
+
|
182
|
+
# @abstract
|
183
|
+
# Provided class objects need to implement a method +build_demo_entry(panel)+.
|
184
|
+
# @return [Array<Class>] array of class objects, which shall be included in a demo report
|
185
|
+
def self.demo_report_classes
|
109
186
|
raise NotImplementedError
|
110
187
|
end
|
188
|
+
|
189
|
+
# @abstract
|
190
|
+
# @return [String] specifying the default extension of a template file
|
191
|
+
def self.default_template_extension
|
192
|
+
raise NotImplementedError
|
193
|
+
end
|
194
|
+
|
195
|
+
# @abstract
|
196
|
+
# @return [String] specifying the default extension of a rendered result file
|
197
|
+
def self.default_result_extension
|
198
|
+
raise NotImplementedError
|
199
|
+
end
|
200
|
+
|
201
|
+
private
|
202
|
+
|
203
|
+
# Called, if the report generation has died with an error.
|
204
|
+
# @param error [StandardError] occured error
|
205
|
+
# @return [void]
|
206
|
+
def died_with_error(error)
|
207
|
+
@error = [error.message] << [error.backtrace]
|
208
|
+
done!
|
209
|
+
end
|
210
|
+
|
211
|
+
def init_before_create
|
212
|
+
@done = false
|
213
|
+
@start_time = nil
|
214
|
+
@end_time = nil
|
215
|
+
@cancel = false
|
216
|
+
@current_pos = 0
|
217
|
+
end
|
218
|
+
|
219
|
+
def done!
|
220
|
+
return if @done
|
221
|
+
|
222
|
+
@destination_file_or_path.close if @destination_file_or_path.is_a?(File)
|
223
|
+
@done = true
|
224
|
+
@end_time = Time.new
|
225
|
+
@start_time ||= @end_time
|
226
|
+
logger.info("Report creation ended after #{@end_time.to_i - @start_time.to_i} seconds with status '#{status}'")
|
227
|
+
notify(:on_after_finish)
|
228
|
+
end
|
229
|
+
|
230
|
+
def notify(event)
|
231
|
+
(@@event_listeners[:all] + @@event_listeners[event]).each do |listener|
|
232
|
+
logger.debug("Informing event listener '#{listener.class}' about event '#{event}' for report '#{object_id}'.")
|
233
|
+
begin
|
234
|
+
res = listener.callback(event, self)
|
235
|
+
logger.debug("Event listener '#{listener.class}' for event '#{event}' and report '#{object_id}' returned "\
|
236
|
+
"with result '#{res}'.")
|
237
|
+
rescue StandardError => e
|
238
|
+
msg = "Event listener '#{listener.class}' for event '#{event}' and report '#{object_id}' returned with "\
|
239
|
+
"error: #{e.message} - #{e.backtrace}."
|
240
|
+
puts msg
|
241
|
+
logger.error(msg)
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
111
245
|
end
|
112
246
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrafanaReporter
|
4
|
+
# The abstract base class, which is to be implemented for different table
|
5
|
+
# output formats. By implementing this class, you e.g. can decide if a table
|
6
|
+
# will be formatted as CSV, JSON or any other format.
|
7
|
+
class AbstractTableFormatStrategy
|
8
|
+
@@subclasses = []
|
9
|
+
|
10
|
+
def self.inherited(obj)
|
11
|
+
@@subclasses << obj
|
12
|
+
end
|
13
|
+
|
14
|
+
# @param abbreviation [String] name of the requested table format strategy
|
15
|
+
# @return [AbstractTableFormatStrategy] fitting strategy instance for the given name
|
16
|
+
def self.get(abbreviation)
|
17
|
+
@@subclasses.select { |item| item.abbreviation == abbreviation }.first.new
|
18
|
+
end
|
19
|
+
|
20
|
+
# @abstract
|
21
|
+
# @return [String] short name of the current stategy, under which it shall be accessible
|
22
|
+
def self.abbreviation
|
23
|
+
raise NotImplementedError
|
24
|
+
end
|
25
|
+
|
26
|
+
# @abstract
|
27
|
+
# @param column [Array] datasource table result
|
28
|
+
# @param include_headline [Boolean] true, if headline should be included in result
|
29
|
+
# @return [String] formatted in table format
|
30
|
+
def format(content, include_headline)
|
31
|
+
raise NotImplementedError
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrafanaReporter
|
4
|
+
# This class is used to query alerts from grafana.
|
5
|
+
class AlertsTableQuery < AbstractQuery
|
6
|
+
# Check if mandatory {Grafana::Variable} +columns+ is specified in variables.
|
7
|
+
#
|
8
|
+
# The value of the +columns+ variable has to be a comma separated list of column titles, which
|
9
|
+
# need to be included in the following list:
|
10
|
+
# - limit
|
11
|
+
# - dashboardId
|
12
|
+
# - panelId
|
13
|
+
# - query
|
14
|
+
# - state
|
15
|
+
# - folderId
|
16
|
+
# - dashboardQuery
|
17
|
+
# - dashboardTag
|
18
|
+
# @return [void]
|
19
|
+
def pre_process
|
20
|
+
raise MissingMandatoryAttributeError, 'columns' unless @raw_query['columns']
|
21
|
+
|
22
|
+
@datasource = Grafana::GrafanaAlertsDatasource.new(nil)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Filter the query result for the given columns and sets the result in the preformatted SQL
|
26
|
+
# result stlye.
|
27
|
+
#
|
28
|
+
# Additionally it applies {AbstractQuery#format_columns}, {AbstractQuery#replace_values} and
|
29
|
+
# {AbstractQuery#filter_columns}.
|
30
|
+
# @return [void]
|
31
|
+
def post_process
|
32
|
+
@result = format_columns(@result, @variables['format'])
|
33
|
+
@result = replace_values(@result, @variables.select { |k, _v| k =~ /^replace_values_\d+/ })
|
34
|
+
@result = filter_columns(@result, @variables['filter_columns'])
|
35
|
+
|
36
|
+
@result = format_table_output(@result,
|
37
|
+
row_divider: @variables['row_divider'],
|
38
|
+
column_divider: @variables['column_divider'],
|
39
|
+
table_formatter: @variables['table_formatter'],
|
40
|
+
include_headline: @variables['include_headline'])
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrafanaReporter
|
4
|
+
# This class is used to query annotations from grafana.
|
5
|
+
class AnnotationsTableQuery < AbstractQuery
|
6
|
+
# Check if mandatory {Grafana::Variable} +columns+ is specified in variables.
|
7
|
+
#
|
8
|
+
# The value of the +columns+ variable has to be a comma separated list of column titles, which
|
9
|
+
# need to be included in the following list:
|
10
|
+
# - limit
|
11
|
+
# - alertId
|
12
|
+
# - userId
|
13
|
+
# - type
|
14
|
+
# - tags
|
15
|
+
# - dashboardId
|
16
|
+
# - panelId
|
17
|
+
# @return [void]
|
18
|
+
def pre_process
|
19
|
+
raise MissingMandatoryAttributeError, 'columns' unless @raw_query['columns']
|
20
|
+
|
21
|
+
@datasource = Grafana::GrafanaAnnotationsDatasource.new(nil)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Filters the query result for the given columns and sets the result
|
25
|
+
# in the preformatted SQL result style.
|
26
|
+
#
|
27
|
+
# Additionally it applies {AbstractQuery#format_columns}, {AbstractQuery#replace_values} and
|
28
|
+
# {AbstractQuery#filter_columns}.
|
29
|
+
# @return [void]
|
30
|
+
def post_process
|
31
|
+
@result = format_columns(@result, @variables['format'])
|
32
|
+
@result = replace_values(@result, @variables.select { |k, _v| k =~ /^replace_values_\d+/ })
|
33
|
+
@result = filter_columns(@result, @variables['filter_columns'])
|
34
|
+
|
35
|
+
@result = format_table_output(@result,
|
36
|
+
row_divider: @variables['row_divider'],
|
37
|
+
column_divider: @variables['column_divider'],
|
38
|
+
table_formatter: @variables['table_formatter'],
|
39
|
+
include_headline: @variables['include_headline'])
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -13,14 +13,15 @@ module GrafanaReporter
|
|
13
13
|
# It can be run to test the grafana connection, render a single template
|
14
14
|
# or run as a service.
|
15
15
|
class Application
|
16
|
-
# Default file name for grafana reporter configuration file
|
17
|
-
CONFIG_FILE = 'grafana_reporter.config'
|
18
|
-
|
19
16
|
# Contains the {Configuration} object of the application.
|
20
17
|
attr_accessor :config
|
21
18
|
|
19
|
+
# Stores the {Webservice} object of the application
|
20
|
+
attr_reader :webservice
|
21
|
+
|
22
22
|
def initialize
|
23
23
|
@config = Configuration.new
|
24
|
+
@webservice = Webservice.new
|
24
25
|
end
|
25
26
|
|
26
27
|
# This is the main method, which is called, if the application is
|
@@ -28,22 +29,26 @@ module GrafanaReporter
|
|
28
29
|
# @param params [Array<String>] command line parameters, mainly ARGV can be used.
|
29
30
|
# @return [Integer] 0 if everything is fine, -1 if execution aborted.
|
30
31
|
def configure_and_run(params = [])
|
31
|
-
config_file =
|
32
|
+
config_file = Configuration::DEFAULT_CONFIG_FILE_NAME
|
32
33
|
tmp_config = Configuration.new
|
33
34
|
action_wizard = false
|
34
35
|
|
35
36
|
parser = OptionParser.new do |opts|
|
36
|
-
if ENV['OCRA_EXECUTABLE']
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
opts.banner = if ENV['OCRA_EXECUTABLE']
|
38
|
+
"Usage: #{ENV['OCRA_EXECUTABLE'].gsub("#{Dir.pwd}/".gsub('/', '\\'), '')} [options]"
|
39
|
+
else
|
40
|
+
"Usage: #{Gem.ruby} #{$PROGRAM_NAME} [options]"
|
41
|
+
end
|
41
42
|
|
42
43
|
opts.on('-c', '--config CONFIG_FILE_NAME', 'Specify custom configuration file,'\
|
43
|
-
" instead of #{
|
44
|
+
" instead of #{Configuration::DEFAULT_CONFIG_FILE_NAME}.") do |file_name|
|
44
45
|
config_file = file_name
|
45
46
|
end
|
46
47
|
|
48
|
+
opts.on('-r', '--register FILE', 'Register a custom plugin, e.g. your own Datasource implementation') do |plugin|
|
49
|
+
require plugin
|
50
|
+
end
|
51
|
+
|
47
52
|
opts.on('-d', '--debug LEVEL', 'Specify detail level: FATAL, ERROR, WARN, INFO, DEBUG.') do |level|
|
48
53
|
tmp_config.set_param('grafana-reporter:debug-level', level)
|
49
54
|
end
|
@@ -61,7 +66,7 @@ module GrafanaReporter
|
|
61
66
|
|
62
67
|
opts.on('--ssl-cert FILE', 'Manually specify a SSL cert file for HTTPS connection to grafana. Only '\
|
63
68
|
'needed if not working properly otherwise.') do |file|
|
64
|
-
if File.
|
69
|
+
if File.file?(file)
|
65
70
|
tmp_config.set_param('grafana-reporter:ssl-cert', file)
|
66
71
|
else
|
67
72
|
config.logger.warn("SSL certificate file #{file} does not exist. Setting will be ignored.")
|
@@ -102,22 +107,14 @@ module GrafanaReporter
|
|
102
107
|
end
|
103
108
|
|
104
109
|
# abort if config file does not exist
|
105
|
-
unless File.
|
110
|
+
unless File.file?(config_file)
|
106
111
|
puts "Config file '#{config_file}' does not exist. Consider calling the configuration wizard"\
|
107
112
|
' with option \'-w\' or use \'-h\' to see help message. Aborting.'
|
108
113
|
return -1
|
109
114
|
end
|
110
115
|
|
111
|
-
# read config file
|
112
|
-
config_hash = nil
|
113
|
-
begin
|
114
|
-
config_hash = YAML.load_file(config_file)
|
115
|
-
rescue StandardError => e
|
116
|
-
raise ConfigurationError, "Could not read config file '#{config_file}' (Error: #{e.message})"
|
117
|
-
end
|
118
|
-
|
119
116
|
# merge command line configuration with read config file
|
120
|
-
@config.
|
117
|
+
@config.load_config_from_file(config_file)
|
121
118
|
@config.merge!(tmp_config)
|
122
119
|
|
123
120
|
run
|
@@ -137,18 +134,24 @@ module GrafanaReporter
|
|
137
134
|
when Configuration::MODE_CONNECTION_TEST
|
138
135
|
res = Grafana::Grafana.new(config.grafana_host(config.test_instance),
|
139
136
|
config.grafana_api_key(config.test_instance),
|
140
|
-
logger: config.logger
|
137
|
+
logger: config.logger).test_connection
|
141
138
|
puts res
|
142
139
|
|
143
140
|
when Configuration::MODE_SINGLE_RENDER
|
144
141
|
begin
|
145
|
-
|
142
|
+
template_ext = config.report_class.default_template_extension
|
143
|
+
report_ext = config.report_class.default_result_extension
|
144
|
+
default_to_file = File.basename(config.template.to_s.gsub(/(?:\.#{template_ext})?$/, ".#{report_ext}"))
|
145
|
+
|
146
|
+
to_file = config.to_file
|
147
|
+
to_file = "#{config.reports_folder}#{default_to_file}" if to_file == true
|
148
|
+
config.report_class.new(config).create_report(config.template, to_file)
|
146
149
|
rescue StandardError => e
|
147
|
-
puts e.message
|
150
|
+
puts "#{e.message}\n#{e.backtrace.join("\n")}"
|
148
151
|
end
|
149
152
|
|
150
153
|
when Configuration::MODE_SERVICE
|
151
|
-
|
154
|
+
@webservice.run(config)
|
152
155
|
end
|
153
156
|
|
154
157
|
0
|