Class: GrafanaReporter::AbstractReport Abstract
- Inherits:
-
Object
- Object
- GrafanaReporter::AbstractReport
- Defined in:
- lib/grafana_reporter/abstract_report.rb
Overview
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.
Direct Known Subclasses
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
-
#cancel ⇒ Boolean
readonly
True, if the report is or shall be cancelled.
-
#done ⇒ Boolen
readonly
True, if the report generation is finished (successfull or not).
-
#end_time ⇒ Time
readonly
Time, when the report generation ended.
-
#logger ⇒ Logger
readonly
Logger object used during report generation.
-
#start_time ⇒ Time
readonly
Time, when the report generation started.
-
#template ⇒ String
readonly
Path to the template.
Class Method Summary collapse
-
.add_event_listener(event, listener) ⇒ Object
Registers a new event listener object.
-
.clear_event_listeners ⇒ Object
Removes all registeres event listener objects.
-
.default_result_extension ⇒ String
abstract
Specifying the default extension of a rendered result file.
-
.default_template_extension ⇒ String
abstract
Specifying the default extension of a template file.
-
.demo_report_classes ⇒ Array<Class>
abstract
Provided class objects need to implement a method build_demo_entry(panel).
Instance Method Summary collapse
-
#build(template, destination_file_or_path, custom_attributes) ⇒ Object
abstract
Needs to be overridden by the report implementation.
-
#cancel! ⇒ void
Call to request cancelling the report generation.
-
#create_report(template, destination_file_or_path = nil, custom_attributes = {}) ⇒ void
Is being called to start the report generation.
-
#delete_file ⇒ void
Deletes the report file object.
-
#error ⇒ Array
Error messages during report generation.
-
#execution_time ⇒ Float
Time in seconds, that the report generation took.
-
#full_log ⇒ String
String containing all messages ([Logger::Severity::DEBUG]) of the logger during report generation.
-
#grafana(instance) ⇒ Grafana::Grafana
The requested grafana instance.
-
#initialize(config) ⇒ AbstractReport
constructor
A new instance of AbstractReport.
-
#next_step ⇒ Integer
Increments the progress.
-
#path ⇒ String
Path to the report destination file.
-
#progress ⇒ Integer
Used to calculate the progress of a report.
-
#status ⇒ String
Status of the report as string, either ‘not started’, ‘in progress’, ‘cancelling’, ‘cancelled’, ‘died’ or ‘finished’.
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
#cancel ⇒ Boolean (readonly)
32 33 34 |
# File 'lib/grafana_reporter/abstract_report.rb', line 32 def cancel @cancel end |
#done ⇒ Boolen (readonly)
35 36 37 |
# File 'lib/grafana_reporter/abstract_report.rb', line 35 def done @done end |
#end_time ⇒ Time (readonly)
26 27 28 |
# File 'lib/grafana_reporter/abstract_report.rb', line 26 def end_time @end_time end |
#logger ⇒ Logger (readonly)
29 30 31 |
# File 'lib/grafana_reporter/abstract_report.rb', line 29 def logger @logger end |
#start_time ⇒ Time (readonly)
23 24 25 |
# File 'lib/grafana_reporter/abstract_report.rb', line 23 def start_time @start_time end |
#template ⇒ String (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_listeners ⇒ Object
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_extension ⇒ String
Returns specifying the default extension of a rendered result file.
201 202 203 |
# File 'lib/grafana_reporter/abstract_report.rb', line 201 def self.default_result_extension raise NotImplementedError end |
.default_template_extension ⇒ String
Returns specifying the default extension of a template file.
195 196 197 |
# File 'lib/grafana_reporter/abstract_report.rb', line 195 def self.default_template_extension raise NotImplementedError end |
.demo_report_classes ⇒ Array<Class>
Provided class objects need to implement a method build_demo_entry(panel).
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
Needs to be overridden by the report implementation.
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.) @error = [e.] done! raise e rescue StandardError => e # catch all errors during execution died_with_error(e) raise e ensure done! end |
#delete_file ⇒ void
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 |
#error ⇒ Array
107 108 109 |
# File 'lib/grafana_reporter/abstract_report.rb', line 107 def error @error || [] end |
#execution_time ⇒ Float
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_log ⇒ String
125 126 127 |
# File 'lib/grafana_reporter/abstract_report.rb', line 125 def full_log logger. 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_step ⇒ Integer
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 |
#path ⇒ String
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 |
#progress ⇒ Integer
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 |
#status ⇒ String
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 |