From 42b5cdaae33cdfa2ddd21d19c75cf0db340a19e8 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 12 Apr 2017 17:30:20 -0400 Subject: [PATCH] progress bars! omg --- src/upgrade.js | 31 +++++++++++++- .../1.5.0/moderation_history_refactor.js | 42 ++++++++++++------- src/upgrades/1.5.0/post_votes_zset.js | 35 ++++++++++------ 3 files changed, 80 insertions(+), 28 deletions(-) diff --git a/src/upgrade.js b/src/upgrade.js index 60b074a953..06e2e7147e 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -157,6 +157,13 @@ Upgrade.process = function (files, skipCount, callback) { var scriptExport = require(file); var date = new Date(scriptExport.timestamp); var version = path.dirname(file).split('/').pop(); + var progress = { + current: 0, + total: 0, + incr: Upgrade.incrementProgress, + script: scriptExport, + date: date, + }; process.stdout.write(' → '.white + String('[' + [date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate()].join('/') + '] ').gray + String(scriptExport.name).reset + '... '); @@ -168,7 +175,9 @@ Upgrade.process = function (files, skipCount, callback) { } // Do the upgrade... - scriptExport.method(function (err) { + scriptExport.method.bind({ + progress: progress, + })(function (err) { if (err) { process.stdout.write('error\n'.red); return next(err); @@ -177,6 +186,12 @@ Upgrade.process = function (files, skipCount, callback) { // Record success in schemaLog db.sortedSetAdd('schemaLog', Date.now(), path.basename(file, '.js')); + if (progress.total > 0) { + process.stdout.clearLine(); + process.stdout.cursorTo(0); + process.stdout.write(' → '.white + String('[' + [date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate()].join('/') + '] ').gray + String(scriptExport.name).reset + '... '); + } + process.stdout.write('OK\n'.green); next(); }); @@ -192,4 +207,18 @@ Upgrade.process = function (files, skipCount, callback) { ], callback); }; +Upgrade.incrementProgress = function () { + this.current += 1; + + // Redraw the progress bar + var percentage = Math.floor((this.current / this.total) * 100) + '%'; + var filled = Math.floor((this.current / this.total) * 15); + var unfilled = 15 - filled; + process.stdout.clearLine(); + process.stdout.cursorTo(0); + + process.stdout.write(' → '.white + String('[' + [this.date.getUTCFullYear(), this.date.getUTCMonth() + 1, this.date.getUTCDate()].join('/') + '] ').gray + String(this.script.name).reset + '... '); + process.stdout.write('[' + (filled ? new Array(filled).join('#') : '') + new Array(unfilled).join(' ') + '] (' + this.current + '/' + this.total + ') ' + percentage); +}; + module.exports = Upgrade; diff --git a/src/upgrades/1.5.0/moderation_history_refactor.js b/src/upgrades/1.5.0/moderation_history_refactor.js index 73b4e8f6d7..b167246e5f 100644 --- a/src/upgrades/1.5.0/moderation_history_refactor.js +++ b/src/upgrades/1.5.0/moderation_history_refactor.js @@ -11,20 +11,32 @@ module.exports = { name: 'Update moderation notes to zset', timestamp: Date.UTC(2017, 2, 22), method: function (callback) { - batch.processSortedSet('users:joindate', function (ids, next) { - async.each(ids, function (uid, next) { - db.getObjectField('user:' + uid, 'moderationNote', function (err, moderationNote) { - if (err || !moderationNote) { - return next(err); - } - var note = { - uid: 1, - note: moderationNote, - timestamp: Date.now(), - }; - db.sortedSetAdd('uid:' + uid + ':moderation:notes', note.timestamp, JSON.stringify(note), next); - }); - }, next); - }, callback); + var progress = this.progress; + + db.sortedSetCard('users:joindate', function (err, numPosts) { + if (err) { + return callback(err); + } + + progress.total = numPosts; + + batch.processSortedSet('users:joindate', function (ids, next) { + async.each(ids, function (uid, next) { + db.getObjectField('user:' + uid, 'moderationNote', function (err, moderationNote) { + if (err || !moderationNote) { + return next(err); + } + var note = { + uid: 1, + note: moderationNote, + timestamp: Date.now(), + }; + + progress.incr(); + db.sortedSetAdd('uid:' + uid + ':moderation:notes', note.timestamp, JSON.stringify(note), next); + }); + }, next); + }, callback); + }); }, }; diff --git a/src/upgrades/1.5.0/post_votes_zset.js b/src/upgrades/1.5.0/post_votes_zset.js index d989602e23..df5a4c522f 100644 --- a/src/upgrades/1.5.0/post_votes_zset.js +++ b/src/upgrades/1.5.0/post_votes_zset.js @@ -10,17 +10,28 @@ module.exports = { name: 'New sorted set posts:votes', timestamp: Date.UTC(2017, 1, 27), method: function (callback) { - require('../../batch').processSortedSet('posts:pid', function (pids, next) { - async.each(pids, function (pid, next) { - db.getObjectFields('post:' + pid, ['upvotes', 'downvotes'], function (err, postData) { - if (err || !postData) { - return next(err); - } - - var votes = parseInt(postData.upvotes || 0, 10) - parseInt(postData.downvotes || 0, 10); - db.sortedSetAdd('posts:votes', votes, pid, next); - }); - }, next); - }, {}, callback); + var progress = this.progress; + + db.sortedSetCard('posts:pid', function (err, numPosts) { + if (err) { + return callback(err); + } + + progress.total = numPosts; + + require('../../batch').processSortedSet('posts:pid', function (pids, next) { + async.eachSeries(pids, function (pid, next) { + db.getObjectFields('post:' + pid, ['upvotes', 'downvotes'], function (err, postData) { + if (err || !postData) { + return next(err); + } + + var votes = parseInt(postData.upvotes || 0, 10) - parseInt(postData.downvotes || 0, 10); + progress.incr(); + db.sortedSetAdd('posts:votes', votes, pid, next); + }); + }, next); + }, {}, callback); + }); }, };