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
Returns a new instance of 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)
Returns 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 |
#done ⇒ Boolen (readonly)
Returns 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_time ⇒ Time (readonly)
Returns time, when the report generation ended.
26 27 28 |
# File 'lib/grafana_reporter/abstract_report.rb', line 26 def end_time @end_time end |
#logger ⇒ Logger (readonly)
Returns logger object used during report generation.
29 30 31 |
# File 'lib/grafana_reporter/abstract_report.rb', line 29 def logger @logger end |
#start_time ⇒ Time (readonly)
Returns time, when the report generation started.
23 24 25 |
# File 'lib/grafana_reporter/abstract_report.rb', line 23 def start_time @start_time end |
#template ⇒ String (readonly)
Returns 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.
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.
199 200 201 |
# File 'lib/grafana_reporter/abstract_report.rb', line 199 def self.default_result_extension raise NotImplementedError end |
.default_template_extension ⇒ String
Returns specifying the default extension of a template file.
193 194 195 |
# File 'lib/grafana_reporter/abstract_report.rb', line 193 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).
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
Needs to be overridden by the report implementation.
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.
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.to_s 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.
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 |
#error ⇒ Array
Returns error messages during report generation.
105 106 107 |
# File 'lib/grafana_reporter/abstract_report.rb', line 105 def error @error || [] end |
#execution_time ⇒ Float
Returns 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_log ⇒ String
Returns 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. end |
#grafana(instance) ⇒ Grafana::Grafana
Returns the requested grafana instance.
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_step ⇒ Integer
Increments the progress.
179 180 181 182 |
# File 'lib/grafana_reporter/abstract_report.rb', line 179 def next_step @current_pos += 1 @current_pos end |
#path ⇒ String
Returns 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 |
#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.
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 |
#status ⇒ String
Returns 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 |