class Progressor::UnlimitedSequence
Attributes
The current loop index, starts at 1
The time the object was created
Public Class Methods
Creates a new UnlimitedSequence
with the given parameters:
-
min_samples
: The number of samples to collect before attempting to calculate a time per iteration. Default: 1 -
max_samples
: The maximum number of measurements to collect and average. Default: 100. -
formatter: A callable that accepts the sequence object and returns a custom formatted string.
# File lib/progressor/unlimited_sequence.rb, line 24 def initialize(min_samples: 1, max_samples: 100, formatter: nil) @min_samples = min_samples @max_samples = max_samples @formatter = formatter raise Error.new("min_samples needs to be a positive number") if min_samples <= 0 raise Error.new("max_samples needs to be larger than min_samples") if max_samples <= min_samples @start_time = Time.now @current = 0 @measurements = [] @averages = [] end
Public Instance Methods
Is supposed to return an estimation for the Estimated Time of Arrival (time until done).
For an UnlimitedSequence
, this always returns nil.
# File lib/progressor/unlimited_sequence.rb, line 94 def eta # No estimation possible end
Returns an estimation for the time per single iteration. Implemented as an average of averages to provide a smoother gradient from loop to loop.
Returns nil if not enough samples have been collected yet.
# File lib/progressor/unlimited_sequence.rb, line 84 def per_iteration return nil if @measurements.count < min_samples average(@averages) end
Adds a duration in seconds to the internal storage of samples. Updates averages accordingly.
# File lib/progressor/unlimited_sequence.rb, line 41 def push(duration) @current += 1 @measurements << duration # only keep last `max_samples` @measurements.shift if @measurements.count > max_samples @averages << average(@measurements) @averages = @averages.compact # only keep last `max_samples` @averages.shift if @averages.count > max_samples end
“Skips” an iteration, which, in the context of an UnlimitedSequence
is a no-op.
# File lib/progressor/unlimited_sequence.rb, line 55 def skip(_n) # Nothing to do end
Outputs a textual representation of the current state of the UnlimitedSequence
. Shows:
-
the current (1-indexed) number of iterations
-
how long since the start time
-
how long a single iteration takes
A custom `formatter` provided at construction time overrides this default output.
# File lib/progressor/unlimited_sequence.rb, line 69 def to_s return @formatter.call(self).to_s if @formatter [ "#{@current + 1}", "t: #{format_time(Time.now - @start_time)}", "t/i: #{format_time(per_iteration)}", ].join(', ') end
Private Instance Methods
# File lib/progressor/unlimited_sequence.rb, line 100 def average(collection) collection.inject(&:+) / collection.count.to_f end