class PowerBar::Rate

Attributes

last_sample_at[R]

Public Class Methods

new(at, window, max_interval=10, interval_step_up=0.1) click to toggle source
Calls superclass method
# File lib/powerbar.rb, line 378
def initialize(at, window, max_interval=10, interval_step_up=0.1)
  super([])
  @last_sample_at = at
  @sample_interval = 0
  @sample_interval_step_up = interval_step_up
  @sample_interval_max = max_interval
  @counter = 0
  @window = window
end

Public Instance Methods

append(at, v) click to toggle source
# File lib/powerbar.rb, line 388
def append(at, v)
  return if @sample_interval > at - @last_sample_at
  @sample_interval += @sample_interval_step_up if @sample_interval < @sample_interval_max

  rate = (v - @counter) / (at - @last_sample_at).to_f
  return if rate.nan?

  @last_sample_at = at
  @counter = v

  self << rate
  shift while @window < length

  self
end
avg() click to toggle source
# File lib/powerbar.rb, line 408
def avg
  sum / size
end
sum() click to toggle source
# File lib/powerbar.rb, line 404
def sum
  inject(:+).to_f
end