mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-06 07:40:43 +01:00
progress bars! omg
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
@@ -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 progress = this.progress;
|
||||
|
||||
var votes = parseInt(postData.upvotes || 0, 10) - parseInt(postData.downvotes || 0, 10);
|
||||
db.sortedSetAdd('posts:votes', votes, pid, next);
|
||||
});
|
||||
}, next);
|
||||
}, {}, callback);
|
||||
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);
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user