class UploadProgress::Progress

Upload Progress abstracts the progress of an upload. It's used by the multipart progress IO that keeps track of the upload progress and creating the application depends on. It contians methods to update the progress during an upload and read the statistics such as received_bytes, total_bytes, completed_percent, bitrate, and remaining_seconds

You can get the current Progress object by calling upload_progress instance method in your controller or view.

Constants

MAX_SAMPLES

Number of samples used to calculate bitrate

MIN_SAMPLE_TIME

Number of seconds between bitrate samples. Updates that occur more frequently than MIN_SAMPLE_TIME will not be queued until this time passes. This behavior gives a good balance of accuracy and load for both fast and slow transfers.

MIN_STALL_TIME

Number of seconds between updates before giving up to try and calculate bitrate anymore

Attributes

last_update_time[R]

The last time the upload history was updated

message[RW]

A message you can set from your controller or view to be rendered in the upload_status_text helper method. If you set a messagein a controller then call session.update to make that message available to your upload_status action.

received_bytes[R]

Number bytes received from the multipart post

total_bytes[R]

Total number of bytes expected from the mutlipart post

Public Class Methods

new(total) click to toggle source

Create a new Progress object passing the expected number of bytes to receive

# File lib/upload_progress/lib/progress.rb, line 44
def initialize(total)
  @total_bytes = total
  reset!
end

Public Instance Methods

bitrate() click to toggle source

Calculates the bitrate in bytes/second. If the transfer is stalled or just started, the bitrate will be 0

# File lib/upload_progress/lib/progress.rb, line 108
def bitrate
  history_bytes, history_time = @history.transpose.map { |vals| vals.inject { |sum, v| sum + v } } 
  history_bytes / history_time rescue 0
end
completed_percent() click to toggle source

Completed percent in integer form from 0..100

# File lib/upload_progress/lib/progress.rb, line 62
def completed_percent
  (@received_bytes * 100 / @total_bytes).to_i rescue 0
end
elapsed_seconds() click to toggle source

Number of seconds elapsed since the start of the upload

# File lib/upload_progress/lib/progress.rb, line 114
def elapsed_seconds
  @last_update_time
end
finished?() click to toggle source

Returns true if there are bytes pending otherwise returns false

# File lib/upload_progress/lib/progress.rb, line 125
def finished?
  remaining_bytes <= 0
end
remaining_bytes() click to toggle source

Number of bytes left for this upload

# File lib/upload_progress/lib/progress.rb, line 57
def remaining_bytes
  @total_bytes - @received_bytes
end
remaining_seconds() click to toggle source

Calculate the seconds remaining based on the current bitrate. Returns O seconds if stalled or if no bytes have been received

# File lib/upload_progress/lib/progress.rb, line 120
def remaining_seconds
  remaining_bytes / bitrate rescue 0
end
reset!() click to toggle source

Resets the received_bytes, last_update_time, message and bitrate, but but maintains the total expected bytes

# File lib/upload_progress/lib/progress.rb, line 51
def reset!
  @received_bytes, @last_update_time, @stalled, @message = 0, 0, false, ''
  reset_history
end
stalled?() click to toggle source

Returns true if there has been a delay in receiving bytes. The delay is set by the constant MIN_STALL_TIME

# File lib/upload_progress/lib/progress.rb, line 136
def stalled?
  @stalled
end
started?() click to toggle source

Returns true if some bytes have been received

# File lib/upload_progress/lib/progress.rb, line 130
def started?
  @received_bytes > 0
end

Private Instance Methods

reset_history() click to toggle source
# File lib/upload_progress/lib/progress.rb, line 141
def reset_history
  @history = [[0,0]]
end