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 +74 -61
- data/examples/readme_client_conf.rb +18 -9
- data/lib/pulse-meter/version.rb +1 -1
- metadata +1 -1
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
|
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
|
-
|
90
|
-
|
91
|
-
|
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
|
-
|
95
|
-
|
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
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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)
|
46
|
-
|
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)
|
57
|
-
|
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
|
|
data/lib/pulse-meter/version.rb
CHANGED