Class: PowerBar::Rate
- Inherits:
-
Array
- Object
- Array
- PowerBar::Rate
- Defined in:
- lib/powerbar.rb
Instance Attribute Summary collapse
-
#last_sample_at ⇒ Object
readonly
Returns the value of attribute last_sample_at.
Instance Method Summary collapse
- #append(at, v) ⇒ Object
- #avg ⇒ Object
-
#initialize(at, window, max_interval = 10, interval_step_up = 0.1) ⇒ Rate
constructor
A new instance of Rate.
- #sum ⇒ Object
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_at ⇒ Object (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 |
#avg ⇒ Object
410 411 412 |
# File 'lib/powerbar.rb', line 410 def avg sum / size end |
#sum ⇒ Object
406 407 408 |
# File 'lib/powerbar.rb', line 406 def sum inject(:+).to_f end |