Skip to content

Commit cac8347

Browse files
committed
-> Better download estimates
1 parent b761333 commit cac8347

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

static/js/downloader.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ require([
188188
queue_byte_size = 0;
189189
bytes_downloaded = 0;
190190
series_count = 0;
191+
start_time = -1;
191192
collections = new Set([]);
192193
cases = new Set([]);
193194
studies = new Set([]);
@@ -212,6 +213,14 @@ require([
212213
return size_display_string(this.bytes_downloaded);
213214
}
214215

216+
get total_duration_estimate() {
217+
if(this.start_time < 0 || this.bytes_downloaded <=0)
218+
return "Calculating..."
219+
let rate = this.bytes_downloaded/((Date.now()-this.start_time)/1000);
220+
let sec_remaining = Math.round((this.queue_byte_size-this.bytes_downloaded)/rate).toFixed(0);
221+
return `~${((sec_remaining-(sec_remaining%60))/60)}m${(sec_remaining%60) > 9 ? ':' : ':0'}${sec_remaining%60}s remaining @ ${size_display_string(rate)}ps`;
222+
}
223+
215224
reset_queue_manager() {
216225
this.queue_byte_size = 0;
217226
this.bytes_downloaded = 0;
@@ -220,12 +229,17 @@ require([
220229
this.cases = new Set([]);
221230
this.studies = new Set([]);
222231
this.cancellation_underway = false;
232+
this.start_time = -1;
223233
}
224234

225235
get active_requests() {
226236
return (this.working_queue.length > 0);
227237
}
228238

239+
set_start_time() {
240+
if(this.start_time < 0) this.start_time = Date.now();
241+
}
242+
229243
cancel() {
230244
this.cancellation_underway = true;
231245
this._emptyQueues();
@@ -263,6 +277,7 @@ require([
263277
}
264278

265279
async get_download_item() {
280+
this.set_start_time();
266281
await this._update_queue();
267282
if(this.working_queue.length > 0 && !this.cancellation_underway) {
268283
return this.working_queue.pop();
@@ -279,6 +294,9 @@ require([
279294
function workerOnMessage(event) {
280295
if(event.data.message === 'update') {
281296
downloader_manager.add_to_done(parseFloat(event.data.size));
297+
let msg = `Download status: ${downloader_manager.in_progress} file(s) downloading${downloader_manager.pending_msg}...`;
298+
let progType = "download";
299+
downloader_manager.progressUpdate(msg, progType);
282300
return;
283301
}
284302
let thisWorker = event.target;
@@ -385,14 +403,16 @@ require([
385403
}
386404
return;
387405
}
388-
let counts = 0;
389406
let read = 0;
407+
let lastReportTime = -1;
408+
let thisChunkTime = -1;
390409
for await(const chunk of response.body) {
391-
if(abort_controller.signal.aborted) break;
410+
if(abort_controller.signal.aborted) break;
392411
outputStream.write(chunk);
393-
counts += 1;
412+
thisChunkTime = Date.now();
394413
read += chunk.length;
395-
if(counts%20 === 0) {
414+
if(lastReportTime < 0 || ((thisChunkTime-lastReportTime) > 2000)) {
415+
lastReportTime = Date.now();
396416
self.postMessage({message: "update", size: read});
397417
read = 0;
398418
}
@@ -430,7 +450,7 @@ require([
430450
}
431451

432452
get overall_progress() {
433-
return `${this.queues.total_bytes_downloaded} / ${this.queues.total_downloads_requested} downloaded `;
453+
return `${this.queues.total_bytes_downloaded} / ${this.queues.total_downloads_requested} downloaded (${this.queues.total_duration_estimate})`;
434454
}
435455

436456
get all_requested() {

0 commit comments

Comments
 (0)