Class: Grafana::Grafana
- Inherits:
-
Object
- Object
- Grafana::Grafana
- Defined in:
- lib/grafana/grafana.rb
Overview
Main class for handling the interaction with one specific Grafana instance.
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#dashboard(dashboard_uid) ⇒ Dashboard
Dashboard object, if it has been found.
-
#dashboard_ids ⇒ Array
Array of dashboard uids within the current grafana object.
-
#datasource_by_id(datasource_id) ⇒ Datasource
Returns the datasource, which has been queried by the datasource id.
-
#datasource_by_name(datasource_name) ⇒ Datasource
Returns the datasource, which has been queried by the datasource name.
-
#datasource_by_uid(datasource_uid) ⇒ Datasource
Returns the datasource, which has been queried by the datasource uid.
-
#initialize(base_uri, key = nil, opts = {}) ⇒ Grafana
constructor
A new instance of Grafana.
-
#organization ⇒ Hash
Information about the current organization.
-
#prepare_request(options = {}) ⇒ WebRequest
Prepares a WebRequest object for the current Grafana instance, which may be enriched with further properties and can then run WebRequest#execute.
-
#test_connection ⇒ String
Used to test a connection to the grafana instance.
-
#version ⇒ String
Grafana version.
Constructor Details
#initialize(base_uri, key = nil, opts = {}) ⇒ Grafana
Returns a new instance of Grafana.
19 20 21 22 23 24 25 26 |
# File 'lib/grafana/grafana.rb', line 19 def initialize(base_uri, key = nil, opts = {}) @base_uri = base_uri @key = key @dashboards = {} @logger = opts[:logger] || ::Logger.new(nil) initialize_datasources unless @base_uri.empty? end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
12 13 14 |
# File 'lib/grafana/grafana.rb', line 12 def logger @logger end |
Instance Method Details
#dashboard(dashboard_uid) ⇒ Dashboard
Returns dashboard object, if it has been found.
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/grafana/grafana.rb', line 124 def dashboard(dashboard_uid) return @dashboards[dashboard_uid] unless @dashboards[dashboard_uid].nil? response = prepare_request({ relative_url: "/api/dashboards/uid/#{dashboard_uid}" }).execute raise DashboardDoesNotExistError, dashboard_uid unless response.is_a?(Net::HTTPOK) # cache dashboard for reuse model = JSON.parse(response.body)['dashboard'] @dashboards[dashboard_uid] = Dashboard.new(model, self) @dashboards[dashboard_uid] end |
#dashboard_ids ⇒ Array
Returns Array of dashboard uids within the current grafana object.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/grafana/grafana.rb', line 109 def dashboard_ids response = prepare_request({ relative_url: '/api/search' }).execute return [] unless response.is_a?(Net::HTTPOK) dashboards = JSON.parse(response.body) dashboards.each do |dashboard| @dashboards[dashboard['uid']] = nil unless @dashboards[dashboard['uid']] end @dashboards.keys end |
#datasource_by_id(datasource_id) ⇒ Datasource
Returns the datasource, which has been queried by the datasource id.
100 101 102 103 104 105 106 |
# File 'lib/grafana/grafana.rb', line 100 def datasource_by_id(datasource_id) clean_nil_datasources datasource = @datasources.select { |name, ds| ds.id == datasource_id.to_i }.values.first raise DatasourceDoesNotExistError.new('id', datasource_id) unless datasource datasource end |
#datasource_by_name(datasource_name) ⇒ Datasource
Returns the datasource, which has been queried by the datasource name.
75 76 77 78 79 80 81 82 |
# File 'lib/grafana/grafana.rb', line 75 def datasource_by_name(datasource_name) datasource_name = 'default' if datasource_name.to_s.empty? # TODO: PRIO add support for grafana builtin datasource types return UnsupportedDatasource.new(nil) if datasource_name.to_s =~ /-- (?:Mixed|Dashboard|Grafana) --/ raise DatasourceDoesNotExistError.new('name', datasource_name) unless @datasources[datasource_name] @datasources[datasource_name] end |
#datasource_by_uid(datasource_uid) ⇒ Datasource
Returns the datasource, which has been queried by the datasource uid.
88 89 90 91 92 93 94 |
# File 'lib/grafana/grafana.rb', line 88 def datasource_by_uid(datasource_uid) clean_nil_datasources datasource = @datasources.select { |ds_name, ds| ds.uid == datasource_uid }.values.first raise DatasourceDoesNotExistError.new('uid', datasource_uid) unless datasource datasource end |
#organization ⇒ Hash
Returns Information about the current organization.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/grafana/grafana.rb', line 29 def organization return @organization if @organization response = prepare_request({ relative_url: '/api/org/' }).execute if response.is_a?(Net::HTTPOK) @organization = JSON.parse(response.body) end @organization end |
#prepare_request(options = {}) ⇒ WebRequest
Prepares a WebRequest object for the current Grafana::Grafana instance, which may be enriched with further properties and can then run WebRequest#execute.
145 146 147 148 |
# File 'lib/grafana/grafana.rb', line 145 def prepare_request( = {}) auth = @key ? { authorization: "Bearer #{@key}" } : {} WebRequest.new(@base_uri, auth.merge({ logger: @logger }).merge()) end |
#test_connection ⇒ String
Used to test a connection to the grafana instance.
Running this function also determines, if the API configured here has Admin or NON-Admin privileges, or even fails on connecting to grafana.
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/grafana/grafana.rb', line 58 def test_connection if prepare_request({ relative_url: '/api/datasources' }).execute.is_a?(Net::HTTPOK) # we have admin rights @logger.warn('Reporter is running with Admin privileges on grafana. This is a potential security risk.') return 'Admin' end # check if we have lower rights return 'Failed' unless prepare_request({ relative_url: '/api/dashboards/home' }).execute.is_a?(Net::HTTPOK) @logger.info('Reporter is running with NON-Admin privileges on grafana.') 'NON-Admin' end |
#version ⇒ String
Returns grafana version.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/grafana/grafana.rb', line 41 def version return @version if @version response = prepare_request({ relative_url: '/api/health' }).execute if response.is_a?(Net::HTTPOK) @version = JSON.parse(response.body)['version'] end @version end |