Refactor progress reporting to cover all build phases
diff --git a/progressStats.js b/progressStats.js
index 64b50e1..a55853f 100644
--- a/progressStats.js
+++ b/progressStats.js
@@ -1,25 +1,50 @@
class ProgressStats {
- constructor(totalDownloads) {
- this.totalDownloads = totalDownloads
- this.downloadCounter = 0
+ constructor(trackerName, totalTasks) {
+ this.trackerName = trackerName
+ this.totalTasks = totalTasks
+ this.taskCounter = 0
+ this.referenceLength = 5
this.timestamps = []
}
logNext() {
- this.downloadCounter++
+ this.taskCounter++
this.timestamps.push({
- "index": this.downloadCounter,
+ "index": this.taskCounter,
"timestamp": Date.now()
})
}
/**
+ * 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()
+ }
+
+ /**
+ * 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)
+ }
+
+ /**
* calculates the percentage of finished downloads
* @returns {string}
*/
getPercentage() {
- var current = (this.downloadCounter / this.totalDownloads)
+ var current = (this.taskCounter / this.totalTasks)
return Math.round(current * 1000.0) / 10.0
}
@@ -29,8 +54,8 @@
*
* @returns {string}
*/
- getTimeLeft(referenceLength) {
- if(this.downloadCounter <= referenceLength){
+ getTimeLeft () {
+ if(this.taskCounter <= this.referenceLength) {
//number of reference downloads must exist before left time can be predicted
return "? minutes"
}
@@ -41,12 +66,12 @@
}
var indexOfStepsBefore = this.timestamps.findIndex((t) => {
- return t.index == (this.downloadCounter - referenceLength)
+ return t.index === (this.taskCounter - this.referenceLength)
})
var lastSteps = this.timestamps[indexOfStepsBefore];
var millisOflastSteps = Date.now() - lastSteps.timestamp
- var downloadsLeft = this.totalDownloads - this.downloadCounter
- var millisecondsLeft = (millisOflastSteps / referenceLength) * downloadsLeft
+ var downloadsLeft = this.totalTasks - this.taskCounter
+ var millisecondsLeft = (millisOflastSteps / this.referenceLength) * downloadsLeft
return this.formatMilliseconds(millisecondsLeft)
}
@@ -73,4 +98,4 @@
}
-module.exports = ProgressStats
\ No newline at end of file
+module.exports = ProgressStats