Class: Metriksd::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/metriksd/registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @interval = options[:interval] || 60
  @window   = options[:window]   || 10 * @interval

  @ignore_current_timeslice = options.fetch(:ignore_current_timeslice, true)

  @mutex = Mutex.new

  @timeslices = Hash.new do |h,k|
    h[k] = Timeslice.new(k)
  end
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



6
7
8
# File 'lib/metriksd/registry.rb', line 6

def interval
  @interval
end

#windowObject (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

Returns:

  • (Boolean)


21
22
23
# File 'lib/metriksd/registry.rb', line 21

def dirty?
  @timeslices.any? { |_, t| t.dirty? }
end

#dirty_timeslicesObject



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

#trimObject



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