Class: DurableHuggingfaceHub::Utils::Progress
- Inherits:
-
Object
- Object
- DurableHuggingfaceHub::Utils::Progress
- 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.
Instance Attribute Summary collapse
-
#current ⇒ Integer
readonly
Current progress.
-
#start_time ⇒ Time
readonly
Start time.
-
#total ⇒ Integer?
readonly
Total size/count expected.
Instance Method Summary collapse
-
#elapsed ⇒ Float
Calculates elapsed time.
-
#eta ⇒ Float?
Estimates time remaining.
-
#finish ⇒ void
Marks the progress as finished.
-
#finished? ⇒ Boolean
Checks if progress is finished.
-
#initialize(total: nil, callback: nil) {|current, total, percentage| ... } ⇒ Progress
constructor
Creates a new Progress tracker.
-
#percentage ⇒ Float?
Calculates the percentage complete.
-
#reset ⇒ void
Resets the progress tracker to its initial state.
-
#set(value) ⇒ void
Sets the current progress to a specific value.
-
#to_s ⇒ String
Returns a string representation of the progress.
-
#update(amount) ⇒ void
Updates the progress.
Constructor Details
#initialize(total: nil, callback: nil) {|current, total, percentage| ... } ⇒ Progress
Creates a new Progress tracker.
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
#current ⇒ Integer (readonly)
Returns Current progress.
26 27 28 |
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 26 def current @current end |
#start_time ⇒ Time (readonly)
Returns Start time.
29 30 31 |
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 29 def start_time @start_time end |
#total ⇒ Integer? (readonly)
Returns Total size/count expected.
23 24 25 |
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 23 def total @total end |
Instance Method Details
#elapsed ⇒ Float
Calculates elapsed time.
105 106 107 |
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 105 def elapsed Time.now - @start_time end |
#eta ⇒ Float?
Estimates time remaining.
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 |
#finish ⇒ void
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.
89 90 91 |
# File 'lib/durable_huggingface_hub/utils/progress.rb', line 89 def finished? @finished end |
#percentage ⇒ Float?
Calculates the percentage complete.
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 |
#reset ⇒ void
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.
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_s ⇒ String
Returns a string representation of the progress.
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.
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 |