Class: GrafanaReporter::Configuration
- Inherits:
-
Object
- Object
- GrafanaReporter::Configuration
- Defined in:
- lib/grafana_reporter/configuration.rb
Overview
Used to store the whole settings, which are necessary to run the reporter. It can read configuration files, but might also be configured programmatically.
This class also contains a function #validate, which ensures that the provided settings are set properly.
Using this class is embedded in the Application::Application#configure_and_run.
Constant Summary collapse
- DEFAULT_CONFIG_FILE_NAME =
Default file name for grafana reporter configuration file
'grafana_reporter.config'
- MODE_CONNECTION_TEST =
Returned by #mode if only a connection test shall be executed.
'test'
- MODE_SINGLE_RENDER =
Returned by #mode if only one configured report shall be rendered.
'single-render'
- MODE_SERVICE =
Returned by #mode if the default webservice shall be started.
'webservice'
Instance Attribute Summary collapse
-
#config ⇒ Object
Used to access the configuration hash.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#report_class ⇒ AbstractReport
Specific report class, which should be used.
Instance Method Summary collapse
-
#default_document_attributes ⇒ Hash
The configuration made with the setting ‘default-document-attributes’ will be passed 1:1 to the asciidoctor report service.
-
#grafana_api_key(instance = 'default') ⇒ String
Configured ‘api_key’ for the requested grafana instance.
-
#grafana_host(instance = 'default') ⇒ String
Configured ‘host’ for the requested grafana instance.
-
#grafana_instances ⇒ Array<String>
Names of the configured grafana_instances.
-
#grafana_ssl_cert(instance = 'default') ⇒ String
Configured ‘ssl-cert’ for the requested grafana instance.
-
#grafana_ssl_disable_verify(instance = 'default') ⇒ String
Configured ‘ssl-disable-verify’ for the requested grafana instance.
-
#images_folder ⇒ String
Returns configured folder, in which temporary images during report generation shall be stored including trailing slash.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#latest_version_check_ok? ⇒ Boolean
Checks if this is the latest ruby-grafana-reporter version.
-
#load_config_from_file(config_file = nil) ⇒ Hash
Reads a given configuration file.
-
#merge!(other_config) ⇒ Object
Merge the given configuration object settings with the current config, i.e.
-
#mode ⇒ String
Mode, in which the reporting shall be executed.
-
#report_retention ⇒ Integer
How many hours a generated report shall be retained, before it shall be deleted.
-
#reports_folder ⇒ String
Configured folder, in which the reports shall be stored including trailing slash.
-
#set_param(path, value) ⇒ Object
Can be used to configure or overwrite single parameters.
-
#template ⇒ String
Full path of configured report template.
-
#templates_folder ⇒ String
Configured folder, in which the report templates are stored including trailing slash.
-
#test_instance ⇒ String
Name of grafana instance, against which a test shall be executed.
-
#to_file ⇒ String
Destination filename for the report in MODE_SINGLE_RENDER.
-
#validate(explicit = false) ⇒ void
This function shall be called, before the configuration object is used in the Application::Application#run.
-
#webserver_port ⇒ Integer
Port, on which the webserver shall run.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
32 33 34 35 |
# File 'lib/grafana_reporter/configuration.rb', line 32 def initialize @config = {} @logger = ::Logger.new($stderr, level: :info) end |
Instance Attribute Details
#config ⇒ Object
Used to access the configuration hash. To make sure, that the configuration is valid, call #validate.
30 31 32 |
# File 'lib/grafana_reporter/configuration.rb', line 30 def config @config end |
#logger ⇒ Object
Returns the value of attribute logger.
16 17 18 |
# File 'lib/grafana_reporter/configuration.rb', line 16 def logger @logger end |
#report_class ⇒ AbstractReport
Returns specific report class, which should be used.
15 16 17 |
# File 'lib/grafana_reporter/configuration.rb', line 15 def report_class @report_class end |
Instance Method Details
#default_document_attributes ⇒ Hash
The configuration made with the setting ‘default-document-attributes’ will be passed 1:1 to the asciidoctor report service. It can be used to preconfigure whatever is essential for the needed report renderings.
163 164 165 |
# File 'lib/grafana_reporter/configuration.rb', line 163 def default_document_attributes get_config('default-document-attributes') || {} end |
#grafana_api_key(instance = 'default') ⇒ String
Returns configured ‘api_key’ for the requested grafana instance.
95 96 97 |
# File 'lib/grafana_reporter/configuration.rb', line 95 def grafana_api_key(instance = 'default') get_config("grafana:#{instance}:api_key") end |
#grafana_host(instance = 'default') ⇒ String
Returns configured ‘host’ for the requested grafana instance.
86 87 88 89 90 91 |
# File 'lib/grafana_reporter/configuration.rb', line 86 def grafana_host(instance = 'default') host = get_config("grafana:#{instance}:host") raise GrafanaInstanceWithoutHostError, instance if host.nil? host end |
#grafana_instances ⇒ Array<String>
Returns names of the configured grafana_instances.
79 80 81 82 |
# File 'lib/grafana_reporter/configuration.rb', line 79 def grafana_instances instances = get_config('grafana') instances.keys end |
#grafana_ssl_cert(instance = 'default') ⇒ String
Returns configured ‘ssl-cert’ for the requested grafana instance.
101 102 103 |
# File 'lib/grafana_reporter/configuration.rb', line 101 def grafana_ssl_cert(instance = 'default') get_config("grafana:#{instance}:ssl-cert") end |
#grafana_ssl_disable_verify(instance = 'default') ⇒ String
Returns configured ‘ssl-disable-verify’ for the requested grafana instance.
107 108 109 |
# File 'lib/grafana_reporter/configuration.rb', line 107 def grafana_ssl_disable_verify(instance = 'default') get_config("grafana:#{instance}:ssl-disable-verify") || false end |
#images_folder ⇒ String
Returns configured folder, in which temporary images during report generation shall be stored including trailing slash. Folder has to be a subfolder of #templates_folder. By default: current folder.
124 125 126 127 128 129 130 131 132 |
# File 'lib/grafana_reporter/configuration.rb', line 124 def images_folder img_path = templates_folder img_path = if img_path.empty? get_config('default-document-attributes:imagesdir').to_s else img_path + get_config('default-document-attributes:imagesdir').to_s end img_path.empty? ? './' : img_path.sub(%r{/*$}, '/') end |
#latest_version_check_ok? ⇒ Boolean
Checks if this is the latest ruby-grafana-reporter version. If and how often the check if performed, depends on the configuration setting ‘check-for-updates`. By default this is 0 (=disabled). If a number >0 is specified, the checks are performed once every n-days on report creation or call of overview webpage.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/grafana_reporter/configuration.rb', line 172 def latest_version_check_ok? return false if @newer_version_exists value = get_config('grafana-reporter:check-for-updates') || 0 return true if value <= 0 # repeat check only every n-th day if @last_version_check return true if (Time.now - @last_version_check) < (value * 24*60*60) end # check for newer version @last_version_check = Time.now url = 'https://github.com/divinity666/ruby-grafana-reporter/releases/latest' response = Grafana::WebRequest.new(url).execute return true if response['location'] =~ /.*[\/v]#{GRAFANA_REPORTER_VERSION.join('.')}$/ @newer_version_exists = true return false end |
#load_config_from_file(config_file = nil) ⇒ Hash
Reads a given configuration file.
40 41 42 43 44 45 |
# File 'lib/grafana_reporter/configuration.rb', line 40 def load_config_from_file(config_file = nil) config_file ||= DEFAULT_CONFIG_FILE_NAME self.config = YAML.load_file(config_file) rescue StandardError => e raise ConfigurationError, "Could not read config file '#{config_file}' (Error: #{e.})" end |
#merge!(other_config) ⇒ Object
Merge the given configuration object settings with the current config, i.e. overwrite and add all settings from the given config, but keep the not specified configs from the current object.
param other_config [Configuration] other configuration object
232 233 234 235 |
# File 'lib/grafana_reporter/configuration.rb', line 232 def merge!(other_config) config.merge!(other_config.config) { |_key, v1, v2| v1.is_a?(Hash) && v2.is_a?(Hash) ? v1.merge(v2) : v2 } update_configuration end |
#mode ⇒ String
Returns mode, in which the reporting shall be executed. One of MODE_CONNECTION_TEST, MODE_SINGLE_RENDER and MODE_SERVICE.
55 56 57 58 59 60 61 62 |
# File 'lib/grafana_reporter/configuration.rb', line 55 def mode if (get_config('grafana-reporter:run-mode') != MODE_CONNECTION_TEST) && (get_config('grafana-reporter:run-mode') != MODE_SINGLE_RENDER) return MODE_SERVICE end get_config('grafana-reporter:run-mode') end |
#report_retention ⇒ Integer
Returns how many hours a generated report shall be retained, before it shall be deleted. By default: 24.
150 151 152 |
# File 'lib/grafana_reporter/configuration.rb', line 150 def report_retention get_config('grafana-reporter:report-retention') || 24 end |
#reports_folder ⇒ String
Returns configured folder, in which the reports shall be stored including trailing slash. By default: current folder.
141 142 143 144 145 146 |
# File 'lib/grafana_reporter/configuration.rb', line 141 def reports_folder result = get_config('grafana-reporter:reports-folder') || '.' return result.sub(%r{/*$}, '/') unless result.empty? result end |
#set_param(path, value) ⇒ Object
Can be used to configure or overwrite single parameters.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/grafana_reporter/configuration.rb', line 212 def set_param(path, value) return if path.nil? levels = path.split(':') last_level = levels.pop cur_pos = @config levels.each do |subpath| cur_pos[subpath] = {} unless cur_pos[subpath] cur_pos = cur_pos[subpath] end cur_pos[last_level] = value update_configuration end |
#template ⇒ String
Returns full path of configured report template. Only needed in MODE_SINGLE_RENDER.
65 66 67 68 69 |
# File 'lib/grafana_reporter/configuration.rb', line 65 def template return nil if get_config('default-document-attributes:var-template').nil? "#{templates_folder}#{get_config('default-document-attributes:var-template')}" end |
#templates_folder ⇒ String
Returns configured folder, in which the report templates are stored including trailing slash. By default: current folder.
113 114 115 116 117 118 |
# File 'lib/grafana_reporter/configuration.rb', line 113 def templates_folder result = get_config('grafana-reporter:templates-folder') || '.' return result.sub(%r{/*$}, '/') unless result.empty? result end |
#test_instance ⇒ String
Returns name of grafana instance, against which a test shall be executed.
135 136 137 |
# File 'lib/grafana_reporter/configuration.rb', line 135 def test_instance get_config('grafana-reporter:test-instance') end |
#to_file ⇒ String
Returns destination filename for the report in MODE_SINGLE_RENDER.
72 73 74 75 76 |
# File 'lib/grafana_reporter/configuration.rb', line 72 def to_file return get_config('to_file') || true if mode == MODE_SINGLE_RENDER get_config('to_file') end |
#validate(explicit = false) ⇒ void
This method returns an undefined value.
This function shall be called, before the configuration object is used in the Application::Application#run. It ensures, that everything is setup properly and all necessary folders exist. Appropriate errors are raised in case of errors.
198 199 200 201 202 203 204 205 206 |
# File 'lib/grafana_reporter/configuration.rb', line 198 def validate(explicit = false) check_deprecation validate_schema(schema(explicit), @config) # check if set folders exist raise FolderDoesNotExistError.new(reports_folder, 'reports-folder') unless File.directory?(reports_folder) raise FolderDoesNotExistError.new(templates_folder, 'templates-folder') unless File.directory?(templates_folder) raise FolderDoesNotExistError.new(images_folder, 'images-folder') unless File.directory?(images_folder) end |
#webserver_port ⇒ Integer
Returns port, on which the webserver shall run. By default: 8815.
155 156 157 |
# File 'lib/grafana_reporter/configuration.rb', line 155 def webserver_port get_config('grafana-reporter:webservice-port') || 8815 end |