blob: f9794ed1073a82fa0bd919049a9c4d958d55f9d0 [file] [log] [blame]
daniel.teske70445a72018-10-05 09:15:42 +02001class ProgressStats {
evansiroky98b19932019-03-31 14:29:06 -07002 constructor (trackerName, totalTasks) {
3 this.trackerName = trackerName
4 this.totalTasks = totalTasks
5 this.taskCounter = 0
6 }
daniel.teske70445a72018-10-05 09:15:42 +02007
evansiroky98b19932019-03-31 14:29:06 -07008 logNext () {
9 this.taskCounter++
10 if (!this.beginTime) {
11 this.beginTime = new Date()
daniel.teske70445a72018-10-05 09:15:42 +020012 }
evansiroky98b19932019-03-31 14:29:06 -070013 }
daniel.teske70445a72018-10-05 09:15:42 +020014
evansiroky98b19932019-03-31 14:29:06 -070015 /**
evansiroky5348a6f2019-01-05 15:39:28 -080016 * Begin a new task. Print the current progress and then increment the number of tasks.
17 * @param {string} A short message about the current task progress
18 * @param {[boolean]} logTimeLeft whether or not to log the time left.
19 */
evansiroky98b19932019-03-31 14:29:06 -070020 beginTask (message, logTimeLeft) {
21 this.printStats(message, logTimeLeft)
22 this.logNext()
23 }
evansiroky5348a6f2019-01-05 15:39:28 -080024
evansiroky98b19932019-03-31 14:29:06 -070025 /**
evansiroky5348a6f2019-01-05 15:39:28 -080026 * Print the current progress.
27 * @param {string} A short message about the current task progress
28 * @param {[boolean]} logTimeLeft whether or not to log the time left.
29 */
evansiroky98b19932019-03-31 14:29:06 -070030 printStats (message, logTimeLeft) {
31 message = `${message}; ${this.trackerName} progress: ${this.getPercentage()}% done`
32 if (logTimeLeft) {
33 message = `${message} - ${this.getTimeLeft()} left`
evansiroky5348a6f2019-01-05 15:39:28 -080034 }
evansiroky98b19932019-03-31 14:29:06 -070035 console.log(message)
36 }
evansiroky5348a6f2019-01-05 15:39:28 -080037
evansiroky98b19932019-03-31 14:29:06 -070038 /**
daniel.teske70445a72018-10-05 09:15:42 +020039 * calculates the percentage of finished downloads
40 * @returns {string}
41 */
evansiroky98b19932019-03-31 14:29:06 -070042 getPercentage () {
43 var current = (this.taskCounter / this.totalTasks)
44 return Math.round(current * 1000.0) / 10.0
45 }
daniel.teske70445a72018-10-05 09:15:42 +020046
evansiroky98b19932019-03-31 14:29:06 -070047 /**
daniel.teske70445a72018-10-05 09:15:42 +020048 * calculates the time left and outputs it in human readable format
evansiroky3046a3d2019-01-05 21:19:14 -080049 * calculation is based on the average time per task so far
daniel.teske70445a72018-10-05 09:15:42 +020050 *
51 * @returns {string}
52 */
evansiroky98b19932019-03-31 14:29:06 -070053 getTimeLeft () {
54 if (this.taskCounter === 0) return '?'
55 const averageTimePerTask = (Date.now() - this.beginTime.getTime()) / this.taskCounter
56 var tasksLeft = this.totalTasks - this.taskCounter
57 var millisecondsLeft = averageTimePerTask * tasksLeft
58 return this.formatMilliseconds(millisecondsLeft)
59 }
daniel.teske70445a72018-10-05 09:15:42 +020060
evansiroky98b19932019-03-31 14:29:06 -070061 /**
daniel.teske70445a72018-10-05 09:15:42 +020062 * inspired from https://stackoverflow.com/questions/19700283/how-to-convert-time-milliseconds-to-hours-min-sec-format-in-javascript
63 * @param millisec
64 * @returns {string}
65 */
evansiroky98b19932019-03-31 14:29:06 -070066 formatMilliseconds (millisec) {
67 var seconds = (millisec / 1000).toFixed(1)
68 var minutes = (millisec / (1000 * 60)).toFixed(1)
69 var hours = (millisec / (1000 * 60 * 60)).toFixed(1)
70 var days = (millisec / (1000 * 60 * 60 * 24)).toFixed(1)
71 if (seconds < 60) {
72 return seconds + ' seconds'
73 } else if (minutes < 60) {
74 return minutes + ' minutes'
75 } else if (hours < 24) {
76 return hours + ' hours'
77 } else {
78 return days + ' days'
daniel.teske70445a72018-10-05 09:15:42 +020079 }
evansiroky98b19932019-03-31 14:29:06 -070080 }
daniel.teske70445a72018-10-05 09:15:42 +020081}
82
evansiroky5348a6f2019-01-05 15:39:28 -080083module.exports = ProgressStats