Class: GrafanaReporter::Asciidoctor::PanelPropertyInlineMacro

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

Overview

Implements the hook

grafana_panel_property:<panel_id>[<options>]

Returns the requested panel property.

Used document parameters

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

grafana_default_dashboard - uid of grafana default dashboard to use

Supported options

field - property to query for, e.g. description or title (mandatory)

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

dashboard - uid of grafana dashboard to use

Instance Method Summary collapse

Methods included from ProcessorMixin

#build_attribute_hash, #current_report

Instance Method Details

#build_demo_entry(panel) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/grafana_reporter/asciidoctor/panel_property_inline_macro.rb', line 63

def build_demo_entry(panel)
  return nil unless panel
  return nil unless panel.model['title']
  return nil if panel.model['title'].strip == ''
  return nil if panel.model['title'].strip == 'Panel Title'

  "this text includes the panel with title grafana_panel_property:#{panel.id}[\"title\","\
  "dashboard=\"#{panel.dashboard.id}\"]"
end

#process(parent, target, attrs) ⇒ Object

:nodoc:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/grafana_reporter/asciidoctor/panel_property_inline_macro.rb', line 31

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 PanelPropertyInlineMacro (instance: #{instance}, dashboard: #{dashboard},"\
                       " panel: #{target}, property: #{attrs[:field]})")

  begin
    query = PanelPropertyQuery.new(@report.grafana(instance).dashboard(dashboard).panel(target),
                                   variables: build_attribute_hash(parent.document.attributes, attrs))
    query.raw_query = { property_name: attrs[:field] }

    description = query.execute
  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

  # translate linebreaks to asciidoctor syntax
  # and HTML encode to make sure, that HTML formattings are respected
  create_inline(parent, :quoted, CGI.escapeHTML(description.gsub(%r{//[^\n]*(?:\n)?}, '').gsub(/\n/, " +\n")))
end