Class: GrafanaReporter::Asciidoctor::AnnotationsTableIncludeProcessor

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

Overview

Implements the hook

include::grafana_annotations[<options>]

Returns the results of alerts query as a asciidoctor table.

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

Supported options

columns - see GrafanaReporter::AnnotationsTableQuery#pre_process (mandatory)

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

dashboard - uid of grafana dashboard to query for, empty string if no filter is wanted

panel - id of the panel to query for

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



86
87
88
# File 'lib/grafana_reporter/asciidoctor/annotations_table_include_processor.rb', line 86

def build_demo_entry(_panel)
  "|===\ninclude::grafana_annotations[columns=\"time,panelId,newState,prevState,text\"]\n|==="
end

#handles?(target) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


43
44
45
# File 'lib/grafana_reporter/asciidoctor/annotations_table_include_processor.rb', line 43

def handles?(target)
  target.start_with? 'grafana_annotations'
end

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

:nodoc:



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
78
79
80
81
82
83
# File 'lib/grafana_reporter/asciidoctor/annotations_table_include_processor.rb', line 48

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

  @report.next_step
  instance = attrs['instance'] || doc.attr('grafana_default_instance') || 'default'
  dashboard_id = attrs['dashboard'] || doc.attr('grafana_default_dashboard')
  panel_id = attrs['panel']
  @report.logger.debug("Processing AnnotationsTableIncludeProcessor (instance: #{instance})")

  grafana_obj = @report.grafana(instance)
  grafana_obj = @report.grafana(instance).dashboard(dashboard_id) if dashboard_id
  grafana_obj = grafana_obj.panel(panel_id) if panel_id

  vars = { 'table_formatter' => 'adoc_plain' }.merge(build_attribute_hash(doc.attributes, attrs))
  query = AnnotationsTableQuery.new(grafana_obj, variables: vars)
  defaults = {}
  defaults['dashboardId'] = dashboard_id if dashboard_id
  defaults['panelId'] = panel_id if panel_id

  selected_attrs = attrs.select do |k, _v|
    k =~ /(?:columns|limit|alertId|dashboardId|panelId|userId|type|tags)/
  end
  query.raw_query = defaults.merge(selected_attrs.each_with_object({}) { |(k, v), h| h[k] = v })

  begin
    reader.unshift_lines query.execute.split("\n")
  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