Module: FnordMetric::GaugeCalculations

Included in:
Gauge
Defined in:
lib/fnordmetric/gauge_calculations.rb

Constant Summary collapse

@@avg_per_session_proc =
proc{ |_v, _t|
  #raise redis.get(tick_key(_t, :"sessions-count")).inspect
  (_v.to_f / (redis.get(tick_key(_t, :"sessions-count"))||0).to_i)
}

Instance Method Summary collapse

Instance Method Details

#calculate_value(_v, _t, opts, block) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/fnordmetric/gauge_calculations.rb', line 29

def calculate_value(_v, _t, opts, block)
  block = @@avg_per_session_proc if opts[:avg_per_session]

  if block
    instance_exec(_v, _t, &block)
  else
    _v
  end
end

#redisObject



39
40
41
# File 'lib/fnordmetric/gauge_calculations.rb', line 39

def redis
  @opts[:redis]
end

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



8
9
10
11
12
13
# File 'lib/fnordmetric/gauge_calculations.rb', line 8

def value_at(time, opts={}, &block)
  _t = tick_at(time)
  _v = redis.hget(key, _t)

  calculate_value(_v, _t, opts, block)
end

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



15
16
17
18
19
20
21
22
23
# File 'lib/fnordmetric/gauge_calculations.rb', line 15

def values_at(times, opts={}, &block)
  times = times.map{ |_t| tick_at(_t) }
  Hash.new.tap do |ret|
    redis.hmget(key, *times).each_with_index do |_v, _n|
      _t = times[_n]
      ret[_t] = calculate_value(_v, _t, opts, block)
    end
  end
end

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



25
26
27
# File 'lib/fnordmetric/gauge_calculations.rb', line 25

def values_in(range, opts={}, &block)
  values_at((tick_at(range.first)..range.last).step(tick))
end