ruby-grafana-reporter 0.3.0 → 0.4.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 (78) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +81 -71
  3. data/bin/ruby-grafana-reporter +5 -5
  4. data/lib/VERSION.rb +3 -2
  5. data/lib/grafana/abstract_datasource.rb +116 -0
  6. data/lib/grafana/dashboard.rb +1 -3
  7. data/lib/grafana/errors.rb +15 -0
  8. data/lib/grafana/grafana.rb +53 -56
  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 +44 -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 +39 -0
  16. data/lib/grafana/sql_datasource.rb +65 -0
  17. data/lib/grafana/variable.rb +1 -0
  18. data/lib/grafana/webrequest.rb +71 -0
  19. data/lib/grafana_reporter/abstract_query.rb +401 -0
  20. data/lib/grafana_reporter/abstract_report.rb +54 -3
  21. data/lib/grafana_reporter/alerts_table_query.rb +44 -0
  22. data/lib/grafana_reporter/annotations_table_query.rb +43 -0
  23. data/lib/grafana_reporter/application/application.rb +12 -8
  24. data/lib/grafana_reporter/application/webservice.rb +18 -6
  25. data/lib/grafana_reporter/asciidoctor/alerts_table_include_processor.rb +90 -0
  26. data/lib/grafana_reporter/asciidoctor/annotations_table_include_processor.rb +89 -0
  27. data/lib/grafana_reporter/asciidoctor/panel_image_block_macro.rb +76 -0
  28. data/lib/grafana_reporter/asciidoctor/panel_image_inline_macro.rb +77 -0
  29. data/lib/grafana_reporter/asciidoctor/panel_property_inline_macro.rb +72 -0
  30. data/lib/grafana_reporter/asciidoctor/panel_query_table_include_processor.rb +98 -0
  31. data/lib/grafana_reporter/asciidoctor/panel_query_value_inline_macro.rb +93 -0
  32. data/lib/grafana_reporter/asciidoctor/processor_mixin.rb +23 -0
  33. data/lib/grafana_reporter/asciidoctor/report.rb +24 -31
  34. data/lib/grafana_reporter/asciidoctor/show_environment_include_processor.rb +46 -0
  35. data/lib/grafana_reporter/asciidoctor/show_help_include_processor.rb +35 -0
  36. data/lib/grafana_reporter/asciidoctor/sql_table_include_processor.rb +92 -0
  37. data/lib/grafana_reporter/asciidoctor/sql_value_inline_macro.rb +88 -0
  38. data/lib/grafana_reporter/asciidoctor/value_as_variable_include_processor.rb +90 -0
  39. data/lib/grafana_reporter/configuration.rb +12 -6
  40. data/lib/grafana_reporter/console_configuration_wizard.rb +115 -65
  41. data/lib/grafana_reporter/demo_report_wizard.rb +87 -0
  42. data/lib/grafana_reporter/errors.rb +33 -0
  43. data/lib/grafana_reporter/help.rb +447 -0
  44. data/lib/grafana_reporter/logger/two_way_logger.rb +1 -1
  45. data/lib/grafana_reporter/panel_image_query.rb +29 -0
  46. data/lib/grafana_reporter/panel_property_query.rb +22 -0
  47. data/lib/grafana_reporter/query_value_query.rb +79 -0
  48. data/lib/grafana_reporter/report_webhook.rb +35 -0
  49. data/lib/{ruby-grafana-reporter.rb → ruby_grafana_reporter.rb} +0 -3
  50. metadata +37 -35
  51. data/lib/grafana/abstract_panel_query.rb +0 -22
  52. data/lib/grafana/abstract_query.rb +0 -132
  53. data/lib/grafana/abstract_sql_query.rb +0 -51
  54. data/lib/grafana/panel_image_query.rb +0 -52
  55. data/lib/grafana_reporter/asciidoctor/alerts_table_query.rb +0 -101
  56. data/lib/grafana_reporter/asciidoctor/annotations_table_query.rb +0 -96
  57. data/lib/grafana_reporter/asciidoctor/errors.rb +0 -40
  58. data/lib/grafana_reporter/asciidoctor/extensions/alerts_table_include_processor.rb +0 -92
  59. data/lib/grafana_reporter/asciidoctor/extensions/annotations_table_include_processor.rb +0 -91
  60. data/lib/grafana_reporter/asciidoctor/extensions/panel_image_block_macro.rb +0 -69
  61. data/lib/grafana_reporter/asciidoctor/extensions/panel_image_inline_macro.rb +0 -68
  62. data/lib/grafana_reporter/asciidoctor/extensions/panel_property_inline_macro.rb +0 -61
  63. data/lib/grafana_reporter/asciidoctor/extensions/panel_query_table_include_processor.rb +0 -78
  64. data/lib/grafana_reporter/asciidoctor/extensions/panel_query_value_inline_macro.rb +0 -73
  65. data/lib/grafana_reporter/asciidoctor/extensions/processor_mixin.rb +0 -20
  66. data/lib/grafana_reporter/asciidoctor/extensions/show_environment_include_processor.rb +0 -43
  67. data/lib/grafana_reporter/asciidoctor/extensions/show_help_include_processor.rb +0 -30
  68. data/lib/grafana_reporter/asciidoctor/extensions/sql_table_include_processor.rb +0 -70
  69. data/lib/grafana_reporter/asciidoctor/extensions/sql_value_inline_macro.rb +0 -66
  70. data/lib/grafana_reporter/asciidoctor/extensions/value_as_variable_include_processor.rb +0 -88
  71. data/lib/grafana_reporter/asciidoctor/help.rb +0 -435
  72. data/lib/grafana_reporter/asciidoctor/panel_first_value_query.rb +0 -36
  73. data/lib/grafana_reporter/asciidoctor/panel_image_query.rb +0 -28
  74. data/lib/grafana_reporter/asciidoctor/panel_property_query.rb +0 -44
  75. data/lib/grafana_reporter/asciidoctor/panel_table_query.rb +0 -40
  76. data/lib/grafana_reporter/asciidoctor/query_mixin.rb +0 -312
  77. data/lib/grafana_reporter/asciidoctor/sql_first_value_query.rb +0 -42
  78. data/lib/grafana_reporter/asciidoctor/sql_table_query.rb +0 -44
@@ -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 {QueryMixin#format_columns}, {QueryMixin#replace_values} and
19
+ # {QueryMixin#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
@@ -18,9 +18,6 @@ require 'asciidoctor-pdf'
18
18
  require 'zip'
19
19
  require_relative 'VERSION'
20
20
 
21
- # TODO: automatically build a template based on configured grafana dashboard
22
- # TODO: add an sample report to github from grafana play (if permitted)
23
-
24
21
  folders = [
25
22
  %w[grafana],
26
23
  %w[grafana_reporter logger],
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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Kohlmeyer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-02 00:00:00.000000000 Z
11
+ date: 2021-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -100,10 +100,9 @@ dependencies:
100
100
  - - "~>"
101
101
  - !ruby/object:Gem::Version
102
102
  version: '3.9'
103
- description: Provides a standalone and a webservice frontend for creating reports
104
- based on asciidoctor, including interfaces to integrate dynamic content captured
105
- from grafana.By default the reports will be converted to PDF documents, whereas
106
- other 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.
107
106
  email: kohly@gmx.de
108
107
  executables:
109
108
  - ruby-grafana-reporter
@@ -113,49 +112,52 @@ extra_rdoc_files:
113
112
  - LICENSE
114
113
  files:
115
114
  - "./lib/VERSION.rb"
116
- - "./lib/grafana/abstract_panel_query.rb"
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"
122
124
  - "./lib/grafana/panel.rb"
123
- - "./lib/grafana/panel_image_query.rb"
125
+ - "./lib/grafana/prometheus_datasource.rb"
126
+ - "./lib/grafana/sql_datasource.rb"
124
127
  - "./lib/grafana/variable.rb"
128
+ - "./lib/grafana/webrequest.rb"
129
+ - "./lib/grafana_reporter/abstract_query.rb"
125
130
  - "./lib/grafana_reporter/abstract_report.rb"
131
+ - "./lib/grafana_reporter/alerts_table_query.rb"
132
+ - "./lib/grafana_reporter/annotations_table_query.rb"
126
133
  - "./lib/grafana_reporter/application/application.rb"
127
134
  - "./lib/grafana_reporter/application/errors.rb"
128
135
  - "./lib/grafana_reporter/application/webservice.rb"
129
- - "./lib/grafana_reporter/asciidoctor/alerts_table_query.rb"
130
- - "./lib/grafana_reporter/asciidoctor/annotations_table_query.rb"
131
- - "./lib/grafana_reporter/asciidoctor/errors.rb"
132
- - "./lib/grafana_reporter/asciidoctor/extensions/alerts_table_include_processor.rb"
133
- - "./lib/grafana_reporter/asciidoctor/extensions/annotations_table_include_processor.rb"
134
- - "./lib/grafana_reporter/asciidoctor/extensions/panel_image_block_macro.rb"
135
- - "./lib/grafana_reporter/asciidoctor/extensions/panel_image_inline_macro.rb"
136
- - "./lib/grafana_reporter/asciidoctor/extensions/panel_property_inline_macro.rb"
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/help.rb"
146
- - "./lib/grafana_reporter/asciidoctor/panel_first_value_query.rb"
147
- - "./lib/grafana_reporter/asciidoctor/panel_image_query.rb"
148
- - "./lib/grafana_reporter/asciidoctor/panel_property_query.rb"
149
- - "./lib/grafana_reporter/asciidoctor/panel_table_query.rb"
150
- - "./lib/grafana_reporter/asciidoctor/query_mixin.rb"
136
+ - "./lib/grafana_reporter/asciidoctor/alerts_table_include_processor.rb"
137
+ - "./lib/grafana_reporter/asciidoctor/annotations_table_include_processor.rb"
138
+ - "./lib/grafana_reporter/asciidoctor/panel_image_block_macro.rb"
139
+ - "./lib/grafana_reporter/asciidoctor/panel_image_inline_macro.rb"
140
+ - "./lib/grafana_reporter/asciidoctor/panel_property_inline_macro.rb"
141
+ - "./lib/grafana_reporter/asciidoctor/panel_query_table_include_processor.rb"
142
+ - "./lib/grafana_reporter/asciidoctor/panel_query_value_inline_macro.rb"
143
+ - "./lib/grafana_reporter/asciidoctor/processor_mixin.rb"
151
144
  - "./lib/grafana_reporter/asciidoctor/report.rb"
152
- - "./lib/grafana_reporter/asciidoctor/sql_first_value_query.rb"
153
- - "./lib/grafana_reporter/asciidoctor/sql_table_query.rb"
145
+ - "./lib/grafana_reporter/asciidoctor/show_environment_include_processor.rb"
146
+ - "./lib/grafana_reporter/asciidoctor/show_help_include_processor.rb"
147
+ - "./lib/grafana_reporter/asciidoctor/sql_table_include_processor.rb"
148
+ - "./lib/grafana_reporter/asciidoctor/sql_value_inline_macro.rb"
149
+ - "./lib/grafana_reporter/asciidoctor/value_as_variable_include_processor.rb"
154
150
  - "./lib/grafana_reporter/configuration.rb"
155
151
  - "./lib/grafana_reporter/console_configuration_wizard.rb"
152
+ - "./lib/grafana_reporter/demo_report_wizard.rb"
156
153
  - "./lib/grafana_reporter/errors.rb"
154
+ - "./lib/grafana_reporter/help.rb"
157
155
  - "./lib/grafana_reporter/logger/two_way_logger.rb"
158
- - "./lib/ruby-grafana-reporter.rb"
156
+ - "./lib/grafana_reporter/panel_image_query.rb"
157
+ - "./lib/grafana_reporter/panel_property_query.rb"
158
+ - "./lib/grafana_reporter/query_value_query.rb"
159
+ - "./lib/grafana_reporter/report_webhook.rb"
160
+ - "./lib/ruby_grafana_reporter.rb"
159
161
  - LICENSE
160
162
  - README.md
161
163
  - bin/ruby-grafana-reporter
@@ -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