Module: Wavefront::Mixins

Included in:
Base
Defined in:
lib/wavefront-sdk/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

#parse_relative_time(time, in_ms = false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/wavefront-sdk/mixins.rb', line 45

def parse_relative_time(time, in_ms = false)
  unless time.start_with?('+', '-')
    raise Wavefront::Exception::InvalidRelativeTime
  end

  m = in_ms ? 1000 : 1

  time = time[1..-1] if time.start_with?('+')
  match = time.match(/^(-?\d*\.?\d*)([smhdwy])$/)
  (match[1].to_f * time_multiplier(match[2]) * m).to_i
rescue NoMethodError
  raise Wavefront::Exception::InvalidRelativeTime
end

#parse_time(time, in_ms = false) ⇒ Integer

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

Parameters:

  • time (Integer, String, Time)

    timestamp

  • in_ms (Boolean) (defaults to: false)

    whether to return epoch milliseconds. Passing in an integer timestamp returns itself, regardless of this value

Returns:

  • (Integer)

    epoch time in seconds

Raises:

  • Wavefront::InvalidTimestamp



20
21
22
23
# File 'lib/wavefront-sdk/mixins.rb', line 20

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.

Parameters:

  • time (String)

    relative time string. Must begin with + or -, followed by a number, finished with a lower-case time unit identifier. See #time_multiplier

  • in_ms (Boolean) (defaults to: false)

    whether to return epoch milliseconds. Passing in an integer timestamp returns itself, regardless of this value

  • ref (Time, DateTime) (defaults to: Time.now)

    calculate time relative to this point. Primarily for easier testing. Defaults to “now”.

Returns:

  • (Integer)

    integer timestamp

Raises:

  • (InvalidRelativeTime)

    if t does not meet requirements



40
41
42
43
# File 'lib/wavefront-sdk/mixins.rb', line 40

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.

Parameters:

  • suffix (Symbol, String)

Returns:

  • (Integer)

    the number of seconds in one unit of the given suffix

Raises:

  • InvalidTimeUnit if the suffix is unknown



70
71
72
73
74
75
# File 'lib/wavefront-sdk/mixins.rb', line 70

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