Class: FnordMetric::Timeseries

Inherits:
Object
  • Object
show all
Defined in:
lib/fnordmetric/timeseries.rb

Instance Method Summary collapse

Constructor Details

#initialize(timeline = {}) ⇒ Timeseries

Returns a new instance of Timeseries.



3
4
5
6
# File 'lib/fnordmetric/timeseries.rb', line 3

def initialize(timeline = {})
  @timeline = Hash.new{ |h,k| h[k] = [0,nil] }
  @timeline.merge!(timeline)
end

Instance Method Details

#incr_denominator(time, value) ⇒ Object



17
18
19
20
# 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) ⇒ Object



8
9
10
11
# 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) ⇒ Object



13
14
15
# 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)) ⇒ Object



43
44
45
46
# 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

#ticksObject



60
61
62
# File 'lib/fnordmetric/timeseries.rb', line 60

def ticks
  @timeline.keys.sort
end

#timeseries(range, window, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object



72
73
74
75
76
77
# 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)) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



64
65
66
67
68
69
70
# 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