pulse-meter 0.4.9 → 0.4.11

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.
Files changed (192) hide show
  1. data/Rakefile +1 -48
  2. data/lib/pulse-meter.rb +1 -68
  3. data/lib/pulse-meter/visualizer.rb +1 -40
  4. data/pulse-meter.gemspec +4 -26
  5. metadata +10 -558
  6. data/Procfile +0 -3
  7. data/bin/pulse +0 -6
  8. data/examples/basic.ru +0 -145
  9. data/examples/basic_sensor_data.rb +0 -96
  10. data/examples/udp_benchmark.rb +0 -29
  11. data/lib/cmd.rb +0 -171
  12. data/lib/pulse-meter/command_aggregator/async.rb +0 -83
  13. data/lib/pulse-meter/command_aggregator/sync.rb +0 -18
  14. data/lib/pulse-meter/command_aggregator/udp.rb +0 -48
  15. data/lib/pulse-meter/extensions/enumerable.rb +0 -24
  16. data/lib/pulse-meter/mixins/cmd.rb +0 -46
  17. data/lib/pulse-meter/mixins/dumper.rb +0 -87
  18. data/lib/pulse-meter/mixins/utils.rb +0 -155
  19. data/lib/pulse-meter/observer.rb +0 -120
  20. data/lib/pulse-meter/observer/extended.rb +0 -34
  21. data/lib/pulse-meter/sensor.rb +0 -61
  22. data/lib/pulse-meter/sensor/base.rb +0 -88
  23. data/lib/pulse-meter/sensor/configuration.rb +0 -106
  24. data/lib/pulse-meter/sensor/counter.rb +0 -39
  25. data/lib/pulse-meter/sensor/hashed_counter.rb +0 -36
  26. data/lib/pulse-meter/sensor/hashed_indicator.rb +0 -24
  27. data/lib/pulse-meter/sensor/indicator.rb +0 -35
  28. data/lib/pulse-meter/sensor/multi.rb +0 -97
  29. data/lib/pulse-meter/sensor/timeline.rb +0 -236
  30. data/lib/pulse-meter/sensor/timeline_reduce.rb +0 -68
  31. data/lib/pulse-meter/sensor/timelined/average.rb +0 -32
  32. data/lib/pulse-meter/sensor/timelined/counter.rb +0 -23
  33. data/lib/pulse-meter/sensor/timelined/hashed_counter.rb +0 -31
  34. data/lib/pulse-meter/sensor/timelined/hashed_indicator.rb +0 -30
  35. data/lib/pulse-meter/sensor/timelined/indicator.rb +0 -23
  36. data/lib/pulse-meter/sensor/timelined/max.rb +0 -19
  37. data/lib/pulse-meter/sensor/timelined/median.rb +0 -14
  38. data/lib/pulse-meter/sensor/timelined/min.rb +0 -19
  39. data/lib/pulse-meter/sensor/timelined/multi_percentile.rb +0 -34
  40. data/lib/pulse-meter/sensor/timelined/percentile.rb +0 -22
  41. data/lib/pulse-meter/sensor/timelined/uniq_counter.rb +0 -22
  42. data/lib/pulse-meter/sensor/timelined/zset_based.rb +0 -38
  43. data/lib/pulse-meter/sensor/uniq_counter.rb +0 -24
  44. data/lib/pulse-meter/server.rb +0 -0
  45. data/lib/pulse-meter/server/command_line_options.rb +0 -0
  46. data/lib/pulse-meter/server/config_options.rb +0 -0
  47. data/lib/pulse-meter/server/sensors.rb +0 -0
  48. data/lib/pulse-meter/udp_server.rb +0 -45
  49. data/lib/pulse-meter/version.rb +0 -3
  50. data/lib/pulse-meter/visualize/app.rb +0 -78
  51. data/lib/pulse-meter/visualize/base.rb +0 -15
  52. data/lib/pulse-meter/visualize/coffee/application.coffee +0 -40
  53. data/lib/pulse-meter/visualize/coffee/collections/page_info_list.coffee +0 -17
  54. data/lib/pulse-meter/visualize/coffee/collections/sensor_info_list.coffee +0 -4
  55. data/lib/pulse-meter/visualize/coffee/collections/widget_list.coffee +0 -14
  56. data/lib/pulse-meter/visualize/coffee/extensions.coffee +0 -26
  57. data/lib/pulse-meter/visualize/coffee/models/dinamic_widget.coffee +0 -34
  58. data/lib/pulse-meter/visualize/coffee/models/page_info.coffee +0 -2
  59. data/lib/pulse-meter/visualize/coffee/models/sensor_info.coffee +0 -2
  60. data/lib/pulse-meter/visualize/coffee/models/widget.coffee +0 -54
  61. data/lib/pulse-meter/visualize/coffee/presenters/area.coffee +0 -2
  62. data/lib/pulse-meter/visualize/coffee/presenters/gauge.coffee +0 -11
  63. data/lib/pulse-meter/visualize/coffee/presenters/line.coffee +0 -2
  64. data/lib/pulse-meter/visualize/coffee/presenters/pie.coffee +0 -20
  65. data/lib/pulse-meter/visualize/coffee/presenters/series.coffee +0 -44
  66. data/lib/pulse-meter/visualize/coffee/presenters/table.coffee +0 -10
  67. data/lib/pulse-meter/visualize/coffee/presenters/timeline.coffee +0 -13
  68. data/lib/pulse-meter/visualize/coffee/presenters/widget.coffee +0 -65
  69. data/lib/pulse-meter/visualize/coffee/router.coffee +0 -21
  70. data/lib/pulse-meter/visualize/coffee/views/dynamic_chart.coffee +0 -91
  71. data/lib/pulse-meter/visualize/coffee/views/dynamic_widget.coffee +0 -58
  72. data/lib/pulse-meter/visualize/coffee/views/page_title.coffee +0 -17
  73. data/lib/pulse-meter/visualize/coffee/views/page_titles.coffee +0 -15
  74. data/lib/pulse-meter/visualize/coffee/views/sensor_info_list.coffee +0 -19
  75. data/lib/pulse-meter/visualize/coffee/views/widget.coffee +0 -99
  76. data/lib/pulse-meter/visualize/coffee/views/widget_chart.coffee +0 -13
  77. data/lib/pulse-meter/visualize/coffee/views/widget_list.coffee +0 -15
  78. data/lib/pulse-meter/visualize/dsl/base.rb +0 -131
  79. data/lib/pulse-meter/visualize/dsl/errors.rb +0 -40
  80. data/lib/pulse-meter/visualize/dsl/layout.rb +0 -27
  81. data/lib/pulse-meter/visualize/dsl/page.rb +0 -33
  82. data/lib/pulse-meter/visualize/dsl/sensor.rb +0 -20
  83. data/lib/pulse-meter/visualize/dsl/widget.rb +0 -37
  84. data/lib/pulse-meter/visualize/dsl/widgets/area.rb +0 -20
  85. data/lib/pulse-meter/visualize/dsl/widgets/gauge.rb +0 -12
  86. data/lib/pulse-meter/visualize/dsl/widgets/line.rb +0 -21
  87. data/lib/pulse-meter/visualize/dsl/widgets/pie.rb +0 -16
  88. data/lib/pulse-meter/visualize/dsl/widgets/table.rb +0 -19
  89. data/lib/pulse-meter/visualize/layout.rb +0 -79
  90. data/lib/pulse-meter/visualize/page.rb +0 -25
  91. data/lib/pulse-meter/visualize/public/css/application.css +0 -56
  92. data/lib/pulse-meter/visualize/public/css/bootstrap.css +0 -4883
  93. data/lib/pulse-meter/visualize/public/css/bootstrap.min.css +0 -729
  94. data/lib/pulse-meter/visualize/public/css/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  95. data/lib/pulse-meter/visualize/public/css/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  96. data/lib/pulse-meter/visualize/public/css/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  97. data/lib/pulse-meter/visualize/public/css/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  98. data/lib/pulse-meter/visualize/public/css/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  99. data/lib/pulse-meter/visualize/public/css/images/ui-bg_glass_75_ffffff_1x400.png +0 -0
  100. data/lib/pulse-meter/visualize/public/css/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  101. data/lib/pulse-meter/visualize/public/css/images/ui-bg_inset-soft_95_fef1ec_1x100.png +0 -0
  102. data/lib/pulse-meter/visualize/public/css/images/ui-icons_222222_256x240.png +0 -0
  103. data/lib/pulse-meter/visualize/public/css/images/ui-icons_2e83ff_256x240.png +0 -0
  104. data/lib/pulse-meter/visualize/public/css/images/ui-icons_454545_256x240.png +0 -0
  105. data/lib/pulse-meter/visualize/public/css/images/ui-icons_888888_256x240.png +0 -0
  106. data/lib/pulse-meter/visualize/public/css/images/ui-icons_cd0a0a_256x240.png +0 -0
  107. data/lib/pulse-meter/visualize/public/css/images/ui-icons_f6cf3b_256x240.png +0 -0
  108. data/lib/pulse-meter/visualize/public/css/jquery-ui-1.8.16.bootstrap.css +0 -1320
  109. data/lib/pulse-meter/visualize/public/favicon.ico +0 -208
  110. data/lib/pulse-meter/visualize/public/img/glyphicons-halflings-white.png +0 -0
  111. data/lib/pulse-meter/visualize/public/img/glyphicons-halflings.png +0 -0
  112. data/lib/pulse-meter/visualize/public/js/application.js +0 -973
  113. data/lib/pulse-meter/visualize/public/js/backbone-min.js +0 -38
  114. data/lib/pulse-meter/visualize/public/js/bootstrap.js +0 -1835
  115. data/lib/pulse-meter/visualize/public/js/jquery-1.7.2.min.js +0 -4
  116. data/lib/pulse-meter/visualize/public/js/jquery-ui-1.8.16.bootstrap.min.js +0 -791
  117. data/lib/pulse-meter/visualize/public/js/jquery-ui-1.8.23.custom.min.js +0 -21
  118. data/lib/pulse-meter/visualize/public/js/jquery-ui-timepicker-addon.js +0 -1687
  119. data/lib/pulse-meter/visualize/public/js/json2.js +0 -487
  120. data/lib/pulse-meter/visualize/public/js/underscore-min.js +0 -32
  121. data/lib/pulse-meter/visualize/sensor.rb +0 -63
  122. data/lib/pulse-meter/visualize/series_extractor.rb +0 -107
  123. data/lib/pulse-meter/visualize/views/main.haml +0 -30
  124. data/lib/pulse-meter/visualize/views/sensors.haml +0 -76
  125. data/lib/pulse-meter/visualize/views/widgets/area.haml +0 -53
  126. data/lib/pulse-meter/visualize/views/widgets/extend_options.haml +0 -11
  127. data/lib/pulse-meter/visualize/views/widgets/gauge.haml +0 -13
  128. data/lib/pulse-meter/visualize/views/widgets/line.haml +0 -54
  129. data/lib/pulse-meter/visualize/views/widgets/pie.haml +0 -13
  130. data/lib/pulse-meter/visualize/views/widgets/table.haml +0 -45
  131. data/lib/pulse-meter/visualize/widget.rb +0 -38
  132. data/lib/pulse-meter/visualize/widgets/gauge.rb +0 -47
  133. data/lib/pulse-meter/visualize/widgets/pie.rb +0 -36
  134. data/lib/pulse-meter/visualize/widgets/timeline.rb +0 -114
  135. data/spec/pulse_meter/command_aggregator/async_spec.rb +0 -53
  136. data/spec/pulse_meter/command_aggregator/sync_spec.rb +0 -25
  137. data/spec/pulse_meter/command_aggregator/udp_spec.rb +0 -45
  138. data/spec/pulse_meter/extensions/enumerable_spec.rb +0 -58
  139. data/spec/pulse_meter/mixins/cmd_spec.rb +0 -117
  140. data/spec/pulse_meter/mixins/dumper_spec.rb +0 -162
  141. data/spec/pulse_meter/mixins/utils_spec.rb +0 -212
  142. data/spec/pulse_meter/observer/extended_spec.rb +0 -92
  143. data/spec/pulse_meter/observer_spec.rb +0 -207
  144. data/spec/pulse_meter/sensor/base_spec.rb +0 -106
  145. data/spec/pulse_meter/sensor/configuration_spec.rb +0 -103
  146. data/spec/pulse_meter/sensor/counter_spec.rb +0 -54
  147. data/spec/pulse_meter/sensor/hashed_counter_spec.rb +0 -43
  148. data/spec/pulse_meter/sensor/hashed_indicator_spec.rb +0 -39
  149. data/spec/pulse_meter/sensor/indicator_spec.rb +0 -43
  150. data/spec/pulse_meter/sensor/multi_spec.rb +0 -137
  151. data/spec/pulse_meter/sensor/timeline_spec.rb +0 -88
  152. data/spec/pulse_meter/sensor/timelined/average_spec.rb +0 -6
  153. data/spec/pulse_meter/sensor/timelined/counter_spec.rb +0 -6
  154. data/spec/pulse_meter/sensor/timelined/hashed_counter_spec.rb +0 -8
  155. data/spec/pulse_meter/sensor/timelined/hashed_indicator_spec.rb +0 -8
  156. data/spec/pulse_meter/sensor/timelined/indicator_spec.rb +0 -6
  157. data/spec/pulse_meter/sensor/timelined/max_spec.rb +0 -7
  158. data/spec/pulse_meter/sensor/timelined/median_spec.rb +0 -7
  159. data/spec/pulse_meter/sensor/timelined/min_spec.rb +0 -7
  160. data/spec/pulse_meter/sensor/timelined/multi_percentile_spec.rb +0 -21
  161. data/spec/pulse_meter/sensor/timelined/percentile_spec.rb +0 -17
  162. data/spec/pulse_meter/sensor/timelined/uniq_counter_spec.rb +0 -9
  163. data/spec/pulse_meter/sensor/uniq_counter_spec.rb +0 -28
  164. data/spec/pulse_meter/udp_server_spec.rb +0 -36
  165. data/spec/pulse_meter/visualize/app_spec.rb +0 -27
  166. data/spec/pulse_meter/visualize/dsl/layout_spec.rb +0 -64
  167. data/spec/pulse_meter/visualize/dsl/page_spec.rb +0 -62
  168. data/spec/pulse_meter/visualize/dsl/sensor_spec.rb +0 -30
  169. data/spec/pulse_meter/visualize/dsl/widget_spec.rb +0 -6
  170. data/spec/pulse_meter/visualize/dsl/widgets/area_spec.rb +0 -44
  171. data/spec/pulse_meter/visualize/dsl/widgets/gauge_spec.rb +0 -22
  172. data/spec/pulse_meter/visualize/dsl/widgets/line_spec.rb +0 -44
  173. data/spec/pulse_meter/visualize/dsl/widgets/pie_spec.rb +0 -35
  174. data/spec/pulse_meter/visualize/dsl/widgets/table_spec.rb +0 -36
  175. data/spec/pulse_meter/visualize/layout_spec.rb +0 -54
  176. data/spec/pulse_meter/visualize/page_spec.rb +0 -153
  177. data/spec/pulse_meter/visualize/sensor_spec.rb +0 -120
  178. data/spec/pulse_meter/visualize/series_extractor_spec.rb +0 -80
  179. data/spec/pulse_meter/visualize/widgets/area_spec.rb +0 -6
  180. data/spec/pulse_meter/visualize/widgets/gauge_spec.rb +0 -63
  181. data/spec/pulse_meter/visualize/widgets/line_spec.rb +0 -6
  182. data/spec/pulse_meter/visualize/widgets/pie_spec.rb +0 -73
  183. data/spec/pulse_meter/visualize/widgets/table_spec.rb +0 -6
  184. data/spec/pulse_meter/visualizer_spec.rb +0 -42
  185. data/spec/pulse_meter_spec.rb +0 -73
  186. data/spec/shared_examples/dsl_widget.rb +0 -106
  187. data/spec/shared_examples/timeline_sensor.rb +0 -439
  188. data/spec/shared_examples/timelined_subclass.rb +0 -23
  189. data/spec/shared_examples/widget.rb +0 -97
  190. data/spec/spec_helper.rb +0 -40
  191. data/spec/support/matchers.rb +0 -34
  192. data/spec/support/observered.rb +0 -40
@@ -1,120 +0,0 @@
1
- require 'pulse-meter'
2
-
3
- module PulseMeter
4
- class Observer
5
- extend PulseMeter::Mixins::Utils
6
-
7
- class << self
8
- # Removes observation from instance method
9
- # @param klass [Class] class
10
- # @param method [Symbol] instance method name
11
- def unobserve_method(klass, method)
12
- with_observer = method_with_observer(method)
13
- if klass.method_defined?(with_observer)
14
- block = unchain_block(method)
15
- klass.class_eval &block
16
- end
17
- end
18
-
19
- # Removes observation from class method
20
- # @param klass [Class] class
21
- # @param method [Symbol] class method name
22
- def unobserve_class_method(klass, method)
23
- with_observer = method_with_observer(method)
24
- if klass.respond_to?(with_observer)
25
- method_owner = metaclass(klass)
26
- block = unchain_block(method)
27
- method_owner.instance_eval &block
28
- end
29
- end
30
-
31
- # Registeres an observer for instance method
32
- # @param klass [Class] class
33
- # @param method [Symbol] instance method
34
- # @param sensor [Object] notifications receiver
35
- # @param proc [Proc] proc to be called in context of receiver each time observed method called
36
- def observe_method(klass, method, sensor, &proc)
37
- with_observer = method_with_observer(method)
38
- unless klass.method_defined?(with_observer)
39
- block = chain_block(method, sensor, &proc)
40
- klass.class_eval &block
41
- end
42
- end
43
-
44
- # Registeres an observer for class method
45
- # @param klass [Class] class
46
- # @param method [Symbol] class method
47
- # @param sensor [Object] notifications receiver
48
- # @param proc [Proc] proc to be called in context of receiver each time observed method called
49
- def observe_class_method(klass, method, sensor, &proc)
50
- with_observer = method_with_observer(method)
51
- unless klass.respond_to?(with_observer)
52
- method_owner = metaclass(klass)
53
- block = chain_block(method, sensor, &proc)
54
- method_owner.instance_eval &block
55
- end
56
- end
57
-
58
- protected
59
-
60
- def define_instrumented_method(method_owner, method, receiver, &handler)
61
- with_observer = method_with_observer(method)
62
- without_observer = method_without_observer(method)
63
- method_owner.send(:define_method, with_observer) do |*args, &block|
64
- start_time = Time.now
65
- begin
66
- self.send(without_observer, *args, &block)
67
- ensure
68
- begin
69
- delta = ((Time.now - start_time) * 1000).to_i
70
- receiver.instance_exec(delta, *args, &handler)
71
- rescue StandardError
72
- end
73
- end
74
- end
75
- end
76
-
77
- private
78
-
79
- def unchain_block(method)
80
- with_observer = method_with_observer(method)
81
- without_observer = method_without_observer(method)
82
-
83
- Proc.new do
84
- alias_method(method, without_observer)
85
- remove_method(with_observer)
86
- remove_method(without_observer)
87
- end
88
- end
89
-
90
- def chain_block(method, receiver, &handler)
91
- with_observer = method_with_observer(method)
92
- without_observer = method_without_observer(method)
93
- me = self
94
-
95
- Proc.new do
96
- alias_method(without_observer, method)
97
- method_owner = self
98
- me.send(:define_instrumented_method, method_owner, method, receiver, &handler)
99
- alias_method(method, with_observer)
100
- end
101
- end
102
-
103
- def metaclass(klass)
104
- klass.class_eval do
105
- class << self
106
- self
107
- end
108
- end
109
- end
110
-
111
- def method_with_observer(method)
112
- "#{method}_with_#{underscore(self).tr('/', '_')}"
113
- end
114
-
115
- def method_without_observer(method)
116
- "#{method}_without_#{underscore(self).tr('/', '_')}"
117
- end
118
- end
119
- end
120
- end
@@ -1,34 +0,0 @@
1
- require 'pulse-meter'
2
-
3
- module PulseMeter
4
- class Observer::Extended < ::PulseMeter::Observer
5
- class << self
6
- protected
7
-
8
- def define_instrumented_method(method_owner, method, receiver, &handler)
9
- with_observer = method_with_observer(method)
10
- without_observer = method_without_observer(method)
11
- method_owner.send(:define_method, with_observer) do |*args, &block|
12
- start_time = Time.now
13
- begin
14
- result = self.send(without_observer, *args, &block)
15
- ensure
16
- begin
17
- delta = ((Time.now - start_time) * 1000).to_i
18
- observe_parameters = {
19
- self: self,
20
- delta: delta,
21
- result: result,
22
- args: args,
23
- exception: $!
24
- }
25
- receiver.instance_exec(observe_parameters, &handler)
26
- rescue StandardError
27
- end
28
- end
29
- end
30
- end
31
-
32
- end
33
- end
34
- end
@@ -1,61 +0,0 @@
1
- require 'pulse-meter/sensor/base'
2
- require 'pulse-meter/sensor/configuration'
3
- require 'pulse-meter/sensor/counter'
4
- require 'pulse-meter/sensor/indicator'
5
- require 'pulse-meter/sensor/hashed_counter'
6
- require 'pulse-meter/sensor/hashed_indicator'
7
- require 'pulse-meter/sensor/multi'
8
- require 'pulse-meter/sensor/uniq_counter'
9
- require 'pulse-meter/sensor/timeline_reduce'
10
- require 'pulse-meter/sensor/timeline'
11
- require 'pulse-meter/sensor/timelined/average'
12
- require 'pulse-meter/sensor/timelined/counter'
13
- require 'pulse-meter/sensor/timelined/indicator'
14
- require 'pulse-meter/sensor/timelined/hashed_counter'
15
- require 'pulse-meter/sensor/timelined/hashed_indicator'
16
- require 'pulse-meter/sensor/timelined/zset_based'
17
- require 'pulse-meter/sensor/timelined/min'
18
- require 'pulse-meter/sensor/timelined/max'
19
- require 'pulse-meter/sensor/timelined/percentile'
20
- require 'pulse-meter/sensor/timelined/multi_percentile'
21
- require 'pulse-meter/sensor/timelined/median'
22
- require 'pulse-meter/sensor/timelined/uniq_counter'
23
-
24
- # Top level sensor module
25
- module PulseMeter
26
-
27
- # Atomic sensor data
28
- SensorData = Struct.new(:start_time, :value)
29
-
30
- # General sensor exception
31
- class SensorError < StandardError; end
32
-
33
- # Exception to be raised when sensor name is malformed
34
- class BadSensorName < SensorError
35
- def initialize(name, options = {})
36
- super("Bad sensor name: `#{name}', only a-z letters and _ are allowed")
37
- end
38
- end
39
-
40
- # Exception to be raised when Redis is not initialized
41
- class RedisNotInitialized < SensorError
42
- def initialize
43
- super("PulseMeter.redis is not set")
44
- end
45
- end
46
-
47
- # Exception to be raised when sensor cannot be dumped
48
- class DumpError < SensorError; end
49
-
50
- # Exception to be raised on attempts of using the same key for different sensors
51
- class DumpConflictError < DumpError; end
52
-
53
- # Exception to be raised when sensor cannot be restored
54
- class RestoreError < SensorError; end
55
-
56
- module Remote
57
- class MessageTooLarge < PulseMeter::SensorError; end
58
- class ConnectionError < PulseMeter::SensorError; end
59
- end
60
- end
61
-
@@ -1,88 +0,0 @@
1
- module PulseMeter
2
- module Sensor
3
- # @abstract Subclass and override {#event} to implement sensor
4
- class Base
5
- include PulseMeter::Mixins::Dumper
6
-
7
- # @!attribute [rw] redis
8
- # @return [Redis]
9
- attr_accessor :redis
10
- # @!attribute [r] name
11
- # @return [String] sensor name
12
- attr_reader :name
13
-
14
- # Initializes sensor and dumps it to redis
15
- # @param name [String] sensor name
16
- # @option options [String] :annotation Sensor annotation
17
- # @raise [BadSensorName] if sensor name is malformed
18
- # @raise [RedisNotInitialized] unless Redis is initialized
19
- def initialize(name, options={})
20
- @name = name.to_s
21
- if options[:annotation]
22
- annotate(options[:annotation])
23
- end
24
- raise BadSensorName, @name unless @name =~ /\A\w+\z/
25
- raise RedisNotInitialized unless PulseMeter.redis
26
- dump!
27
- end
28
-
29
- # Returns Redis instance
30
- def redis
31
- PulseMeter.redis
32
- end
33
-
34
- def command_aggregator
35
- PulseMeter.command_aggregator
36
- end
37
-
38
- # Saves annotation to Redis
39
- # @param description [String] Sensor annotation
40
- def annotate(description)
41
- redis.set(desc_key, description)
42
- end
43
-
44
- # Retrieves annotation from Redis
45
- # @return [String] Sensor annotation
46
- def annotation
47
- redis.get(desc_key)
48
- end
49
-
50
- # Cleans up all sensor metadata in Redis
51
- def cleanup
52
- redis.del(desc_key)
53
- cleanup_dump
54
- end
55
-
56
- # Processes event
57
- # @param value [Object] value produced by some kind of event
58
- def event(value)
59
- process_event(value)
60
- true
61
- rescue StandardError => e
62
- false
63
- end
64
-
65
- protected
66
-
67
- # Forms Redis key to store annotation
68
- def desc_key
69
- "pulse_meter:desc:#{name}"
70
- end
71
-
72
- # For a block
73
- # @yield Executes it within Redis multi
74
- def multi
75
- redis.multi do
76
- yield
77
- end
78
- end
79
-
80
- private
81
-
82
- def process_event(value)
83
- # do nothing here
84
- end
85
-
86
- end
87
- end
88
- end
@@ -1,106 +0,0 @@
1
- module PulseMeter
2
- module Sensor
3
- # Constructs multiple sensors from configuration passed
4
- class Configuration
5
- include PulseMeter::Mixins::Utils
6
- include Enumerable
7
-
8
- # Initializes sensors
9
- # @param opts [Hash] sensors' configuration
10
- def initialize(opts = {})
11
- @opts = opts
12
- end
13
-
14
- # Returns previously initialized sensor by name
15
- # @param name [Symbol] sensor name
16
- # @yield [sensor] Gives sensor(if it is found) to the block
17
- def sensor(name)
18
- raise ArgumentError, "need a block" unless block_given?
19
- with_resque do
20
- s = sensors[name.to_s]
21
- yield(s) if s
22
- end
23
- end
24
-
25
- # Returns true value if sensor with specified name exists in configuration, false otherwise
26
- # @param name [Symbol] sensor name
27
- def has_sensor?(name)
28
- has_sensor = false
29
- with_resque do
30
- has_sensor = sensors.has_key?(name)
31
- end
32
- has_sensor
33
- end
34
-
35
- # Adds sensor
36
- # @param name [Symbol] sensor name
37
- # @param opts [Hash] sensor options
38
- def add_sensor(name, opts)
39
- with_resque do
40
- sensors[name.to_s] = create_sensor(name, opts)
41
- end
42
- end
43
-
44
- # Iterates over each sensor
45
- def each
46
- with_resque do
47
- sensors.each_value do |s|
48
- yield(s)
49
- end
50
- end
51
- end
52
-
53
- # Invokes event(_at) for any sensor
54
- # @raise [ArgumentError] unless sensor exists
55
- def method_missing(name, *args)
56
- with_resque do
57
- name = name.to_s
58
- if sensors.has_key?(name)
59
- sensors[name].event(*args)
60
- elsif name =~ /\A(.*)_at\z/
61
- sensor_name = $1
62
- sensors[sensor_name].event_at(*args) if sensors.has_key?(sensor_name)
63
- end
64
- end
65
- end
66
-
67
- private
68
-
69
- def with_resque
70
- yield
71
- rescue StandardError => e
72
- PulseMeter.error "Configuration error: #{e}, #{e.backtrace.join("\n")}"
73
- nil
74
- end
75
-
76
-
77
- # Tries to create a specific sensor
78
- # @param name [Symbol] sensor name
79
- # @param opts [Hash] sensor options
80
- def create_sensor(name, opts)
81
- sensor_type = opts.respond_to?(:sensor_type) ? opts.sensor_type : opts[:sensor_type]
82
- klass_s = sensor_class(sensor_type)
83
- klass = constantize(klass_s)
84
- raise ArgumentError, "#{klass_s} is not a valid class for a sensor" unless klass
85
- args = (opts.respond_to?(:args) ? opts.args : opts[:args]) || {}
86
- klass.new(name, symbolize_keys(args.to_hash))
87
- end
88
-
89
- def sensor_class(sensor_type)
90
- entries = sensor_type.to_s.split('/').map do |entry|
91
- entry.split('_').map(&:capitalize).join
92
- end
93
- entries.unshift('PulseMeter::Sensor').join('::')
94
- end
95
-
96
- # Lazy collection of sensors, specified by opts
97
- # @raise [ArgumentError] unless one of the sensors exists
98
- def sensors
99
- @sensors ||= @opts.each_with_object({}){ |(name, opts), sensor_acc|
100
- sensor_acc[name.to_s] = create_sensor(name, opts)
101
- }
102
- end
103
-
104
- end
105
- end
106
- end
@@ -1,39 +0,0 @@
1
- # Static counter
2
- module PulseMeter
3
- module Sensor
4
- class Counter < Base
5
-
6
- # Cleans up all sensor metadata in Redis
7
- def cleanup
8
- redis.del(value_key)
9
- super
10
- end
11
-
12
- # Increments counter value by 1
13
- def incr
14
- event(1)
15
- end
16
-
17
- # Gets counter value
18
- # @return [Fixnum]
19
- def value
20
- redis.get(value_key).to_i
21
- end
22
-
23
- # Gets redis key by which counter value is stored
24
- # @return [String]
25
- def value_key
26
- @value_key ||= "pulse_meter:value:#{name}"
27
- end
28
-
29
- private
30
-
31
- # Processes event by incremnting counter by given value
32
- # @param value [Fixnum] increment
33
- def process_event(value)
34
- command_aggregator.incrby(value_key, value.to_i)
35
- end
36
-
37
- end
38
- end
39
- end