Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
divinity666 committed Aug 30, 2022
2 parents 34236c0 + fc43bbb commit a134806
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 20 deletions.
26 changes: 25 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
dist: focal
language: ruby
rvm:
- 2.5.5
- 2.7.4
- 3.0.1
before_install:
- export TZ=Europe/Berlin
deploy:
- provider: releases
api_key:
secure: lUCi8h+Qhp3UFCD3yxCGGb3PYk8krt442ehygHCxz/OVBeu/6LRPgMNpH7mmT99PFyw8au8EElyZ7GjG9pkfObzJYt5NBP+35f4WvWNcM2wSIFqAeCQKgrOHhTsFfCtPNfERi63kiiSeownA+8zrMmqy+Gshzym+JiCGiffepPwvQMtiSJ3pNolx4lsYLKVdTLHWsWCt+YVPX00bg14cKhLGJxF4+LeVEO83U2yLQTauRVDyzwC7xwcKNsLLdftILLtd020hsKx/DwlRjp/dqyJECmI1wHQ2rd0BVz+jw41X08533w1pevlje0SKWoJjsQBGqWqKbsdXGrZ2MlfPWTdFySfok+2DLCsZpbO57/txTuAGCIAjbHGIoMh8e/dYbqVEAAuEgYjcpe8nDL/fT22nH7HZFwALwZeTts002eBmEk6RjADq2OsR1ftYozuWOBVJNo+oFWxgBFODQhcmnDygnRYMCkqbGK/vc7qmMai+QUiB1dbOryLK71qodf1GBO8uG+8WydVjnOHET3p8vlF9dx2EF1m9pHVy26DxMCExDmPe4TKLIK2t1uOziaHFz1FZQSnjwXc8NnJpeMBE24xq34v17QcAZEfRfv5L/59CQ77FB1ZqM91tHBFYPRNfAQpKB+0X7VIhPBD3Z+uY0t+vgOspY4hpPt6Orf3VAB4=
file_glob: true
file:
- "*.gem"
- "ruby-grafana-reporter*.rb"
on:
branch: master
tags: true
repo: divinity666/ruby-grafana-reporter
skip_cleanup: true
draft: true

- provider: rubygems
api_key:
secure: Vf6r7uC6B57yIqNCRtAWZI7BXzGKwpe5/CsxhL6giNlW0YMJLbvE50GdOveEyOSGwf4PTSWZSSP5ZRdpXYob9JVnUCBi2F+fuRnWxVZvxTrohSunUm7sbHGobrNd+vv01hgsIcgGZtPqquowYZ8y9lpQBfoXnDfMEyRACrvfDFol8Skb8vZxC/+ZPwF7NzC0SQQn+gbSQ49fYYnmqFTMIU1+Aj8CgzfGN//0kqO9U0TSwCfBesXewD/IEiBBOoZgiEP8XFwn+JCvHJnhHo0bS+71xKGZmTfhgN6Sw3IWD6fSX+yrbl0F61ycmIe0f+Hrr1ch7uybOINlakRuVGJ6PtH3fK/1air0k/IoGH+2muhnJFY28tHVJo56pKv4b8I9JrPUygV47PnDNiSTre4EcdETc3yYwgTs+u75NNO6PrW7hX3ySWZX9j5+mtldy8LR0KT9c5QsV9W0xZh4QloCnu0GFbQNgFssrbgKbsF0j+R/sLDjX+XkHGGaBSxhCsqBP0ZcYvhZQfQkaeFZGRx03qzRLFqaSPSmTANsmv4WTL32UpHLghGjg0wPnjegyjj5UgH1iaPse7avFm5YZVm2VW9pPqk/sxNUhWvFwGu3unKJpWwI7e2wGYwiSrZaEppFhAcMvCeiLU29X+2I0T+pXFpNBzuNr59XksjOppUqUcs=
gem: ruby-grafana-reporter
on:
branch: master
tags: true
repo: divinity666/ruby-grafana-reporter
skip_cleanup: true
47 changes: 35 additions & 12 deletions FUNCTION_CALLS.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/VERSION.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Version information
GRAFANA_REPORTER_VERSION = [0, 6, 0].freeze
GRAFANA_REPORTER_VERSION = [0, 6, 1].freeze
# Release date
GRAFANA_REPORTER_RELEASE_DATE = '2022-08-01'
GRAFANA_REPORTER_RELEASE_DATE = '2022-08-30'
8 changes: 7 additions & 1 deletion lib/grafana/abstract_datasource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,13 @@ def preformat_dataframe_response(response_body)
data = data['frames']
headers = []
data.first['schema']['fields'].each do |headline|
header = headline['config']['displayNameFromDS'].nil? ? headline['name'] : headline['config']['displayNameFromDS']
use_name_only = true
if not headline['config'].nil?
if not headline['config']['displayNameFromDS'].nil?
use_name_only = false
end
end
header = use_name_only ? headline['name'] : headline['config']['displayNameFromDS']
headers << header
end
content = data.first['data']['values'][0].zip(data.first['data']['values'][1])
Expand Down
6 changes: 6 additions & 0 deletions lib/grafana_reporter/abstract_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def execute
@result = @datasource.request(from: from, to: to, raw_query: raw_query, variables: @variables,
prepared_request: @grafana.prepare_request, timeout: timeout,
grafana_version: @grafana.version)
if @variables['verbose_log']
@logger.debug("Raw result: #{@result}") if @variables['verbose_log'].raw_value.downcase == "true"
end
rescue ::Grafana::GrafanaError
# grafana errors will be directly passed through
raise
Expand All @@ -95,6 +98,9 @@ def execute
raise DatasourceRequestInvalidReturnValueError.new(@datasource, @result) unless datasource_response_valid?

post_process
if @variables['verbose_log']
@logger.debug("Formatted result: #{@result}") if @variables['verbose_log'].raw_value.downcase == "true"
end
@result
end

Expand Down
46 changes: 44 additions & 2 deletions lib/grafana_reporter/asciidoctor/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def raw_help_yaml
two digit decimals of a float. Several column formats are separated by `,`, i.e. `%.2f,%.3f` would
apply `%.2f` to the first column and `%.3f` to the second column. All other columns would not be
formatted. You may also format time in milliseconds to a time format by specifying e.g. `date:iso`.
Commas in format strings are supported, but have to be escaped by useing `_,`.
Commas in format strings are supported, but have to be escaped by using `_,`.
Execution of related functions is applied in the following order `format`,
`replace_values`, `filter_columns`, `transpose`.
see: 'https://ruby-doc.org/core/Kernel.html#method-i-sprintf'
Expand All @@ -217,11 +217,16 @@ def raw_help_yaml
`replace_values`, `filter_columns`, `transpose`.
see: https://ruby-doc.org/core/Regexp.html#class-Regexp-label-Character+Classes
include_headline:
call: include_headline="true"
description: >-
Adds the headline of the columns as first row of the resulting table.
filter_columns:
call: filter_columns="<column_name_1>,<column_name_2>,..."
description: >-
Removes specified columns from result. Commas in format strings are supported, but have to be
escaped by useing `_,`. Execution of related functions is applied in the following order
escaped by using `_,`. Execution of related functions is applied in the following order
`format`, `replace_values`, `filter_columns`, `transpose`.
transpose:
Expand Down Expand Up @@ -271,6 +276,12 @@ def raw_help_yaml
description: >-
Optional parameter for Prometheus `instant` queries. Ignored for other datasources than Prometheus.
verbose_log:
call: verbose_log="true"
description: >-
Setting this option will show additional information about the returned query results in the log as
DEBUG messages.
# ----------------------------------
# FUNCTION DOCUMENTATION STARTS HERE
# ----------------------------------
Expand Down Expand Up @@ -313,6 +324,7 @@ def raw_help_yaml
filter_columns:
format:
from:
include_headline:
instance:
replace_values:
row_divider:
Expand Down Expand Up @@ -348,6 +360,7 @@ def raw_help_yaml
filter_columns:
format:
from:
include_headline:
instance:
replace_values:
row_divider:
Expand Down Expand Up @@ -409,6 +422,7 @@ def raw_help_yaml
filter_columns:
format:
from:
include_headline:
instance:
replace_values:
row_divider:
Expand All @@ -420,6 +434,7 @@ def raw_help_yaml
to_timezone:
instant:
interval:
verbose_log:
grafana_panel_query_value:
call: 'grafana_panel_query_value:<panel_id>[query="<query_letter>",options]'
Expand All @@ -444,6 +459,7 @@ def raw_help_yaml
to_timezone:
instant:
interval:
verbose_log:
grafana_sql_table:
call: 'include::grafana_sql_table:<datasource_id>[sql="<sql_query>",options]'
Expand All @@ -456,6 +472,7 @@ def raw_help_yaml
filter_columns:
format:
from:
include_headline:
instance:
replace_values:
row_divider:
Expand All @@ -467,6 +484,7 @@ def raw_help_yaml
to_timezone:
instant:
interval:
verbose_log:
grafana_sql_value:
call: 'grafana_sql_value:<datasource_id>[sql="<sql_query>",options]'
Expand All @@ -490,6 +508,30 @@ def raw_help_yaml
to_timezone:
instant:
interval:
verbose_log:
grafana_value_as_variable:
call: 'include::grafana_value_as_variable[call="<grafana_reporter_call>",variable_name="<your_variable_name>",options]'
description: >-
Executes the given +<grafana_reporter_call>+ and stored the resulting value
in the given +<your_variable_name>+, so that it can be used in asciidoctor
at any position with +{<your_variable_name>}+.
A sample call could look like this: +include:grafana_value_as_variable[call="grafana_sql_value:1",variable_name="my_variable",sql="SELECT 'looks good'",<any_other_option>]+
If the function succeeds, it will add this to the asciidoctor file:
+:my_variable: looks good+
Please note, that you may add any other option to the call. These will
simply be passed 1:1 to the +<grafana_reporter_call>+.
options:
call:
call: call="<grafana_reporter_call>"
description: Call to grafana reporter function, for which the result shall be stored as variable. Please note that only functions without +include::+ are supported here.
variable_name:
call: variable_name="<your_variable_name>"
description: Name of the variable, which will get the value assigned.
YAML_HELP
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grafana_reporter/asciidoctor/processor_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def build_attribute_hash(document_hash, item_hash)
k =~ /^(?:timeout|from|to)$/ ||
k =~ /filter_columns|format|replace_values_.*|transpose|from_timezone|
to_timezone|result_type|query|table_formatter|include_headline|
column_divider|row_divider|instant|interval/x
column_divider|row_divider|instant|interval|verbose_log/x
end)

result
Expand Down
2 changes: 1 addition & 1 deletion ruby-grafana-reporter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Gem::Specification.new do |s|
s.bindir = 'bin'

s.add_runtime_dependency 'asciidoctor', '~>2.0'
s.add_runtime_dependency 'asciidoctor-pdf', '~>2.2'
s.add_runtime_dependency 'asciidoctor-pdf', '~>2.3'
# the following package includes an interface to zip, which is also needed here
# make sure that supported zip versions match - look in sub-dependency 'gepub'
# s.add_runtime_dependency 'asciidoctor-epub3', '~>1.5.1'
Expand Down
11 changes: 11 additions & 0 deletions spec/integration/sql_table_include_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,20 @@
expect(@report.logger).to receive(:debug).exactly(5).times.with(any_args)
expect(@report.logger).to receive(:debug).with(/"from":"#{Time.utc(Time.new.year,1,1).to_i * 1000}".*"to":"#{(Time.utc(Time.new.year + 1,1,1) - 1).to_i * 1000}"/)
expect(@report.logger).to receive(:debug).with(/Received response/)
expect(@report.logger).not_to receive(:debug).with("Raw result: {:header=>[\"1\"], :content=>[[1]]}")
expect(Asciidoctor.convert("include::grafana_sql_table:#{STUBS[:datasource_sql]}[sql=\"SELECT 1\",from_timezone=\"UTC\",to_timezone=\"UTC\",from=\"now/y\",to=\"now/y\"]", to_file: false)).not_to include('GrafanaReporterError')
end

it 'can print verbose information' do
@report.logger.level = ::Logger::Severity::DEBUG
expect(@report.logger).to receive(:debug).exactly(5).times.with(any_args)
expect(@report.logger).to receive(:debug).with(/"from":"#{Time.utc(Time.new.year,1,1).to_i * 1000}".*"to":"#{(Time.utc(Time.new.year + 1,1,1) - 1).to_i * 1000}"/)
expect(@report.logger).to receive(:debug).with(/Received response/)
expect(@report.logger).to receive(:debug).with("Raw result: {:header=>[\"1\"], :content=>[[1]]}")
expect(@report.logger).to receive(:debug).with("Formatted result: | 1")
expect(Asciidoctor.convert("include::grafana_sql_table:#{STUBS[:datasource_sql]}[sql=\"SELECT 1\",from_timezone=\"UTC\",to_timezone=\"UTC\",from=\"now/y\",to=\"now/y\",verbose_log=\"true\"]", to_file: false)).not_to include('GrafanaReporterError')
end

it 'shows fatal error if sql statement is missing' do
expect(@report.logger).to receive(:error).with("GrafanaError: No SQL statement has been specified. (Grafana::MissingSqlQueryError)")
expect(Asciidoctor.convert("include::grafana_sql_table:#{STUBS[:datasource_sql]}[from=\"now/y\",to=\"now/y\"]", to_file: false)).to include('GrafanaError: No SQL statement has been specified. (Grafana::MissingSqlQueryError)')
Expand Down
6 changes: 6 additions & 0 deletions spec/models/prometheus_datasource_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
include Grafana

require 'json'

describe PrometheusDatasource do
subject { PrometheusDatasource.new({}) }

it 'raises an error if an improper response format is given' do
expect { subject.send(:preformat_response, "wrong format") }.to raise_error(UnsupportedQueryResponseReceivedError)
end

it 'does handle the query results from bug #31' do
expect { subject.send(:preformat_response, {"results":{"A":{"frames":[{"schema":{"name":"up{app=\"\", criticality=\"low\", env=\"production\", hostname=\"\", instance=\"9100\", job=\"node\", org=\"Root entity\"}","refId":"A","meta":{"custom":{"resultType":"matrix"}},"fields":[{"name":"Time","type":"time","typeInfo":{"frame":"time.Time"}},{"name":"Value","type":"number","typeInfo":{"frame":"float64","nullable":true},"labels":{"__name__":"up","app":"","criticality":"low","env":"production","hostname":"","instance":"9100","job":"node","org":"Root entity"},"config":{"displayNameFromDS":"your_name"}}]},"data":{"values":[[1660730100000,1660730400000,1660730700000,1660731000000,1660731300000,1660731600000,1660731900000,1660732200000,1660732500000,1660732800000,1660733100000,1660733400000,1660733700000,1660734000000,1660734300000,1660734600000,1660734900000,1660735200000,1660735500000,1660735800000,1660736100000,1660736400000,1660736700000,1660737000000,1660737300000,1660737600000,1660737900000,1660738200000,1660738500000,1660738800000,1660739100000,1660739400000,1660739700000,1660740000000,1660740300000,1660740600000,1660740900000,1660741200000,1660741500000,1660741800000,1660742100000,1660742400000,1660742700000,1660743000000,1660743300000,1660743600000,1660743900000,1660744200000,1660744500000,1660744800000,1660745100000,1660745400000,1660745700000,1660746000000,1660746300000,1660746600000,1660746900000,1660747200000,1660747500000,1660747800000,1660748100000,1660748400000,1660748700000,1660749000000,1660749300000,1660749600000,1660749900000,1660750200000,1660750500000,1660750800000,1660751100000,1660751400000,1660751700000],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]}}]}}}.to_json) }.not_to raise_error()
end
end

0 comments on commit a134806

Please sign in to comment.