class GrafanaReporter::AbstractReport

@abstract Override {#build} and {#progress}.

This class is used to build a report on basis of a given configuration and template.

Objects of this class are also stored in {Application::Application}, unless the retention time is over.

Constants

EVENT_CALLBACKS

Array of supported event callback symbols

Attributes

cancel[R]

@return [Boolean] true, if the report is or shall be cancelled

done[R]

@return [Boolen] true, if the report generation is finished (successfull or not)

end_time[R]

@return [Time] time, when the report generation ended

logger[R]

@return [Logger] logger object used during report generation

start_time[R]

@return [Time] time, when the report generation started

template[R]

@return [String] path to the template

Public Class Methods

add_event_listener(event, listener) click to toggle source

Registers a new event listener object. @param event [Symbol] one of EVENT_CALLBACKS @param listener [Object] object responding to callback(event_symbol, object)

# File lib/grafana_reporter/abstract_report.rb, line 50
def self.add_event_listener(event, listener)
  @@event_listeners[event] = [] if @@event_listeners[event] == []
  @@event_listeners[event].push(listener)
end
clear_event_listeners() click to toggle source

Removes all registeres event listener objects

# File lib/grafana_reporter/abstract_report.rb, line 56
def self.clear_event_listeners
  @@event_listeners = {}
  @@event_listeners.default = []
end
default_result_extension() click to toggle source

@abstract @return [String] specifying the default extension of a rendered result file

# File lib/grafana_reporter/abstract_report.rb, line 197
def self.default_result_extension
  raise NotImplementedError
end
default_template_extension() click to toggle source

@abstract @return [String] specifying the default extension of a template file

# File lib/grafana_reporter/abstract_report.rb, line 191
def self.default_template_extension
  raise NotImplementedError
end
demo_report_classes() click to toggle source

@abstract Provided class objects need to implement a method +build_demo_entry(panel)+. @return [Array<Class>] array of class objects, which shall be included in a demo report

# File lib/grafana_reporter/abstract_report.rb, line 185
def self.demo_report_classes
  raise NotImplementedError
end
new(config) click to toggle source

@param config [Configuration] configuration object

# File lib/grafana_reporter/abstract_report.rb, line 38
def initialize(config)
  @config = config
  @logger = Logger::TwoWayDelegateLogger.new
  @logger.additional_logger = @config.logger
  @grafana_instances = {}

  init_before_create
end

Public Instance Methods

build(template, destination_file_or_path, custom_attributes) click to toggle source

@abstract Needs to be overridden by the report implementation.

# File lib/grafana_reporter/abstract_report.rb, line 162
def build(template, destination_file_or_path, custom_attributes)
  raise NotImplementedError
end
cancel!() click to toggle source

Call to request cancelling the report generation. @return [void]

# File lib/grafana_reporter/abstract_report.rb, line 74
def cancel!
  @cancel = true
  logger.info('Cancelling report generation invoked.')
  notify(:on_after_cancel)
end
create_report(template, destination_file_or_path = nil, custom_attributes = {}) click to toggle source

Is being called to start the report generation. To execute the specific report generation, this function calls the abstract {#build} method with the given parameters. @param template [String] path to the template to be used, trailing extension may be omitted, whereas {#default_template_extension} will be appended @param destination_file_or_path [String or File] path to the destination report or file object to use @param custom_attributes [Hash] custom attributes, which shall be merged with priority over the configuration @return [void]

# File lib/grafana_reporter/abstract_report.rb, line 133
def create_report(template, destination_file_or_path = nil, custom_attributes = {})
  init_before_create
  @template = template
  @destination_file_or_path = destination_file_or_path
  @custom_attributes = custom_attributes

  # automatically add extension, if a file with default template extension exists
  @template = "#{@template}.#{self.class.default_template_extension}" if File.file?("#{@template}.#{self.class.default_template_extension}") && !File.file?(@template.to_s)
  raise MissingTemplateError, @template.to_s unless File.file?(@template.to_s)

  notify(:on_before_create)
  @start_time = Time.new
  logger.info("Report started at #{@start_time}")
  build
rescue MissingTemplateError => e
  @logger.error(e.message)
  @error = [e.message]
  done!
  raise e
rescue StandardError => e
  # catch all errors during execution
  died_with_error(e)
  raise e
ensure
  done!
end
delete_file() click to toggle source

Deletes the report file object. @return [void]

# File lib/grafana_reporter/abstract_report.rb, line 87
def delete_file
  if @destination_file_or_path.is_a?(Tempfile)
    @destination_file_or_path.unlink
  elsif @destination_file_or_path.is_a?(File)
    @destination_file_or_path.delete
  end
  @destination_file_or_path = nil
end
error() click to toggle source

@return [Array] error messages during report generation.

# File lib/grafana_reporter/abstract_report.rb, line 105
def error
  @error || []
end
execution_time() click to toggle source

@return [Float] time in seconds, that the report generation took

# File lib/grafana_reporter/abstract_report.rb, line 97
def execution_time
  return nil if start_time.nil?
  return end_time - start_time unless end_time.nil?

  Time.now - start_time
end
full_log() click to toggle source

@return [String] string containing all messages ([Logger::Severity::DEBUG]) of the logger during report

generation.
# File lib/grafana_reporter/abstract_report.rb, line 123
def full_log
  logger.internal_messages
end
grafana(instance) click to toggle source

@param instance [String] requested grafana instance @return [Grafana::Grafana] the requested grafana instance.

# File lib/grafana_reporter/abstract_report.rb, line 63
def grafana(instance)
  unless @grafana_instances[instance]
    @grafana_instances[instance] = ::Grafana::Grafana.new(@config.grafana_host(instance),
                                                          @config.grafana_api_key(instance),
                                                          logger: @logger)
  end
  @grafana_instances[instance]
end
next_step() click to toggle source

Increments the progress. @return [Integer] number of the current progress position.

# File lib/grafana_reporter/abstract_report.rb, line 177
def next_step
  @current_pos += 1
  @current_pos
end
path() click to toggle source

@return [String] path to the report destination file

# File lib/grafana_reporter/abstract_report.rb, line 81
def path
  @destination_file_or_path.respond_to?(:path) ? @destination_file_or_path.path : @destination_file_or_path
end
progress() click to toggle source

Used to calculate the progress of a report. By default expects +@total_steps+ to contain the total number of steps, which will be processed with each call of {#next_step}. @return [Integer] number between 0 and 100, representing the current progress of the report creation.

# File lib/grafana_reporter/abstract_report.rb, line 169
def progress
  return @current_pos.to_i if @total_steps.to_i.zero?

  @current_pos.to_f / @total_steps
end
status() click to toggle source

@return [String] status of the report as string, either 'not started', 'in progress', 'cancelling',

'cancelled', 'died' or 'finished'.
# File lib/grafana_reporter/abstract_report.rb, line 111
def status
  return 'not started' unless @start_time
  return 'cancelled' if done && cancel
  return 'cancelling' if !done && cancel
  return 'finished' if done && error.empty?
  return 'died' if done && !error.empty?

  'in progress'
end

Private Instance Methods

died_with_error(error) click to toggle source

Called, if the report generation has died with an error. @param error [StandardError] occured error @return [void]

# File lib/grafana_reporter/abstract_report.rb, line 206
def died_with_error(error)
  @error = [error.message] << [error.backtrace]
  done!
end
done!() click to toggle source
# File lib/grafana_reporter/abstract_report.rb, line 219
def done!
  return if @done

  @destination_file_or_path.close if @destination_file_or_path.is_a?(File)
  @done = true
  @end_time = Time.new
  @start_time ||= @end_time
  logger.info("Report creation ended after #{@end_time.to_i - @start_time.to_i} seconds with status '#{status}'")
  notify(:on_after_finish)
end
init_before_create() click to toggle source
# File lib/grafana_reporter/abstract_report.rb, line 211
def init_before_create
  @done = false
  @start_time = nil
  @end_time = nil
  @cancel = false
  @current_pos = 0
end
notify(event) click to toggle source
# File lib/grafana_reporter/abstract_report.rb, line 230
def notify(event)
  (@@event_listeners[:all] + @@event_listeners[event]).each do |listener|
    logger.debug("Informing event listener '#{listener.class}' about event '#{event}' for report '#{object_id}'.")
    begin
      res = listener.callback(event, self)
      logger.debug("Event listener '#{listener.class}' for event '#{event}' and report '#{object_id}' returned "\
                   "with result '#{res}'.")
    rescue StandardError => e
      msg = "Event listener '#{listener.class}' for event '#{event}' and report '#{object_id}' returned with "\
            "error: #{e.message} - #{e.backtrace}."
      puts msg
      logger.error(msg)
    end
  end
end