feat: async/await controllers/admin/dashboard

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 647713f3fa
commit 32ea04a872

@ -54,3 +54,5 @@ function getLatestVersion(callback) {
exports.getLatestVersion = getLatestVersion; exports.getLatestVersion = getLatestVersion;
exports.isPrerelease = isPrerelease; exports.isPrerelease = isPrerelease;
require('../promisify')(exports);

@ -1,82 +1,77 @@
'use strict'; 'use strict';
var async = require('async'); const nconf = require('nconf');
var nconf = require('nconf'); const semver = require('semver');
var semver = require('semver'); const winston = require('winston');
var winston = require('winston');
const _ = require('lodash'); const _ = require('lodash');
var versions = require('../../admin/versions'); const versions = require('../../admin/versions');
var db = require('../../database'); const db = require('../../database');
var meta = require('../../meta'); const meta = require('../../meta');
const analytics = require('../../analytics').async; const analytics = require('../../analytics').async;
var plugins = require('../../plugins'); const plugins = require('../../plugins');
var user = require('../../user'); const user = require('../../user');
var utils = require('../../utils'); const utils = require('../../utils');
var dashboardController = module.exports; const dashboardController = module.exports;
dashboardController.get = function (req, res, next) { dashboardController.get = async function (req, res) {
async.waterfall([ const [stats, notices, latestVersion, lastrestart] = await Promise.all([
function (next) { getStats(),
async.parallel({ getNotices(),
stats: getStats, getLatestVersion(),
notices: function (next) { getLastRestart(),
var notices = [ ]);
{ const version = nconf.get('version');
done: !meta.reloadRequired,
doneText: '[[admin/general/dashboard:restart-not-required]]', res.render('admin/general/dashboard', {
notDoneText: '[[admin/general/dashboard:restart-required]]', version: version,
}, lookupFailed: latestVersion === null,
{ latestVersion: latestVersion,
done: plugins.hasListeners('filter:search.query'), upgradeAvailable: latestVersion && semver.gt(latestVersion, version),
doneText: '[[admin/general/dashboard:search-plugin-installed]]', currentPrerelease: versions.isPrerelease.test(version),
notDoneText: '[[admin/general/dashboard:search-plugin-not-installed]]', notices: notices,
tooltip: '[[admin/general/dashboard:search-plugin-tooltip]]', stats: stats,
link: '/admin/extend/plugins', canRestart: !!process.send,
}, lastrestart: lastrestart,
]; });
};
if (global.env !== 'production') {
notices.push({ async function getNotices() {
done: false, const notices = [
notDoneText: '[[admin/general/dashboard:running-in-development]]', {
}); done: !meta.reloadRequired,
} doneText: '[[admin/general/dashboard:restart-not-required]]',
notDoneText: '[[admin/general/dashboard:restart-required]]',
plugins.fireHook('filter:admin.notices', notices, next);
},
latestVersion: function (next) {
versions.getLatestVersion(function (err, result) {
if (err) {
winston.error('[acp] Failed to fetch latest version', err);
}
next(null, err ? null : result);
});
},
lastrestart: function (next) {
getLastRestart(next);
},
}, next);
}, },
function (results) { {
var version = nconf.get('version'); done: plugins.hasListeners('filter:search.query'),
doneText: '[[admin/general/dashboard:search-plugin-installed]]',
res.render('admin/general/dashboard', { notDoneText: '[[admin/general/dashboard:search-plugin-not-installed]]',
version: version, tooltip: '[[admin/general/dashboard:search-plugin-tooltip]]',
lookupFailed: results.latestVersion === null, link: '/admin/extend/plugins',
latestVersion: results.latestVersion,
upgradeAvailable: results.latestVersion && semver.gt(results.latestVersion, version),
currentPrerelease: versions.isPrerelease.test(version),
notices: results.notices,
stats: results.stats,
canRestart: !!process.send,
lastrestart: results.lastrestart,
});
}, },
], next); ];
};
if (global.env !== 'production') {
notices.push({
done: false,
notDoneText: '[[admin/general/dashboard:running-in-development]]',
});
}
return await plugins.fireHook('filter:admin.notices', notices);
}
async function getLatestVersion() {
try {
const result = await versions.getLatestVersion();
return result;
} catch (err) {
winston.error('[acp] Failed to fetch latest version', err);
}
return null;
}
dashboardController.getAnalytics = async (req, res, next) => { dashboardController.getAnalytics = async (req, res, next) => {
// Basic validation // Basic validation
@ -112,82 +107,48 @@ dashboardController.getAnalytics = async (req, res, next) => {
}); });
}; };
function getStats(callback) { async function getStats() {
async.waterfall([ const results = await Promise.all([
function (next) { getStatsForSet('ip:recent', 'uniqueIPCount'),
async.parallel([ getStatsForSet('users:joindate', 'userCount'),
function (next) { getStatsForSet('posts:pid', 'postCount'),
getStatsForSet('ip:recent', 'uniqueIPCount', next); getStatsForSet('topics:tid', 'topicCount'),
}, ]);
function (next) { results[0].name = '[[admin/general/dashboard:unique-visitors]]';
getStatsForSet('users:joindate', 'userCount', next); results[1].name = '[[admin/general/dashboard:users]]';
}, results[2].name = '[[admin/general/dashboard:posts]]';
function (next) { results[3].name = '[[admin/general/dashboard:topics]]';
getStatsForSet('posts:pid', 'postCount', next); return results;
},
function (next) {
getStatsForSet('topics:tid', 'topicCount', next);
},
], next);
},
function (results, next) {
results[0].name = '[[admin/general/dashboard:unique-visitors]]';
results[1].name = '[[admin/general/dashboard:users]]';
results[2].name = '[[admin/general/dashboard:posts]]';
results[3].name = '[[admin/general/dashboard:topics]]';
next(null, results);
},
], callback);
} }
function getStatsForSet(set, field, callback) { async function getStatsForSet(set, field) {
var terms = { const terms = {
day: 86400000, day: 86400000,
week: 604800000, week: 604800000,
month: 2592000000, month: 2592000000,
}; };
var now = Date.now(); const now = Date.now();
async.parallel({ return await utils.promiseParallel({
day: function (next) { day: db.sortedSetCount(set, now - terms.day, '+inf'),
db.sortedSetCount(set, now - terms.day, '+inf', next); week: db.sortedSetCount(set, now - terms.week, '+inf'),
}, month: db.sortedSetCount(set, now - terms.month, '+inf'),
week: function (next) { alltime: getGlobalField(field),
db.sortedSetCount(set, now - terms.week, '+inf', next); });
},
month: function (next) {
db.sortedSetCount(set, now - terms.month, '+inf', next);
},
alltime: function (next) {
getGlobalField(field, next);
},
}, callback);
} }
function getGlobalField(field, callback) { async function getGlobalField(field) {
db.getObjectField('global', field, function (err, count) { const count = await db.getObjectField('global', field);
callback(err, parseInt(count, 10) || 0); return parseInt(count, 10) || 0;
});
} }
function getLastRestart(callback) { async function getLastRestart() {
var lastrestart; const lastrestart = await db.getObject('lastrestart');
async.waterfall([ if (!lastrestart) {
function (next) { return null;
db.getObject('lastrestart', next); }
}, const userData = await user.getUserData(lastrestart.uid);
function (_lastrestart, next) { lastrestart.user = userData;
lastrestart = _lastrestart; lastrestart.timestampISO = utils.toISOString(lastrestart.timestamp);
if (!lastrestart) { return lastrestart;
return callback();
}
user.getUserData(lastrestart.uid, next);
},
function (userData, next) {
lastrestart.user = userData;
lastrestart.timestampISO = utils.toISOString(lastrestart.timestamp);
next(null, lastrestart);
},
], callback);
} }

Loading…
Cancel
Save