diff options
author | Evan Siroky <evan.siroky@yahoo.com> | 2018-12-31 22:21:48 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-31 22:21:48 -0800 |
commit | 854b6ec0a4a20c9caaa346d615ec7e87f0e9ea72 (patch) | |
tree | 88fee5207653fd69c4b9adcfd618f8852cee460d | |
parent | e45d058932e9fb2adb3e437f8cd57dc0e3e278a5 (diff) | |
parent | 70445a7b3676c06d85c5d0b6b9ca6b3cec8e5781 (diff) | |
download | timezone-boundary-builder-854b6ec0a4a20c9caaa346d615ec7e87f0e9ea72.tar.gz |
Merge pull request #47 from dnltsk/master
:sparkles: ProgressStats introduced
-rw-r--r-- | index.js | 6 | ||||
-rw-r--r-- | progressStats.js | 76 |
2 files changed, 81 insertions, 1 deletions
@@ -16,6 +16,9 @@ var osmBoundarySources = require('./osmBoundarySources.json') var zoneCfg = require('./timezones.json') var expectedZoneOverlaps = require('./expectedZoneOverlaps.json') +const ProgressStats = require('./progressStats') +var progressStats = new ProgressStats(Object.keys(osmBoundarySources).length) + // allow building of only a specified zones var filteredIndex = process.argv.indexOf('--filtered-zones') if (filteredIndex > -1 && process.argv[filteredIndex + 1]) { @@ -163,7 +166,8 @@ var downloadOsmBoundary = function (boundaryId, boundaryCallback) { asynclib.auto({ downloadFromOverpass: function (cb) { - console.log('downloading from overpass') + progressStats.logNext() + console.log('downloading from overpass; overall progress ' + progressStats.getPercentage() + '% done - ' + progressStats.getTimeLeft(5) + ' left') fetchIfNeeded(boundaryFilename, boundaryCallback, cb, function () { var overpassResponseHandler = function (err, data) { if (err) { diff --git a/progressStats.js b/progressStats.js new file mode 100644 index 0000000..64b50e1 --- /dev/null +++ b/progressStats.js @@ -0,0 +1,76 @@ +class ProgressStats { + + constructor(totalDownloads) { + this.totalDownloads = totalDownloads + this.downloadCounter = 0 + this.timestamps = [] + } + + logNext() { + this.downloadCounter++ + this.timestamps.push({ + "index": this.downloadCounter, + "timestamp": Date.now() + }) + } + + /** + * calculates the percentage of finished downloads + * @returns {string} + */ + getPercentage() { + var current = (this.downloadCounter / this.totalDownloads) + return Math.round(current * 1000.0) / 10.0 + } + + /** + * calculates the time left and outputs it in human readable format + * calculation is based on a reference length, that can be defined. + * + * @returns {string} + */ + getTimeLeft(referenceLength) { + if(this.downloadCounter <= referenceLength){ + //number of reference downloads must exist before left time can be predicted + return "? minutes" + } + var processDurationInSeconds = (Date.now() - this.timestamps[0].timestamp) / 1000 + if(processDurationInSeconds < 60){ + //process must run longer than 60seconds before left time can be predicted + return "? minutes" + } + + var indexOfStepsBefore = this.timestamps.findIndex((t) => { + return t.index == (this.downloadCounter - referenceLength) + }) + var lastSteps = this.timestamps[indexOfStepsBefore]; + var millisOflastSteps = Date.now() - lastSteps.timestamp + var downloadsLeft = this.totalDownloads - this.downloadCounter + var millisecondsLeft = (millisOflastSteps / referenceLength) * downloadsLeft + 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" + } + } + +} + +module.exports = ProgressStats
\ No newline at end of file |