pulse-meter 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -83,16 +83,18 @@ There are several caveats with timeline sensors:
83
83
 
84
84
  ### Observers
85
85
 
86
- Observer allows to notify a sensor each time some class or instance method is called
86
+ Observer allows to notify sensors each time some class or instance method is called
87
87
  Suppose you have a user model and want to count users distribytion by name. To do this you have to observe class method `create` of User class:
88
88
 
89
- counter = PulseMeter::Sensor::HashedCounter.new :users_by_name
90
- PulseMeter::Observer.observe_class_method(User, :create, counter) do |execution_time, attrs|
91
- event({attrs[:name] => 1})
89
+ sensors = PulseMeter::Sensor::Configuration.new(
90
+ users_by_name: {sensor_type: 'hashed_counter'}
91
+ )
92
+ PulseMeter::Observer.observe_class_method(User, :create, sensors) do |execution_time, attrs|
93
+ users_by_name({attrs[:name] => 1})
92
94
  end
93
95
 
94
- Block recieves all execution time and observed method's argements and is executed in context of sensor passed to observer (this means that event method refers to `counter`).
95
- To observe instance methods use `observe_method`.
96
+ Each time the observed method is called, the block recieves all method's arguments prepended with method's execution time. Block is executed in context of the receiver object passed to observer (this means that `users_by_name` method refers to `sensors`).
97
+ One should use `observe_method` to observe instance methods.
96
98
 
97
99
  `unobserve_class_method` and `unobserve_method` remove observations from class or instace method.
98
100
 
@@ -162,62 +164,73 @@ Just create sensor objects and write data. Some examples below.
162
164
  # 2012-05-24 11:07:00 +0400: 3.0
163
165
  # 2012-05-24 11:08:00 +0400: 7.0
164
166
 
165
- There is also an alternative and a bit more DRY way for sensor creation, management and usage using `PulseMeter::Sensor::Configuration` class. It is also convenient for creating a bunch of sensors from some configuration data.
167
+ There is also an alternative and a bit more DRY way for sensor creation, management and usage using `PulseMeter::Sensor::Configuration` class. It is also convenient for creating a bunch of sensors from some configuration data. Using and creating sensors through `PulseMeter::Sensor::Configuration` also allows to ignore any i/o errors (i.e. redis server unavailability), and this is generally the required case.
166
168
 
167
- require 'pulse-meter'
168
- PulseMeter.redis = Redis.new
169
-
170
- sensors = PulseMeter::Sensor::Configuration.new(
171
- my_counter: {sensor_type: 'counter'},
172
- my_value: {sensor_type: 'indicator'},
173
- my_h_counter: {sensor_type: 'hashed_counter'},
174
- my_t_counter: {
175
- sensor_type: 'timelined/counter',
176
- args: {
177
- interval: 60, # count for each minute
178
- ttl: 24 * 60 * 60 # keep data one day
179
- }
180
- },
181
- my_t_max: {
182
- sensor_type: 'timelined/max',
183
- args: {
184
- interval: 60, # count for each minute
185
- ttl: 24 * 60 * 60 # keep data one day
186
- }
187
- }
188
- )
189
-
190
- sensors.my_counter(1)
191
- sensors.my_counter(2)
192
- puts sensors.sensor(:my_counter).value
193
-
194
- sensors.my_value(3.14)
195
- sensors.my_value(2.71)
196
- puts sensors.sensor(:my_value).value
197
-
198
- sensors.my_h_counter(:x => 1)
199
- sensors.my_h_counter(:y => 5)
200
- sensors.my_h_counter(:y => 1)
201
- p sensors.sensor(:my_h_counter).value
202
-
203
- sensors.my_t_counter(1)
204
- sensors.my_t_counter(1)
205
- sleep(60)
206
- sensors.my_t_counter(1)
207
- sensors.sensor(:my_t_counter).timeline(2 * 60).each do |v|
208
- puts "#{v.start_time}: #{v.value}"
209
- end
210
-
211
- sensors.my_t_max(3)
212
- sensors.my_t_max(1)
213
- sensors.my_t_max(2)
214
- sleep(60)
215
- sensors.my_t_max(5)
216
- sensors.my_t_max(7)
217
- sensors.my_t_max(6)
218
- sensors.sensor(:my_t_max).timeline(2 * 60).each do |v|
219
- puts "#{v.start_time}: #{v.value}"
220
- end
169
+ require 'pulse-meter'
170
+ PulseMeter.redis = Redis.new
171
+
172
+ sensors = PulseMeter::Sensor::Configuration.new(
173
+ my_counter: {sensor_type: 'counter'},
174
+ my_value: {sensor_type: 'indicator'},
175
+ my_h_counter: {sensor_type: 'hashed_counter'},
176
+ my_t_counter: {
177
+ sensor_type: 'timelined/counter',
178
+ args: {
179
+ interval: 60, # count for each minute
180
+ ttl: 24 * 60 * 60 # keep data one day
181
+ }
182
+ },
183
+ my_t_max: {
184
+ sensor_type: 'timelined/max',
185
+ args: {
186
+ interval: 60, # count for each minute
187
+ ttl: 24 * 60 * 60 # keep data one day
188
+ }
189
+ }
190
+ )
191
+
192
+ sensors.my_counter(1)
193
+ sensors.my_counter(2)
194
+ sensors.sensor(:my_counter) do |s|
195
+ puts s.value
196
+ end
197
+
198
+ sensors.my_value(3.14)
199
+ sensors.my_value(2.71)
200
+ sensors.sensor(:my_value) do |s|
201
+ puts s.value
202
+ end
203
+
204
+
205
+ sensors.my_h_counter(:x => 1)
206
+ sensors.my_h_counter(:y => 5)
207
+ sensors.my_h_counter(:y => 1)
208
+ sensors.sensor(:my_h_counter) do |s|
209
+ p s.value
210
+ end
211
+
212
+ sensors.my_t_counter(1)
213
+ sensors.my_t_counter(1)
214
+ sleep(60)
215
+ sensors.my_t_counter(1)
216
+ sensors.sensor(:my_t_counter) do |s|
217
+ s.timeline(2 * 60).each do |v|
218
+ puts "#{v.start_time}: #{v.value}"
219
+ end
220
+ end
221
+
222
+ sensors.my_t_max(3)
223
+ sensors.my_t_max(1)
224
+ sensors.my_t_max(2)
225
+ sleep(60)
226
+ sensors.my_t_max(5)
227
+ sensors.my_t_max(7)
228
+ sensors.my_t_max(6)
229
+ sensors.sensor(:my_t_max) do |s|
230
+ s.timeline(2 * 60).each do |v|
231
+ puts "#{v.start_time}: #{v.value}"
232
+ end
233
+ end
221
234
 
222
235
  ## Command line interface
223
236
 
@@ -3,8 +3,6 @@ $: << File.join(File.absolute_path(__FILE__), '..', 'lib')
3
3
  require 'pulse-meter'
4
4
  PulseMeter.redis = Redis.new
5
5
 
6
- # static sensor examples
7
-
8
6
  sensors = PulseMeter::Sensor::Configuration.new(
9
7
  my_counter: {sensor_type: 'counter'},
10
8
  my_value: {sensor_type: 'indicator'},
@@ -27,23 +25,32 @@ sensors = PulseMeter::Sensor::Configuration.new(
27
25
 
28
26
  sensors.my_counter(1)
29
27
  sensors.my_counter(2)
30
- puts sensors.sensor(:my_counter).value
28
+ sensors.sensor(:my_counter) do |s|
29
+ puts s.value
30
+ end
31
31
 
32
32
  sensors.my_value(3.14)
33
33
  sensors.my_value(2.71)
34
- puts sensors.sensor(:my_value).value
34
+ sensors.sensor(:my_value) do |s|
35
+ puts s.value
36
+ end
37
+
35
38
 
36
39
  sensors.my_h_counter(:x => 1)
37
40
  sensors.my_h_counter(:y => 5)
38
41
  sensors.my_h_counter(:y => 1)
39
- p sensors.sensor(:my_h_counter).value
42
+ sensors.sensor(:my_h_counter) do |s|
43
+ p s.value
44
+ end
40
45
 
41
46
  sensors.my_t_counter(1)
42
47
  sensors.my_t_counter(1)
43
48
  sleep(60)
44
49
  sensors.my_t_counter(1)
45
- sensors.sensor(:my_t_counter).timeline(2 * 60).each do |v|
46
- puts "#{v.start_time}: #{v.value}"
50
+ sensors.sensor(:my_t_counter) do |s|
51
+ s.timeline(2 * 60).each do |v|
52
+ puts "#{v.start_time}: #{v.value}"
53
+ end
47
54
  end
48
55
 
49
56
  sensors.my_t_max(3)
@@ -53,7 +60,9 @@ sleep(60)
53
60
  sensors.my_t_max(5)
54
61
  sensors.my_t_max(7)
55
62
  sensors.my_t_max(6)
56
- sensors.sensor(:my_t_max).timeline(2 * 60).each do |v|
57
- puts "#{v.start_time}: #{v.value}"
63
+ sensors.sensor(:my_t_max) do |s|
64
+ s.timeline(2 * 60).each do |v|
65
+ puts "#{v.start_time}: #{v.value}"
66
+ end
58
67
  end
59
68
 
@@ -1,3 +1,3 @@
1
1
  module PulseMeter
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  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.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: