Class: PowerBar::Rate

Inherits:
Array
  • Object
show all
Defined in:
lib/powerbar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(at, window, max_interval = 10, interval_step_up = 0.1) ⇒ Rate

Returns a new instance of Rate.



380
381
382
383
384
385
386
387
388
# File 'lib/powerbar.rb', line 380

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

Instance Attribute Details

#last_sample_atObject (readonly)

Returns the value of attribute last_sample_at.



379
380
381
# File 'lib/powerbar.rb', line 379

def last_sample_at
  @last_sample_at
end

Instance Method Details

#append(at, v) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/powerbar.rb', line 390

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

#avgObject



410
411
412
# File 'lib/powerbar.rb', line 410

def avg
  sum / size
end

#sumObject



406
407
408
# File 'lib/powerbar.rb', line 406

def sum
  inject(:+).to_f
end