Class: GrafanaReporter::Asciidoctor::PanelImageInlineMacro

Inherits:
Asciidoctor::Extensions::InlineMacroProcessor
  • Object
show all
Includes:
ProcessorMixin
Defined in:
lib/grafana_reporter/asciidoctor/panel_image_inline_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



69
70
71
72
73
74
75
# File 'lib/grafana_reporter/asciidoctor/panel_image_inline_macro.rb', line 69

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

  "see here: grafana_panel_image:#{panel.id}[dashboard=\"#{panel.dashboard.id}\","\
  'width="90%"] - a working inline image'
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
66
# File 'lib/grafana_reporter/asciidoctor/panel_image_inline_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 PanelImageInlineMacro (instance: #{instance}, dashboard: #{dashboard},"\
                       " panel: #{target})")

  begin
    # set alt text to a default, because otherwise asciidoctor fails
    attrs['alt'] = '' unless attrs['alt']
    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_inline(parent, :quoted, e.message)
  rescue GrafanaReporterError => e
    @report.logger.error(e.message)
    return create_inline(parent, :quoted, e.message)
  rescue StandardError => e
    @report.logger.fatal("#{e.message}\n#{e.backtrace.join("\n")}")
    return create_inline(parent, :quoted, "#{e.message}\n#{e.backtrace.join("\n")}")
  end

  create_inline(parent, :image, nil, { target: image_path, attributes: attrs })
end