Class: Salus::Accumulator

Inherits:
Metric
  • Object
show all
Defined in:
lib/salus/metric/accumulator.rb

Constant Summary collapse

STORAGE_DEPTH =
1

Instance Method Summary collapse

Methods inherited from Metric

#clear, descendants, #expired?, inherited, #initialize, #load, #mute?, #save, #timestamp, #to_h, #ttl, #value

Methods included from Lockable

#broadcast, #signal, #synchronize, #wait, #wait_until

Methods included from Logging

#log

Constructor Details

This class inherits a constructor from Salus::Metric

Instance Method Details

#push(opts = {}, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/salus/metric/accumulator.rb', line 5

def push(opts={}, &block)
  opts = {} unless opts.is_a?(Hash)

  synchronize do
    opts.each do |k, v|
      validate(k, v)
      @opts[k] = v unless [:value, :ttl, :timestamp].include?(k)
    end

    if block_given?
      v = begin
        yield
      rescue Exception => e
        log DEBUG, e
        nil
      end
      validate(:value, v)
      opts[:value] = v
    end

    prev  = @values.empty? ? 0 : (@values.last.value || 0)
    curr  = opts[:value] || 0
    
    @values << Value.new(prev+curr, opts[:timestamp] || Time.now.to_f, opts[:ttl] || @opts[:ttl])
    @needs_update = true
  end
end