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

API:

  • private

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:

  • the interval for measurement samples

API:

  • private



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

API:

  • public



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:

  • the mean rate

API:

  • public



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

API:

  • private



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:

  • the current rate in decimal or 0 if cannot be determined

API:

  • public



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

API:

  • public



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:

  • the time of the sampling

  • the current value of progress

API:

  • public



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

API:

  • private



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

API:

  • public



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