Module: Wavefront::Mixins

Included in:
Alerting, Cli::BatchWrite, Cli::Events, Cli::Ts, Cli::Write, Events, Metadata, Response::Graphite
Defined in:
lib/wavefront/mixins.rb

Overview

Various things which help around the place

Instance Method Summary collapse

Instance Method Details

#hash_to_qs(payload) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/wavefront/mixins.rb', line 54

def hash_to_qs(payload)
  #
  # Make a properly escaped query string out of a key: value
  # hash.
  #
  URI.escape(payload.map { |k, v| [k, v].join('=') }.join('&'))
end

#interpolate_schema(label, host, prefix_length) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/wavefront/mixins.rb', line 22

def interpolate_schema(label, host, prefix_length)
  label_parts = label.split('.')
  interpolated = []
  interpolated << label_parts.shift(prefix_length)
  interpolated << host
  interpolated << label_parts
  interpolated.flatten!
  interpolated.join('.')
end

#parse_time(t) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wavefront/mixins.rb', line 32

def parse_time(t)
  #
  # Return a time as an integer, however it might come in.
  #
  begin
    return t if t.is_a?(Integer)
    return t.to_i if t.is_a?(Time)
    return t.to_i if t.is_a?(String) && t.match(/^\d+$/)
    DateTime.parse("#{t} #{Time.now.getlocal.zone}").to_time.utc.to_i
  rescue
    raise "cannot parse timestamp '#{t}'."
  end
end

#time_to_ms(t) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/wavefront/mixins.rb', line 46

def time_to_ms(t)
  #
  # Return the time as milliseconds since the epoch
  #
  return false unless t.is_a?(Integer)
  (t.to_f * 1000).round
end

#uri_concat(*args) ⇒ Object



62
63
64
# File 'lib/wavefront/mixins.rb', line 62

def uri_concat(*args)
  args.join('/').squeeze('/')
end