Class: GrafanaReporter::AbstractReport Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/grafana_reporter/abstract_report.rb

Overview

This class is 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 GrafanaReporter::Application::Application, unless the retention time is over.

Constant Summary collapse

EVENT_CALLBACKS =

Array of supported event callback symbols

%i[all on_before_create on_after_cancel on_after_finish].freeze
@@event_listeners =

Class variable for storing event listeners

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ AbstractReport

Returns a new instance of AbstractReport.

Parameters:



38
39
40
41
42
43
44
45
# 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

Instance Attribute Details

#cancelBoolean (readonly)

Returns true, if the report is or shall be cancelled.

Returns:

  • (Boolean)

    true, if the report is or shall be cancelled



32
33
34
# File 'lib/grafana_reporter/abstract_report.rb', line 32

def cancel
  @cancel
end

#doneBoolen (readonly)

Returns true, if the report generation is finished (successfull or not).

Returns:

  • (Boolen)

    true, if the report generation is finished (successfull or not)



35
36
37
# File 'lib/grafana_reporter/abstract_report.rb', line 35

def done
  @done
end

#end_timeTime (readonly)

Returns time, when the report generation ended.

Returns:

  • (Time)

    time, when the report generation ended



26
27
28
# File 'lib/grafana_reporter/abstract_report.rb', line 26

def end_time
  @end_time
end

#loggerLogger (readonly)

Returns logger object used during report generation.

Returns:

  • (Logger)

    logger object used during report generation



29
30
31
# File 'lib/grafana_reporter/abstract_report.rb', line 29

def logger
  @logger
end

#start_timeTime (readonly)

Returns time, when the report generation started.

Returns:

  • (Time)

    time, when the report generation started



23
24
25
# File 'lib/grafana_reporter/abstract_report.rb', line 23

def start_time
  @start_time
end

#templateString (readonly)

Returns path to the template.

Returns:

  • (String)

    path to the template



20
21
22
# File 'lib/grafana_reporter/abstract_report.rb', line 20

def template
  @template
end

Class Method Details

.add_event_listener(event, listener) ⇒ Object

Registers a new event listener object.

Parameters:

  • event (Symbol)

    one of EVENT_CALLBACKS

  • listener (Object)

    object responding to #callback(event_symbol, object)



50
51
52
53
# 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_listenersObject

Removes all registeres event listener objects



56
57
58
59
# File 'lib/grafana_reporter/abstract_report.rb', line 56

def self.clear_event_listeners
  @@event_listeners = {}
  @@event_listeners.default = []
end

.default_result_extensionString

This method is abstract.

Returns specifying the default extension of a rendered result file.

Returns:

  • (String)

    specifying the default extension of a rendered result file

Raises:

  • (NotImplementedError)


199
200
201
# File 'lib/grafana_reporter/abstract_report.rb', line 199

def self.default_result_extension
  raise NotImplementedError
end

.default_template_extensionString

This method is abstract.

Returns specifying the default extension of a template file.

Returns:

  • (String)

    specifying the default extension of a template file

Raises:

  • (NotImplementedError)


193
194
195
# File 'lib/grafana_reporter/abstract_report.rb', line 193

def self.default_template_extension
  raise NotImplementedError
end

.demo_report_classesArray<Class>

This method is abstract.

Provided class objects need to implement a method build_demo_entry(panel).

Returns:

  • (Array<Class>)

    array of class objects, which shall be included in a demo report

Raises:

  • (NotImplementedError)


187
188
189
# File 'lib/grafana_reporter/abstract_report.rb', line 187

def self.demo_report_classes
  raise NotImplementedError
end

Instance Method Details

#build(template, destination_file_or_path, custom_attributes) ⇒ Object

This method is abstract.

Needs to be overridden by the report implementation.

Raises:

  • (NotImplementedError)


164
165
166
# File 'lib/grafana_reporter/abstract_report.rb', line 164

def build(template, destination_file_or_path, custom_attributes)
  raise NotImplementedError
end

#cancel!void

This method returns an undefined value.

Call to request cancelling the report generation.



74
75
76
77
78
# 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 = {}) ⇒ void

This method returns an undefined value.

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.

Parameters:

  • template (String)

    path to the template to be used, trailing extension may be omitted, whereas #default_template_extension will be appended

  • destination_file_or_path (String or File) (defaults to: nil)

    path to the destination report or file object to use

  • custom_attributes (Hash) (defaults to: {})

    custom attributes, which shall be merged with priority over the configuration



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# 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}.#{self.class.default_template_extension}" unless File.file?(@template.to_s)

  notify(:on_before_create)
  @start_time = Time.new
  logger.info("Report started at #{@start_time}")
  logger.info("You are running ruby-grafana-reporter version #{GRAFANA_REPORTER_VERSION.join('.')}.")
  logger.info("A newer version is released. Check out https://github.com/divinity666/ruby-grafana-reporter/releases/latest") unless @config.latest_version_check_ok?
  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_filevoid

This method returns an undefined value.

Deletes the report file object.



87
88
89
90
91
92
93
94
# 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

#errorArray

Returns error messages during report generation.

Returns:

  • (Array)

    error messages during report generation.



105
106
107
# File 'lib/grafana_reporter/abstract_report.rb', line 105

def error
  @error || []
end

#execution_timeFloat

Returns time in seconds, that the report generation took.

Returns:

  • (Float)

    time in seconds, that the report generation took



97
98
99
100
101
102
# 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_logString

Returns string containing all messages ([Logger::Severity::DEBUG]) of the logger during report generation.

Returns:

  • (String)

    string containing all messages ([Logger::Severity::DEBUG]) of the logger during report generation.



123
124
125
# File 'lib/grafana_reporter/abstract_report.rb', line 123

def full_log
  logger.internal_messages
end

#grafana(instance) ⇒ Grafana::Grafana

Returns the requested grafana instance.

Parameters:

  • instance (String)

    requested grafana instance

Returns:



63
64
65
66
67
68
69
70
# 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_stepInteger

Increments the progress.

Returns:

  • (Integer)

    number of the current progress position.



179
180
181
182
# File 'lib/grafana_reporter/abstract_report.rb', line 179

def next_step
  @current_pos += 1
  @current_pos
end

#pathString

Returns path to the report destination file.

Returns:

  • (String)

    path to the report destination file



81
82
83
# 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

#progressInteger

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.

Returns:

  • (Integer)

    number between 0 and 100, representing the current progress of the report creation.



171
172
173
174
175
# File 'lib/grafana_reporter/abstract_report.rb', line 171

def progress
  return @current_pos.to_i if @total_steps.to_i.zero?

  @current_pos.to_f / @total_steps
end

#statusString

Returns status of the report as string, either ‘not started’, ‘in progress’, ‘cancelling’, ‘cancelled’, ‘died’ or ‘finished’.

Returns:

  • (String)

    status of the report as string, either ‘not started’, ‘in progress’, ‘cancelling’, ‘cancelled’, ‘died’ or ‘finished’.



111
112
113
114
115
116
117
118
119
# 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