aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaniel.teske <daniel.teske@mbition.io>2018-10-05 09:15:42 +0200
committerdaniel.teske <daniel.teske@mbition.io>2018-10-05 09:15:42 +0200
commit70445a7b3676c06d85c5d0b6b9ca6b3cec8e5781 (patch)
tree45203e86fa98d1d4e94bcaecf86cf423ff3d881c
parent188855d82534e6fa05f0faabd712b1b84235b376 (diff)
downloadtimezone-boundary-builder-70445a7b3676c06d85c5d0b6b9ca6b3cec8e5781.tar.gz
:sparkles: ProgressStats introduced
-rw-r--r--index.js6
-rw-r--r--progressStats.js76
2 files changed, 81 insertions, 1 deletions
diff --git a/index.js b/index.js
index 879016d..7564b59 100644
--- a/index.js
+++ b/index.js
@@ -14,6 +14,9 @@ var overpass = require('query-overpass')
var osmBoundarySources = require('./osmBoundarySources.json')
var zoneCfg = require('./timezones.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]) {
@@ -161,7 +164,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