Class: DurableHuggingfaceHub::Utils::Progress

Inherits:
Object
  • Object
show all
Defined in:
lib/durable_huggingface_hub/utils/progress.rb

Overview

Progress tracking for long-running operations.

This module provides a simple progress tracking mechanism for operations like file downloads. It supports custom callbacks for progress updates.

Examples:

Basic progress tracking

progress = Progress.new(total: 1000)
progress.update(100)  # 10% complete
progress.update(500)  # 50% complete
progress.finish

With callback

progress = Progress.new(total: 1000) do |current, total, percentage|
  puts "Progress: #{percentage.round(1)}% (#{current}/#{total})"
end
progress.update(500)  # Calls callback

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total: nil, callback: nil) {|current, total, percentage| ... } ⇒ Progress

Creates a new Progress tracker.

Parameters:

  • total (Integer, nil) (defaults to: nil)

    Total size/count expected

  • callback (Proc, nil) (defaults to: nil)

    Callback to invoke on updates

Yields:

Yield Parameters:

  • current (Integer)

    Current progress

  • total (Integer, nil)

    Total expected

  • percentage (Float)

    Percentage complete (0-100)

Raises:



40
41
42
43
44
45
46
47
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 40

def initialize(total: nil, callback: nil, &block)
  validate_total(total)
  @total = total
  @current = 0
  @start_time = Time.now
  @callback = callback || block
  @finished = false
end

Instance Attribute Details

#currentInteger (readonly)

Returns Current progress.

Returns:

  • (Integer)

    Current progress



26
27
28
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 26

def current
  @current
end

#start_timeTime (readonly)

Returns Start time.

Returns:

  • (Time)

    Start time



29
30
31
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 29

def start_time
  @start_time
end

#totalInteger? (readonly)

Returns Total size/count expected.

Returns:

  • (Integer, nil)

    Total size/count expected



23
24
25
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 23

def total
  @total
end

Instance Method Details

#elapsedFloat

Calculates elapsed time.

Returns:

  • (Float)

    Elapsed seconds



105
106
107
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 105

def elapsed
  Time.now - @start_time
end

#etaFloat?

Estimates time remaining.

Returns:

  • (Float, nil)

    Estimated seconds remaining or nil if unknown



112
113
114
115
116
117
118
119
120
121
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 112

def eta
  return nil unless @total&.positive? && @current.positive?

  elapsed_time = elapsed
  return nil if elapsed_time <= 0

  rate = @current.to_f / elapsed_time
  remaining = @total - @current
  remaining / rate
end

#finishvoid

This method returns an undefined value.

Marks the progress as finished.



78
79
80
81
82
83
84
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 78

def finish
  return if @finished

  @finished = true
  @current = @total if @total
  notify_callback
end

#finished?Boolean

Checks if progress is finished.

Returns:

  • (Boolean)

    True if finished



89
90
91
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 89

def finished?
  @finished
end

#percentageFloat?

Calculates the percentage complete.

Returns:

  • (Float, nil)

    Percentage (0-100) or nil if total unknown



96
97
98
99
100
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 96

def percentage
  return nil unless @total&.positive?

  (@current.to_f / @total * 100).round(2)
end

#resetvoid

This method returns an undefined value.

Resets the progress tracker to its initial state.



126
127
128
129
130
131
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 126

def reset
  @current = 0
  @start_time = Time.now
  @finished = false
  notify_callback
end

#set(value) ⇒ void

This method returns an undefined value.

Sets the current progress to a specific value.

Parameters:

  • value (Integer)

    New current value

Raises:



67
68
69
70
71
72
73
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 67

def set(value)
  return if @finished

  validate_value(value)
  @current = value
  notify_callback
end

#to_sString

Returns a string representation of the progress.

Returns:

  • (String)

    String representation



136
137
138
139
140
141
142
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 136

def to_s
  if @total
    "#{@current}/#{@total} (#{percentage&.round(1)}%)"
  else
    "#{@current} completed"
  end
end

#update(amount) ⇒ void

This method returns an undefined value.

Updates the progress.

Parameters:

  • amount (Integer)

    Amount to add to current progress

Raises:



54
55
56
57
58
59
60
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 54

def update(amount)
  return if @finished

  validate_amount(amount)
  @current += amount
  notify_callback
end