blob: f125abf67e1006c898cb56af3f39c1917d4566f4 [file] [log] [blame]
daniel.teske70445a72018-10-05 09:15:42 +02001class ProgressStats {
2
evansiroky5348a6f2019-01-05 15:39:28 -08003 constructor(trackerName, totalTasks) {
4 this.trackerName = trackerName
5 this.totalTasks = totalTasks
6 this.taskCounter = 0
daniel.teske70445a72018-10-05 09:15:42 +02007 }
8
9 logNext() {
evansiroky5348a6f2019-01-05 15:39:28 -080010 this.taskCounter++
evansiroky3046a3d2019-01-05 21:19:14 -080011 if (!this.beginTime) {
12 this.beginTime = new Date()
13 }
daniel.teske70445a72018-10-05 09:15:42 +020014 }
15
16 /**
evansiroky5348a6f2019-01-05 15:39:28 -080017 * Begin a new task. Print the current progress and then increment the number of tasks.
18 * @param {string} A short message about the current task progress
19 * @param {[boolean]} logTimeLeft whether or not to log the time left.
20 */
21 beginTask (message, logTimeLeft) {
22 this.printStats(message, logTimeLeft)
23 this.logNext()
24 }
25
26 /**
27 * Print the current progress.
28 * @param {string} A short message about the current task progress
29 * @param {[boolean]} logTimeLeft whether or not to log the time left.
30 */
31 printStats (message, logTimeLeft) {
32 message = `${message}; ${this.trackerName} progress: ${this.getPercentage()}% done`
33 if (logTimeLeft) {
34 message = `${message} - ${this.getTimeLeft()} left`
35 }
36 console.log(message)
37 }
38
39 /**
daniel.teske70445a72018-10-05 09:15:42 +020040 * calculates the percentage of finished downloads
41 * @returns {string}
42 */
43 getPercentage() {
evansiroky5348a6f2019-01-05 15:39:28 -080044 var current = (this.taskCounter / this.totalTasks)
daniel.teske70445a72018-10-05 09:15:42 +020045 return Math.round(current * 1000.0) / 10.0
46 }
47
48 /**
49 * calculates the time left and outputs it in human readable format
evansiroky3046a3d2019-01-05 21:19:14 -080050 * calculation is based on the average time per task so far
daniel.teske70445a72018-10-05 09:15:42 +020051 *
52 * @returns {string}
53 */
evansiroky5348a6f2019-01-05 15:39:28 -080054 getTimeLeft () {
evansiroky3046a3d2019-01-05 21:19:14 -080055 if (this.taskCounter === 0) return '?'
56 const averageTimePerTask = (Date.now() - this.beginTime.getTime()) / this.taskCounter
57 var tasksLeft = this.totalTasks - this.taskCounter
58 var millisecondsLeft = averageTimePerTask * tasksLeft
daniel.teske70445a72018-10-05 09:15:42 +020059 return this.formatMilliseconds(millisecondsLeft)
60 }
61
62 /**
63 * inspired from https://stackoverflow.com/questions/19700283/how-to-convert-time-milliseconds-to-hours-min-sec-format-in-javascript
64 * @param millisec
65 * @returns {string}
66 */
67 formatMilliseconds(millisec) {
68 var seconds = (millisec / 1000).toFixed(1);
69 var minutes = (millisec / (1000 * 60)).toFixed(1);
70 var hours = (millisec / (1000 * 60 * 60)).toFixed(1);
71 var days = (millisec / (1000 * 60 * 60 * 24)).toFixed(1);
72 if (seconds < 60) {
73 return seconds + " seconds";
74 } else if (minutes < 60) {
75 return minutes + " minutes";
76 } else if (hours < 24) {
77 return hours + " hours";
78 } else {
79 return days + " days"
80 }
81 }
82
83}
84
evansiroky5348a6f2019-01-05 15:39:28 -080085module.exports = ProgressStats