class FnordMetric::Timeseries

Public Class Methods

new(timeline = {}) click to toggle source
# File lib/fnordmetric/timeseries.rb, line 3
def initialize(timeline = {})
  @timeline = Hash.new{ |h,k| h[k] = [0,nil] }
  @timeline.merge!(timeline)
end

Public Instance Methods

incr_denominator(time, value) click to toggle source
# File lib/fnordmetric/timeseries.rb, line 17
def incr_denominator(time, value)
  @timeline[time.to_i][-1] ||= 0
  @timeline[time.to_i][-1] += value
end
incr_fraction(time, numerator, denominator) click to toggle source
# File lib/fnordmetric/timeseries.rb, line 8
def incr_fraction(time, numerator, denominator)
      incr_numerator(time, numerator) if numerator
      incr_denominator(time, denominator) if denominator
end
incr_numerator(time, value) click to toggle source
# File lib/fnordmetric/timeseries.rb, line 13
def incr_numerator(time, value)
      @timeline[time.to_i][0] += value
end
sum(range = (ticks.first..ticks.last)) click to toggle source
# File lib/fnordmetric/timeseries.rb, line 43
def sum(range = (ticks.first..ticks.last))
  @timeline
    .inject(0){ |s,(t,v)| s + (range.include?(t) ? value_at(t) : 0) }
end
ticks() click to toggle source
# File lib/fnordmetric/timeseries.rb, line 60
def ticks
  @timeline.keys.sort
end
timeseries(range, window, &block) click to toggle source
# File lib/fnordmetric/timeseries.rb, line 22
def timeseries(range, window, &block)
  res = Hash.new{ |h,k| h[k] = [0,0] }

  (((range.size)/window.to_f).ceil+1).times.map do |n| 
    res[((range.first+window*(n-1))/window.to_f).floor*window] = [0,0]
  end

  @timeline.each do |time, vals|
    next unless range.include?(time)
    wtime = (time/window.to_f).floor * window
    if block
      res[wtime] = block.call(*vals)
    else
      res[wtime][0] += vals[0]
      res[wtime][1] += vals[1]
    end
  end
  
  FnordMetric::Timeseries.new(res)
end
to_json(&block) click to toggle source
# File lib/fnordmetric/timeseries.rb, line 72
def to_json(&block)
  @timeline.to_a
    .sort{ |a,b| a[0] <=> b[0] }
    .map { |t,v| { :x => t, :y => block.call(*v), :v0 => v[0], :v1 => v[1] } }
    .to_json
end
trend(range = (ticks.first..ticks.last)) click to toggle source
# File lib/fnordmetric/timeseries.rb, line 48
def trend(range = (ticks.first..ticks.last))
  range ||= (ticks.first..ticks.last)    

  rvals = @timeline.to_a
    .select{ |t,v| range.include?(t) }
    .sort{ |a,b| a.first <=> b.first }
    .map{ |t,v| value_at(t) }

  return 0 if rvals.size == 0
  (rvals.last - rvals.first).to_f / rvals.first
end
value_at(time) click to toggle source
# File lib/fnordmetric/timeseries.rb, line 64
def value_at(time)
  if @timeline[time][1].to_i > 0
    @timeline[time][0] / @timeline[time][1].to_f
  else
    @timeline[time][0]
  end    
end