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



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)



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

def cancel
  @cancel
end

#doneBoolen (readonly)



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

def done
  @done
end

#end_timeTime (readonly)



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

def end_time
  @end_time
end

#loggerLogger (readonly)



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

def logger
  @logger
end

#start_timeTime (readonly)



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

def start_time
  @start_time
end

#templateString (readonly)



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.



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.

Raises:

  • (NotImplementedError)


201
202
203
# File 'lib/grafana_reporter/abstract_report.rb', line 201

def self.default_result_extension
  raise NotImplementedError
end

.default_template_extensionString

This method is abstract.

Returns specifying the default extension of a template file.

Raises:

  • (NotImplementedError)


195
196
197
# File 'lib/grafana_reporter/abstract_report.rb', line 195

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).

Raises:

  • (NotImplementedError)


189
190
191
# File 'lib/grafana_reporter/abstract_report.rb', line 189

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)


166
167
168
# File 'lib/grafana_reporter/abstract_report.rb', line 166

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.



76
77
78
79
80
# File 'lib/grafana_reporter/abstract_report.rb', line 76

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.



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
161
162
# File 'lib/grafana_reporter/abstract_report.rb', line 135

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.



89
90
91
92
93
94
95
96
# File 'lib/grafana_reporter/abstract_report.rb', line 89

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



107
108
109
# File 'lib/grafana_reporter/abstract_report.rb', line 107

def error
  @error || []
end

#execution_timeFloat



99
100
101
102
103
104
# File 'lib/grafana_reporter/abstract_report.rb', line 99

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



125
126
127
# File 'lib/grafana_reporter/abstract_report.rb', line 125

def full_log
  logger.internal_messages
end

#grafana(instance) ⇒ Grafana::Grafana



63
64
65
66
67
68
69
70
71
72
# 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),
                                                          ssl_disable_verify: @config.grafana_ssl_disable_verify(instance),
                                                          ssl_cert: @config.grafana_ssl_cert(instance),
                                                          logger: @logger)
  end
  @grafana_instances[instance]
end

#next_stepInteger

Increments the progress.



181
182
183
184
# File 'lib/grafana_reporter/abstract_report.rb', line 181

def next_step
  @current_pos += 1
  @current_pos
end

#pathString



83
84
85
# File 'lib/grafana_reporter/abstract_report.rb', line 83

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.



173
174
175
176
177
# File 'lib/grafana_reporter/abstract_report.rb', line 173

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

  @current_pos.to_f / @total_steps
end

#statusString



113
114
115
116
117
118
119
120
121
# File 'lib/grafana_reporter/abstract_report.rb', line 113

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