diff options
author | evansiroky <evan.siroky@yahoo.com> | 2019-03-31 14:29:06 -0700 |
---|---|---|
committer | evansiroky <evan.siroky@yahoo.com> | 2019-03-31 14:29:06 -0700 |
commit | 98b199387082ff01cf581a2188aedac1af479690 (patch) | |
tree | 7696126a3bb30b7345b793a066a34b27227187e5 /progressStats.js | |
parent | b1bfaf5789d991ed733a67216f7dee4536f14c13 (diff) | |
download | timezone-boundary-builder-98b199387082ff01cf581a2188aedac1af479690.tar.gz |
Add standard linter
Diffstat (limited to 'progressStats.js')
-rw-r--r-- | progressStats.js | 102 |
1 files changed, 50 insertions, 52 deletions
diff --git a/progressStats.js b/progressStats.js index f125abf..f9794ed 100644 --- a/progressStats.js +++ b/progressStats.js @@ -1,85 +1,83 @@ class ProgressStats { + constructor (trackerName, totalTasks) { + this.trackerName = trackerName + this.totalTasks = totalTasks + this.taskCounter = 0 + } - constructor(trackerName, totalTasks) { - this.trackerName = trackerName - this.totalTasks = totalTasks - this.taskCounter = 0 + logNext () { + this.taskCounter++ + if (!this.beginTime) { + this.beginTime = new Date() } + } - logNext() { - this.taskCounter++ - if (!this.beginTime) { - this.beginTime = new Date() - } - } - - /** + /** * Begin a new task. Print the current progress and then increment the number of tasks. * @param {string} A short message about the current task progress * @param {[boolean]} logTimeLeft whether or not to log the time left. */ - beginTask (message, logTimeLeft) { - this.printStats(message, logTimeLeft) - this.logNext() - } + beginTask (message, logTimeLeft) { + this.printStats(message, logTimeLeft) + this.logNext() + } - /** + /** * Print the current progress. * @param {string} A short message about the current task progress * @param {[boolean]} logTimeLeft whether or not to log the time left. */ - printStats (message, logTimeLeft) { - message = `${message}; ${this.trackerName} progress: ${this.getPercentage()}% done` - if (logTimeLeft) { - message = `${message} - ${this.getTimeLeft()} left` - } - console.log(message) + printStats (message, logTimeLeft) { + message = `${message}; ${this.trackerName} progress: ${this.getPercentage()}% done` + if (logTimeLeft) { + message = `${message} - ${this.getTimeLeft()} left` } + console.log(message) + } - /** + /** * calculates the percentage of finished downloads * @returns {string} */ - getPercentage() { - var current = (this.taskCounter / this.totalTasks) - return Math.round(current * 1000.0) / 10.0 - } + getPercentage () { + var current = (this.taskCounter / this.totalTasks) + return Math.round(current * 1000.0) / 10.0 + } - /** + /** * calculates the time left and outputs it in human readable format * calculation is based on the average time per task so far * * @returns {string} */ - getTimeLeft () { - if (this.taskCounter === 0) return '?' - const averageTimePerTask = (Date.now() - this.beginTime.getTime()) / this.taskCounter - var tasksLeft = this.totalTasks - this.taskCounter - var millisecondsLeft = averageTimePerTask * tasksLeft - return this.formatMilliseconds(millisecondsLeft) - } + getTimeLeft () { + if (this.taskCounter === 0) return '?' + const averageTimePerTask = (Date.now() - this.beginTime.getTime()) / this.taskCounter + var tasksLeft = this.totalTasks - this.taskCounter + var millisecondsLeft = averageTimePerTask * tasksLeft + return this.formatMilliseconds(millisecondsLeft) + } - /** + /** * inspired from https://stackoverflow.com/questions/19700283/how-to-convert-time-milliseconds-to-hours-min-sec-format-in-javascript * @param millisec * @returns {string} */ - formatMilliseconds(millisec) { - var seconds = (millisec / 1000).toFixed(1); - var minutes = (millisec / (1000 * 60)).toFixed(1); - var hours = (millisec / (1000 * 60 * 60)).toFixed(1); - var days = (millisec / (1000 * 60 * 60 * 24)).toFixed(1); - if (seconds < 60) { - return seconds + " seconds"; - } else if (minutes < 60) { - return minutes + " minutes"; - } else if (hours < 24) { - return hours + " hours"; - } else { - return days + " days" - } + formatMilliseconds (millisec) { + var seconds = (millisec / 1000).toFixed(1) + var minutes = (millisec / (1000 * 60)).toFixed(1) + var hours = (millisec / (1000 * 60 * 60)).toFixed(1) + var days = (millisec / (1000 * 60 * 60 * 24)).toFixed(1) + if (seconds < 60) { + return seconds + ' seconds' + } else if (minutes < 60) { + return minutes + ' minutes' + } else if (hours < 24) { + return hours + ' hours' + } else { + return days + ' days' } - + } } module.exports = ProgressStats |