Fairly functional

This commit is contained in:
Bryan Ashby 2019-01-17 21:27:37 -07:00
parent 2a3271ef4e
commit 4e1997302e
1 changed files with 20 additions and 7 deletions

View File

@ -44,6 +44,13 @@ exports.getModule = class TopXModule extends MenuModule {
const userPropValues = _.values(UserProps); const userPropValues = _.values(UserProps);
const userLogValues = _.values(UserLogNames); const userLogValues = _.values(UserLogNames);
const hasMci = (c, t) => {
if(!Array.isArray(t)) {
t = [ t ];
}
return t.some(t => _.isObject(mciData, [ 'menu', `${t}${c}` ]));
};
return this.validateConfigFields( return this.validateConfigFields(
{ {
mciMap : (key, config) => { mciMap : (key, config) => {
@ -75,7 +82,7 @@ exports.getModule = class TopXModule extends MenuModule {
return false; return false;
} }
// VM# must exist for this mci // VM# must exist for this mci
if(!_.isObject(mciData, [ 'menu', `VM${mci}` ])) { if(!hasMci(mci, ['VM'])) {
return false; return false;
} }
break; break;
@ -121,17 +128,18 @@ exports.getModule = class TopXModule extends MenuModule {
case 'userEventLog' : return this.populateTopXUserEventLog(listView, mciCode, cb); case 'userEventLog' : return this.populateTopXUserEventLog(listView, mciCode, cb);
// we should not hit here; validation happens up front // 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) { rowsToItems(rows, cb) {
async.map(rows, (row, nextRow) => { let position = 1;
async.mapSeries(rows, (row, nextRow) => {
this.loadUserInfo(row.user_id, (err, userInfo) => { this.loadUserInfo(row.user_id, (err, userInfo) => {
if(err) { if(err) {
return nextRow(err); return nextRow(err);
} }
return nextRow(null, Object.assign(userInfo, { value : row.value })); return nextRow(null, Object.assign(userInfo, { position : position++, value : row.value }));
}); });
}, },
(err, items) => { (err, items) => {
@ -142,12 +150,15 @@ exports.getModule = class TopXModule extends MenuModule {
populateTopXUserEventLog(listView, mciCode, cb) { populateTopXUserEventLog(listView, mciCode, cb) {
const count = listView.dimens.height || 1; const count = listView.dimens.height || 1;
const daysBack = this.config.mciMap[mciCode].daysBack; const daysBack = this.config.mciMap[mciCode].daysBack;
const whereDate = daysBack ? `AND DATETIME(timestamp) >= DATETIME('now', '-${daysBack} days')` : ''; const shouldSum = _.get(this.config.mciMap[mciCode], 'sum', true);
const valueSql = shouldSum ? 'SUM(CAST(log_value AS INTEGER))' : 'COUNT()';
const dateSql = daysBack ? `AND DATETIME(timestamp) >= DATETIME('now', '-${daysBack} days')` : '';
SysDb.all( SysDb.all(
`SELECT user_id, SUM(CASE WHEN typeof(log_value) IS 'text' THEN 1 ELSE CAST(log_value AS INTEGER) END) AS value `SELECT user_id, ${valueSql} AS value
FROM user_event_log FROM user_event_log
WHERE log_name = ? ${whereDate} WHERE log_name = ? ${dateSql}
GROUP BY user_id GROUP BY user_id
ORDER BY value DESC ORDER BY value DESC
LIMIT ${count};`, LIMIT ${count};`,
@ -163,6 +174,7 @@ exports.getModule = class TopXModule extends MenuModule {
} }
listView.setItems(items); listView.setItems(items);
listView.redraw(); listView.redraw();
return cb(null);
}); });
} }
); );
@ -188,6 +200,7 @@ exports.getModule = class TopXModule extends MenuModule {
} }
listView.setItems(items); listView.setItems(items);
listView.redraw(); listView.redraw();
return cb(null);
}); });
} }
); );