Class: GrafanaReporter::Asciidoctor::Report

Inherits:
GrafanaReporter::AbstractReport show all
Defined in:
lib/grafana_reporter/asciidoctor/report.rb

Overview

Implementation of a specific GrafanaReporter::AbstractReport. It is used to build reports specifically for asciidoctor results.

Constant Summary

Constants inherited from GrafanaReporter::AbstractReport

GrafanaReporter::AbstractReport::EVENT_CALLBACKS

Instance Attribute Summary

Attributes inherited from GrafanaReporter::AbstractReport

#cancel, #done, #end_time, #logger, #start_time, #template

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GrafanaReporter::AbstractReport

add_event_listener, #cancel!, clear_event_listeners, #create_report, #delete_file, #error, #execution_time, #full_log, #grafana, #next_step, #path, #progress, #status

Constructor Details

#initialize(config) ⇒ Report

Returns a new instance of Report.



10
11
12
13
# File 'lib/grafana_reporter/asciidoctor/report.rb', line 10

def initialize(config)
  super
  @image_files = []
end

Class Method Details

.default_result_extensionObject

See Also:

  • AbstractReport#default_result_extension


94
95
96
# File 'lib/grafana_reporter/asciidoctor/report.rb', line 94

def self.default_result_extension
  'pdf'
end

.default_template_extensionObject

See Also:

  • AbstractReport#default_template_extension


89
90
91
# File 'lib/grafana_reporter/asciidoctor/report.rb', line 89

def self.default_template_extension
  'adoc'
end

.demo_report_classesObject

See Also:

  • AbstractReport#demo_report_classes


99
100
101
102
103
# File 'lib/grafana_reporter/asciidoctor/report.rb', line 99

def self.demo_report_classes
  [AlertsTableIncludeProcessor, AnnotationsTableIncludeProcessor, PanelImageBlockMacro, PanelImageInlineMacro,
   PanelPropertyInlineMacro, PanelQueryTableIncludeProcessor, PanelQueryValueInlineMacro,
   SqlTableIncludeProcessor, SqlValueInlineMacro, ShowHelpIncludeProcessor, ShowEnvironmentIncludeProcessor]
end

Instance Method Details

#buildObject

Starts to create an asciidoctor report. It utilizes all extensions in the GrafanaReporter::Asciidoctor namespace to realize the conversion.



18
19
20
21
22
23
24
25
26
27
28
29
30
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
61
62
63
64
65
66
67
68
69
70
# File 'lib/grafana_reporter/asciidoctor/report.rb', line 18

def build
  attrs = { 'convert-backend' => 'pdf' }.merge(@config.default_document_attributes.merge(@custom_attributes))
  logger.debug("Document attributes: #{attrs}")

  initialize_step_counter

  # register necessary extensions for the current report
  ::Asciidoctor::LoggerManager.logger = logger

  registry = ::Asciidoctor::Extensions::Registry.new
  registry.inline_macro PanelImageInlineMacro.new.current_report(self)
  registry.inline_macro PanelQueryValueInlineMacro.new.current_report(self)
  registry.inline_macro PanelPropertyInlineMacro.new.current_report(self)
  registry.inline_macro SqlValueInlineMacro.new.current_report(self)
  registry.block_macro PanelImageBlockMacro.new.current_report(self)
  registry.include_processor ValueAsVariableIncludeProcessor.new.current_report(self)
  registry.include_processor PanelQueryTableIncludeProcessor.new.current_report(self)
  registry.include_processor SqlTableIncludeProcessor.new.current_report(self)
  registry.include_processor ShowEnvironmentIncludeProcessor.new.current_report(self)
  registry.include_processor ShowHelpIncludeProcessor.new.current_report(self)
  registry.include_processor AnnotationsTableIncludeProcessor.new.current_report(self)
  registry.include_processor AlertsTableIncludeProcessor.new.current_report(self)

  ::Asciidoctor.convert_file(@template, extension_registry: registry, backend: attrs['convert-backend'],
                                        to_file: path, attributes: attrs, header_footer: true)

  # store report including all images as ZIP file, if the result is not a PDF
  if attrs['convert-backend'] != 'pdf'
    # build zip file
    buffer = Zip::OutputStream.write_buffer do |zipfile|
      # add report file
      zipfile.put_next_entry("#{path.gsub(@config.reports_folder, '').gsub(/\.[\w\d]+$/, '')}.#{attrs['convert-backend']}")
      zipfile.write File.open(path, 'rb') { |f| f.read }

      # add image files
      @image_files.each do |file|
        zipfile.put_next_entry(file.path.gsub(@config.images_folder, ''))
        zipfile.write File.open(file.path, 'rb') { |f| f.read }
      end
    end

    # write zip file
    begin
      File.open(path, 'wb') do |f|
        f.write buffer.string
      end
    rescue StandardError => e
      logger.fatal("Could not overwrite file '#{path}' with zipped file. (#{e.message}).")
    end
  end

  clean_image_files
end

#save_image_file(img_data) ⇒ String

Called to save a temporary image file. After the final generation of the report, these temporary files will automatically be removed.

Parameters:

  • img_data (String)

    image file raw data, which shall be saved

Returns:

  • (String)

    path to the temporary file.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/grafana_reporter/asciidoctor/report.rb', line 76

def save_image_file(img_data)
  file = Tempfile.new(['gf_image_', '.png'], @config.images_folder.to_s)
  file.binmode
  file.write(img_data)
  path = file.path.gsub(/#{@config.images_folder}/, '')

  @image_files << file
  file.close

  path
end