class OpenTSDBConsumer::Query

Attributes

client[RW]
metrics[RW]

Public Class Methods

new(metrics, client) click to toggle source
# File lib/opentsdb-consumer/query.rb, line 7
def initialize(metrics, client)
  @metrics = [metrics].flatten
  @client = client
end

Public Instance Methods

latest_value(start: '1m-ago') click to toggle source
# File lib/opentsdb-consumer/query.rb, line 21
def latest_value(start: '1m-ago')
  result = run(start: start)
  result.latest_value if result
end
run(start: '1h-ago') click to toggle source
# File lib/opentsdb-consumer/query.rb, line 12
def run(start: '1h-ago')
  response = client.post body: request_body(start)
  parsed_body = JSON.parse(response.body)
  return OpenTSDBConsumer::Result.build(parsed_body) if response.status < 400

  response_message = parsed_body['error']['message']
  fail error_for_response(response_message), response_message
end

Private Instance Methods

error_for_response(response_message) click to toggle source
# File lib/opentsdb-consumer/query.rb, line 32
def error_for_response(response_message)
  case response_message
  when /^No such name for 'tagv'/
    InvalidTag
  when /^No such name for 'metrics'/
    InvalidMetric
  else
    QueryError
  end
end
request_body(start) click to toggle source
# File lib/opentsdb-consumer/query.rb, line 28
def request_body(start)
  { start: start, queries: metrics.map(&:to_h) }.to_json
end