Class: GrafanaReporter::Asciidoctor::PanelQueryValueInlineMacro

Inherits:
Asciidoctor::Extensions::InlineMacroProcessor
  • Object
show all
Includes:
ProcessorMixin
Defined in:
lib/grafana_reporter/asciidoctor/panel_query_value_inline_macro.rb

Overview

Implements the hook

grafana_panel_query_value:<panel_id>[<options>]

Returns the first value of the resulting SQL query.

Used document parameters

grafana_default_instance - name of grafana instance, ‘default’ if not specified

grafana_default_dashboard - uid of grafana default dashboard to use

from - ‘from’ time for the sql query

to - ‘to’ time for the sql query

All other variables starting with var- will be used to replace grafana templating strings in the given SQL query.

Supported options

query - query letter, which shall be used, e.g. C (mandatory)

instance - name of grafana instance, ‘default’ if not specified

dashboard - uid of grafana dashboard to use

from - ‘from’ time for the sql query

to - ‘to’ time for the sql query

format - see GrafanaReporter::AbstractQuery#format_columns

replace_values - see GrafanaReporter::AbstractQuery#replace_values

filter_columns - see GrafanaReporter::AbstractQuery#filter_columns

Instance Method Summary collapse

Methods included from ProcessorMixin

#build_attribute_hash, #current_report

Instance Method Details

#build_demo_entry(panel) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/grafana_reporter/asciidoctor/panel_query_value_inline_macro.rb', line 75

def build_demo_entry(panel)
  return nil unless panel
  return nil unless panel.model['targets']

  ref_id = nil
  panel.model['targets'].each do |item|
    if !item['hide'] && !panel.query(item['refId']).to_s.empty?
      ref_id = item['refId']
      break
    end
  end
  return nil unless ref_id

  "it's easily possible to include the query value: grafana_panel_query_value:#{panel.id}[query=\"#{ref_id}\""\
  ",dashboard=\"#{panel.dashboard.id}\"] - just within this text."
end

#process(parent, target, attrs) ⇒ Object

:nodoc:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/grafana_reporter/asciidoctor/panel_query_value_inline_macro.rb', line 47

def process(parent, target, attrs)
  return if @report.cancel

  @report.next_step
  instance = attrs['instance'] || parent.document.attr('grafana_default_instance') || 'default'
  dashboard = attrs['dashboard'] || parent.document.attr('grafana_default_dashboard')
  attrs['result_type'] = 'panel_value'
  @report.logger.debug("Processing PanelQueryValueInlineMacro (instance: #{instance}, dashboard: #{dashboard},"\
                       " panel: #{target}, query: #{attrs['query']})")

  begin
    panel = @report.grafana(instance).dashboard(dashboard).panel(target)
    query = QueryValueQuery.new(panel, variables: build_attribute_hash(parent.document.attributes, attrs))

    create_inline(parent, :quoted, query.execute)
  rescue Grafana::GrafanaError => e
    @report.logger.error(e.message)
    create_inline(parent, :quoted, e.message)
  rescue GrafanaReporterError => e
    @report.logger.error(e.message)
    create_inline(parent, :quoted, e.message)
  rescue StandardError => e
    @report.logger.fatal("#{e.message}\n#{e.backtrace.join("\n")}")
    create_inline(parent, :quoted, "#{e.message}\n#{e.backtrace.join("\n")}")
  end
end