mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-30 02:25:55 +01:00
feat: async/await controllers/admin/dashboard
This commit is contained in:
@@ -54,3 +54,5 @@ function getLatestVersion(callback) {
|
|||||||
|
|
||||||
exports.getLatestVersion = getLatestVersion;
|
exports.getLatestVersion = getLatestVersion;
|
||||||
exports.isPrerelease = isPrerelease;
|
exports.isPrerelease = isPrerelease;
|
||||||
|
|
||||||
|
require('../promisify')(exports);
|
||||||
|
|||||||
@@ -1,28 +1,44 @@
|
|||||||
'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');
|
||||||
|
|
||||||
|
res.render('admin/general/dashboard', {
|
||||||
|
version: version,
|
||||||
|
lookupFailed: latestVersion === null,
|
||||||
|
latestVersion: latestVersion,
|
||||||
|
upgradeAvailable: latestVersion && semver.gt(latestVersion, version),
|
||||||
|
currentPrerelease: versions.isPrerelease.test(version),
|
||||||
|
notices: notices,
|
||||||
|
stats: stats,
|
||||||
|
canRestart: !!process.send,
|
||||||
|
lastrestart: lastrestart,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
async function getNotices() {
|
||||||
|
const notices = [
|
||||||
{
|
{
|
||||||
done: !meta.reloadRequired,
|
done: !meta.reloadRequired,
|
||||||
doneText: '[[admin/general/dashboard:restart-not-required]]',
|
doneText: '[[admin/general/dashboard:restart-not-required]]',
|
||||||
@@ -44,39 +60,18 @@ dashboardController.get = function (req, res, next) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
plugins.fireHook('filter:admin.notices', notices, next);
|
return await plugins.fireHook('filter:admin.notices', notices);
|
||||||
},
|
|
||||||
latestVersion: function (next) {
|
|
||||||
versions.getLatestVersion(function (err, result) {
|
|
||||||
if (err) {
|
|
||||||
winston.error('[acp] Failed to fetch latest version', err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
next(null, err ? null : result);
|
async function getLatestVersion() {
|
||||||
});
|
try {
|
||||||
},
|
const result = await versions.getLatestVersion();
|
||||||
lastrestart: function (next) {
|
return result;
|
||||||
getLastRestart(next);
|
} catch (err) {
|
||||||
},
|
winston.error('[acp] Failed to fetch latest version', err);
|
||||||
}, next);
|
}
|
||||||
},
|
return null;
|
||||||
function (results) {
|
}
|
||||||
var version = nconf.get('version');
|
|
||||||
|
|
||||||
res.render('admin/general/dashboard', {
|
|
||||||
version: version,
|
|
||||||
lookupFailed: results.latestVersion === null,
|
|
||||||
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);
|
|
||||||
};
|
|
||||||
|
|
||||||
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) {
|
|
||||||
getStatsForSet('users:joindate', 'userCount', next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
getStatsForSet('posts:pid', 'postCount', next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
getStatsForSet('topics:tid', 'topicCount', next);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
results[0].name = '[[admin/general/dashboard:unique-visitors]]';
|
results[0].name = '[[admin/general/dashboard:unique-visitors]]';
|
||||||
results[1].name = '[[admin/general/dashboard:users]]';
|
results[1].name = '[[admin/general/dashboard:users]]';
|
||||||
results[2].name = '[[admin/general/dashboard:posts]]';
|
results[2].name = '[[admin/general/dashboard:posts]]';
|
||||||
results[3].name = '[[admin/general/dashboard:topics]]';
|
results[3].name = '[[admin/general/dashboard:topics]]';
|
||||||
|
return results;
|
||||||
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) {
|
|
||||||
db.getObjectField('global', field, function (err, count) {
|
|
||||||
callback(err, parseInt(count, 10) || 0);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLastRestart(callback) {
|
async function getGlobalField(field) {
|
||||||
var lastrestart;
|
const count = await db.getObjectField('global', field);
|
||||||
async.waterfall([
|
return parseInt(count, 10) || 0;
|
||||||
function (next) {
|
|
||||||
db.getObject('lastrestart', next);
|
|
||||||
},
|
|
||||||
function (_lastrestart, next) {
|
|
||||||
lastrestart = _lastrestart;
|
|
||||||
if (!lastrestart) {
|
|
||||||
return callback();
|
|
||||||
}
|
}
|
||||||
user.getUserData(lastrestart.uid, next);
|
|
||||||
},
|
async function getLastRestart() {
|
||||||
function (userData, next) {
|
const lastrestart = await db.getObject('lastrestart');
|
||||||
|
if (!lastrestart) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const userData = await user.getUserData(lastrestart.uid);
|
||||||
lastrestart.user = userData;
|
lastrestart.user = userData;
|
||||||
lastrestart.timestampISO = utils.toISOString(lastrestart.timestamp);
|
lastrestart.timestampISO = utils.toISOString(lastrestart.timestamp);
|
||||||
next(null, lastrestart);
|
return lastrestart;
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user