Module: Wavefront::Mixins

Included in:
ApiCaller, CoreApi, Distribution, Response
Defined in:
lib/wavefront-sdk/support/mixins.rb

Overview

Methods which help out in the SDK, but may also be useful when coding against the SDK.

Instance Method Summary collapse

Instance Method Details

#log(message, severity = :info) ⇒ Object



91
92
93
# File 'lib/wavefront-sdk/support/mixins.rb', line 91

def log(message, severity = :info)
  logger.log(message, severity)
end

#parse_relative_time(time, in_ms = false) ⇒ Integer

Do the real work for #relative_time



54
55
56
57
58
59
60
61
62
63
# File 'lib/wavefront-sdk/support/mixins.rb', line 54

def parse_relative_time(time, in_ms = false)
  unless valid_relative_time?(time)
    raise Wavefront::Exception::InvalidRelativeTime
  end

  m = in_ms ? 1000 : 1
  time.delete!('+')
  match = time.match(/^(-?\d*\.?\d*)([smhdwy])$/)
  (match[1].to_f * time_multiplier(match[2]) * m).to_i
end

#parse_time(time, in_ms = false) ⇒ Integer

Return a time as an integer, however it might come in.

Raises:

  • Wavefront::InvalidTimestamp



23
24
25
26
# File 'lib/wavefront-sdk/support/mixins.rb', line 23

def parse_time(time, in_ms = false)
  return relative_time(time, in_ms) if time =~ /^[\-+]/
  ParseTime.new(time, in_ms).parse!
end

#relative_time(time, in_ms = false, ref = Time.now) ⇒ Integer

Return a timestamp described by the given string. That is, ‘+5m’ is five minutes in the future, and ‘-.1h’ is half an hour ago.

Raises:

  • (InvalidRelativeTime)

    if t does not meet requirements



43
44
45
46
# File 'lib/wavefront-sdk/support/mixins.rb', line 43

def relative_time(time, in_ms = false, ref = Time.now)
  ref = in_ms ? ref.to_datetime.strftime('%Q') : ref.to_time
  ref.to_i + parse_relative_time(time, in_ms)
end

#time_multiplier(suffix) ⇒ Integer

naively return the number of seconds from the given multiplier. This makes absolutely no attempt to compensate for any kind of daylight savings or calendar adjustment. A day is always going to 60 seconds x 60 minutes x 24 hours, and a year will always have 365 days.

Raises:

  • InvalidTimeUnit if the suffix is unknown



84
85
86
87
88
89
# File 'lib/wavefront-sdk/support/mixins.rb', line 84

def time_multiplier(suffix)
  u = { s: 1, m: 60, h: 3600, d: 86_400, w: 604_800, y: 31_536_000 }

  return u[suffix.to_sym] if u.key?(suffix.to_sym)
  raise Wavefront::Exception::InvalidTimeUnit
end

#valid_relative_time?(time) ⇒ Bool

Is a relative time valid?



69
70
71
# File 'lib/wavefront-sdk/support/mixins.rb', line 69

def valid_relative_time?(time)
  time =~ /^[+-](-?\d*\.?\d*)[smhdwy]$/
end