Class: Metriksd::Registry
- Inherits:
-
Object
- Object
- Metriksd::Registry
- Defined in:
- lib/metriksd/registry.rb
Instance Attribute Summary collapse
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#window ⇒ Object
readonly
Returns the value of attribute window.
Instance Method Summary collapse
- #dirty? ⇒ Boolean
- #dirty_timeslices ⇒ Object
-
#initialize(options = {}) ⇒ Registry
constructor
A new instance of Registry.
- #push(data) ⇒ Object (also: #<<)
- #rounded_time(time) ⇒ Object
- #trim ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Registry
Returns a new instance of Registry.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/metriksd/registry.rb', line 8 def initialize( = {}) @interval = [:interval] || 60 @window = [:window] || 10 * @interval @ignore_current_timeslice = .fetch(:ignore_current_timeslice, true) @mutex = Mutex.new @timeslices = Hash.new do |h,k| h[k] = Timeslice.new(k) end end |
Instance Attribute Details
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
6 7 8 |
# File 'lib/metriksd/registry.rb', line 6 def interval @interval end |
#window ⇒ Object (readonly)
Returns the value of attribute window.
6 7 8 |
# File 'lib/metriksd/registry.rb', line 6 def window @window end |
Instance Method Details
#dirty? ⇒ Boolean
21 22 23 |
# File 'lib/metriksd/registry.rb', line 21 def dirty? @timeslices.any? { |_, t| t.dirty? } end |
#dirty_timeslices ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/metriksd/registry.rb', line 35 def dirty_timeslices trim @mutex.synchronize do if @ignore_current_timeslice current_time = rounded_time(Time.now) end @timeslices.values.select do |t| t.dirty? && (!current_time || current_time != t.time) end end end |
#push(data) ⇒ Object Also known as: <<
25 26 27 28 29 30 31 32 |
# File 'lib/metriksd/registry.rb', line 25 def push(data) t = rounded_time(data.time) timeslice = nil @mutex.synchronize do @timeslices[t] << data end end |
#rounded_time(time) ⇒ Object
49 50 51 52 |
# File 'lib/metriksd/registry.rb', line 49 def rounded_time(time) time = time.to_i time - (time % @interval) end |
#trim ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/metriksd/registry.rb', line 54 def trim oldest_time = Time.now.to_i - @window @mutex.synchronize do @timeslices.delete_if do |time, _| time < oldest_time end end end |