Class: Omnizip::ETA::RateCalculator

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

Overview

Calculates processing rates from sample history.

This class computes various rates (bytes/sec, files/sec) with smoothing over a time window to reduce noise from fluctuations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sample_history:, window_seconds: 10.0) ⇒ RateCalculator

Initialize a new rate calculator

Parameters:

  • sample_history (SampleHistory)

    History of samples

  • window_seconds (Float) (defaults to: 10.0)

    Time window for rate calculation



20
21
22
23
# File 'lib/omnizip/eta/rate_calculator.rb', line 20

def initialize(sample_history:, window_seconds: 10.0)
  @sample_history = sample_history
  @window_seconds = window_seconds
end

Instance Attribute Details

#sample_historyObject (readonly)

Returns the value of attribute sample_history.



14
15
16
# File 'lib/omnizip/eta/rate_calculator.rb', line 14

def sample_history
  @sample_history
end

#window_secondsObject (readonly)

Returns the value of attribute window_seconds.



14
15
16
# File 'lib/omnizip/eta/rate_calculator.rb', line 14

def window_seconds
  @window_seconds
end

Instance Method Details

#bytes_per_secondFloat

Calculate current bytes per second rate

Returns:

  • (Float)

    Bytes per second over recent window



28
29
30
# File 'lib/omnizip/eta/rate_calculator.rb', line 28

def bytes_per_second
  sample_history.recent_rate(window_seconds)
end

#files_per_secondFloat

Calculate current files per second rate

Returns:

  • (Float)

    Files per second over recent window



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/omnizip/eta/rate_calculator.rb', line 42

def files_per_second
  recent = sample_history.recent_samples(window_seconds)
  return 0.0 if recent.size < 2

  first = recent.first
  last = recent.last

  time_diff = last.timestamp - first.timestamp
  return 0.0 if time_diff <= 0

  files_diff = last.files_processed - first.files_processed
  files_diff / time_diff
end

#format_rate(rate = bytes_per_second) ⇒ String

Format bytes per second as human-readable string

Parameters:

  • rate (Float) (defaults to: bytes_per_second)

    Rate in bytes/second

Returns:

  • (String)

    Formatted rate (e.g., "2.5 MB/s")



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/omnizip/eta/rate_calculator.rb', line 73

def format_rate(rate = bytes_per_second)
  return "0 B/s" if rate.zero?

  if rate < 1024
    "#{rate.round(1)} B/s"
  elsif rate < 1024 * 1024
    "#{(rate / 1024.0).round(1)} KB/s"
  elsif rate < 1024 * 1024 * 1024
    "#{(rate / (1024.0 * 1024.0)).round(1)} MB/s"
  else
    "#{(rate / (1024.0 * 1024.0 * 1024.0)).round(1)} GB/s"
  end
end

#instantaneous_rateFloat

Calculate instantaneous rate (last two samples)

Returns:

  • (Float)

    Bytes per second between last two samples



59
60
61
62
63
64
65
66
67
# File 'lib/omnizip/eta/rate_calculator.rb', line 59

def instantaneous_rate
  return 0.0 if sample_history.size < 2

  samples = sample_history.samples
  last = samples[-1]
  previous = samples[-2]

  last.rate_since(previous)
end

#megabytes_per_secondFloat

Calculate current megabytes per second rate

Returns:

  • (Float)

    Megabytes per second



35
36
37
# File 'lib/omnizip/eta/rate_calculator.rb', line 35

def megabytes_per_second
  bytes_per_second / (1024.0 * 1024.0)
end

#stable?(threshold: 0.2) ⇒ Boolean

Check if rate is stable (low variance)

Parameters:

  • threshold (Float) (defaults to: 0.2)

    Max coefficient of variation for stability

Returns:

  • (Boolean)

    true if rate is stable



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/omnizip/eta/rate_calculator.rb', line 91

def stable?(threshold: 0.2)
  return false if sample_history.size < 5

  mean_rate = bytes_per_second
  return true if mean_rate.zero? # No data = stable

  std_dev = sample_history.rate_std_dev
  coefficient_of_variation = std_dev / mean_rate

  coefficient_of_variation < threshold
end