Class: GrafanaReporter::Asciidoctor::SqlValueInlineMacro

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

Overview

Implements the hook

grafana_sql_value:<datasource_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

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

sql - sql statement (mandatory)

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

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/grafana_reporter/asciidoctor/sql_value_inline_macro.rb', line 80

def build_demo_entry(panel)
  return nil unless panel
  return nil unless panel.model['type'] == 'singlestat'

  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

  "grafana_sql_value:#{panel.dashboard.grafana.datasource_by_name(panel.model['datasource']).id}"\
  "[sql=\"#{panel.query(ref_id).gsub(/"/, '\"').gsub("\n", ' ').gsub(/\\/, '\\\\')}\",from=\"now-1h\","\
  'to="now"]'
end

#process(parent, target, attrs) ⇒ Object

See Also:

  • GrafanaReporter::Asciidoctor::SqlFirstValueQuery


41
42
43
44
45
46
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
73
74
75
76
77
# File 'lib/grafana_reporter/asciidoctor/sql_value_inline_macro.rb', line 41

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

  @report.next_step
  instance = attrs['instance'] || parent.document.attr('grafana_default_instance') || 'default'
  attrs['result_type'] = 'sql_value'
  sql = attrs['sql']
  @report.logger.debug("Processing SqlValueInlineMacro (instance: #{instance}, datasource: #{target},"\
                       " sql: #{sql})")

  # translate sql statement to fix asciidoctor issue
  # refer https://github.com/asciidoctor/asciidoctor/issues/4072#issuecomment-991305715
  sql_translated = CGI::unescapeHTML(sql) if sql
  if sql != sql_translated
    @report.logger.debug("Translating SQL query to fix asciidoctor issue: #{sql_translated}")
    sql = sql_translated
  end

  begin
    # catch properly if datasource could not be identified
    query = QueryValueQuery.new(@report.grafana(instance),
                                variables: build_attribute_hash(parent.document.attributes, attrs))
    query.datasource = @report.grafana(instance).datasource_by_id(target)
    query.raw_query = sql

    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