hallmonitor 4.0.0 → 4.1.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: 2bf8d13dcc1fef8473f9ba94777c6ccdc764f19c
4
- data.tar.gz: deeab3f14fd9fa69015ed13aaa607edf9239b1d9
3
+ metadata.gz: d535fc73fa9953de660b0884b36496a159c20d7b
4
+ data.tar.gz: 336bb38e055f533a545d859eb92461b97f025438
5
5
  SHA512:
6
- metadata.gz: 814c9f8c30f73f40dd1ad3b474ede958444dfe2b0918fc0de87166dad1259ac3733efe62ca30adbbeb11c658b01ded7b196eadfaad63196e7e4ccd6969d893a1
7
- data.tar.gz: affa457a4e18eac08dba4d6c4d0f5fc1580534a277b17b475257b7164ba8400d72e1e7f172481545ca91d2b79278ba7987e41956e8f1bdc0548456354f5b260b
6
+ metadata.gz: 8b637d698988d1f05b2603df29c94fc5fc7de6c8a845e331e8acc2599c093e675d9fc5b2d3214fd749c93eaedf00650cd94f99b941095a006f5413a0de6014ff
7
+ data.tar.gz: 58add99e48674c87f883545c05b2a4b90301892b708086d1196c74cea358b4cf41498b9f212770ed2f66fd924f5ee8cec72d176443ed935b789540eb5ad8091e
@@ -8,7 +8,18 @@ module Hallmonitor
8
8
  # An outputter for InfluxDB
9
9
  class Influxdb < Outputter
10
10
  # Simple EventData struct, used to communicate with an optional Transformer
11
- EventData = Struct.new(:name, :tags, :fields)
11
+ EventData = Struct.new(:name, :tags, :fields, :timestamp)
12
+
13
+ # Maps Influxdb client precisions to multipliers for Time#to_r to get
14
+ # the value to send to influxdb for the timestamp
15
+ PRECISION_MAP = {
16
+ 'ns' => 10**9,
17
+ 'u' => 10**6,
18
+ 'ms' => 10**3,
19
+ 's' => 1,
20
+ 'm' => 1 / 60,
21
+ 'h' => 1 / 3600
22
+ }.freeze
12
23
 
13
24
  # @return [#transform(Event, EventData)] Object used to transform data
14
25
  # before it is sent to InfluxDB
@@ -39,6 +50,7 @@ module Hallmonitor
39
50
  @tags = {}.merge(tags)
40
51
  @client = influxdb_client || raise('Must supply an InfluxDb client')
41
52
  @transformer = transformer
53
+ @precision_mult = PRECISION_MAP[@client.config.time_precision || 's']
42
54
  end
43
55
 
44
56
  # Sends events to InfluxDB instance
@@ -54,7 +66,7 @@ module Hallmonitor
54
66
  # @param data [EventData] Struct of data we're building for InfluxDB
55
67
  def transform_and_write(event, event_data)
56
68
  event_data = @transformer.transform(event, event_data) if @transformer
57
- data = { tags: event_data.tags, values: event_data.fields }
69
+ data = { tags: event_data.tags, values: event_data.fields, timestamp: event_data.timestamp }
58
70
  @client.write_point(event_data.name, data)
59
71
  end
60
72
 
@@ -75,9 +87,17 @@ module Hallmonitor
75
87
  data.name = event.name
76
88
  data.tags = @tags.merge(event.tags.merge(type: type))
77
89
  data.fields = value.is_a?(Hash) ? value : { value: value }
90
+ data.timestamp = calc_timestamp(event)
78
91
  data
79
92
  end
80
93
 
94
+ # Calculates a timestamp based on the indicated {Time} object
95
+ # and the influxdb client's precision
96
+ # @param event [Hallmonitor::Event]
97
+ def calc_timestamp(event)
98
+ (event.time.to_r * @precision_mult).to_i if event.time.respond_to?(:to_r)
99
+ end
100
+
81
101
  def build_timer_data(event)
82
102
  build_data(event, 'timer', event.duration)
83
103
  end
@@ -1,7 +1,7 @@
1
1
  module Hallmonitor
2
2
  module Version
3
3
  MAJOR = 4
4
- MINOR = 0
4
+ MINOR = 1
5
5
  PATCH = 0
6
6
  BUILD = nil
7
7
 
@@ -6,7 +6,9 @@ module Hallmonitor
6
6
  RSpec.describe Influxdb do
7
7
  let(:influxdb_client) { nil }
8
8
  let(:default_tags) { {} }
9
- let(:outputter) { described_class.new(influxdb_client, tags: default_tags) }
9
+ let(:outputter) do
10
+ described_class.new(influxdb_client, tags: default_tags)
11
+ end
10
12
 
11
13
  context '#initialize' do
12
14
  context 'with a bad influxdb client' do
@@ -16,9 +18,13 @@ module Hallmonitor
16
18
  end
17
19
 
18
20
  context 'with a good client' do
19
- let(:influxdb_client) { double('influxdb_client') }
21
+ let(:influxdb_client) { instance_double(InfluxDB::Client) }
20
22
  before do
21
23
  allow(influxdb_client).to receive(:write_point)
24
+ allow(influxdb_client).to(
25
+ receive(:config)
26
+ .and_return(double(time_precision: 'ms'))
27
+ )
22
28
  end
23
29
 
24
30
  it 'does not raise an error' do
@@ -38,10 +44,18 @@ module Hallmonitor
38
44
  let(:expected_data) do
39
45
  {
40
46
  values: { value: expected_value },
41
- tags: default_tags.merge(tags).merge(type: expected_type)
47
+ tags: default_tags.merge(tags).merge(type: expected_type),
48
+ timestamp: a_value_within(1000).of((Time.now.to_r * 10**3).to_i)
42
49
  }
43
50
  end
44
51
 
52
+ before do
53
+ allow(influxdb_client).to(
54
+ receive(:config)
55
+ .and_return(double(time_precision: 'ms'))
56
+ )
57
+ end
58
+
45
59
  it 'sends the correct information to influxdb' do
46
60
  expect(influxdb_client).to(
47
61
  receive(:write_point).with(event_name, expected_data))
@@ -79,7 +93,8 @@ module Hallmonitor
79
93
  let(:expected_data) do
80
94
  {
81
95
  values: { value: expected_value },
82
- tags: { additional: 'foo' }
96
+ tags: { additional: 'foo' },
97
+ timestamp: nil
83
98
  }
84
99
  end
85
100
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hallmonitor
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris TenHarmsel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-13 00:00:00.000000000 Z
11
+ date: 2017-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json