ruby-grafana-reporter 0.5.1 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +0 -0
  3. data/lib/VERSION.rb +2 -2
  4. data/lib/grafana/abstract_datasource.rb +35 -4
  5. data/lib/grafana/dashboard.rb +0 -0
  6. data/lib/grafana/errors.rb +7 -0
  7. data/lib/grafana/grafana.rb +11 -0
  8. data/lib/grafana/grafana_environment_datasource.rb +0 -0
  9. data/lib/grafana/grafana_property_datasource.rb +0 -0
  10. data/lib/grafana/graphite_datasource.rb +10 -3
  11. data/lib/grafana/image_rendering_datasource.rb +0 -0
  12. data/lib/grafana/influxdb_datasource.rb +12 -3
  13. data/lib/grafana/panel.rb +28 -1
  14. data/lib/grafana/prometheus_datasource.rb +12 -2
  15. data/lib/grafana/sql_datasource.rb +16 -6
  16. data/lib/grafana/variable.rb +0 -0
  17. data/lib/grafana/webrequest.rb +0 -0
  18. data/lib/grafana_reporter/abstract_query.rb +1 -1
  19. data/lib/grafana_reporter/abstract_report.rb +0 -0
  20. data/lib/grafana_reporter/abstract_table_format_strategy.rb +0 -0
  21. data/lib/grafana_reporter/alerts_table_query.rb +0 -0
  22. data/lib/grafana_reporter/annotations_table_query.rb +0 -0
  23. data/lib/grafana_reporter/application/webservice.rb +0 -0
  24. data/lib/grafana_reporter/asciidoctor/adoc_plain_table_format_strategy.rb +0 -0
  25. data/lib/grafana_reporter/asciidoctor/panel_image_block_macro.rb +0 -0
  26. data/lib/grafana_reporter/asciidoctor/panel_image_inline_macro.rb +0 -0
  27. data/lib/grafana_reporter/asciidoctor/processor_mixin.rb +0 -0
  28. data/lib/grafana_reporter/asciidoctor/show_environment_include_processor.rb +0 -0
  29. data/lib/grafana_reporter/asciidoctor/sql_value_inline_macro.rb +0 -0
  30. data/lib/grafana_reporter/asciidoctor/value_as_variable_include_processor.rb +0 -0
  31. data/lib/grafana_reporter/console_configuration_wizard.rb +0 -0
  32. data/lib/grafana_reporter/csv_table_format_strategy.rb +0 -0
  33. data/lib/grafana_reporter/demo_report_wizard.rb +0 -0
  34. data/lib/grafana_reporter/errors.rb +0 -0
  35. data/lib/grafana_reporter/panel_image_query.rb +0 -0
  36. data/lib/grafana_reporter/query_value_query.rb +4 -1
  37. data/lib/grafana_reporter/reporter_environment_datasource.rb +0 -0
  38. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a7dd2bc839c1b488d0c4e13d2837686a9c274845ec4f1a2ef46e95708aa4e96d
4
- data.tar.gz: 353185c2bbe335ab78b0b2dca7e4edc561ccdfcc1fa72e12d139b8b93da57698
3
+ metadata.gz: f39ad036bedee33e59c42d02f3fcd755508b09c5fbb90d732681ddb35fefea11
4
+ data.tar.gz: 5a837b1a1e3a755d34cfc398eae35c58ae1d908476162d3f2e595afd6f26cf5b
5
5
  SHA512:
6
- metadata.gz: '079bfb28f6365ecd10612076212447015839c1c31bbb976c99c29dbfd4a5af9e9d45d7d542b6e30d019beee4c409c3a86f0ab84aa6ae5db02352b985d04d2841'
7
- data.tar.gz: b4d008a5bd31f825a88ec428b3b9e3b117f60156bdfd1609fea3b06d64ff9037a177a7dcdbee5490d79c177e41abb51e7e345b5b82f056448d7509712dcc6d07
6
+ metadata.gz: 24148398cb3361619c0a1ae6464d7840fcecaa4812669757765bfe615de76cd65189ee44d4f4d18799f2e7792545e6dd4cefcec10e8d0836f3c45f5f318bbfb9
7
+ data.tar.gz: 0f66267327c1963ea5d18e3720f25ff41115a4a448d598cd313ab5130d1bc033a29905e96833da3401276176d1c7eebee1ae87ef91d670d052d5fcd683340838
data/README.md CHANGED
File without changes
data/lib/VERSION.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Version information
4
- GRAFANA_REPORTER_VERSION = [0, 5, 1].freeze
4
+ GRAFANA_REPORTER_VERSION = [0, 5, 4].freeze
5
5
  # Release date
6
- GRAFANA_REPORTER_RELEASE_DATE = '2021-12-18'
6
+ GRAFANA_REPORTER_RELEASE_DATE = '2022-03-25'
@@ -57,6 +57,11 @@ module Grafana
57
57
  @model['name']
58
58
  end
59
59
 
60
+ # @return [String] unique ID of the datasource
61
+ def uid
62
+ @model['uid']
63
+ end
64
+
60
65
  # @return [Integer] ID of the datasource
61
66
  def id
62
67
  @model['id'].to_i
@@ -108,15 +113,14 @@ module Grafana
108
113
  raise NotImplementedError
109
114
  end
110
115
 
111
- private
112
-
113
116
  # Replaces the grafana variables in the given string with their replacement value.
114
117
  #
115
118
  # @param string [String] string in which the variables shall be replaced
116
119
  # @param variables [Hash<String,Variable>] Hash containing the variables, which shall be replaced in the
117
120
  # given string
121
+ # @param overwrite_default_format [String] {Variable#value_formatted} value, if a custom default format should be used, otherwise {#default_variable_format} is used as default, which may be overwritten
118
122
  # @return [String] string in which all variables are properly replaced
119
- def replace_variables(string, variables = {})
123
+ def replace_variables(string, variables, overwrite_default_format = nil)
120
124
  res = string
121
125
  repeat = true
122
126
  repeat_count = 0
@@ -136,7 +140,8 @@ module Grafana
136
140
  next unless var_name =~ /^\w+$/
137
141
 
138
142
  res = res.gsub(/(?:\$\{#{var_name}(?::(?<format>\w+))?\}|\$#{var_name}(?!\w))/) do
139
- format = default_variable_format
143
+ format = overwrite_default_format
144
+ format = default_variable_format if overwrite_default_format.nil?
140
145
  if $LAST_MATCH_INFO
141
146
  format = $LAST_MATCH_INFO[:format] if $LAST_MATCH_INFO[:format]
142
147
  end
@@ -148,5 +153,31 @@ module Grafana
148
153
 
149
154
  res
150
155
  end
156
+
157
+ private
158
+
159
+ # Provides a general method to handle the given query response as general Grafana Dataframe format.
160
+ #
161
+ # This method throws {UnsupportedQueryResponseReceivedError} if the given query response is not a
162
+ # properly formattes dataframe
163
+ #
164
+ # @param response_body [String] raw response body
165
+ def preformat_dataframe_response(response_body)
166
+ json = JSON.parse(response_body)
167
+ data = json['results'].values.first
168
+
169
+ # TODO: check how multiple frames have to be handled
170
+ data = data['frames']
171
+ headers = []
172
+ data.first['schema']['fields'].each do |headline|
173
+ header = headline['config']['displayNameFromDS'].nil? ? headline['name'] : headline['config']['displayNameFromDS']
174
+ headers << header
175
+ end
176
+ content = data.first['data']['values'][0].zip(data.first['data']['values'][1])
177
+ return { header: headers, content: content }
178
+
179
+ rescue
180
+ raise UnsupportedQueryResponseReceivedError, response_body
181
+ end
151
182
  end
152
183
  end
File without changes
@@ -71,4 +71,11 @@ module Grafana
71
71
  super("The datasource query provided, does not look like a grafana datasource target (received: #{query}).")
72
72
  end
73
73
  end
74
+
75
+ # Raised if a datasource query returned with an unsupported result
76
+ class UnsupportedQueryResponseReceivedError < GrafanaError
77
+ def initialize(response)
78
+ super("The datasource request returned with an unsupported response format (received: #{response}).")
79
+ end
80
+ end
74
81
  end
@@ -81,6 +81,17 @@ module Grafana
81
81
  @datasources[datasource_name]
82
82
  end
83
83
 
84
+ # Returns the datasource, which has been queried by the datasource uid.
85
+ #
86
+ # @param datasource_uid [String] unique id of the searched datasource
87
+ # @return [Datasource] Datasource for the specified datasource unique id
88
+ def datasource_by_uid(datasource_uid)
89
+ datasource = @datasources.select { |_name, ds| ds.uid == datasource_uid }.values.first
90
+ raise DatasourceDoesNotExistError.new('uid', datasource_uid) unless datasource
91
+
92
+ datasource
93
+ end
94
+
84
95
  # Returns the datasource, which has been queried by the datasource id.
85
96
  #
86
97
  # @param datasource_id [Integer] id of the searched datasource
File without changes
File without changes
@@ -43,10 +43,14 @@ module Grafana
43
43
 
44
44
  private
45
45
 
46
- # @see AbstractDatasource#preformat_response
47
46
  def preformat_response(response_body)
48
- json = JSON.parse(response_body)
47
+ begin
48
+ return preformat_dataframe_response(response_body)
49
+ rescue
50
+ # TODO: show an info, that the response if not a dataframe
51
+ end
49
52
 
53
+ json = JSON.parse(response_body)
50
54
  header = ['time']
51
55
  content = {}
52
56
 
@@ -66,7 +70,10 @@ module Grafana
66
70
  end
67
71
  end
68
72
 
69
- { header: header, content: content.to_a.map(&:flatten).sort { |a, b| a[0] <=> b[0] } }
73
+ return { header: header, content: content.to_a.map(&:flatten).sort { |a, b| a[0] <=> b[0] } }
74
+
75
+ rescue
76
+ raise UnsupportedQueryResponseReceivedError, response_body
70
77
  end
71
78
  end
72
79
  end
File without changes
@@ -127,10 +127,16 @@ module Grafana
127
127
  "#{res} #{parts.join(', ')}"
128
128
  end
129
129
 
130
- # @see AbstractDatasource#preformat_response
131
130
  def preformat_response(response_body)
131
+ begin
132
+ return preformat_dataframe_response(response_body)
133
+ rescue
134
+ # TODO: show an info, that the response if not a dataframe
135
+ end
136
+
132
137
  # TODO: how to handle multiple query results?
133
- json = JSON.parse(response_body)['results'].first['series']
138
+ json = JSON.parse(response_body)
139
+ json = json['results'].first['series']
134
140
  return {} if json.nil?
135
141
 
136
142
  header = ['time']
@@ -152,7 +158,10 @@ module Grafana
152
158
  end
153
159
  end
154
160
 
155
- { header: header, content: content.to_a.map(&:flatten).sort { |a, b| a[0] <=> b[0] } }
161
+ return { header: header, content: content.to_a.map(&:flatten).sort { |a, b| a[0] <=> b[0] } }
162
+
163
+ rescue
164
+ raise UnsupportedQueryResponseReceivedError, response_body
156
165
  end
157
166
  end
158
167
  end
data/lib/grafana/panel.rb CHANGED
@@ -12,6 +12,11 @@ module Grafana
12
12
  def initialize(model, dashboard)
13
13
  @model = model
14
14
  @dashboard = dashboard
15
+
16
+ @datasource_uid_or_name = @model['datasource']
17
+ if @model['datasource'].is_a?(Hash)
18
+ @datasource_uid_or_name = @model['datasource']['uid']
19
+ end
15
20
  end
16
21
 
17
22
  # @return [String] content of the requested field or +''+ if not found
@@ -26,9 +31,22 @@ module Grafana
26
31
  @model['id']
27
32
  end
28
33
 
34
+ # This method should always be called before the +datasource+ method of a
35
+ # panel is invoked, to ensure that the variable names in the datasource
36
+ # field are resolved.
37
+ #
38
+ # @param variables [Hash] variables hash, which should be use to resolve variable datasource
39
+ def resolve_variable_datasource(variables)
40
+ @datasource_uid_or_name = AbstractDatasource.new(nil).replace_variables(@datasource_uid_or_name, variables, 'raw') if @datasource_uid_or_name.is_a?(String)
41
+ end
42
+
29
43
  # @return [Datasource] datasource object specified for the current panel
30
44
  def datasource
31
- dashboard.grafana.datasource_by_name(@model['datasource'])
45
+ if datasource_kind_is_uid?
46
+ dashboard.grafana.datasource_by_uid(@datasource_uid_or_name)
47
+ else
48
+ dashboard.grafana.datasource_by_name(@datasource_uid_or_name)
49
+ end
32
50
  end
33
51
 
34
52
  # @return [String] query string for the requested query letter
@@ -43,5 +61,14 @@ module Grafana
43
61
  def render_url
44
62
  "/render/d-solo/#{@dashboard.id}?panelId=#{@model['id']}"
45
63
  end
64
+
65
+ private
66
+
67
+ def datasource_kind_is_uid?
68
+ if @model['datasource'].is_a?(Hash)
69
+ return true
70
+ end
71
+ false
72
+ end
46
73
  end
47
74
  end
@@ -55,8 +55,14 @@ module Grafana
55
55
 
56
56
  private
57
57
 
58
- # @see AbstractDatasource#preformat_response
59
58
  def preformat_response(response_body)
59
+ # TODO: show raw response body to debug case https://github.com/divinity666/ruby-grafana-reporter/issues/24
60
+ begin
61
+ return preformat_dataframe_response(response_body)
62
+ rescue
63
+ # TODO: show an info, that the response if not a dataframe
64
+ end
65
+
60
66
  json = JSON.parse(response_body)
61
67
 
62
68
  # handle response with error result
@@ -64,6 +70,7 @@ module Grafana
64
70
  return { header: ['error'], content: [[ json['error'] ]] }
65
71
  end
66
72
 
73
+ # handle former result formats
67
74
  result_type = json['data']['resultType']
68
75
  json = json['data']['result']
69
76
 
@@ -101,7 +108,10 @@ module Grafana
101
108
  end
102
109
  end
103
110
 
104
- { header: headers, content: content.to_a.map(&:flatten).sort { |a, b| a[0] <=> b[0] } }
111
+ return { header: headers, content: content.to_a.map(&:flatten).sort { |a, b| a[0] <=> b[0] } }
112
+
113
+ rescue
114
+ raise UnsupportedQueryResponseReceivedError, response_body
105
115
  end
106
116
  end
107
117
  end
@@ -47,6 +47,12 @@ module Grafana
47
47
  private
48
48
 
49
49
  def preformat_response(response_body)
50
+ begin
51
+ return preformat_dataframe_response(response_body)
52
+ rescue
53
+ # TODO: show an info, that the response if not a dataframe
54
+ end
55
+
50
56
  results = {}
51
57
  results.default = []
52
58
  results[:header] = []
@@ -57,16 +63,20 @@ module Grafana
57
63
  results[:header] = results[:header] + ['SQL Error']
58
64
  results[:content] = [[query_result['error']]]
59
65
 
60
- elsif query_result['tables']
61
- query_result['tables'].each do |table|
62
- results[:header] = results[:header] + table['columns'].map { |header| header['text'] }
63
- results[:content] = table['rows']
66
+ elsif query_result.key?('tables')
67
+ if query_result['tables']
68
+ query_result['tables'].each do |table|
69
+ results[:header] = results[:header] + table['columns'].map { |header| header['text'] }
70
+ results[:content] = table['rows']
71
+ end
64
72
  end
65
-
66
73
  end
67
74
  end
68
75
 
69
- results
76
+ return results
77
+
78
+ rescue
79
+ raise UnsupportedQueryResponseReceivedError, response_body
70
80
  end
71
81
  end
72
82
  end
File without changes
File without changes
@@ -89,7 +89,7 @@ module GrafanaReporter
89
89
  # grafana errors will be directly passed through
90
90
  raise
91
91
  rescue StandardError => e
92
- raise DatasourceRequestInternalError.new(@datasource, e.message)
92
+ raise DatasourceRequestInternalError.new(@datasource, "#{e.message}\n#{e.backtrace.join("\n")}")
93
93
  end
94
94
 
95
95
  raise DatasourceRequestInvalidReturnValueError.new(@datasource, @result) unless datasource_response_valid?
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -5,7 +5,10 @@ module GrafanaReporter
5
5
  class QueryValueQuery < AbstractQuery
6
6
  # @see Grafana::AbstractQuery#pre_process
7
7
  def pre_process
8
- @datasource = @panel.datasource if @panel
8
+ if @panel
9
+ @panel.resolve_variable_datasource(@variables)
10
+ @datasource = @panel.datasource
11
+ end
9
12
 
10
13
  @variables['result_type'] ||= Variable.new('')
11
14
  end
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.5.1
4
+ version: 0.5.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-12-18 00:00:00.000000000 Z
11
+ date: 2022-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor