daniel.teske | 70445a7 | 2018-10-05 09:15:42 +0200 | [diff] [blame] | 1 | class ProgressStats { |
| 2 | |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 3 | constructor(trackerName, totalTasks) { |
| 4 | this.trackerName = trackerName |
| 5 | this.totalTasks = totalTasks |
| 6 | this.taskCounter = 0 |
daniel.teske | 70445a7 | 2018-10-05 09:15:42 +0200 | [diff] [blame] | 7 | } |
| 8 | |
| 9 | logNext() { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 10 | this.taskCounter++ |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame^] | 11 | if (!this.beginTime) { |
| 12 | this.beginTime = new Date() |
| 13 | } |
daniel.teske | 70445a7 | 2018-10-05 09:15:42 +0200 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | /** |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 17 | * 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.teske | 70445a7 | 2018-10-05 09:15:42 +0200 | [diff] [blame] | 40 | * calculates the percentage of finished downloads |
| 41 | * @returns {string} |
| 42 | */ |
| 43 | getPercentage() { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 44 | var current = (this.taskCounter / this.totalTasks) |
daniel.teske | 70445a7 | 2018-10-05 09:15:42 +0200 | [diff] [blame] | 45 | return Math.round(current * 1000.0) / 10.0 |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * calculates the time left and outputs it in human readable format |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame^] | 50 | * calculation is based on the average time per task so far |
daniel.teske | 70445a7 | 2018-10-05 09:15:42 +0200 | [diff] [blame] | 51 | * |
| 52 | * @returns {string} |
| 53 | */ |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 54 | getTimeLeft () { |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame^] | 55 | 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.teske | 70445a7 | 2018-10-05 09:15:42 +0200 | [diff] [blame] | 59 | 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 | |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 85 | module.exports = ProgressStats |