2015-10-18 02:58:07 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2018-07-22 18:56:56 +00:00
|
|
|
const { MenuModule } = require('./menu_module.js');
|
|
|
|
const StatLog = require('./stat_log.js');
|
|
|
|
const User = require('./user.js');
|
|
|
|
const sysDb = require('./database.js').dbs.system;
|
|
|
|
const { Errors } = require('./enig_error.js');
|
2018-11-24 05:18:15 +00:00
|
|
|
const UserProps = require('./user_property.js');
|
2018-11-26 02:05:16 +00:00
|
|
|
const SysLogKeys = require('./system_log.js');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const moment = require('moment');
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
2015-10-18 02:58:07 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'Last Callers',
|
|
|
|
desc : 'Last callers to the system',
|
|
|
|
author : 'NuSkooler',
|
|
|
|
packageName : 'codes.l33t.enigma.lastcallers'
|
2015-10-18 02:58:07 +00:00
|
|
|
};
|
|
|
|
|
2018-07-22 18:56:56 +00:00
|
|
|
const MciViewIds = {
|
|
|
|
callerList : 1,
|
2015-10-18 02:58:07 +00:00
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.getModule = class LastCallersModule extends MenuModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2018-07-21 20:32:06 +00:00
|
|
|
|
|
|
|
this.actionIndicators = _.get(options, 'menuConfig.config.actionIndicators', {});
|
|
|
|
this.actionIndicatorDefault = _.get(options, 'menuConfig.config.actionIndicatorDefault', '-');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mciReady(mciData, cb) {
|
|
|
|
super.mciReady(mciData, err => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2018-07-21 20:32:06 +00:00
|
|
|
async.waterfall(
|
2018-06-22 05:15:04 +00:00
|
|
|
[
|
2018-12-17 18:56:07 +00:00
|
|
|
(callback) => {
|
2018-07-21 20:32:06 +00:00
|
|
|
this.prepViewController('callers', 0, mciData.menu, err => {
|
2018-12-17 18:56:07 +00:00
|
|
|
return callback(err);
|
2018-07-21 20:32:06 +00:00
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2018-12-17 18:56:07 +00:00
|
|
|
(callback) => {
|
2018-07-21 20:32:06 +00:00
|
|
|
this.fetchHistory( (err, loginHistory) => {
|
2018-12-17 18:56:07 +00:00
|
|
|
return callback(err, loginHistory);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
2018-12-17 18:56:07 +00:00
|
|
|
(loginHistory, callback) => {
|
2018-07-21 20:32:06 +00:00
|
|
|
this.loadUserForHistoryItems(loginHistory, (err, updatedHistory) => {
|
2018-12-17 18:56:07 +00:00
|
|
|
return callback(err, updatedHistory);
|
2018-07-21 20:32:06 +00:00
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2018-12-17 18:56:07 +00:00
|
|
|
(loginHistory, callback) => {
|
2018-07-22 18:56:56 +00:00
|
|
|
const callersView = this.viewControllers.callers.getView(MciViewIds.callerList);
|
|
|
|
if(!callersView) {
|
|
|
|
return cb(Errors.MissingMci(`Missing caller list MCI ${MciViewIds.callerList}`));
|
|
|
|
}
|
2018-07-21 20:32:06 +00:00
|
|
|
callersView.setItems(loginHistory);
|
2018-06-22 05:15:04 +00:00
|
|
|
callersView.redraw();
|
2018-12-17 18:56:07 +00:00
|
|
|
return callback(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
],
|
2018-07-21 20:32:06 +00:00
|
|
|
err => {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(err) {
|
2018-07-21 20:32:06 +00:00
|
|
|
this.client.log.warn( { error : err.message }, 'Error loading last callers');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-07-21 20:32:06 +00:00
|
|
|
return cb(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2018-07-21 20:32:06 +00:00
|
|
|
|
|
|
|
getCollapse(conf) {
|
|
|
|
let collapse = _.get(this, conf);
|
2018-07-26 03:18:30 +00:00
|
|
|
collapse = collapse && collapse.match(/^([0-9]+)\s*(minutes?|seconds?|hours?|days?|months?)$/);
|
2018-07-21 20:32:06 +00:00
|
|
|
if(collapse) {
|
|
|
|
return moment.duration(parseInt(collapse[1]), collapse[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchHistory(cb) {
|
2018-07-22 18:56:56 +00:00
|
|
|
const callersView = this.viewControllers.callers.getView(MciViewIds.callerList);
|
2018-07-21 20:32:06 +00:00
|
|
|
if(!callersView || 0 === callersView.dimens.height) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
StatLog.getSystemLogEntries(
|
2018-11-26 02:05:16 +00:00
|
|
|
SysLogKeys.UserLoginHistory,
|
2018-07-21 20:32:06 +00:00
|
|
|
StatLog.Order.TimestampDesc,
|
|
|
|
200, // max items to fetch - we need more than max displayed for filtering/etc.
|
|
|
|
(err, loginHistory) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const dateTimeFormat = _.get(
|
|
|
|
this, 'menuConfig.config.dateTimeFormat', this.client.currentTheme.helpers.getDateFormat('short'));
|
|
|
|
|
|
|
|
loginHistory = loginHistory.map(item => {
|
|
|
|
try {
|
|
|
|
const historyItem = JSON.parse(item.log_value);
|
|
|
|
if(_.isObject(historyItem)) {
|
|
|
|
item.userId = historyItem.userId;
|
|
|
|
item.sessionId = historyItem.sessionId;
|
|
|
|
} else {
|
|
|
|
item.userId = historyItem; // older format
|
|
|
|
item.sessionId = '-none-';
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
return null; // we'll filter this out
|
|
|
|
}
|
|
|
|
|
|
|
|
item.timestamp = moment(item.timestamp);
|
|
|
|
|
|
|
|
return Object.assign(
|
|
|
|
item,
|
|
|
|
{
|
|
|
|
ts : moment(item.timestamp).format(dateTimeFormat)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const hideSysOp = _.get(this, 'menuConfig.config.sysop.hide');
|
|
|
|
const sysOpCollapse = this.getCollapse('menuConfig.config.sysop.collapse');
|
|
|
|
|
2018-07-22 03:38:06 +00:00
|
|
|
const collapseList = (withUserId, minAge) => {
|
2018-07-21 20:32:06 +00:00
|
|
|
let lastUserId;
|
|
|
|
let lastTimestamp;
|
|
|
|
loginHistory = loginHistory.filter(item => {
|
2018-07-22 03:38:06 +00:00
|
|
|
const secApart = lastTimestamp ? moment.duration(lastTimestamp.diff(item.timestamp)).asSeconds() : 0;
|
|
|
|
const collapse = (null === withUserId ? true : withUserId === item.userId) &&
|
|
|
|
(lastUserId === item.userId) &&
|
|
|
|
(secApart < minAge);
|
2018-07-21 20:32:06 +00:00
|
|
|
|
|
|
|
lastUserId = item.userId;
|
|
|
|
lastTimestamp = item.timestamp;
|
|
|
|
|
2018-07-22 03:38:06 +00:00
|
|
|
return !collapse;
|
2018-07-21 20:32:06 +00:00
|
|
|
});
|
2018-07-22 03:38:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if(hideSysOp) {
|
|
|
|
loginHistory = loginHistory.filter(item => false === User.isRootUserId(item.userId));
|
|
|
|
} else if(sysOpCollapse) {
|
|
|
|
collapseList(User.RootUserID, sysOpCollapse.asSeconds());
|
2018-07-21 20:32:06 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 03:38:06 +00:00
|
|
|
const userCollapse = this.getCollapse('menuConfig.config.user.collapse');
|
2018-07-21 20:32:06 +00:00
|
|
|
if(userCollapse) {
|
2018-07-22 03:38:06 +00:00
|
|
|
collapseList(null, userCollapse.asSeconds());
|
2018-07-21 20:32:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cb(
|
|
|
|
null,
|
|
|
|
loginHistory.slice(0, callersView.dimens.height) // trim the fat
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadUserForHistoryItems(loginHistory, cb) {
|
|
|
|
const getPropOpts = {
|
2018-11-24 05:18:15 +00:00
|
|
|
names : [ UserProps.RealName, UserProps.Location, UserProps.Affiliations ]
|
2018-07-21 20:32:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const actionIndicatorNames = _.map(this.actionIndicators, (v, k) => k);
|
|
|
|
let indicatorSumsSql;
|
|
|
|
if(actionIndicatorNames.length > 0) {
|
|
|
|
indicatorSumsSql = actionIndicatorNames.map(i => {
|
2019-01-18 03:18:02 +00:00
|
|
|
return `SUM(CASE WHEN log_name='${_.snakeCase(i)}' THEN 1 ELSE 0 END) AS ${i}`;
|
2018-07-21 20:32:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-17 18:56:07 +00:00
|
|
|
async.map(loginHistory, (item, nextHistoryItem) => {
|
2018-07-21 20:32:06 +00:00
|
|
|
User.getUserName(item.userId, (err, userName) => {
|
|
|
|
if(err) {
|
2018-12-17 18:56:07 +00:00
|
|
|
return nextHistoryItem(null, null);
|
2018-07-21 20:32:06 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 18:56:56 +00:00
|
|
|
item.userName = item.text = userName;
|
2018-07-21 20:32:06 +00:00
|
|
|
|
|
|
|
User.loadProperties(item.userId, getPropOpts, (err, props) => {
|
2018-11-24 05:18:15 +00:00
|
|
|
item.location = (props && props[UserProps.Location]) || '';
|
|
|
|
item.affiliation = item.affils = (props && props[UserProps.Affiliations]) || '';
|
|
|
|
item.realName = (props && props[UserProps.RealName]) || '';
|
2018-07-21 20:32:06 +00:00
|
|
|
|
|
|
|
if(!indicatorSumsSql) {
|
2018-12-17 18:56:07 +00:00
|
|
|
return nextHistoryItem(null, item);
|
2018-07-21 20:32:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sysDb.get(
|
|
|
|
`SELECT ${indicatorSumsSql.join(', ')}
|
|
|
|
FROM user_event_log
|
|
|
|
WHERE user_id=? AND session_id=?
|
|
|
|
LIMIT 1;`,
|
|
|
|
[ item.userId, item.sessionId ],
|
|
|
|
(err, results) => {
|
|
|
|
if(_.isObject(results)) {
|
|
|
|
item.actions = '';
|
|
|
|
Object.keys(results).forEach(n => {
|
|
|
|
const indicator = results[n] > 0 ? this.actionIndicators[n] || this.actionIndicatorDefault : this.actionIndicatorDefault;
|
|
|
|
item[n] = indicator;
|
|
|
|
item.actions += indicator;
|
|
|
|
});
|
|
|
|
}
|
2018-12-17 18:56:07 +00:00
|
|
|
return nextHistoryItem(null, item);
|
2018-07-21 20:32:06 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(err, mapped) => {
|
|
|
|
return cb(err, mapped.filter(item => item)); // remove deleted
|
|
|
|
});
|
|
|
|
}
|
2015-11-26 01:03:47 +00:00
|
|
|
};
|