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


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

def self.default_result_extension
  'pdf'
end

.default_template_extensionObject

See Also:

  • AbstractReport#default_template_extension


96
97
98
# File 'lib/grafana_reporter/asciidoctor/report.rb', line 96

def self.default_template_extension
  'adoc'
end

.demo_report_classesObject

See Also:

  • AbstractReport#demo_report_classes


106
107
108
109
110
# File 'lib/grafana_reporter/asciidoctor/report.rb', line 106

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
71
72
73
74
75
76
77
# 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 als images as ZIP file, if the result is not a PDF
  if attrs['convert-backend'] != 'pdf'
    # build zip file
    zip_file = Tempfile.new('gf_zip')
    buffer = Zip::OutputStream.write_buffer do |zipfile|
      # add report file
      zipfile.put_next_entry("#{path.gsub(@config.reports_folder, '')}.#{attrs['convert-backend']}")
      zipfile.write File.read(path)

      # add image files
      @image_files.each do |file|
        zipfile.put_next_entry(file.path.gsub(@config.images_folder, ''))
        zipfile.write File.read(file.path)
      end
    end
    File.open(zip_file, 'wb') do |f|
      f.write buffer.string
    end

    # replace original file with zip file
    zip_file.rewind
    begin
      File.write(path, zip_file.read)
    rescue StandardError => e
      logger.fatal("Could not overwrite report file '#{path}' with ZIP file. (#{e.message}).")
    end

    # cleanup temporary zip file
    zip_file.close
    zip_file.unlink
  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.



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/grafana_reporter/asciidoctor/report.rb', line 83

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