Class: GrafanaReporter::Asciidoctor::SqlTableIncludeProcessor

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

Overview

Implements the hook

include::grafana_sql_table:<datasource_id>[<options>]

Returns the results of the SQL query as a asciidoctor table.

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



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

def build_demo_entry(panel)
  return nil unless panel
  return nil unless panel.model['type'].include?('table')

  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

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

#handles?(target) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


38
39
40
# File 'lib/grafana_reporter/asciidoctor/sql_table_include_processor.rb', line 38

def handles?(target)
  target.start_with? 'grafana_sql_table:'
end

#process(doc, reader, target, attrs) ⇒ Object

:nodoc:



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
# File 'lib/grafana_reporter/asciidoctor/sql_table_include_processor.rb', line 43

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

  @report.next_step
  instance = attrs['instance'] || doc.attr('grafana_default_instance') || 'default'
  attrs['result_type'] = 'sql_table'
  @report.logger.debug("Processing SqlTableIncludeProcessor (instance: #{instance},"\
                       " datasource: #{target.split(':')[1]}, sql: #{attrs['sql']})")

  begin
    # catch properly if datasource could not be identified
    vars = { 'table_formatter' => 'adoc_plain' }.merge(build_attribute_hash(doc.attributes, attrs))
    query = QueryValueQuery.new(@report.grafana(instance), variables: vars)
    query.datasource = @report.grafana(instance).datasource_by_id(target.split(':')[1].to_i)
    query.raw_query = attrs['sql']

    reader.unshift_lines query.execute.split("\n")
  rescue Grafana::GrafanaError => e
    @report.logger.error(e.message)
    reader.unshift_line "|#{e.message}"
  rescue GrafanaReporterError => e
    @report.logger.error(e.message)
    reader.unshift_line "|#{e.message}"
  rescue StandardError => e
    @report.logger.fatal("#{e.message}\n#{e.backtrace.join("\n")}")
    reader.unshift_line "|#{e.message}\n#{e.backtrace.join("\n")}"
  end

  reader
end