Class: TTY::ProgressBar::Meter Private
- Inherits:
-
Object
- Object
- TTY::ProgressBar::Meter
- Defined in:
- lib/tty/progressbar/meter.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Used by TTY::ProgressBar to measure progress rate per interval by default 1s
Instance Method Summary collapse
-
#clear ⇒ Object
Reset the meter by clearing out it’s metrics.
-
#initialize(interval) ⇒ Meter
constructor
private
Create Meter.
-
#mean_rate ⇒ Number
(also: #avg_rate)
The mean rate of all the sampled rates.
-
#prune_samples(at) ⇒ Object
private
Remove samples that are obsolete.
-
#rate ⇒ Number
The current rate of sampling for a given interval.
-
#rates ⇒ Object
Group all rates per interval.
-
#sample(at, value) ⇒ Object
Update meter with value.
-
#save_rate(at) ⇒ Object
private
If we crossed a period boundary since @start_time, save the rate for #rates.
-
#start ⇒ Object
Start sampling timer.
Constructor Details
#initialize(interval) ⇒ Meter
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create Meter
16 17 18 19 |
# File 'lib/tty/progressbar/meter.rb', line 16 def initialize(interval) @interval = interval || 1 # 1 sec start end |
Instance Method Details
#clear ⇒ Object
Reset the meter by clearing out it’s metrics
111 112 113 |
# File 'lib/tty/progressbar/meter.rb', line 111 def clear start end |
#mean_rate ⇒ Number Also known as: avg_rate
The mean rate of all the sampled rates
98 99 100 101 102 103 104 105 |
# 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 |
#prune_samples(at) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Remove samples that are obsolete
51 52 53 54 55 56 |
# 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 ⇒ Number
The current rate of sampling for a given interval
75 76 77 78 79 80 81 82 83 |
# 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 ⇒ Object
Group all rates per interval
88 89 90 |
# File 'lib/tty/progressbar/meter.rb', line 88 def rates @rates + [rate] end |
#sample(at, value) ⇒ Object
Update meter with value
41 42 43 44 45 46 |
# 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) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
If we crossed a period boundary since @start_time, save the rate for #rates
62 63 64 65 66 67 |
# 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 ⇒ Object
Start sampling timer
24 25 26 27 28 29 30 |
# File 'lib/tty/progressbar/meter.rb', line 24 def start @start_time = Time.now @current = 0 @samples = [[@start_time, 0]] @rates = [] @start_time end |