Class: GrafanaReporter::Asciidoctor::PanelImageBlockMacro

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

Overview

Implements the hook

grafana_panel_image::<panel_id>[<options>]

Stores the queried panel as a temporary image file and returns a relative asciidoctor link to the storage location, which can then be included in the report.

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

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

Instance Method Summary collapse

Methods included from ProcessorMixin

#build_attribute_hash, #current_report

Instance Method Details

#build_demo_entry(panel) ⇒ Object



68
69
70
71
72
73
# File 'lib/grafana_reporter/asciidoctor/panel_image_block_macro.rb', line 68

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

  "grafana_panel_image::#{panel.id}[dashboard=\"#{panel.dashboard.id}\",width=\"50%\"]"
end

#process(parent, target, attrs) ⇒ Object

:nodoc:



37
38
39
40
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
# File 'lib/grafana_reporter/asciidoctor/panel_image_block_macro.rb', line 37

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')
  @report.logger.debug("Processing PanelImageBlockMacro (instance: #{instance}, dashboard: #{dashboard},"\
                       " panel: #{target})")

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

    image = query.execute
    image_path = @report.save_image_file(image)
  rescue Grafana::GrafanaError => e
    @report.logger.error(e.message)
    return create_paragraph(parent, e.message, attrs)
  rescue GrafanaReporterError => e
    @report.logger.error(e.message)
    return create_paragraph(parent, e.message, attrs)
  rescue StandardError => e
    @report.logger.fatal("#{e.message}\n#{e.backtrace.join("\n")}")
    return create_paragraph(parent, "#{e.message}\n#{e.backtrace.join("\n")}", attrs)
  end

  attrs['target'] = image_path
  create_image_block(parent, attrs)
end