Class: Grafana::InfluxDbDatasource
- Inherits:
-
AbstractDatasource
- Object
- AbstractDatasource
- Grafana::InfluxDbDatasource
- Defined in:
- lib/grafana/influxdb_datasource.rb
Overview
Implements the interface to Prometheus datasources.
Instance Attribute Summary
Attributes inherited from AbstractDatasource
Class Method Summary collapse
Instance Method Summary collapse
- #default_variable_format ⇒ Object
- #raw_query_from_panel_model(panel_query_target) ⇒ Object
-
#request(query_description) ⇒ Object
:database
needs to contain the InfluxDb database name:raw_query
needs to contain a InfluxDb query as String.
Methods inherited from AbstractDatasource
build_instance, #category, #id, inherited, #initialize, #name, #replace_variables, #type, #uid
Constructor Details
This class inherits a constructor from Grafana::AbstractDatasource
Class Method Details
.handles?(model) ⇒ Boolean
7 8 9 10 |
# File 'lib/grafana/influxdb_datasource.rb', line 7 def self.handles?(model) tmp = new(model) tmp.type == 'influxdb' end |
Instance Method Details
#default_variable_format ⇒ Object
53 54 55 |
# File 'lib/grafana/influxdb_datasource.rb', line 53 def default_variable_format 'regex' end |
#raw_query_from_panel_model(panel_query_target) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/grafana/influxdb_datasource.rb', line 45 def raw_query_from_panel_model(panel_query_target) return panel_query_target['query'] if panel_query_target['rawQuery'] # build composed queries build_select(panel_query_target['select']) + build_from(panel_query_target) + build_where(panel_query_target['tags']) + build_group_by(panel_query_target['groupBy']) end |
#request(query_description) ⇒ Object
:database
needs to contain the InfluxDb database name :raw_query
needs to contain a InfluxDb query as String
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/grafana/influxdb_datasource.rb', line 15 def request(query_description) raise MissingSqlQueryError if query_description[:raw_query].nil? # replace variables query = replace_variables(query_description[:raw_query], query_description[:variables]) # Unfortunately the grafana internal variables are not replaced in the grafana backend, but in the # frontend, i.e. we have to replace them here manually # replace $timeFilter variable query = query.gsub(/\$timeFilter(?=\W|$)/, "time >= #{query_description[:from]}ms and time <= #{query_description[:to]}ms") interval = query_description[:variables].delete('interval') || ((query_description[:to].to_i - query_description[:from].to_i) / 1000).to_i interval = interval.raw_value if interval.is_a?(Variable) # replace grafana variables $__interval and $__interval_ms in query # TODO: check where calculation and replacement of interval variable should take place query = query.gsub(/\$(?:__)?interval(?=\W|$)/, "#{interval.is_a?(String) ? interval : "#{(interval / 1000).to_i}s"}") query = query.gsub(/\$(?:__)?interval_ms(?=\W|$)/, "#{interval}") url = "/api/datasources/proxy/#{id}/query?db=#{@model['database']}&q=#{ERB::Util.url_encode(query)}&epoch=ms" webrequest = query_description[:prepared_request] webrequest.relative_url = url webrequest..merge!({ request: Net::HTTP::Get }) result = webrequest.execute(query_description[:timeout]) preformat_response(result.body) end |