class TTY::ProgressBar::Meter
Used by {ProgressBar} to measure progress rate per interval by default 1s
@api private
Public Class Methods
new(interval)
click to toggle source
Create Meter
@param [Integer] interval
the interval for measurement samples
@api private
# File lib/tty/progressbar/meter.rb, line 16 def initialize(interval) @interval = interval || 1 # 1 sec start end
Public Instance Methods
clear()
click to toggle source
Reset the meter by clearing out it's metrics
@api public
# File lib/tty/progressbar/meter.rb, line 111 def clear start end
mean_rate()
click to toggle source
The mean rate of all the sampled rates
@return [Number]
the mean rate
@api public
# File lib/tty/progressbar/meter.rb, line 98 def mean_rate last_at, last_value = @samples.last if last_at == @start_time 0 else last_value / (last_at - @start_time) end end
Also aliased as: avg_rate
prune_samples(at)
click to toggle source
Remove samples that are obsolete
@api private
# File lib/tty/progressbar/meter.rb, line 51 def prune_samples(at) cutoff = at - @interval while @samples.size > 1 && (@samples.first.first < cutoff) @samples.shift end end
rate()
click to toggle source
The current rate of sampling for a given interval
@return [Number]
the current rate in decimal or 0 if cannot be determined
@api public
# File lib/tty/progressbar/meter.rb, line 75 def rate first_at, first_value = @samples.first last_at, last_value = @samples.last if first_at == last_at 0 else (last_value - first_value) / (last_at - first_at) end end
rates()
click to toggle source
Group all rates per interval
@api public
# File lib/tty/progressbar/meter.rb, line 88 def rates @rates + [rate] end
sample(at, value)
click to toggle source
Update meter with value
@param [Time] at
the time of the sampling
@param [Integer] value
the current value of progress
@api public
# File lib/tty/progressbar/meter.rb, line 41 def sample(at, value) @current += value prune_samples(at) @samples << [at, @current] save_rate(at) end
save_rate(at)
click to toggle source
If we crossed a period boundary since @start_time, save the rate for {#rates}
@api private
# File lib/tty/progressbar/meter.rb, line 62 def save_rate(at) period_index = ((at - @start_time) / @interval).floor while period_index > @rates.size @rates << rate end end
start()
click to toggle source
Start sampling timer
@api public
# File lib/tty/progressbar/meter.rb, line 24 def start @start_time = Time.now @current = 0 @samples = [[@start_time, 0]] @rates = [] @start_time end