Class: Opower::TimeSeries::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/time_series/query.rb

Overview

Represents a query that can be sent to an OpenTSDB instance through a [TSDBClient] object.

Defined Under Namespace

Classes: GraphingRequest, MetricQuery

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Query

Creates a new Query object.

This object also supports all of the options available to the REST API for OpenTSDB. See opentsdb.net/http-api.html#/q_Parameters for more information.

Parameters:

  • config (Hash) (defaults to: {})

    The configuration for this query.

Options Hash (config):

  • :format (String)

    The format to return data with. Defaults to ‘json’.

  • :start (String, Integer, DateTime)

    The start time. Required field.

  • :end (String, Integer, DateTime)

    The end time. Optional field.

  • :m (Hash)

    Array of metric hashes. This maps to what OpenTSDB expects as the metrics to query.

    • :aggregator [String] The aggregation type to utilize. Optional. Defaults to ‘sum’ if omitted.

    • :metric [String] The metric name. Required.

    • :tags [Hash] Hash consisting of Tag Key / Tag Value pairs. Optional.

    • :down_sample [Hash] to specify downsampling period and function

      • :period [String] The period of time to downsample one

      • :function [String] The function [min, max, sum, avg, dev]

  • padding (Boolean)

    If set to true, OpenTSDB (>= 2.0) will pad the start/end period.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/time_series/query.rb', line 28

def initialize(config = {})
  @request = config
  @format = config.delete(:format)

  # Check that 'start' and 'm' parameters required by OpenTSDB are present
  @requirements = [:start, :m]
  validate_metrics

  @metrics = @request.delete(:m)
  check_metrics
  convert_dates

  # Create 'm' array - this is the
  @request[:m] = @metrics.map(&MetricQuery.method(:new))
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



7
8
9
# File 'lib/time_series/query.rb', line 7

def format
  @format
end

#metricsObject

Returns the value of attribute metrics.



7
8
9
# File 'lib/time_series/query.rb', line 7

def metrics
  @metrics
end

#requestObject

Returns the value of attribute request.



7
8
9
# File 'lib/time_series/query.rb', line 7

def request
  @request
end

#responseObject

Returns the value of attribute response.



7
8
9
# File 'lib/time_series/query.rb', line 7

def response
  @response
end

Instance Method Details

#as_graphString

Returns the current query as a URL to a PNG generated by OpenTSDB

Returns:

  • (String)

    url to gnuplot graph



47
48
49
# File 'lib/time_series/query.rb', line 47

def as_graph
  GraphingRequest.new(@request).to_s
end