Class: Classifier::Streaming::Progress

Inherits:
Object
  • Object
show all
Defined in:
lib/classifier/streaming/progress.rb

Overview

Progress tracking object yielded to blocks during batch/stream operations. Provides information about training progress including completion percentage, elapsed time, processing rate, and estimated time remaining.

Examples:

Basic usage with train_batch

classifier.train_batch(:spam, documents, batch_size: 100) do |progress|
  puts "#{progress.completed}/#{progress.total} (#{progress.percent}%)"
  puts "Rate: #{progress.rate.round(1)} docs/sec"
  puts "ETA: #{progress.eta&.round}s" if progress.eta
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total: nil, completed: 0) ⇒ Progress

Returns a new instance of Progress.



25
26
27
28
29
30
# File 'lib/classifier/streaming/progress.rb', line 25

def initialize(total: nil, completed: 0)
  @completed = completed
  @total = total
  @start_time = Time.now
  @current_batch = 0
end

Instance Attribute Details

#completedObject

Returns the value of attribute completed.



22
23
24
# File 'lib/classifier/streaming/progress.rb', line 22

def completed
  @completed
end

#current_batchObject

Returns the value of attribute current_batch.



22
23
24
# File 'lib/classifier/streaming/progress.rb', line 22

def current_batch
  @current_batch
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



21
22
23
# File 'lib/classifier/streaming/progress.rb', line 21

def start_time
  @start_time
end

#totalObject (readonly)

Returns the value of attribute total.



21
22
23
# File 'lib/classifier/streaming/progress.rb', line 21

def total
  @total
end

Instance Method Details

#complete?Boolean

Returns true if the operation is complete.

Returns:

  • (Boolean)


75
76
77
78
79
# File 'lib/classifier/streaming/progress.rb', line 75

def complete?
  return false unless @total

  @completed >= @total
end

#elapsedObject

Returns the elapsed time in seconds since the operation started.



45
46
47
# File 'lib/classifier/streaming/progress.rb', line 45

def elapsed
  Time.now - @start_time
end

#etaObject

Returns the estimated time remaining in seconds. Returns nil if total is unknown or rate is zero.



64
65
66
67
68
69
70
# File 'lib/classifier/streaming/progress.rb', line 64

def eta
  return nil unless @total
  return nil if rate.zero?
  return 0.0 if @completed >= @total

  (@total - @completed) / rate
end

#percentObject

Returns the completion percentage (0-100). Returns nil if total is unknown.



36
37
38
39
40
# File 'lib/classifier/streaming/progress.rb', line 36

def percent
  return nil unless @total&.positive?

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

#rateObject

Returns the processing rate in items per second. Returns 0 if no time has elapsed.



53
54
55
56
57
58
# File 'lib/classifier/streaming/progress.rb', line 53

def rate
  e = elapsed
  return 0.0 if e.zero?

  @completed / e
end

#to_hObject

Returns a hash representation of the progress state.



84
85
86
87
88
89
90
91
92
93
# File 'lib/classifier/streaming/progress.rb', line 84

def to_h
  {
    completed: @completed,
    total: @total,
    percent: percent,
    elapsed: elapsed.round(2),
    rate: rate.round(2),
    eta: eta&.round(2)
  }
end