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.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +337 -170
  3. data/bin/ruby-grafana-reporter +5 -5
  4. data/lib/VERSION.rb +3 -2
  5. data/lib/grafana/abstract_datasource.rb +149 -0
  6. data/lib/grafana/dashboard.rb +1 -3
  7. data/lib/grafana/errors.rb +20 -5
  8. data/lib/grafana/grafana.rb +52 -57
  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 +37 -0
  12. data/lib/grafana/graphite_datasource.rb +72 -0
  13. data/lib/grafana/image_rendering_datasource.rb +44 -0
  14. data/lib/grafana/influxdb_datasource.rb +70 -0
  15. data/lib/grafana/panel.rb +10 -4
  16. data/lib/grafana/prometheus_datasource.rb +67 -0
  17. data/lib/grafana/sql_datasource.rb +70 -0
  18. data/lib/grafana/unsupported_datasource.rb +7 -0
  19. data/lib/grafana/variable.rb +27 -21
  20. data/lib/grafana/webrequest.rb +71 -0
  21. data/lib/grafana_reporter/abstract_query.rb +478 -0
  22. data/lib/grafana_reporter/abstract_report.rb +152 -18
  23. data/lib/grafana_reporter/abstract_table_format_strategy.rb +34 -0
  24. data/lib/grafana_reporter/alerts_table_query.rb +43 -0
  25. data/lib/grafana_reporter/annotations_table_query.rb +42 -0
  26. data/lib/grafana_reporter/application/application.rb +28 -25
  27. data/lib/grafana_reporter/application/webservice.rb +80 -39
  28. data/lib/grafana_reporter/asciidoctor/adoc_plain_table_format_strategy.rb +25 -0
  29. data/lib/grafana_reporter/asciidoctor/alerts_table_include_processor.rb +92 -0
  30. data/lib/grafana_reporter/asciidoctor/annotations_table_include_processor.rb +91 -0
  31. data/lib/grafana_reporter/asciidoctor/help.rb +336 -313
  32. data/lib/grafana_reporter/asciidoctor/panel_image_block_macro.rb +78 -0
  33. data/lib/grafana_reporter/asciidoctor/panel_image_inline_macro.rb +80 -0
  34. data/lib/grafana_reporter/asciidoctor/panel_property_inline_macro.rb +74 -0
  35. data/lib/grafana_reporter/asciidoctor/panel_query_table_include_processor.rb +99 -0
  36. data/lib/grafana_reporter/asciidoctor/panel_query_value_inline_macro.rb +93 -0
  37. data/lib/grafana_reporter/asciidoctor/processor_mixin.rb +50 -0
  38. data/lib/grafana_reporter/asciidoctor/report.rb +41 -82
  39. data/lib/grafana_reporter/asciidoctor/show_environment_include_processor.rb +46 -0
  40. data/lib/grafana_reporter/asciidoctor/show_help_include_processor.rb +35 -0
  41. data/lib/grafana_reporter/asciidoctor/sql_table_include_processor.rb +94 -0
  42. data/lib/grafana_reporter/asciidoctor/sql_value_inline_macro.rb +90 -0
  43. data/lib/grafana_reporter/asciidoctor/value_as_variable_include_processor.rb +90 -0
  44. data/lib/grafana_reporter/configuration.rb +26 -8
  45. data/lib/grafana_reporter/console_configuration_wizard.rb +109 -67
  46. data/lib/grafana_reporter/csv_table_format_strategy.rb +23 -0
  47. data/lib/grafana_reporter/demo_report_wizard.rb +104 -0
  48. data/lib/grafana_reporter/erb/demo_report_builder.rb +46 -0
  49. data/lib/grafana_reporter/erb/report.rb +36 -0
  50. data/lib/grafana_reporter/erb/report_jail.rb +21 -0
  51. data/lib/grafana_reporter/errors.rb +57 -0
  52. data/lib/grafana_reporter/logger/{two_way_logger.rb → two_way_delegate_logger.rb} +1 -1
  53. data/lib/grafana_reporter/panel_image_query.rb +25 -0
  54. data/lib/grafana_reporter/panel_property_query.rb +22 -0
  55. data/lib/grafana_reporter/query_value_query.rb +61 -0
  56. data/lib/grafana_reporter/report_webhook.rb +39 -0
  57. data/lib/ruby_grafana_extension.rb +8 -0
  58. data/lib/{ruby-grafana-reporter.rb → ruby_grafana_reporter.rb} +1 -3
  59. metadata +49 -38
  60. data/lib/grafana/abstract_panel_query.rb +0 -22
  61. data/lib/grafana/abstract_query.rb +0 -132
  62. data/lib/grafana/abstract_sql_query.rb +0 -51
  63. data/lib/grafana/panel_image_query.rb +0 -52
  64. data/lib/grafana_reporter/asciidoctor/alerts_table_query.rb +0 -101
  65. data/lib/grafana_reporter/asciidoctor/annotations_table_query.rb +0 -96
  66. data/lib/grafana_reporter/asciidoctor/errors.rb +0 -40
  67. data/lib/grafana_reporter/asciidoctor/extensions/alerts_table_include_processor.rb +0 -92
  68. data/lib/grafana_reporter/asciidoctor/extensions/annotations_table_include_processor.rb +0 -91
  69. data/lib/grafana_reporter/asciidoctor/extensions/panel_image_block_macro.rb +0 -69
  70. data/lib/grafana_reporter/asciidoctor/extensions/panel_image_inline_macro.rb +0 -68
  71. data/lib/grafana_reporter/asciidoctor/extensions/panel_property_inline_macro.rb +0 -61
  72. data/lib/grafana_reporter/asciidoctor/extensions/panel_query_table_include_processor.rb +0 -78
  73. data/lib/grafana_reporter/asciidoctor/extensions/panel_query_value_inline_macro.rb +0 -73
  74. data/lib/grafana_reporter/asciidoctor/extensions/processor_mixin.rb +0 -20
  75. data/lib/grafana_reporter/asciidoctor/extensions/show_environment_include_processor.rb +0 -43
  76. data/lib/grafana_reporter/asciidoctor/extensions/show_help_include_processor.rb +0 -30
  77. data/lib/grafana_reporter/asciidoctor/extensions/sql_table_include_processor.rb +0 -70
  78. data/lib/grafana_reporter/asciidoctor/extensions/sql_value_inline_macro.rb +0 -66
  79. data/lib/grafana_reporter/asciidoctor/extensions/value_as_variable_include_processor.rb +0 -88
  80. data/lib/grafana_reporter/asciidoctor/panel_first_value_query.rb +0 -36
  81. data/lib/grafana_reporter/asciidoctor/panel_image_query.rb +0 -28
  82. data/lib/grafana_reporter/asciidoctor/panel_property_query.rb +0 -44
  83. data/lib/grafana_reporter/asciidoctor/panel_table_query.rb +0 -40
  84. data/lib/grafana_reporter/asciidoctor/query_mixin.rb +0 -312
  85. data/lib/grafana_reporter/asciidoctor/sql_first_value_query.rb +0 -42
  86. data/lib/grafana_reporter/asciidoctor/sql_table_query.rb +0 -44
@@ -18,15 +18,13 @@ 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],
27
24
  %w[grafana_reporter],
28
25
  %w[grafana_reporter asciidoctor extensions],
29
26
  %w[grafana_reporter asciidoctor],
27
+ %w[grafana_reporter erb],
30
28
  %w[grafana_reporter application]
31
29
  ]
32
30
  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.3.0
4
+ version: 0.4.4
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-07-12 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
@@ -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,61 @@ 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"
124
+ - "./lib/grafana/influxdb_datasource.rb"
122
125
  - "./lib/grafana/panel.rb"
123
- - "./lib/grafana/panel_image_query.rb"
126
+ - "./lib/grafana/prometheus_datasource.rb"
127
+ - "./lib/grafana/sql_datasource.rb"
128
+ - "./lib/grafana/unsupported_datasource.rb"
124
129
  - "./lib/grafana/variable.rb"
130
+ - "./lib/grafana/webrequest.rb"
131
+ - "./lib/grafana_reporter/abstract_query.rb"
125
132
  - "./lib/grafana_reporter/abstract_report.rb"
133
+ - "./lib/grafana_reporter/abstract_table_format_strategy.rb"
134
+ - "./lib/grafana_reporter/alerts_table_query.rb"
135
+ - "./lib/grafana_reporter/annotations_table_query.rb"
126
136
  - "./lib/grafana_reporter/application/application.rb"
127
137
  - "./lib/grafana_reporter/application/errors.rb"
128
138
  - "./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"
139
+ - "./lib/grafana_reporter/asciidoctor/adoc_plain_table_format_strategy.rb"
140
+ - "./lib/grafana_reporter/asciidoctor/alerts_table_include_processor.rb"
141
+ - "./lib/grafana_reporter/asciidoctor/annotations_table_include_processor.rb"
145
142
  - "./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"
143
+ - "./lib/grafana_reporter/asciidoctor/panel_image_block_macro.rb"
144
+ - "./lib/grafana_reporter/asciidoctor/panel_image_inline_macro.rb"
145
+ - "./lib/grafana_reporter/asciidoctor/panel_property_inline_macro.rb"
146
+ - "./lib/grafana_reporter/asciidoctor/panel_query_table_include_processor.rb"
147
+ - "./lib/grafana_reporter/asciidoctor/panel_query_value_inline_macro.rb"
148
+ - "./lib/grafana_reporter/asciidoctor/processor_mixin.rb"
151
149
  - "./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"
150
+ - "./lib/grafana_reporter/asciidoctor/show_environment_include_processor.rb"
151
+ - "./lib/grafana_reporter/asciidoctor/show_help_include_processor.rb"
152
+ - "./lib/grafana_reporter/asciidoctor/sql_table_include_processor.rb"
153
+ - "./lib/grafana_reporter/asciidoctor/sql_value_inline_macro.rb"
154
+ - "./lib/grafana_reporter/asciidoctor/value_as_variable_include_processor.rb"
154
155
  - "./lib/grafana_reporter/configuration.rb"
155
156
  - "./lib/grafana_reporter/console_configuration_wizard.rb"
157
+ - "./lib/grafana_reporter/csv_table_format_strategy.rb"
158
+ - "./lib/grafana_reporter/demo_report_wizard.rb"
159
+ - "./lib/grafana_reporter/erb/demo_report_builder.rb"
160
+ - "./lib/grafana_reporter/erb/report.rb"
161
+ - "./lib/grafana_reporter/erb/report_jail.rb"
156
162
  - "./lib/grafana_reporter/errors.rb"
157
- - "./lib/grafana_reporter/logger/two_way_logger.rb"
158
- - "./lib/ruby-grafana-reporter.rb"
163
+ - "./lib/grafana_reporter/logger/two_way_delegate_logger.rb"
164
+ - "./lib/grafana_reporter/panel_image_query.rb"
165
+ - "./lib/grafana_reporter/panel_property_query.rb"
166
+ - "./lib/grafana_reporter/query_value_query.rb"
167
+ - "./lib/grafana_reporter/report_webhook.rb"
168
+ - "./lib/ruby_grafana_extension.rb"
169
+ - "./lib/ruby_grafana_reporter.rb"
159
170
  - LICENSE
160
171
  - README.md
161
172
  - bin/ruby-grafana-reporter
@@ -173,7 +184,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
173
184
  requirements:
174
185
  - - ">="
175
186
  - !ruby/object:Gem::Version
176
- version: 2.3.3
187
+ version: '2.5'
177
188
  required_rubygems_version: !ruby/object:Gem::Requirement
178
189
  requirements:
179
190
  - - ">="
@@ -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
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Grafana
4
- # @abstract
5
- #
6
- # Used as a superclass for all queries, which execute SQL queries against {Grafana}.
7
- #
8
- # @see AbstractQuery
9
- class AbstractSqlQuery < AbstractQuery
10
- attr_reader :sql, :datasource_id
11
-
12
- # @param raw_sql [String] raw sql statement, as it can be sent to a SQL database
13
- # @param datasource_id [Integer] ID of the datasource against which the query is run
14
- def initialize(raw_sql, datasource_id)
15
- super()
16
- @sql = raw_sql
17
- @datasource_id = datasource_id
18
- end
19
-
20
- # @return [String] relative URL, where the request has to be sent to.
21
- def url
22
- '/api/tsdb/query'
23
- end
24
-
25
- # @return [Hash] request, which executes the SQL statement against the specified datasource
26
- def request
27
- {
28
- body: {
29
- from: @from,
30
- to: @to,
31
- queries: [rawSql: @sql, datasourceId: @datasource_id.to_i, format: 'table']
32
- }.to_json,
33
- request: Net::HTTP::Post
34
- }
35
- end
36
-
37
- # Replaces all variables in the SQL statement.
38
- def pre_process(grafana)
39
- raise MissingSqlQueryError if @sql.nil?
40
- unless grafana.datasource_id_exists?(@datasource_id.to_i)
41
- raise DatasourceDoesNotExistError.new('id', @datasource_id)
42
- end
43
-
44
- @sql = replace_variables(@sql, grafana_variables)
45
- # remove comments in query
46
- @sql.gsub!(/--[^\r\n]*(?:[\r\n]+|$)/, ' ')
47
- @sql.gsub!(/\r\n/, ' ')
48
- @sql.gsub!(/\n/, ' ')
49
- end
50
- end
51
- end
@@ -1,52 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Grafana
4
- # Query, which allows to render a {Panel} as a PNG image.
5
- class PanelImageQuery < AbstractPanelQuery
6
- # Returns the URL for rendering the panel. Uses {Panel#render_url} and sets additional url
7
- # parameters according {https://grafana.com/docs/grafana/latest/reference/share_panel Grafana Share Panel}.
8
- #
9
- # @see AbstractQuery#url
10
- # @return [String] string for rendering the panel
11
- def url
12
- @panel.render_url + url_parameters
13
- end
14
-
15
- # Changes the result of the request to be of type +image/png+.
16
- #
17
- # @see AbstractQuery#request
18
- def request
19
- { accept: 'image/png' }
20
- end
21
-
22
- # Adds default variables for querying the image.
23
- #
24
- # @see AbstractQuery#pre_process
25
- def pre_process(_grafana)
26
- @variables['fullscreen'] = Variable.new(true)
27
- @variables['theme'] = Variable.new('light')
28
- @variables['timeout'] = Variable.new(timeout) if timeout
29
- @variables['timeout'] ||= Variable.new(60)
30
- end
31
-
32
- # Checks if the rendering has been performed properly.
33
- # If so, the resulting image is stored in the @result variable, otherwise an error is raised.
34
- #
35
- # @see AbstractQuery#post_process
36
- def post_process
37
- raise ImageCouldNotBeRenderedError, @panel if @result.body.include?('<html')
38
- end
39
-
40
- private
41
-
42
- def url_parameters
43
- url_vars = variables.select { |k, _v| k =~ /^(?:timeout|height|width|theme|fullscreen)/ || k =~ /^var-.+/ }
44
- url_vars['from'] = Variable.new(@from) if @from
45
- url_vars['to'] = Variable.new(@to) if @to
46
- url_params = URI.encode_www_form(url_vars.map { |k, v| [k, v.raw_value.to_s] })
47
- return '' if url_params.empty?
48
-
49
- "&#{url_params}"
50
- end
51
- end
52
- end
@@ -1,101 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'query_mixin'
4
-
5
- module GrafanaReporter
6
- module Asciidoctor
7
- # This class is used to query alerts from grafana.
8
- class AlertsTableQuery < Grafana::AbstractQuery
9
- include QueryMixin
10
-
11
- # @option opts [Grafana::Dashboard] :dashboard dashboard, if alerts shall be filtered for a dashboard
12
- # @option opts [Grafnaa::Oanel] :panel panel, if alerts shall be filtered for a panel
13
- def initialize(opts = {})
14
- super()
15
-
16
- @dashboard = opts[:dashboard]
17
- @panel = opts[:panel]
18
- @dashboard = @panel.dashboard if @panel
19
-
20
- extract_dashboard_variables(@dashboard) if @dashboard
21
- end
22
-
23
- # @return [String] URL for querying alerts
24
- def url
25
- "/api/alerts#{url_parameters}"
26
- end
27
-
28
- # @return [Hash] empty hash object
29
- def request
30
- {}
31
- end
32
-
33
- # Check if mandatory {Grafana::Variable} +columns+ is specified in variables.
34
- #
35
- # The value of the +columns+ variable has to be a comma separated list of column titles, which
36
- # need to be included in the following list:
37
- # - limit
38
- # - dashboardId
39
- # - panelId
40
- # - query
41
- # - state
42
- # - folderId
43
- # - dashboardQuery
44
- # - dashboardTag
45
- # @return [void]
46
- def pre_process(_grafana)
47
- raise MissingMandatoryAttributeError, 'columns' unless @variables['columns']
48
-
49
- @from = translate_date(@from, @variables['grafana-report-timestamp'], false, @variables['from_timezone'] ||
50
- @variables['grafana_default_from_timezone'])
51
- @to = translate_date(@to, @variables['grafana-report-timestamp'], true, @variables['to_timezone'] ||
52
- @variables['grafana_default_to_timezone'])
53
- end
54
-
55
- # Filter the query result for the given columns and sets the result in the preformatted SQL
56
- # result stlye.
57
-
58
- # Additionally it applies {QueryMixin#format_columns}, {QueryMixin#replace_values} and
59
- # {QueryMixin#filter_columns}.
60
- # @return [void]
61
- def post_process
62
- # extract data from returned json
63
- result = JSON.parse(@result.body)
64
- content = []
65
- begin
66
- result.each { |item| content << item.fetch_values(*@variables['columns'].raw_value.split(',')) }
67
- rescue KeyError => e
68
- raise MalformedAttributeContentError.new(e.message, 'columns', @variables['columns'])
69
- end
70
-
71
- result = {}
72
- result[:header] = [@variables['columns'].raw_value.split(',')]
73
- result[:content] = content
74
-
75
- result = format_columns(result, @variables['format'])
76
- result = replace_values(result, @variables.select { |k, _v| k =~ /^replace_values_\d+/ })
77
- result = filter_columns(result, @variables['filter_columns'])
78
-
79
- @result = result[:content].map { |row| "| #{row.map { |item| item.to_s.gsub('|', '\\|') }.join(' | ')}" }
80
- end
81
-
82
- private
83
-
84
- def url_parameters
85
- url_vars = {}
86
- url_vars['dashboardId'] = ::Grafana::Variable.new(@dashboard.id) if @dashboard
87
- url_vars['panelId'] = ::Grafana::Variable.new(@panel.id) if @panel
88
-
89
- url_vars.merge!(variables.select do |k, _v|
90
- k =~ /^(?:limit|dashboardId|panelId|query|state|folderId|dashboardQuery|dashboardTag)/x
91
- end)
92
- url_vars['from'] = ::Grafana::Variable.new(@from) if @from
93
- url_vars['to'] = ::Grafana::Variable.new(@to) if @to
94
- url_params = URI.encode_www_form(url_vars.map { |k, v| [k, v.raw_value.to_s] })
95
- return '' if url_params.empty?
96
-
97
- "?#{url_params}"
98
- end
99
- end
100
- end
101
- end