ruby-grafana-reporter 0.6.6 → 0.7.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5e48d471e3c09009d65b08924a8d8ff1f4a2259fde86a2525f56554ee96f23a
4
- data.tar.gz: 0e095029e10bd6bef2ae80ad866b243c13d3c1ebd5555c9b75d651d6da5ce15c
3
+ metadata.gz: 49639b5da410d46b0ad6cf16a187a3f30389fac610497de09d1e1466a66f22f5
4
+ data.tar.gz: 673791b6d38cd46e54292903f07e0fdd16070f5e0abf999565fecf04b930efbb
5
5
  SHA512:
6
- metadata.gz: f6bc6604e3fed7fba3fe3057d42432098363f056c03afdf6c8f961d80abc0b0ad4505a01f895d44421774b5bb1a9227bb9bdef747cea7fe15669ebdf149b7611
7
- data.tar.gz: fa8ec80a80e51d214c50d54e9d1dbf6d196c11e5320f749ac2f3d2c9b5ed5979ae3a71924ce5d47d43489e4e18c381b5d8d05ad47a3805122a5ba93d9947950f
6
+ metadata.gz: d4bb03253d7681c580c96f11e5e86112e036087b927ff01e0e490bebeb4aeadd5f213f9de7202007523b8bf384ed36f6fcc040d5b81aa5aceef88c7cb191ab50
7
+ data.tar.gz: d3695d4d6ce451c01e7ad33a28a942f697d019215cca603827bda44af488e292c4befaf658d266e19b5ca3e84ab1a06cbc5f4f5e189d262b5c8bb415c4ae1013
data/README.md CHANGED
@@ -26,12 +26,10 @@ Reporting Service for Grafana
26
26
 
27
27
  ## Your support is appreciated!
28
28
 
29
- Hey there! Let me spend some personal words here:
30
-
31
- I provide you this software free of charge - and I'm happy, that I can do so. I have already
29
+ Hey there! I provide you this software free of charge. I have already
32
30
  spend a lot of my private time in developing, maintaining and supporting it.
33
31
 
34
- If you enjoy my work, I would feel greatly honoured, if you buy me a coffee:
32
+ If you enjoy my work, feel free to
35
33
 
36
34
  [![buymeacoffee](https://az743702.vo.msecnd.net/cdn/kofi3.png?v=0)](https://ko-fi.com/divinity666)
37
35
 
@@ -50,6 +48,10 @@ dashboards and to use it in your custom templates to finally create reports in P
50
48
 
51
49
  By default (an extended version of) Asciidoctor is enabled as template language.
52
50
 
51
+ ## Getting started
52
+
53
+ ![GettingStarted](./assets/GettingStartedAnimation.gif)
54
+
53
55
  ## Features
54
56
 
55
57
  * Supports creation of reports for multiple [grafana](https://github.com/grafana/grafana)
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, 6, 6].freeze
4
+ GRAFANA_REPORTER_VERSION = [0, 7, 0].freeze
5
5
  # Release date
6
- GRAFANA_REPORTER_RELEASE_DATE = '2024-02-22'
6
+ GRAFANA_REPORTER_RELEASE_DATE = '2024-05-19'
@@ -68,6 +68,23 @@ module Grafana
68
68
  'NON-Admin'
69
69
  end
70
70
 
71
+ # Returns the datasource, which has been queried by model entry in the panel model.
72
+ #
73
+ # @param model_entry [Object] model entry of the searched datasource (e.g. String or Hash)
74
+ # @return [Datasource] Datasource for the specified datasource model entry
75
+ def datasource_by_model_entry(model_entry)
76
+ datasource = nil
77
+ if model_entry.is_a?(String)
78
+ datasource = datasource_by_name(model_entry)
79
+ elsif model_entry.is_a?(Hash)
80
+ datasource = datasource_by_uid(model_entry['uid'])
81
+ end
82
+
83
+ raise DatasourceDoesNotExistError.new('model entry', model_entry) unless datasource
84
+
85
+ datasource
86
+ end
87
+
71
88
  # Returns the datasource, which has been queried by the datasource name.
72
89
  #
73
90
  # @param datasource_name [String] name of the searched datasource
@@ -86,6 +103,8 @@ module Grafana
86
103
  # @param datasource_uid [String] unique id of the searched datasource
87
104
  # @return [Datasource] Datasource for the specified datasource unique id
88
105
  def datasource_by_uid(datasource_uid)
106
+ raise DatasourceDoesNotExistError.new('uid', datasource_uid) unless datasource_uid
107
+
89
108
  clean_nil_datasources
90
109
  datasource = @datasources.select { |ds_name, ds| ds.uid == datasource_uid }.values.first
91
110
  raise DatasourceDoesNotExistError.new('uid', datasource_uid) unless datasource
@@ -383,7 +383,7 @@ module GrafanaReporter
383
383
  delta_match = date_spec.match(/^(?<op>(?:-|\+))(?<count>\d+)?(?<unit>[smhdwMy])/)
384
384
  if delta_match
385
385
  date = delta_date(date, "#{delta_match[:op]}#{delta_match[:count] || 1}".to_i, delta_match[:unit])
386
- date_spec = date_spec.gsub(/^#{delta_match[:op]}#{delta_match[:count]}#{delta_match[:unit]}/, '')
386
+ date_spec = date_spec.gsub(/^#{delta_match[:op] == '+' ? '\+' : '-'}#{delta_match[:count]}#{delta_match[:unit]}/, '')
387
387
  end
388
388
 
389
389
  raise TimeRangeUnknownError, orig_date unless fit_match || delta_match
@@ -229,6 +229,12 @@ end}
229
229
  escaped by using `_,`. Execution of related functions is applied in the following order
230
230
  `format`, `replace_values`, `filter_columns`, `transpose`.
231
231
 
232
+ select_value:
233
+ call: select_value="<select_value>"
234
+ description: >-
235
+ Allows the selection of a specific value from the result set. Supported options are `min`, `max`, `avg`,
236
+ `sum`, `first`, `last`.
237
+
232
238
  transpose:
233
239
  call: transpose="true"
234
240
  description: >-
@@ -456,6 +462,7 @@ end}
456
462
  from:
457
463
  instance:
458
464
  replace_values:
465
+ select_value:
459
466
  timeout:
460
467
  to:
461
468
  from_timezone:
@@ -505,6 +512,7 @@ end}
505
512
  from:
506
513
  instance:
507
514
  replace_values:
515
+ select_value:
508
516
  timeout:
509
517
  to:
510
518
  from_timezone:
@@ -80,7 +80,6 @@ module GrafanaReporter
80
80
  # @see ProcessorMixin#build_demo_entry
81
81
  def build_demo_entry(panel)
82
82
  return nil unless panel
83
- return nil unless panel.model['type'].include?('table')
84
83
 
85
84
  ref_id = nil
86
85
  panel.model['targets'].each do |item|
@@ -74,7 +74,6 @@ module GrafanaReporter
74
74
  # @see ProcessorMixin#build_demo_entry
75
75
  def build_demo_entry(panel)
76
76
  return nil unless panel
77
- return nil unless panel.model['type'] == 'singlestat'
78
77
 
79
78
  ref_id = nil
80
79
  panel.model['targets'].each do |item|
@@ -40,7 +40,7 @@ module GrafanaReporter
40
40
  k =~ /^(?:timeout|from|to)$/ ||
41
41
  k =~ /filter_columns|format|replace_values_.*|transpose|from_timezone|
42
42
  to_timezone|result_type|query|table_formatter|include_headline|
43
- column_divider|row_divider|instant|interval|verbose_log/x
43
+ column_divider|row_divider|instant|interval|verbose_log|select_value/x
44
44
  end)
45
45
 
46
46
  result
@@ -74,7 +74,6 @@ module GrafanaReporter
74
74
  # @see ProcessorMixin#build_demo_entry
75
75
  def build_demo_entry(panel)
76
76
  return nil unless panel
77
- return nil unless panel.model['type'].include?('table')
78
77
 
79
78
  ref_id = nil
80
79
  panel.model['targets'].each do |item|
@@ -85,8 +84,8 @@ module GrafanaReporter
85
84
  end
86
85
  return nil unless ref_id
87
86
 
88
- "|===\ninclude::grafana_sql_table:#{panel.dashboard.grafana.datasource_by_name(panel.model['datasource']).id}"\
89
- "[sql=\"#{panel.query(ref_id).gsub(/"/, '\"').gsub("\n", ' ').gsub(/\\/, '\\\\')}\",filter_columns=\"time\","\
87
+ "|===\ninclude::grafana_sql_table:#{panel.dashboard.grafana.datasource_by_model_entry(panel.model['datasource']).id}"\
88
+ "[sql=\"#{panel.query(ref_id).gsub(/"/, '\"').gsub("\r\n", ' ').gsub("\n", ' ').gsub(/\\/, '\\\\')}\",filter_columns=\"time\","\
90
89
  "dashboard=\"#{panel.dashboard.id}\",from=\"now-1h\",to=\"now\"]\n|==="
91
90
  end
92
91
  end
@@ -79,7 +79,6 @@ module GrafanaReporter
79
79
  # @see ProcessorMixin#build_demo_entry
80
80
  def build_demo_entry(panel)
81
81
  return nil unless panel
82
- return nil unless panel.model['type'] == 'singlestat'
83
82
 
84
83
  ref_id = nil
85
84
  panel.model['targets'].each do |item|
@@ -90,8 +89,8 @@ module GrafanaReporter
90
89
  end
91
90
  return nil unless ref_id
92
91
 
93
- "grafana_sql_value:#{panel.dashboard.grafana.datasource_by_name(panel.model['datasource']).id}"\
94
- "[sql=\"#{panel.query(ref_id).gsub(/"/, '\"').gsub("\n", ' ').gsub(/\\/, '\\\\')}\",from=\"now-1h\","\
92
+ "grafana_sql_value:#{panel.dashboard.grafana.datasource_by_model_entry(panel.model['datasource']).id}"\
93
+ "[sql=\"#{panel.query(ref_id).gsub(/"/, '\"').gsub("\r\n", ' ').gsub("\n", ' ').gsub(/\\/, '\\\\')}\",from=\"now-1h\","\
95
94
  'to="now"]'
96
95
  end
97
96
  end
@@ -79,6 +79,15 @@ module GrafanaReporter
79
79
  end
80
80
  end
81
81
 
82
+ # Thrown, if the value configuration in {QueryValueQuery#select_value} is
83
+ # invalid.
84
+ class UnsupportedSelectValueStatementError < GrafanaReporterError
85
+ def initialize(statement)
86
+ super("Unsupported 'select_value' specified in template file: '#{statement}'. Supported values are 'min', 'max', "\
87
+ "'avg', 'sum', 'first', 'last'.")
88
+ end
89
+ end
90
+
82
91
  # Thrown, if a configured parameter is malformed.
83
92
  class MalformedAttributeContentError < GrafanaReporterError
84
93
  def initialize(message, attribute, content)
@@ -33,7 +33,30 @@ module GrafanaReporter
33
33
 
34
34
  when /(?:panel_value|sql_value)/
35
35
  tmp = @result[:content] || []
36
- @result = tmp.flatten.first
36
+ # use only first column of return values and replace null values with zero
37
+ tmp = tmp.map{ |item| item[0] || 0 }
38
+
39
+ # as default behaviour we fallback to the first_value, as this was the default in older releases
40
+ select_value = 'first'
41
+ select_value = @variables['select_value'].raw_value if @variables['select_value']
42
+ case select_value
43
+ when 'min'
44
+ result = tmp.min
45
+ when 'max'
46
+ result = tmp.max
47
+ when 'avg'
48
+ result = tmp.size > 0 ? tmp.sum / tmp.size : 0
49
+ when 'sum'
50
+ result = tmp.sum
51
+ when 'last'
52
+ result = tmp.last
53
+ when 'first'
54
+ result = tmp.first
55
+ else
56
+ raise UnsupportedSelectValueStatementError, @variables['select_value'].raw_value
57
+ end
58
+
59
+ @result = result
37
60
 
38
61
  else
39
62
  raise StandardError, "Unsupported 'result_type' received: '#{@variables['result_type'].raw_value}'"
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.6.6
4
+ version: 0.7.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: 2024-02-22 00:00:00.000000000 Z
11
+ date: 2024-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -206,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
206
  - !ruby/object:Gem::Version
207
207
  version: '0'
208
208
  requirements: []
209
- rubygems_version: 3.1.4
209
+ rubygems_version: 3.1.6
210
210
  signing_key:
211
211
  specification_version: 4
212
212
  summary: Reporter Service for Grafana