First pass formatting with Prettier
* Added .prettierrc.json * Added .prettierignore * Formatted
This commit is contained in:
192
core/top_x.js
192
core/top_x.js
@@ -2,111 +2,128 @@
|
||||
'use strict';
|
||||
|
||||
// ENiGMA½
|
||||
const { MenuModule } = require('./menu_module.js');
|
||||
const UserProps = require('./user_property.js');
|
||||
const UserLogNames = require('./user_log_name.js');
|
||||
const { Errors } = require('./enig_error.js');
|
||||
const UserDb = require('./database.js').dbs.user;
|
||||
const SysDb = require('./database.js').dbs.system;
|
||||
const User = require('./user.js');
|
||||
const { MenuModule } = require('./menu_module.js');
|
||||
const UserProps = require('./user_property.js');
|
||||
const UserLogNames = require('./user_log_name.js');
|
||||
const { Errors } = require('./enig_error.js');
|
||||
const UserDb = require('./database.js').dbs.user;
|
||||
const SysDb = require('./database.js').dbs.system;
|
||||
const User = require('./user.js');
|
||||
|
||||
// deps
|
||||
const _ = require('lodash');
|
||||
const async = require('async');
|
||||
const _ = require('lodash');
|
||||
const async = require('async');
|
||||
|
||||
exports.moduleInfo = {
|
||||
name : 'TopX',
|
||||
desc : 'Displays users top X stats',
|
||||
author : 'NuSkooler',
|
||||
packageName : 'codes.l33t.enigma.topx',
|
||||
name: 'TopX',
|
||||
desc: 'Displays users top X stats',
|
||||
author: 'NuSkooler',
|
||||
packageName: 'codes.l33t.enigma.topx',
|
||||
};
|
||||
|
||||
const FormIds = {
|
||||
menu : 0,
|
||||
menu: 0,
|
||||
};
|
||||
|
||||
exports.getModule = class TopXModule extends MenuModule {
|
||||
constructor(options) {
|
||||
super(options);
|
||||
this.config = Object.assign({}, _.get(options, 'menuConfig.config'), { extraArgs : options.extraArgs });
|
||||
this.config = Object.assign({}, _.get(options, 'menuConfig.config'), {
|
||||
extraArgs: options.extraArgs,
|
||||
});
|
||||
}
|
||||
|
||||
mciReady(mciData, cb) {
|
||||
super.mciReady(mciData, err => {
|
||||
if(err) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
async.series(
|
||||
[
|
||||
(callback) => {
|
||||
const userPropValues = _.values(UserProps);
|
||||
const userLogValues = _.values(UserLogNames);
|
||||
callback => {
|
||||
const userPropValues = _.values(UserProps);
|
||||
const userLogValues = _.values(UserLogNames);
|
||||
|
||||
const hasMci = (c, t) => {
|
||||
if(!Array.isArray(t)) {
|
||||
t = [ t ];
|
||||
if (!Array.isArray(t)) {
|
||||
t = [t];
|
||||
}
|
||||
return t.some(t => _.isObject(mciData, [ 'menu', `${t}${c}` ]));
|
||||
return t.some(t => _.isObject(mciData, ['menu', `${t}${c}`]));
|
||||
};
|
||||
|
||||
return this.validateConfigFields(
|
||||
{
|
||||
mciMap : (key, config) => {
|
||||
const mciCodes = Object.keys(config.mciMap).map(mci => {
|
||||
return parseInt(mci);
|
||||
}).filter(mci => !isNaN(mci));
|
||||
if(0 === mciCodes.length) {
|
||||
mciMap: (key, config) => {
|
||||
const mciCodes = Object.keys(config.mciMap)
|
||||
.map(mci => {
|
||||
return parseInt(mci);
|
||||
})
|
||||
.filter(mci => !isNaN(mci));
|
||||
if (0 === mciCodes.length) {
|
||||
return false;
|
||||
}
|
||||
return mciCodes.every(mci => {
|
||||
const o = config.mciMap[mci];
|
||||
if(!_.isObject(o)) {
|
||||
if (!_.isObject(o)) {
|
||||
return false;
|
||||
}
|
||||
const type = o.type;
|
||||
switch(type) {
|
||||
case 'userProp' :
|
||||
if(!userPropValues.includes(o.value)) {
|
||||
switch (type) {
|
||||
case 'userProp':
|
||||
if (!userPropValues.includes(o.value)) {
|
||||
return false;
|
||||
}
|
||||
// VM# must exist for this mci
|
||||
if(!_.isObject(mciData, [ 'menu', `VM${mci}` ])) {
|
||||
if (
|
||||
!_.isObject(mciData, [
|
||||
'menu',
|
||||
`VM${mci}`,
|
||||
])
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'userEventLog' :
|
||||
if(!userLogValues.includes(o.value)) {
|
||||
case 'userEventLog':
|
||||
if (!userLogValues.includes(o.value)) {
|
||||
return false;
|
||||
}
|
||||
// VM# must exist for this mci
|
||||
if(!hasMci(mci, ['VM'])) {
|
||||
if (!hasMci(mci, ['VM'])) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
default :
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
},
|
||||
},
|
||||
callback
|
||||
);
|
||||
},
|
||||
(callback) => {
|
||||
return this.prepViewController('menu', FormIds.menu, mciData.menu, callback);
|
||||
callback => {
|
||||
return this.prepViewController(
|
||||
'menu',
|
||||
FormIds.menu,
|
||||
mciData.menu,
|
||||
callback
|
||||
);
|
||||
},
|
||||
callback => {
|
||||
async.forEachSeries(
|
||||
Object.keys(this.config.mciMap),
|
||||
(mciCode, nextMciCode) => {
|
||||
return this.populateTopXList(mciCode, nextMciCode);
|
||||
},
|
||||
err => {
|
||||
return callback(err);
|
||||
}
|
||||
);
|
||||
},
|
||||
(callback) => {
|
||||
async.forEachSeries(Object.keys(this.config.mciMap), (mciCode, nextMciCode) => {
|
||||
return this.populateTopXList(mciCode, nextMciCode);
|
||||
},
|
||||
err => {
|
||||
return callback(err);
|
||||
});
|
||||
}
|
||||
],
|
||||
err => {
|
||||
return cb(err);
|
||||
@@ -117,43 +134,57 @@ exports.getModule = class TopXModule extends MenuModule {
|
||||
|
||||
populateTopXList(mciCode, cb) {
|
||||
const listView = this.viewControllers.menu.getView(mciCode);
|
||||
if(!listView) {
|
||||
if (!listView) {
|
||||
return cb(Errors.UnexpectedState(`Failed to get view for MCI ${mciCode}`));
|
||||
}
|
||||
|
||||
const type = this.config.mciMap[mciCode].type;
|
||||
switch(type) {
|
||||
case 'userProp' : return this.populateTopXUserProp(listView, mciCode, cb);
|
||||
case 'userEventLog' : return this.populateTopXUserEventLog(listView, mciCode, cb);
|
||||
switch (type) {
|
||||
case 'userProp':
|
||||
return this.populateTopXUserProp(listView, mciCode, cb);
|
||||
case 'userEventLog':
|
||||
return this.populateTopXUserEventLog(listView, mciCode, cb);
|
||||
|
||||
// we should not hit here; validation happens up front
|
||||
default : return cb(Errors.UnexpectedState(`Unexpected type: ${type}`));
|
||||
default:
|
||||
return cb(Errors.UnexpectedState(`Unexpected type: ${type}`));
|
||||
}
|
||||
}
|
||||
|
||||
rowsToItems(rows, cb) {
|
||||
let position = 1;
|
||||
async.mapSeries(rows, (row, nextRow) => {
|
||||
this.loadUserInfo(row.user_id, (err, userInfo) => {
|
||||
if(err) {
|
||||
return nextRow(err);
|
||||
}
|
||||
return nextRow(null, Object.assign(userInfo, { position : position++, value : row.value }));
|
||||
});
|
||||
},
|
||||
(err, items) => {
|
||||
return cb(err, items);
|
||||
});
|
||||
async.mapSeries(
|
||||
rows,
|
||||
(row, nextRow) => {
|
||||
this.loadUserInfo(row.user_id, (err, userInfo) => {
|
||||
if (err) {
|
||||
return nextRow(err);
|
||||
}
|
||||
return nextRow(
|
||||
null,
|
||||
Object.assign(userInfo, {
|
||||
position: position++,
|
||||
value: row.value,
|
||||
})
|
||||
);
|
||||
});
|
||||
},
|
||||
(err, items) => {
|
||||
return cb(err, items);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
populateTopXUserEventLog(listView, mciCode, cb) {
|
||||
const mciMap = this.config.mciMap[mciCode];
|
||||
const count = listView.dimens.height || 1;
|
||||
const daysBack = mciMap.daysBack;
|
||||
const mciMap = this.config.mciMap[mciCode];
|
||||
const count = listView.dimens.height || 1;
|
||||
const daysBack = mciMap.daysBack;
|
||||
const shouldSum = _.get(mciMap, 'sum', true);
|
||||
|
||||
const valueSql = shouldSum ? 'SUM(CAST(log_value AS INTEGER))' : 'COUNT()';
|
||||
const dateSql = daysBack ? `AND DATETIME(timestamp) >= DATETIME('now', '-${daysBack} days')` : '';
|
||||
const valueSql = shouldSum ? 'SUM(CAST(log_value AS INTEGER))' : 'COUNT()';
|
||||
const dateSql = daysBack
|
||||
? `AND DATETIME(timestamp) >= DATETIME('now', '-${daysBack} days')`
|
||||
: '';
|
||||
|
||||
SysDb.all(
|
||||
`SELECT user_id, ${valueSql} AS value
|
||||
@@ -162,14 +193,14 @@ exports.getModule = class TopXModule extends MenuModule {
|
||||
GROUP BY user_id
|
||||
ORDER BY value DESC
|
||||
LIMIT ${count};`,
|
||||
[ mciMap.value ],
|
||||
[mciMap.value],
|
||||
(err, rows) => {
|
||||
if(err) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
this.rowsToItems(rows, (err, items) => {
|
||||
if(err) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
listView.setItems(items);
|
||||
@@ -188,14 +219,14 @@ exports.getModule = class TopXModule extends MenuModule {
|
||||
WHERE prop_name = ?
|
||||
ORDER BY value DESC
|
||||
LIMIT ${count};`,
|
||||
[ this.config.mciMap[mciCode].value ],
|
||||
[this.config.mciMap[mciCode].value],
|
||||
(err, rows) => {
|
||||
if(err) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
this.rowsToItems(rows, (err, items) => {
|
||||
if(err) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
listView.setItems(items);
|
||||
@@ -208,25 +239,26 @@ exports.getModule = class TopXModule extends MenuModule {
|
||||
|
||||
loadUserInfo(userId, cb) {
|
||||
const getPropOpts = {
|
||||
names : [ UserProps.RealName, UserProps.Location, UserProps.Affiliations ]
|
||||
names: [UserProps.RealName, UserProps.Location, UserProps.Affiliations],
|
||||
};
|
||||
|
||||
const userInfo = { userId };
|
||||
User.getUserName(userId, (err, userName) => {
|
||||
if(err) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
userInfo.userName = userName;
|
||||
|
||||
User.loadProperties(userId, getPropOpts, (err, props) => {
|
||||
if(err) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
userInfo.location = props[UserProps.Location] || '';
|
||||
userInfo.affils = userInfo.affiliation = props[UserProps.Affiliations] || '';
|
||||
userInfo.realName = props[UserProps.RealName] || '';
|
||||
userInfo.location = props[UserProps.Location] || '';
|
||||
userInfo.affils = userInfo.affiliation =
|
||||
props[UserProps.Affiliations] || '';
|
||||
userInfo.realName = props[UserProps.RealName] || '';
|
||||
|
||||
return cb(null, userInfo);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user