wavefront-sdk 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2002dacfb813974ef1b2450b4843142bc399d472
4
- data.tar.gz: 6b6f2ec545568e1ae155b98c9ecf0b6d8b7e6737
3
+ metadata.gz: 6ee9f41acd3fe0813ce536d55ec05f27faddc67d
4
+ data.tar.gz: 4c316c36eb1256aaa7821465bc33a6a16a0beb26
5
5
  SHA512:
6
- metadata.gz: 7f36ed4ec001d74f134c5e9b893cc11e9f71872b885960ceb5da87d7d8915c5b63dbc362604d599f768efa826ea6efa63a17802a8f1bea4ce254fd41184620ae
7
- data.tar.gz: 1641bf8dadb681d1401c332136fc7dd139379fb65c890e40f5deeee129de973c7f97a19587cdf31285d78c7a76e8d0ae89d4296de180fdf1b91dd70e1be713a3
6
+ metadata.gz: 3543d7d92e10e2b050c02619ebf72ad62a42eaabcd3e7981daf86a840f432c141bc67671dcf526d1333277783444a7c6dc47ae358cc6dd5b4e8d505f883ae3d8
7
+ data.tar.gz: 314e7259a6bd111678b3295966a78deb524f821630ea2725f178fdc19856c240fb6ce59d3fa717f16369e01ebfce50c77f51bf0add382f55080bd73ac134ff17
@@ -14,6 +14,8 @@ module Wavefront
14
14
  class InvalidGranularity < ::Exception; end
15
15
  class InvalidHostname < ::Exception; end
16
16
  class InvalidIntegrationId < ::Exception; end
17
+ class InvalidRelativeTime < ::Exception; end
18
+ class InvalidTimeUnit < ::Exception; end
17
19
  class InvalidMaintenanceWindowId < ::Exception; end
18
20
  class InvalidMessageId < ::Exception; end
19
21
  class InvalidMetricName < ::Exception; end
@@ -1,3 +1,4 @@
1
+ require 'date'
1
2
  require_relative './exception'
2
3
 
3
4
  module Wavefront
@@ -5,7 +6,9 @@ module Wavefront
5
6
  # Return a time as an integer, however it might come in.
6
7
  #
7
8
  # @param t [Integer, String, Time] timestamp
8
- # @param ms [Boolean] whether to return epoch milliseconds
9
+ # @param ms [Boolean] whether to return epoch milliseconds.
10
+ # Passing in an integer timestamp returns itself, regardless
11
+ # of this value
9
12
  # @return [Integer] epoch time in seconds
10
13
  # @raise Wavefront::InvalidTimestamp
11
14
  #
@@ -15,10 +18,15 @@ module Wavefront
15
18
  # through. No validation, because maybe the user means one
16
19
  # second past the epoch, or the year 2525.
17
20
  #
18
- return t if t.is_a?(Integer)
21
+ return t if t.is_a?(Numeric)
19
22
 
20
23
  if t.is_a?(String)
21
24
  return t.to_i if t.match(/^\d+$/)
25
+
26
+ if t.start_with?('-') || t.start_with?('+')
27
+ return relative_time(t, ms)
28
+ end
29
+
22
30
  begin
23
31
  t = DateTime.parse("#{t} #{Time.now.getlocal.zone}")
24
32
  rescue
@@ -28,10 +36,59 @@ module Wavefront
28
36
 
29
37
  ms ? t.to_datetime.strftime('%Q').to_i : t.strftime('%s').to_i
30
38
  end
39
+
40
+ # Return a timetamp described by the given string. That is,
41
+ # '+5m' is five minutes in the future, and '-.1h' is half an
42
+ # hour ago.
43
+ #
44
+ # @param t [String] relative time string. Must begin with + or
45
+ # -, followed by a number, finished with a lower-case time
46
+ # unit identifier. See #time_multiplier
47
+ # @param ref [Time, DateTime] calculate time relative to this
48
+ # point. Primarily for easier testing. Defaults to "now".
49
+ # @return [Integer] integer timestamp
50
+ # @raise [InvalidRelativeTime] if t does not meet requirements
51
+ #
52
+ def relative_time(t, ms = false, ref = DateTime.now)
53
+ ref = ms ? ref.to_date.strftime('%Q') : ref.to_time
54
+ ref.to_i + parse_relative_time(t, ms)
55
+ end
56
+
57
+ def parse_relative_time(t, ms = false)
58
+ unless t.start_with?('+') || t.start_with?('-')
59
+ raise Wavefront::Exception::InvalidRelativeTime
60
+ end
61
+
62
+ m = ms ? 1000 : 1
63
+
64
+ t = t[1..-1] if t.start_with?('+')
65
+ match = t.match(/^(-?\d*\.?\d*)([smhdwy])$/)
66
+ (match[1].to_f * time_multiplier(match[2]) * m).to_i
67
+ rescue NoMethodError
68
+ raise Wavefront::Exception::InvalidRelativeTime
69
+ end
70
+
71
+ # naively return the number of seconds from the given
72
+ # multiplier. This makes absolutely no attempt to compensate for
73
+ # any kind of daylight savings or calendar adjustment. A day is
74
+ # always going to 60 seconds x 60 minutes x 24 hours, and a
75
+ # year will always have 365 days.
76
+ #
77
+ # @param suffix [Symbol, String]
78
+ # @return [Integer] the number of seconds in one unit of the
79
+ # given suffix
80
+ # @raise InvalidTimeUnit if the suffix is unknown
81
+ #
82
+ def time_multiplier(suffix)
83
+ u = { s: 1, m: 60, h: 3600, d: 86400, w: 604800, y: 31536000 }
84
+
85
+ return u[suffix.to_sym] if u.key?(suffix.to_sym)
86
+ raise Wavefront::Exception::InvalidTimeUnit
87
+ end
31
88
  end
32
89
  end
33
90
 
34
- # Extensions to the Hash class
91
+ # Extensions to stdlib Hash
35
92
  #
36
93
  class Hash
37
94
 
@@ -1 +1 @@
1
- WF_SDK_VERSION = '1.1.0'.freeze
1
+ WF_SDK_VERSION = '1.2.0'.freeze
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative '../spec_helper'
4
4
  require_relative '../../lib/wavefront-sdk/mixins'
5
+ require 'spy/integration'
5
6
 
6
7
  # Test SDK mixins
7
8
  #
@@ -23,5 +24,69 @@ class WavefrontMixinsTest < MiniTest::Test
23
24
  assert_raises(Wavefront::Exception::InvalidTimestamp) do
24
25
  parse_time('nonsense')
25
26
  end
27
+
28
+ trt_spy = Spy.on(self, :relative_time).and_call_through
29
+
30
+ parse_time('+5m')
31
+ parse_time('-0.5m')
32
+
33
+ assert_equal trt_spy.calls.length, 2
34
+ end
35
+
36
+ def test_relative_time
37
+ base = Time.now
38
+ bi = base.to_i
39
+ ms_base = base.to_date
40
+ mbi = ms_base.strftime('%Q').to_i
41
+
42
+ assert_equal relative_time('+60s', false, base), bi + 60
43
+ assert_equal relative_time('-60s', false, base), bi - 60
44
+ assert_equal relative_time('+.5m', false, base), bi + 30
45
+ assert_equal relative_time('-000.50m', false, base), bi - 30
46
+
47
+ assert_equal relative_time('+60s', true, ms_base), mbi + 60 * 1000
48
+ assert_equal relative_time('-60s', true, ms_base), mbi - 60 * 1000
49
+ assert_equal relative_time('+.5m', true, ms_base), mbi + 30 * 1000
50
+ assert_equal relative_time('-000.50m', true, ms_base), mbi - 30 * 1000
51
+
52
+ assert_raises(Wavefront::Exception::InvalidRelativeTime) do
53
+ relative_time('5m')
54
+ end
55
+ end
56
+
57
+ def test_parse_relative_time
58
+ assert_equal parse_relative_time('-5s'), -5
59
+ assert_equal parse_relative_time('-5s', true), -5000
60
+ assert_equal parse_relative_time('+10000s'), 10000
61
+ assert_equal parse_relative_time('-5m'), -300
62
+ assert_equal parse_relative_time('-.5m'), -30
63
+ assert_equal parse_relative_time('-0.5m'), -30
64
+ assert_equal parse_relative_time('+5m'), 300
65
+ assert_equal parse_relative_time('+.5m'), 30
66
+ assert_equal parse_relative_time('+.50m'), 30
67
+ assert_equal parse_relative_time('+.50m', true), 30 * 1000
68
+ assert_equal parse_relative_time('+.50m'), 30
69
+ assert_equal parse_relative_time('+1.5d'), 60 * 60 * 24 * 1.5
70
+ assert_equal parse_relative_time('-1.5d'), 60 * 60 * 24 * -1.5
71
+
72
+ ['-1.5p', '1.5m', '+1.3.5s'].each do
73
+ assert_raises Wavefront::Exception::InvalidRelativeTime do |t|
74
+ parse_relative_time(t)
75
+ end
76
+ end
77
+ end
78
+
79
+ def test_time_multiplier
80
+ assert_equal time_multiplier(:s), 1
81
+ assert_equal time_multiplier('s'), 1
82
+ assert_equal time_multiplier(:m), 60
83
+ assert_equal time_multiplier(:h), 60 * 60
84
+ assert_equal time_multiplier(:d), 60 * 60 * 24
85
+ assert_equal time_multiplier(:w), 60 * 60 * 24 * 7
86
+ assert_equal time_multiplier(:y), 60 * 60 * 24 * 365
87
+
88
+ assert_raises(Wavefront::Exception::InvalidTimeUnit) do
89
+ time_multiplier(:p)
90
+ end
26
91
  end
27
92
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wavefront-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Fisher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-09 00:00:00.000000000 Z
11
+ date: 2017-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -257,7 +257,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
257
257
  version: '0'
258
258
  requirements: []
259
259
  rubyforge_project:
260
- rubygems_version: 2.6.13
260
+ rubygems_version: 2.6.14
261
261
  signing_key:
262
262
  specification_version: 4
263
263
  summary: SDK for Wavefront API v2