Class: Omnizip::Progress::OperationProgress

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/progress/operation_progress.rb

Overview

Represents the current state of an operation's progress.

This class stores all data about the current progress of an operation, including file counts, byte counts, current file being processed, and calculated percentages.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total_files:, total_bytes:) ⇒ OperationProgress

Initialize a new operation progress tracker

Parameters:

  • total_files (Integer)

    Total number of files to process

  • total_bytes (Integer)

    Total bytes to process



22
23
24
25
26
27
28
29
# File 'lib/omnizip/progress/operation_progress.rb', line 22

def initialize(total_files:, total_bytes:)
  @total_files = total_files
  @total_bytes = total_bytes
  @files_done = 0
  @bytes_done = 0
  @current_file = nil
  @start_time = Time.now
end

Instance Attribute Details

#bytes_doneObject (readonly)

Returns the value of attribute bytes_done.



15
16
17
# File 'lib/omnizip/progress/operation_progress.rb', line 15

def bytes_done
  @bytes_done
end

#current_fileObject (readonly)

Returns the value of attribute current_file.



15
16
17
# File 'lib/omnizip/progress/operation_progress.rb', line 15

def current_file
  @current_file
end

#files_doneObject (readonly)

Returns the value of attribute files_done.



15
16
17
# File 'lib/omnizip/progress/operation_progress.rb', line 15

def files_done
  @files_done
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



15
16
17
# File 'lib/omnizip/progress/operation_progress.rb', line 15

def start_time
  @start_time
end

#total_bytesObject (readonly)

Returns the value of attribute total_bytes.



15
16
17
# File 'lib/omnizip/progress/operation_progress.rb', line 15

def total_bytes
  @total_bytes
end

#total_filesObject (readonly)

Returns the value of attribute total_files.



15
16
17
# File 'lib/omnizip/progress/operation_progress.rb', line 15

def total_files
  @total_files
end

Instance Method Details

#bytes_percentFloat

Calculate percentage of bytes complete

Returns:

  • (Float)

    Percentage of bytes complete (0.0-100.0)



63
64
65
66
67
# File 'lib/omnizip/progress/operation_progress.rb', line 63

def bytes_percent
  return 0.0 if total_bytes.zero?

  (bytes_done.to_f / total_bytes * 100.0).round(1)
end

#complete?Boolean

Check if operation is complete

Returns:

  • (Boolean)

    true if all files and bytes processed



93
94
95
# File 'lib/omnizip/progress/operation_progress.rb', line 93

def complete?
  files_done >= total_files && bytes_done >= total_bytes
end

#elapsed_secondsFloat

Get elapsed time in seconds

Returns:

  • (Float)

    Seconds elapsed since start



72
73
74
# File 'lib/omnizip/progress/operation_progress.rb', line 72

def elapsed_seconds
  Time.now - start_time
end

#files_percentFloat

Calculate percentage of files complete

Returns:

  • (Float)

    Percentage of files complete (0.0-100.0)



54
55
56
57
58
# File 'lib/omnizip/progress/operation_progress.rb', line 54

def files_percent
  return 0.0 if total_files.zero?

  (files_done.to_f / total_files * 100.0).round(1)
end

#percentageFloat

Calculate overall percentage complete

Returns:

  • (Float)

    Percentage complete (0.0-100.0)



45
46
47
48
49
# File 'lib/omnizip/progress/operation_progress.rb', line 45

def percentage
  return 0.0 if total_bytes.zero?

  (bytes_done.to_f / total_bytes * 100.0).round(1)
end

#remaining_bytesInteger

Get remaining bytes to process

Returns:

  • (Integer)

    Bytes remaining



79
80
81
# File 'lib/omnizip/progress/operation_progress.rb', line 79

def remaining_bytes
  total_bytes - bytes_done
end

#remaining_filesInteger

Get remaining files to process

Returns:

  • (Integer)

    Files remaining



86
87
88
# File 'lib/omnizip/progress/operation_progress.rb', line 86

def remaining_files
  total_files - files_done
end

#to_hHash

Get progress as hash for serialization

Returns:

  • (Hash)

    Progress data



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/omnizip/progress/operation_progress.rb', line 100

def to_h
  {
    total_files: total_files,
    total_bytes: total_bytes,
    files_done: files_done,
    bytes_done: bytes_done,
    current_file: current_file,
    percentage: percentage,
    files_percent: files_percent,
    bytes_percent: bytes_percent,
    elapsed_seconds: elapsed_seconds,
    remaining_bytes: remaining_bytes,
    remaining_files: remaining_files,
    complete: complete?,
  }
end

#update(files:, bytes:, current_file: nil) ⇒ Object

Update progress with new values

Parameters:

  • files (Integer)

    Number of files completed

  • bytes (Integer)

    Number of bytes processed

  • current_file (String) (defaults to: nil)

    Name of file currently processing



36
37
38
39
40
# File 'lib/omnizip/progress/operation_progress.rb', line 36

def update(files:, bytes:, current_file: nil)
  @files_done = files
  @bytes_done = bytes
  @current_file = current_file
end