Class: Grafana::InfluxDbDatasource

Inherits:
AbstractDatasource show all
Defined in:
lib/grafana/influxdb_datasource.rb

Overview

Implements the interface to Prometheus datasources.

Instance Attribute Summary

Attributes inherited from AbstractDatasource

#model

Class Method Summary collapse

Instance Method Summary collapse

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

Returns:

  • (Boolean)

See Also:



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_formatObject



78
79
80
# File 'lib/grafana/influxdb_datasource.rb', line 78

def default_variable_format
  'regex'
end

#raw_query_from_panel_model(panel_query_target) ⇒ Object



70
71
72
73
74
75
# File 'lib/grafana/influxdb_datasource.rb', line 70

def raw_query_from_panel_model(panel_query_target)
  return panel_query_target['query'] if panel_query_target['query'] or 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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}")

  webrequest = query_description[:prepared_request]
  request = {}

  ver = query_description[:grafana_version].split('.').map{|x| x.to_i}
  if ver[0] >= 8
    webrequest.relative_url = "/api/ds/query?ds_type=influxdb"

    request = {
      request: Net::HTTP::Post,
      body: {
        from: query_description[:from],
        to: query_description[:to],
        queries: [
          {
            datasource: {type: "influxdb"},
            datasourceId: id,
            intervalMs: interval,
            query: query
          }
      ]}.to_json
    }
  else
    webrequest.relative_url = "/api/datasources/proxy/#{id}/query?db=#{@model['database']}&q=#{ERB::Util.url_encode(query)}&epoch=ms"
    request = {
      request: Net::HTTP::Get
    }
  end

  webrequest.options.merge!(request)


  result = webrequest.execute(query_description[:timeout])
  preformat_response(result.body)
end