Module: TSD::Format
- Defined in:
- lib/tsd_client/format.rb
Class Method Summary collapse
- .put(options) ⇒ Object
- .query(options, format = 'ascii') ⇒ Object
- .query_metric(options) ⇒ Object
- .tags(tags) ⇒ Object
- .time(input) ⇒ Object
Class Method Details
.put(options) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/tsd_client/format.rb', line 64 def self.put [:metric, :value].any? {|key| raise "missing key: #{key}" unless .has_key? key} [:time] ||= Time.now [:tags] ||= {host: Socket.gethostname} ['put', [:metric], [:time].to_i, [:value], ([:tags])].flatten.join("\s") end |
.query(options, format = 'ascii') ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/tsd_client/format.rb', line 42 def self.query , format = 'ascii' raise 'must provide key: :start' unless [:start] = [:metric, :aggregator, :rate, :downsample, :tags] # process query params params = .reduce({m: query_metric()}) do |params, (option, value)| unless .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(format).join('&') end |
.query_metric(options) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/tsd_client/format.rb', line 25 def self.query_metric raise 'must provide key: :metric' unless [:metric] = {aggregator: 'sum', rate: false}.merge metric_query = [[:aggregator]] metric_query << [:downsample] if [:downsample] metric_query << 'rate' if [:rate] if [:tags] metric_query << [[:metric], '{' + ([:tags]).join(',') + '}'].join else metric_query << [:metric] end metric_query.join ':' end |
.tags(tags) ⇒ Object
19 20 21 22 23 |
# File 'lib/tsd_client/format.rb', line 19 def self. .collect do |tag, value| [tag, value].join '=' end end |
.time(input) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/tsd_client/format.rb', line 6 def self.time input case input.class.name when 'Time' input.strftime '%Y/%m/%d-%H:%M:%S' when 'Date' time input.to_time when 'Fixnum' time Time.at input else input end end |