ruby-grafana-reporter 0.5.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f39ad036bedee33e59c42d02f3fcd755508b09c5fbb90d732681ddb35fefea11
4
- data.tar.gz: 5a837b1a1e3a755d34cfc398eae35c58ae1d908476162d3f2e595afd6f26cf5b
3
+ metadata.gz: e198f995d87598512fbe2a4578edf8be48630bab415d14c5348cf23a57e47df7
4
+ data.tar.gz: c1f5a3c85ba1081fbfc0b526b1c4e15412d146a8f88af897a0620fd762687482
5
5
  SHA512:
6
- metadata.gz: 24148398cb3361619c0a1ae6464d7840fcecaa4812669757765bfe615de76cd65189ee44d4f4d18799f2e7792545e6dd4cefcec10e8d0836f3c45f5f318bbfb9
7
- data.tar.gz: 0f66267327c1963ea5d18e3720f25ff41115a4a448d598cd313ab5130d1bc033a29905e96833da3401276176d1c7eebee1ae87ef91d670d052d5fcd683340838
6
+ metadata.gz: 9e5af10cd799f63a8ac662db422cebe1a8f7d1484c5e29e54ec73280c966efc7a57059c5fbe2bef2717b27fe6c2fa1c22c819fce3c851d98d568fd556291d933
7
+ data.tar.gz: 97c1477d8e97a6afa096d7dc4557d4ae15e9aee87ca541af165f898f3cfd1a20d002e2b06c68c98dd4f0c436299129e766e0023f34b6f1a2639d4fcabcfde73d
data/README.md CHANGED
@@ -310,6 +310,7 @@ webservice or a rendering process starts.
310
310
  This is just a collection of things, I am heading for in future, without a schedule.
311
311
 
312
312
  * Support grafana internal datasources
313
+ * Support additional templating variable types
313
314
  * Solve code TODOs
314
315
  * Become [rubocop](https://rubocop.org/) ready
315
316
 
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, 4].freeze
4
+ GRAFANA_REPORTER_VERSION = [0, 6, 0].freeze
5
5
  # Release date
6
- GRAFANA_REPORTER_RELEASE_DATE = '2022-03-25'
6
+ GRAFANA_REPORTER_RELEASE_DATE = '2022-08-01'
@@ -82,6 +82,7 @@ module Grafana
82
82
  # }
83
83
  #
84
84
  # @param query_description [Hash] query description, which will requested:
85
+ # @option query_description [String] :grafana_version grafana version, for which the request is to be prepared
85
86
  # @option query_description [String] :from +from+ timestamp
86
87
  # @option query_description [String] :to +to+ timestamp
87
88
  # @option query_description [Integer] :timeout expected timeout for the request
@@ -58,7 +58,14 @@ module Grafana
58
58
  list = @model['templating']['list']
59
59
  return unless list.is_a? Array
60
60
 
61
- list.each { |item| @variables << Variable.new(item, self) }
61
+ list.each do |item|
62
+ begin
63
+ @variables << Variable.new(item, self)
64
+ rescue => e
65
+ # TODO: show this message as a warning - needs test cleanup
66
+ @grafana.logger.debug(e.message)
67
+ end
68
+ end
62
69
  end
63
70
 
64
71
  # read panels
@@ -86,7 +86,15 @@ module Grafana
86
86
  # @param datasource_uid [String] unique id of the searched datasource
87
87
  # @return [Datasource] Datasource for the specified datasource unique id
88
88
  def datasource_by_uid(datasource_uid)
89
- datasource = @datasources.select { |_name, ds| ds.uid == datasource_uid }.values.first
89
+ datasource = @datasources.select do |ds_name, ds|
90
+ if (ds.nil?)
91
+ # print debug info for https://github.com/divinity666/ruby-grafana-reporter/issues/29
92
+ @logger.warn("Datasource with name #{ds_name} is nil, which should never happen. Check logs for details.")
93
+ false
94
+ else
95
+ ds.uid == datasource_uid
96
+ end
97
+ end.values.first
90
98
  raise DatasourceDoesNotExistError.new('uid', datasource_uid) unless datasource
91
99
 
92
100
  datasource
@@ -156,6 +164,12 @@ module Grafana
156
164
  json = JSON.parse(settings.body)
157
165
  json['datasources'].select { |_k, v| v['id'].to_i.positive? }.each do |ds_name, ds_value|
158
166
  @datasources[ds_name] = AbstractDatasource.build_instance(ds_value)
167
+
168
+ # print debug info for https://github.com/divinity666/ruby-grafana-reporter/issues/29
169
+ if @datasources[ds_name].nil?
170
+ @logger.error("Datasource with name '#{ds_name}' and configuration: '#{ds_value}' could not be initialized.")
171
+ @datasources.delete(ds_name)
172
+ end
159
173
  end
160
174
  @datasources['default'] = @datasources[json['defaultDatasource']]
161
175
  end
@@ -24,21 +24,15 @@ module Grafana
24
24
  interval = interval.raw_value if interval.is_a?(Variable)
25
25
  query = query_hash[:query] || query_description[:raw_query]
26
26
 
27
- url = if instant
28
- "/api/datasources/proxy/#{id}/api/v1/query?time=#{query_description[:to]}&query="\
29
- "#{CGI.escape(replace_variables(query, query_description[:variables]))}"
27
+ ver = query_description[:grafana_version].split('.').map{|x| x.to_i}
28
+ request = nil
29
+ if (ver[0] == 7 and ver[1] < 5) or ver[0] < 7
30
+ request = prepare_get_request({query_description: query_description, instant: instant, interval: interval, query: query})
30
31
  else
31
- "/api/datasources/proxy/#{id}/api/v1/query_range?start=#{query_description[:from]}"\
32
- "&end=#{query_description[:to]}"\
33
- "&query=#{CGI.escape(replace_variables(query, query_description[:variables]))}"\
34
- "&step=#{interval}"
32
+ request = prepare_post_request({query_description: query_description, instant: instant, interval: interval, query: query})
35
33
  end
36
34
 
37
- webrequest = query_description[:prepared_request]
38
- webrequest.relative_url = url
39
- webrequest.options.merge!({ request: Net::HTTP::Get })
40
-
41
- result = webrequest.execute(query_description[:timeout])
35
+ result = request.execute(query_description[:timeout])
42
36
  preformat_response(result.body)
43
37
  end
44
38
 
@@ -54,13 +48,67 @@ module Grafana
54
48
  end
55
49
 
56
50
  private
51
+ def prepare_get_request(hash)
52
+ url = if hash[:instant]
53
+ "/api/datasources/proxy/#{id}/api/v1/query?time=#{hash[:query_description][:to]}&query="\
54
+ "#{CGI.escape(replace_variables(hash[:query], hash[:query_description][:variables]))}"
55
+ else
56
+ "/api/datasources/proxy/#{id}/api/v1/query_range?start=#{hash[:query_description][:from]}"\
57
+ "&end=#{hash[:query_description][:to]}"\
58
+ "&query=#{CGI.escape(replace_variables(hash[:query], hash[:query_description][:variables]))}"\
59
+ "&step=#{hash[:interval]}"
60
+ end
61
+
62
+ webrequest = hash[:query_description][:prepared_request]
63
+ webrequest.relative_url = url
64
+ webrequest.options.merge!({ request: Net::HTTP::Get })
65
+
66
+ webrequest
67
+ end
68
+
69
+ def prepare_post_request(hash)
70
+ webrequest = hash[:query_description][:prepared_request]
71
+ webrequest.relative_url = '/api/ds/query'
72
+
73
+ params = {
74
+ from: hash[:query_description][:from],
75
+ to: hash[:query_description][:to],
76
+ queries: [{
77
+ datasource: { type: type, uid: uid },
78
+ datasourceId: id,
79
+ exemplar: false,
80
+ expr: hash[:query],
81
+ format: 'time_series',
82
+ interval: '',
83
+ # intervalFactor: ### 2,
84
+ # intervalMs: ### 15000,
85
+ # legendFormat: '', ### {{job}}
86
+ # maxDataPoints: 999,
87
+ metric: '',
88
+ queryType: 'timeSeriesQuery',
89
+ refId: 'A',
90
+ # requestId: '14A',
91
+ # utcOffsetSec: 7200,
92
+ step: hash[:interval]
93
+ }],
94
+ range: {
95
+ #from: ### "2022-07-31T16:19:26.198Z",
96
+ #to: ### "2022-07-31T16:19:26.198Z",
97
+ raw: { from: hash[:query_description][:variables]['from'].raw_value, to: hash[:query_description][:variables]['to'].raw_value }
98
+ }
99
+ }
100
+
101
+ webrequest.options.merge!({ request: Net::HTTP::Post, body: params.to_json })
102
+
103
+ webrequest
104
+ end
57
105
 
58
106
  def preformat_response(response_body)
59
107
  # TODO: show raw response body to debug case https://github.com/divinity666/ruby-grafana-reporter/issues/24
60
108
  begin
61
109
  return preformat_dataframe_response(response_body)
62
110
  rescue
63
- # TODO: show an info, that the response if not a dataframe
111
+ # TODO: show an info, that the response is not a dataframe
64
112
  end
65
113
 
66
114
  json = JSON.parse(response_body)
@@ -240,7 +240,7 @@ module Grafana
240
240
  if !@config['current'].nil?
241
241
  self.raw_value = @config['current']['value']
242
242
  else
243
- raise GrafanaError.new("Grafana variable with type '#{@config['type']}' and name '#{@config['name']}' could not be handled properly. Please raise a ticket.")
243
+ raise GrafanaError.new("Grafana variable with type '#{@config['type']}' and name '#{@config['name']}' cannot be handled properly by the reporter. Check your results and raise a ticket on github.")
244
244
  end
245
245
  end
246
246
  end
@@ -13,7 +13,6 @@ module GrafanaReporter
13
13
  attr_reader :variables, :result, :panel, :dashboard
14
14
 
15
15
  def timeout
16
- # TODO: PRIO check where value priorities should be evaluated
17
16
  return @variables['timeout'].raw_value if @variables['timeout']
18
17
  return @variables['grafana_default_timeout'].raw_value if @variables['grafana_default_timeout']
19
18
 
@@ -81,7 +80,8 @@ module GrafanaReporter
81
80
 
82
81
  begin
83
82
  @result = @datasource.request(from: from, to: to, raw_query: raw_query, variables: @variables,
84
- prepared_request: @grafana.prepare_request, timeout: timeout)
83
+ prepared_request: @grafana.prepare_request, timeout: timeout,
84
+ grafana_version: @grafana.version)
85
85
  rescue ::Grafana::GrafanaError
86
86
  # grafana errors will be directly passed through
87
87
  raise
File without changes
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.4
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Kohlmeyer
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-25 00:00:00.000000000 Z
11
+ date: 2022-08-01 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.6'
33
+ version: '2.2'
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.6'
40
+ version: '2.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rubyzip
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -178,7 +178,7 @@ licenses:
178
178
  metadata:
179
179
  source_code_uri: https://github.com/divinity666/ruby-grafana-reporter
180
180
  bug_tracker_uri: https://github.com/divinity666/ruby-grafana-reporter/issues
181
- post_install_message:
181
+ post_install_message:
182
182
  rdoc_options: []
183
183
  require_paths:
184
184
  - lib
@@ -186,7 +186,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
186
186
  requirements:
187
187
  - - ">="
188
188
  - !ruby/object:Gem::Version
189
- version: '2.5'
189
+ version: '2.7'
190
190
  required_rubygems_version: !ruby/object:Gem::Requirement
191
191
  requirements:
192
192
  - - ">="
@@ -194,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
194
  version: '0'
195
195
  requirements: []
196
196
  rubygems_version: 3.2.5
197
- signing_key:
197
+ signing_key:
198
198
  specification_version: 4
199
199
  summary: Reporter Service for Grafana
200
200
  test_files: []