progress bars! omg

v1.18.x
Julian Lam 8 years ago
parent ef2b245a82
commit 42b5cdaae3

@ -157,6 +157,13 @@ Upgrade.process = function (files, skipCount, callback) {
var scriptExport = require(file); var scriptExport = require(file);
var date = new Date(scriptExport.timestamp); var date = new Date(scriptExport.timestamp);
var version = path.dirname(file).split('/').pop(); 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 + '... '); 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... // Do the upgrade...
scriptExport.method(function (err) { scriptExport.method.bind({
progress: progress,
})(function (err) {
if (err) { if (err) {
process.stdout.write('error\n'.red); process.stdout.write('error\n'.red);
return next(err); return next(err);
@ -177,6 +186,12 @@ Upgrade.process = function (files, skipCount, callback) {
// Record success in schemaLog // Record success in schemaLog
db.sortedSetAdd('schemaLog', Date.now(), path.basename(file, '.js')); 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); process.stdout.write('OK\n'.green);
next(); next();
}); });
@ -192,4 +207,18 @@ Upgrade.process = function (files, skipCount, callback) {
], 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; module.exports = Upgrade;

@ -11,6 +11,15 @@ module.exports = {
name: 'Update moderation notes to zset', name: 'Update moderation notes to zset',
timestamp: Date.UTC(2017, 2, 22), timestamp: Date.UTC(2017, 2, 22),
method: function (callback) { method: function (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) { batch.processSortedSet('users:joindate', function (ids, next) {
async.each(ids, function (uid, next) { async.each(ids, function (uid, next) {
db.getObjectField('user:' + uid, 'moderationNote', function (err, moderationNote) { db.getObjectField('user:' + uid, 'moderationNote', function (err, moderationNote) {
@ -22,9 +31,12 @@ module.exports = {
note: moderationNote, note: moderationNote,
timestamp: Date.now(), timestamp: Date.now(),
}; };
progress.incr();
db.sortedSetAdd('uid:' + uid + ':moderation:notes', note.timestamp, JSON.stringify(note), next); db.sortedSetAdd('uid:' + uid + ':moderation:notes', note.timestamp, JSON.stringify(note), next);
}); });
}, next); }, next);
}, callback); }, callback);
});
}, },
}; };

@ -10,17 +10,28 @@ module.exports = {
name: 'New sorted set posts:votes', name: 'New sorted set posts:votes',
timestamp: Date.UTC(2017, 1, 27), timestamp: Date.UTC(2017, 1, 27),
method: function (callback) { method: function (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) { require('../../batch').processSortedSet('posts:pid', function (pids, next) {
async.each(pids, function (pid, next) { async.eachSeries(pids, function (pid, next) {
db.getObjectFields('post:' + pid, ['upvotes', 'downvotes'], function (err, postData) { db.getObjectFields('post:' + pid, ['upvotes', 'downvotes'], function (err, postData) {
if (err || !postData) { if (err || !postData) {
return next(err); return next(err);
} }
var votes = parseInt(postData.upvotes || 0, 10) - parseInt(postData.downvotes || 0, 10); var votes = parseInt(postData.upvotes || 0, 10) - parseInt(postData.downvotes || 0, 10);
progress.incr();
db.sortedSetAdd('posts:votes', votes, pid, next); db.sortedSetAdd('posts:votes', votes, pid, next);
}); });
}, next); }, next);
}, {}, callback); }, {}, callback);
});
}, },
}; };

Loading…
Cancel
Save