Class: TTY::ProgressBar::Meter Private

Inherits:
Object
  • Object
show all
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

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

Parameters:

  • interval (Integer)

    the interval for measurement samples



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

#clearObject

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_rateNumber Also known as: avg_rate

The mean rate of all the sampled rates

Returns:

  • (Number)

    the mean rate



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

#rateNumber

The current rate of sampling for a given interval

Returns:

  • (Number)

    the current rate in decimal or 0 if cannot be determined



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

#ratesObject

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

Parameters:

  • at (Time)

    the time of the sampling

  • value (Integer)

    the current value of progress



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

#startObject

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