Class: Omnizip::ETA::ExponentialSmoothingEstimator

Inherits:
TimeEstimator
  • Object
show all
Defined in:
lib/omnizip/eta/exponential_smoothing_estimator.rb

Overview

ETA estimator using exponential smoothing.

This estimator uses exponential smoothing to give more weight to recent samples while still considering historical data. This provides a good balance between responsiveness and stability.

The smoothing factor (alpha) determines how much weight to give to new samples: 0.0 = ignore new data, 1.0 = only use new data.

Instance Attribute Summary collapse

Attributes inherited from TimeEstimator

#rate_calculator, #sample_history

Instance Method Summary collapse

Methods inherited from TimeEstimator

#add_sample, #confidence_interval, #format_time, #sufficient_samples?

Constructor Details

#initialize(smoothing_factor: 0.3, **options) ⇒ ExponentialSmoothingEstimator

Initialize a new exponential smoothing estimator

Parameters:

  • smoothing_factor (Float) (defaults to: 0.3)

    Alpha value (0.0-1.0), default 0.3

  • sample_history (SampleHistory)

    History of samples

  • rate_calculator (RateCalculator)

    Rate calculator



25
26
27
28
29
# File 'lib/omnizip/eta/exponential_smoothing_estimator.rb', line 25

def initialize(smoothing_factor: 0.3, **options)
  super(**options)
  @smoothing_factor = smoothing_factor.clamp(0.0, 1.0)
  @smoothed_rate = nil
end

Instance Attribute Details

#smoothed_rateObject (readonly)

Returns the value of attribute smoothed_rate.



18
19
20
# File 'lib/omnizip/eta/exponential_smoothing_estimator.rb', line 18

def smoothed_rate
  @smoothed_rate
end

#smoothing_factorObject (readonly)

Returns the value of attribute smoothing_factor.



18
19
20
# File 'lib/omnizip/eta/exponential_smoothing_estimator.rb', line 18

def smoothing_factor
  @smoothing_factor
end

Instance Method Details

#estimate(remaining_bytes) ⇒ Models::ETAResult

Estimate time remaining using exponential smoothing

Parameters:

  • remaining_bytes (Integer)

    Bytes remaining to process

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/omnizip/eta/exponential_smoothing_estimator.rb', line 35

def estimate(remaining_bytes)
  return zero_result if remaining_bytes <= 0

  unless sufficient_samples?
    return Models::ETAResult.new.tap do |result|
      result.seconds_remaining = 0.0
      result.formatted = "calculating..."
      result.confidence_lower = 0.0
      result.confidence_upper = 0.0
    end
  end

  # Update smoothed rate
  current_rate = rate_calculator.bytes_per_second

  @smoothed_rate = if @smoothed_rate.nil?
                     current_rate
                   else
                     (smoothing_factor * current_rate) +
                       ((1.0 - smoothing_factor) * @smoothed_rate)
                   end

  # Calculate ETA
  seconds_remaining = if @smoothed_rate.positive?
                        remaining_bytes / @smoothed_rate
                      else
                        Float::INFINITY
                      end

  # Calculate confidence interval
  lower, upper = confidence_interval(seconds_remaining)

  Models::ETAResult.new.tap do |result|
    result.seconds_remaining = seconds_remaining
    result.formatted = format_time(seconds_remaining)
    result.confidence_lower = lower
    result.confidence_upper = upper
  end
end

#resetObject

Reset smoothed rate (e.g., when operation changes significantly)



76
77
78
# File 'lib/omnizip/eta/exponential_smoothing_estimator.rb', line 76

def reset
  @smoothed_rate = nil
end