Class: DownloadProgress

Inherits:
Object
  • Object
show all
Extended by:
Logging
Defined in:
lib/download_progress.rb

Overview

An object of this class will inform about the download-progress and give estimates.

Constant Summary collapse

Show_Seconds =

Minimum of remaining seconds to have their number displayed. See below. With a value of 120, the first possible indication of seconds will be: 1 minute, 59 seconds

120
@@log =
init_logger()

Instance Method Summary collapse

Methods included from Logging

init_logger, log_level=, log_target=

Methods included from File_Checking

#file_check, file_check

Constructor Details

#initialize(file, size = 0, do_start = true) ⇒ DownloadProgress

Sets some reasonable defaults and starts counting, if do_start is true


41
42
43
44
45
46
47
48
# File 'lib/download_progress.rb', line 41

def initialize(file, size = 0, do_start = true)	
	@bi = nil
	@start_time = nil
	@log = @@log
	@file = file
	@size = size
	start if do_start
end

Instance Method Details

#progressObject

retrives or calculates values


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/download_progress.rb', line 67

def progress()
	if(@start_time)
		@current_size = File.size(@file)
		@percent = @current_size.to_f / (@size.to_f / 100)
		@remaining_time = nil
		@dsize = nil
		if(@current_size > 1000)
			now = Time.now
			time = now.to_i - @start_time.to_i
			@dsize = @current_size - @initial_size
			if @dsize > 0
				btime = time.to_f / @dsize 
				total_time = (@size * btime).to_i
				@remaining_time = (@start_time + total_time) - now.to_i
			end
		end
	end
end

#progress_barObject

returns a progress bar, showing the amount of data already downloaded.


87
88
89
90
# File 'lib/download_progress.rb', line 87

def progress_bar
	progress
	"[%-50s] (%i/%i)" %[('#' * ((50.0/100) * @percent)), time_units(@remaining_time, with_secs ), byte_units(@dsize.to_f/time)]
end

#startObject

Starts counting bytes and seconds


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/download_progress.rb', line 51

def start
	if(File.exist?(@file) )
		@start_time = Time.now
		@initial_size = File.size(@file)		# @log.debug('initial size is ' << @initial_size.to_s)

		@bi = BusyIndicator.new(true, 100) {sleep 0.4; status}
	else
		@log.error('The file ' << @file << 'does not exist! Aborting.')
		exit false
	end
end

#statusObject

gives a summary of the download status and an estimate TODO: define fixed-with fields for all data.


94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/download_progress.rb', line 94

def status()
	progress	# puts "progress() calculates : @current_size " << @current_size << ", @remaining_time " <<  @remaining_time

	if(@start_time)
		p = "%s of %s (%.2f%%). " %[byte_units(@current_size, 0), byte_units(@size, 0), @percent] 
		if(@remaining_time)			# show seconds if less than Show_Seconds seconds remain

			with_secs = (@remaining_time < Show_Seconds)
			p << "Time remaining: %s (%s/sec)" %[time_units(@remaining_time, with_secs ), byte_units(@dsize.to_f/time) ]
		end
	end
end

#stopObject


63
64
65
# File 'lib/download_progress.rb', line 63

def stop
	@bi.stop if @bi
end