Module: TSD::Format

Defined in:
lib/tsd_client/format.rb

Class Method Summary collapse

Class Method Details

.put(options) ⇒ Object



61
62
63
64
65
66
# File 'lib/tsd_client/format.rb', line 61

def self.put options
  [:metric, :value].any? {|key| raise "missing key: #{key}" unless options.has_key? key}
  options  = {time: Time.now, tags: {}}.merge options

  ['put', options[:metric], options[:time].to_i, options[:value], tags(options[:tags])].flatten.join("\s")
end

.query(options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tsd_client/format.rb', line 39

def self.query options
  raise 'must provide key: :start' unless options[:start]
  options_filter = [:metric, :aggregator, :rate, :downsample, :tags]

  # process query params
  params = options.reduce({m: query_metric(options)}) do |params, (option, value)|
    unless options_filter.include? option
      params[option] = case option
      when :start, :end # time formatting
        time value
      else
        value
      end        
    end

    params
  end

  # assemble query
  URI.escape '/q?' + params.map {|option, value| [option, value].join('=')}.unshift('ascii').join('&')      
end

.query_metric(options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tsd_client/format.rb', line 22

def self.query_metric options
  raise 'must provide key: :metric' unless options[:metric]
  options = {aggregator: 'sum', rate: false}.merge options

  metric_query = [options[:aggregator]]
  metric_query << options[:downsample] if options[:downsample]
  metric_query << 'rate' if options[:rate]

  if options[:tags]
    metric_query << [options[:metric], '{' + tags(options[:tags]).join(',') + '}'].join
  else
    metric_query << options[:metric]
  end

  metric_query.join ':'
end

.tags(tags) ⇒ Object



16
17
18
19
20
# File 'lib/tsd_client/format.rb', line 16

def self.tags tags
  tags.collect do |tag, value|
    [tag, value].join '='
  end
end

.time(input) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/tsd_client/format.rb', line 5

def self.time input
  case input.class.name
  when 'Time'
    input.strftime '%Y/%m/%d-%H:%M:%S'
  when 'Fixnum'
    time Time.at input
  else
    input
  end
end