Module: FnordMetric::GaugeCalculations

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

Constant Summary collapse

@@avg_per_session_proc =
proc{ |_v, _t|
  (_v.to_f / (sync_redis.get(tick_key(_t, :"sessions-count"))||1).to_i)
}
@@count_per_session_proc =
proc{ |_v, _t|
  (sync_redis.get(tick_key(_t, :"sessions-count"))||0).to_i
}
@@avg_per_count_proc =
proc{ |_v, _t|
  (_v.to_f / (sync_redis.get(tick_key(_t, :"value-count"))||1).to_i)
}

Instance Method Summary collapse

Instance Method Details

#calculate_value(_v, _t, opts, block, _c = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fnordmetric/gauge_calculations.rb', line 54

def calculate_value(_v, _t, opts, block, _c = nil)
  block = @@avg_per_session_proc if unique? && average?

  calc = if average? && _c
    (_v.to_f / (_c||1).to_i)
  elsif block
    instance_exec(_v, _t, &block)
  else
    _v
  end

  if calc && @opts[:scale_by]
    calc = calc.to_f * @opts[:scale_by].to_f
  end

  calc
end

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fnordmetric/gauge_calculations.rb', line 72

def field_values_at(time, opts={}, &block)
  opts[:max_fields] ||= 50
  field_values = sync_redis.zrevrange(
    tick_key(time, opts[:append]),
    0, opts[:max_fields]-1,
    :withscores => true
  )

  unless field_values.first.is_a?(Array)
    field_values = field_values.in_groups_of(2).map do |key, val|
      [key, Float(val)]
    end
  end

  field_values.map do |key, val|
    [key, calculate_value("%.f" % val, time, opts, block)]
  end
end

#field_values_total(time) ⇒ Object



91
92
93
# File 'lib/fnordmetric/gauge_calculations.rb', line 91

def field_values_total(time)
  (sync_redis.get(tick_key(time, :count))||0).to_i
end

#fraction_values_in(range, _append = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/fnordmetric/gauge_calculations.rb', line 95

def fraction_values_in(range, _append=nil)
  Hash.new{ |h,k| h[k] = [0,0] }.tap do |vals|
    ticks_in(range, retention).each do |_tick|
      sync_redis.hgetall(retention_key(_tick, _append)).each do |k, v|
        kx = k.split("-")
        vals[kx.first.to_i][kx.last == "denominator" ? 1 : 0] += v.to_f
      end
    end
  end
end

#ticks_in(r, _tick = tick, overflow = 0) ⇒ Object



15
16
17
# File 'lib/fnordmetric/gauge_calculations.rb', line 15

def ticks_in(r, _tick=tick, overflow=0)
  (((r.last-r.first)/_tick.to_f).ceil+1+overflow).times.map{ |n| tick_at(r.first + _tick*(n-1), _tick) }
end

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



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fnordmetric/gauge_calculations.rb', line 25

def value_at(time, opts={}, &block)
  _t = tick_at(time)

  _v = if respond_to?(:_value_at)
    _value_at(key, _t)
  else
    _c = sync_redis.hget(key(:"mean-counts"), _t)
    sync_redis.hget(key, _t)
  end

  calculate_value(_v, _t, opts, block, _c)
end

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fnordmetric/gauge_calculations.rb', line 38

def values_at(times, opts={}, &block)
  times = times.map{ |_t| tick_at(_t) }
  Hash.new.tap do |ret|
    if respond_to?(:_values_at)
      _values_at(times, opts={}, &block)
    else
      ret_counts = sync_redis.hmget(key(:"mean-counts"), *times)
      sync_redis.hmget(key, *times)
    end.each_with_index do |_v, _n|
      _t = times[_n]
      _c = ret_counts ? ret_counts[_n] : nil
      ret[_t] = calculate_value(_v, _t, opts, block, _c)
    end
  end
end

#values_in(range) ⇒ Object



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

def values_in(range)
  ticks = ticks_in(range)
  ticks << tick_at(range.last) if ticks.size == 0
  values_at(ticks)
end