Class: Omnizip::ETA::MovingAverageEstimator
- Inherits:
-
TimeEstimator
- Object
- TimeEstimator
- Omnizip::ETA::MovingAverageEstimator
- Defined in:
- lib/omnizip/eta/moving_average_estimator.rb
Overview
ETA estimator using simple moving average.
This estimator calculates the average rate over recent samples and uses that to estimate time remaining. Simpler than exponential smoothing but may be less responsive to changes.
Instance Attribute Summary collapse
-
#window_size ⇒ Object
readonly
Returns the value of attribute window_size.
Attributes inherited from TimeEstimator
#rate_calculator, #sample_history
Instance Method Summary collapse
-
#estimate(remaining_bytes) ⇒ Models::ETAResult
Estimate time remaining using moving average.
-
#initialize(window_size: 10, **options) ⇒ MovingAverageEstimator
constructor
Initialize a new moving average estimator.
Methods inherited from TimeEstimator
#add_sample, #confidence_interval, #format_time, #sufficient_samples?
Constructor Details
#initialize(window_size: 10, **options) ⇒ MovingAverageEstimator
Initialize a new moving average estimator
22 23 24 25 |
# File 'lib/omnizip/eta/moving_average_estimator.rb', line 22 def initialize(window_size: 10, **) super(**) @window_size = window_size end |
Instance Attribute Details
#window_size ⇒ Object (readonly)
Returns the value of attribute window_size.
15 16 17 |
# File 'lib/omnizip/eta/moving_average_estimator.rb', line 15 def window_size @window_size end |
Instance Method Details
#estimate(remaining_bytes) ⇒ Models::ETAResult
Estimate time remaining using moving average
31 32 33 34 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 |
# File 'lib/omnizip/eta/moving_average_estimator.rb', line 31 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 # Get average rate over recent samples avg_rate = calculate_average_rate # Calculate ETA seconds_remaining = if avg_rate.positive? remaining_bytes / avg_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 |