pulse-meter 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/examples/basic_sensor_data.rb +46 -29
- data/lib/pulse-meter.rb +1 -0
- data/lib/pulse-meter/mixins/utils.rb +12 -0
- data/lib/pulse-meter/sensor/configuration.rb +47 -0
- data/lib/pulse-meter/version.rb +1 -1
- data/pulse-meter.gemspec +1 -0
- data/spec/pulse_meter/mixins/utils_spec.rb +7 -0
- data/spec/pulse_meter/sensor/configuration_spec.rb +83 -0
- metadata +21 -2
@@ -4,46 +4,63 @@ require "pulse-meter"
|
|
4
4
|
|
5
5
|
PulseMeter.redis = Redis.new
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
cfg = PulseMeter::Sensor::Configuration.new(
|
8
|
+
lama_count: {
|
9
|
+
type: 'timelined/counter',
|
10
|
+
args: {
|
11
|
+
annotation: 'Lama Count',
|
12
|
+
interval: 10,
|
13
|
+
ttl: 3600
|
14
|
+
}
|
15
|
+
},
|
12
16
|
|
13
|
-
lama_average_age
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
17
|
+
lama_average_age: {
|
18
|
+
type: 'timelined/average',
|
19
|
+
args: {
|
20
|
+
annotation: 'Lama Average Age',
|
21
|
+
interval: 20,
|
22
|
+
ttl: 3600
|
23
|
+
}
|
24
|
+
},
|
18
25
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
26
|
+
rhino_count: {
|
27
|
+
type: 'timelined/counter',
|
28
|
+
args: {
|
29
|
+
annotation: 'Rhino Count',
|
30
|
+
interval: 10,
|
31
|
+
ttl: 3600
|
32
|
+
}
|
33
|
+
},
|
24
34
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
35
|
+
goose_count: {
|
36
|
+
type: 'timelined/hashed_counter',
|
37
|
+
args: {
|
38
|
+
annotation: 'Goose Count',
|
39
|
+
interval: 10,
|
40
|
+
ttl: 3600
|
41
|
+
}
|
42
|
+
},
|
30
43
|
|
31
|
-
rhino_average_age
|
32
|
-
|
33
|
-
|
34
|
-
|
44
|
+
rhino_average_age: {
|
45
|
+
type: 'timelined/average',
|
46
|
+
args: {
|
47
|
+
annotation: 'Rhino average age',
|
48
|
+
interval: 20,
|
49
|
+
ttl: 3600
|
50
|
+
}
|
51
|
+
}
|
35
52
|
)
|
36
53
|
|
37
54
|
while true
|
38
55
|
sleep(Random.rand)
|
39
56
|
STDERR.puts "tick"
|
40
|
-
|
41
|
-
|
42
|
-
lama_average_age
|
43
|
-
rhino_average_age
|
57
|
+
cfg.lama_count(1)
|
58
|
+
cfg.rhino_count(2)
|
59
|
+
cfg.lama_average_age(Random.rand(50))
|
60
|
+
cfg.rhino_average_age(Random.rand(100))
|
44
61
|
|
45
62
|
10.times do
|
46
63
|
goose_n = Random.rand(4)
|
47
|
-
|
64
|
+
cfg.goose_count("goose_#{goose_n}" => 1)
|
48
65
|
end
|
49
66
|
end
|
data/lib/pulse-meter.rb
CHANGED
@@ -75,6 +75,18 @@ module PulseMeter
|
|
75
75
|
(first_letter_upper ? first.capitalize : first.downcase) + terms.map(&:capitalize).join
|
76
76
|
end
|
77
77
|
|
78
|
+
# Symbolizes hash keys
|
79
|
+
def symbolize_keys(h)
|
80
|
+
h.each_with_object({}) do |(k, v), acc|
|
81
|
+
new_k = if k.respond_to?(:to_sym)
|
82
|
+
k.to_sym
|
83
|
+
else
|
84
|
+
k
|
85
|
+
end
|
86
|
+
acc[new_k] = v
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
78
90
|
# Deeply capitalizes Array values or Hash keys
|
79
91
|
def camelize_keys(item)
|
80
92
|
case item
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module PulseMeter
|
2
|
+
module Sensor
|
3
|
+
class Configuration
|
4
|
+
include PulseMeter::Mixins::Utils
|
5
|
+
|
6
|
+
def initialize(opts = {})
|
7
|
+
@sensors = {}
|
8
|
+
opts.each do |name, opts|
|
9
|
+
add_sensor(name, opts)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_sensor(name, opts)
|
14
|
+
type = opts.respond_to?(:type) ? opts.type : opts[:type]
|
15
|
+
klass_s = sensor_class(type)
|
16
|
+
klass = constantize(klass_s)
|
17
|
+
raise ArgumentError, "#{klass_s} is not a valid class for a sensor" unless klass
|
18
|
+
args = (opts.respond_to?(:args) ? opts.args : opts[:args]) || {}
|
19
|
+
@sensors[name.to_s] = klass.new(name, symbolize_keys(args.to_hash))
|
20
|
+
end
|
21
|
+
|
22
|
+
def sensor(name)
|
23
|
+
@sensors[name.to_s]
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_missing(name, *args)
|
27
|
+
name = name.to_s
|
28
|
+
if @sensors.has_key?(name)
|
29
|
+
@sensors[name].event(*args)
|
30
|
+
else
|
31
|
+
raise ArgumentError, "Unknown sensor: `#{name}'"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def sensor_class(type)
|
38
|
+
entries = type.to_s.split('/').map do |entry|
|
39
|
+
entry.split('_').map(&:capitalize).join
|
40
|
+
end
|
41
|
+
entries.unshift('PulseMeter::Sensor').join('::')
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/pulse-meter/version.rb
CHANGED
data/pulse-meter.gemspec
CHANGED
@@ -28,6 +28,7 @@ Gem::Specification.new do |gem|
|
|
28
28
|
gem.add_runtime_dependency('thor')
|
29
29
|
|
30
30
|
gem.add_development_dependency('foreman')
|
31
|
+
gem.add_development_dependency('hashie')
|
31
32
|
gem.add_development_dependency('mock_redis')
|
32
33
|
gem.add_development_dependency('rack-test')
|
33
34
|
gem.add_development_dependency('rake')
|
@@ -131,4 +131,11 @@ describe PulseMeter::Mixins::Utils do
|
|
131
131
|
dummy.camelize_keys({ :aa_bb_cc => [ { :dd_ee => 123 }, 456 ] }).should =={ 'aaBbCc' => [ { 'ddEe' => 123 }, 456 ] }
|
132
132
|
end
|
133
133
|
end
|
134
|
+
|
135
|
+
describe "#symbolize_keys" do
|
136
|
+
it "should convert symbolizable keys to symbols" do
|
137
|
+
dummy.symbolize_keys({"a" => 5, 6 => 7}).should == {a: 5, 6 => 7}
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
134
141
|
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe PulseMeter::Sensor::Configuration do
|
4
|
+
describe "#add_sensor" do
|
5
|
+
let(:cfg) {described_class.new}
|
6
|
+
|
7
|
+
it "should create sensor available under passed name" do
|
8
|
+
cfg.sensor(:foo).should be_nil
|
9
|
+
cfg.add_sensor(:foo, type: 'counter')
|
10
|
+
cfg.sensor(:foo).should_not be_nil
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should event shortcut for the sensor" do
|
14
|
+
cfg.add_sensor(:foo, type: 'counter')
|
15
|
+
sensor = cfg.sensor(:foo)
|
16
|
+
sensor.should_receive(:event).with(321)
|
17
|
+
cfg.foo(321)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should create sensor with correct type" do
|
21
|
+
cfg.add_sensor(:foo, type: 'counter')
|
22
|
+
cfg.sensor(:foo).should be_kind_of(PulseMeter::Sensor::Counter)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should raise exception if sensor type is bad" do
|
26
|
+
expect{ cfg.add_sensor(:foo, type: 'baaaar') }.to raise_exception(ArgumentError)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should pass args to created sensor" do
|
30
|
+
cfg.add_sensor(:foo, type: 'counter', args: {annotation: "My Foo Counter"} )
|
31
|
+
cfg.sensor(:foo).annotation.should == "My Foo Counter"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should accept hashie-objects" do
|
35
|
+
class Dummy
|
36
|
+
def type
|
37
|
+
'counter'
|
38
|
+
end
|
39
|
+
def args
|
40
|
+
Hashie::Mash.new(annotation: "My Foo Counter")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
cfg.add_sensor(:foo, Dummy.new)
|
45
|
+
cfg.sensor(:foo).annotation.should == "My Foo Counter"
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
describe ".new" do
|
52
|
+
it "should add passed sensor setting hash using keys as names" do
|
53
|
+
opts = {
|
54
|
+
cnt: {
|
55
|
+
type: 'counter'
|
56
|
+
},
|
57
|
+
ind: {
|
58
|
+
type: 'indicator'
|
59
|
+
}
|
60
|
+
}
|
61
|
+
cfg1 = described_class.new(opts)
|
62
|
+
cfg2 = described_class.new
|
63
|
+
opts.each{|k,v| cfg2.add_sensor(k, v)}
|
64
|
+
cfg1.to_yaml.should == cfg2.to_yaml
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "#sensor" do
|
69
|
+
it "should give access to added sensors" do
|
70
|
+
opts = {
|
71
|
+
cnt: {
|
72
|
+
type: 'counter',
|
73
|
+
args: {
|
74
|
+
annotation: "MySensor"
|
75
|
+
}
|
76
|
+
},
|
77
|
+
}
|
78
|
+
cfg = described_class.new(opts)
|
79
|
+
cfg.sensor(:cnt).annotation.should == "MySensor"
|
80
|
+
cfg.sensor("cnt").annotation.should == "MySensor"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pulse-meter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-06-
|
13
|
+
date: 2012-06-05 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: gon-sinatra
|
@@ -140,6 +140,22 @@ dependencies:
|
|
140
140
|
- - ! '>='
|
141
141
|
- !ruby/object:Gem::Version
|
142
142
|
version: '0'
|
143
|
+
- !ruby/object:Gem::Dependency
|
144
|
+
name: hashie
|
145
|
+
requirement: !ruby/object:Gem::Requirement
|
146
|
+
none: false
|
147
|
+
requirements:
|
148
|
+
- - ! '>='
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
type: :development
|
152
|
+
prerelease: false
|
153
|
+
version_requirements: !ruby/object:Gem::Requirement
|
154
|
+
none: false
|
155
|
+
requirements:
|
156
|
+
- - ! '>='
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
143
159
|
- !ruby/object:Gem::Dependency
|
144
160
|
name: mock_redis
|
145
161
|
requirement: !ruby/object:Gem::Requirement
|
@@ -303,6 +319,7 @@ files:
|
|
303
319
|
- lib/pulse-meter/mixins/utils.rb
|
304
320
|
- lib/pulse-meter/sensor.rb
|
305
321
|
- lib/pulse-meter/sensor/base.rb
|
322
|
+
- lib/pulse-meter/sensor/configuration.rb
|
306
323
|
- lib/pulse-meter/sensor/counter.rb
|
307
324
|
- lib/pulse-meter/sensor/hashed_counter.rb
|
308
325
|
- lib/pulse-meter/sensor/indicator.rb
|
@@ -347,6 +364,7 @@ files:
|
|
347
364
|
- spec/pulse_meter/mixins/dumper_spec.rb
|
348
365
|
- spec/pulse_meter/mixins/utils_spec.rb
|
349
366
|
- spec/pulse_meter/sensor/base_spec.rb
|
367
|
+
- spec/pulse_meter/sensor/configuration_spec.rb
|
350
368
|
- spec/pulse_meter/sensor/counter_spec.rb
|
351
369
|
- spec/pulse_meter/sensor/hashed_counter_spec.rb
|
352
370
|
- spec/pulse_meter/sensor/indicator_spec.rb
|
@@ -403,6 +421,7 @@ test_files:
|
|
403
421
|
- spec/pulse_meter/mixins/dumper_spec.rb
|
404
422
|
- spec/pulse_meter/mixins/utils_spec.rb
|
405
423
|
- spec/pulse_meter/sensor/base_spec.rb
|
424
|
+
- spec/pulse_meter/sensor/configuration_spec.rb
|
406
425
|
- spec/pulse_meter/sensor/counter_spec.rb
|
407
426
|
- spec/pulse_meter/sensor/hashed_counter_spec.rb
|
408
427
|
- spec/pulse_meter/sensor/indicator_spec.rb
|