2016-07-28 03:48:13 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const sysDb = require('./database.js').dbs.system;
|
2018-11-23 21:47:18 +00:00
|
|
|
const {
|
|
|
|
getISOTimestampString
|
|
|
|
} = require('./database.js');
|
2018-11-26 03:13:48 +00:00
|
|
|
const Errors = require('./enig_error.js');
|
2016-07-28 03:48:13 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
2018-11-26 03:13:48 +00:00
|
|
|
const moment = require('moment');
|
2016-07-28 03:48:13 +00:00
|
|
|
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
System Event Log & Stats
|
|
|
|
------------------------
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
System & user specific:
|
|
|
|
* Events for generating various statistics, logs such as last callers, etc.
|
|
|
|
* Stats such as counters
|
2016-07-28 03:48:13 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
User specific stats are simply an alternate interface to user properties, while
|
|
|
|
system wide entries are handled on their own. Both are read accessible non-blocking
|
|
|
|
making them easily available for MCI codes for example.
|
2016-07-28 03:48:13 +00:00
|
|
|
*/
|
|
|
|
class StatLog {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor() {
|
|
|
|
this.systemStats = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
init(cb) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Load previous state/values of |this.systemStats|
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
sysDb.each(
|
|
|
|
`SELECT stat_name, stat_value
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM system_stat;`,
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, row) => {
|
|
|
|
if(row) {
|
|
|
|
self.systemStats[row.stat_name] = row.stat_value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get KeepDays() {
|
|
|
|
return {
|
|
|
|
Forever : -1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
get KeepType() {
|
|
|
|
return {
|
2018-06-23 03:26:46 +00:00
|
|
|
Forever : 'forever',
|
|
|
|
Days : 'days',
|
|
|
|
Max : 'max',
|
|
|
|
Count : 'max',
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
get Order() {
|
|
|
|
return {
|
2018-06-23 03:26:46 +00:00
|
|
|
Timestamp : 'timestamp_asc',
|
|
|
|
TimestampAsc : 'timestamp_asc',
|
|
|
|
TimestampDesc : 'timestamp_desc',
|
|
|
|
Random : 'random',
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-26 02:05:16 +00:00
|
|
|
setNonPersistentSystemStat(statName, statValue) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.systemStats[statName] = statValue;
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:13:48 +00:00
|
|
|
incrementNonPersistentSystemStat(statName, incrementBy) {
|
|
|
|
incrementBy = incrementBy || 1;
|
|
|
|
|
|
|
|
let newValue = parseInt(this.systemStats[statName]);
|
|
|
|
if(!isNaN(newValue)) {
|
|
|
|
newValue += incrementBy;
|
|
|
|
} else {
|
|
|
|
newValue = incrementBy;
|
|
|
|
}
|
|
|
|
this.setNonPersistentSystemStat(statName, newValue);
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
setSystemStat(statName, statValue, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// live stats
|
2018-06-22 05:15:04 +00:00
|
|
|
this.systemStats[statName] = statValue;
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// persisted stats
|
2018-06-22 05:15:04 +00:00
|
|
|
sysDb.run(
|
|
|
|
`REPLACE INTO system_stat (stat_name, stat_value)
|
2018-06-23 03:26:46 +00:00
|
|
|
VALUES (?, ?);`,
|
2018-06-22 05:15:04 +00:00
|
|
|
[ statName, statValue ],
|
|
|
|
err => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// cb optional - callers may fire & forget
|
2018-06-22 05:15:04 +00:00
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getSystemStat(statName) { return this.systemStats[statName]; }
|
|
|
|
|
|
|
|
getSystemStatNum(statName) {
|
|
|
|
return parseInt(this.getSystemStat(statName)) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
incrementSystemStat(statName, incrementBy, cb) {
|
2018-11-26 03:13:48 +00:00
|
|
|
const newValue = this.incrementNonPersistentSystemStat(statName, incrementBy);
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.setSystemStat(statName, newValue, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// User specific stats
|
|
|
|
// These are simply convience methods to the user's properties
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
setUserStat(user, statName, statValue, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// note: cb is optional in PersistUserProperty
|
2018-06-22 05:15:04 +00:00
|
|
|
return user.persistProperty(statName, statValue, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
getUserStat(user, statName) {
|
|
|
|
return user.properties[statName];
|
|
|
|
}
|
|
|
|
|
|
|
|
getUserStatNum(user, statName) {
|
|
|
|
return parseInt(this.getUserStat(user, statName)) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
incrementUserStat(user, statName, incrementBy, cb) {
|
|
|
|
incrementBy = incrementBy || 1;
|
|
|
|
|
|
|
|
let newValue = parseInt(user.properties[statName]);
|
|
|
|
if(newValue) {
|
|
|
|
if(!_.isNumber(newValue)) {
|
|
|
|
return cb(new Error(`Value for ${statName} is not a number!`));
|
|
|
|
}
|
|
|
|
|
|
|
|
newValue += incrementBy;
|
|
|
|
} else {
|
|
|
|
newValue = incrementBy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.setUserStat(user, statName, newValue, cb);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// the time "now" in the ISO format we use and love :)
|
2018-11-23 21:47:18 +00:00
|
|
|
get now() {
|
|
|
|
return getISOTimestampString();
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
appendSystemLogEntry(logName, logValue, keep, keepType, cb) {
|
|
|
|
sysDb.run(
|
|
|
|
`INSERT INTO system_event_log (timestamp, log_name, log_value)
|
2018-06-23 03:26:46 +00:00
|
|
|
VALUES (?, ?, ?);`,
|
2018-06-22 05:15:04 +00:00
|
|
|
[ this.now, logName, logValue ],
|
|
|
|
() => {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Handle keep
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(-1 === keep) {
|
|
|
|
if(cb) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(keepType) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// keep # of days
|
2018-06-22 05:15:04 +00:00
|
|
|
case 'days' :
|
|
|
|
sysDb.run(
|
|
|
|
`DELETE FROM system_event_log
|
2018-06-23 03:26:46 +00:00
|
|
|
WHERE log_name = ? AND timestamp <= DATETIME("now", "-${keep} day");`,
|
2018-06-22 05:15:04 +00:00
|
|
|
[ logName ],
|
|
|
|
err => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// cb optional - callers may fire & forget
|
2018-06-22 05:15:04 +00:00
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'count':
|
|
|
|
case 'max' :
|
2018-06-23 03:26:46 +00:00
|
|
|
// keep max of N/count
|
2018-06-22 05:15:04 +00:00
|
|
|
sysDb.run(
|
|
|
|
`DELETE FROM system_event_log
|
2018-06-23 03:26:46 +00:00
|
|
|
WHERE id IN(
|
|
|
|
SELECT id
|
|
|
|
FROM system_event_log
|
|
|
|
WHERE log_name = ?
|
|
|
|
ORDER BY id DESC
|
|
|
|
LIMIT -1 OFFSET ${keep}
|
|
|
|
);`,
|
2018-06-22 05:15:04 +00:00
|
|
|
[ logName ],
|
|
|
|
err => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'forever' :
|
|
|
|
default :
|
2018-06-23 03:26:46 +00:00
|
|
|
// nop
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:13:48 +00:00
|
|
|
/*
|
|
|
|
Find System Log entries by |filter|:
|
|
|
|
|
|
|
|
filter.logName (required)
|
|
|
|
filter.resultType = (obj) | count
|
|
|
|
where obj contains timestamp and log_value
|
|
|
|
filter.limit
|
|
|
|
filter.date - exact date to filter against
|
|
|
|
filter.order = (timestamp) | timestamp_asc | timestamp_desc | random
|
|
|
|
*/
|
|
|
|
findSystemLogEntries(filter, cb) {
|
|
|
|
filter = filter || {};
|
|
|
|
if(!_.isString(filter.logName)) {
|
|
|
|
return cb(Errors.MissingParam('filter.logName is required'));
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 03:13:48 +00:00
|
|
|
filter.resultType = filter.resultType || 'obj';
|
|
|
|
filter.order = filter.order || 'timestamp';
|
|
|
|
|
|
|
|
let sql;
|
|
|
|
if('count' === filter.resultType) {
|
|
|
|
sql =
|
|
|
|
`SELECT COUNT() AS count
|
|
|
|
FROM system_event_log`;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2018-11-26 03:13:48 +00:00
|
|
|
sql =
|
|
|
|
`SELECT timestamp, log_value
|
|
|
|
FROM system_event_log`;
|
|
|
|
}
|
|
|
|
|
|
|
|
sql += ' WHERE log_name = ?';
|
|
|
|
|
|
|
|
if(filter.date) {
|
|
|
|
filter.date = moment(filter.date);
|
2018-11-28 04:21:00 +00:00
|
|
|
sql += ` AND DATE(timestamp, "localtime") = DATE("${filter.date.format('YYYY-MM-DD')}")`;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 03:13:48 +00:00
|
|
|
if('count' !== filter.resultType) {
|
|
|
|
switch(filter.order) {
|
|
|
|
case 'timestamp' :
|
|
|
|
case 'timestamp_asc' :
|
|
|
|
sql += ' ORDER BY timestamp ASC';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'timestamp_desc' :
|
|
|
|
sql += ' ORDER BY timestamp DESC';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'random' :
|
|
|
|
sql += ' ORDER BY RANDOM()';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_.isNumber(filter.limit) && 0 !== filter.limit) {
|
|
|
|
sql += ` LIMIT ${filter.limit}`;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sql += ';';
|
|
|
|
|
2018-11-26 03:13:48 +00:00
|
|
|
if('count' === filter.resultType) {
|
|
|
|
sysDb.get(sql, [ filter.logName ], (err, row) => {
|
|
|
|
return cb(err, row ? row.count : 0);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
sysDb.all(sql, [ filter.logName ], (err, rows) => {
|
|
|
|
return cb(err, rows);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getSystemLogEntries(logName, order, limit, cb) {
|
|
|
|
if(!cb && _.isFunction(limit)) {
|
|
|
|
cb = limit;
|
|
|
|
limit = 0;
|
|
|
|
} else {
|
|
|
|
limit = limit || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const filter = {
|
|
|
|
logName,
|
|
|
|
order,
|
|
|
|
limit,
|
|
|
|
};
|
|
|
|
return this.findSystemLogEntries(filter, cb);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
appendUserLogEntry(user, logName, logValue, keepDays, cb) {
|
|
|
|
sysDb.run(
|
2018-07-21 20:32:06 +00:00
|
|
|
`INSERT INTO user_event_log (timestamp, user_id, session_id, log_name, log_value)
|
|
|
|
VALUES (?, ?, ?, ?, ?);`,
|
|
|
|
[ this.now, user.userId, user.sessionId, logName, logValue ],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
if(cb) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Handle keepDays
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(-1 === keepDays) {
|
|
|
|
if(cb) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sysDb.run(
|
|
|
|
`DELETE FROM user_event_log
|
2018-06-23 03:26:46 +00:00
|
|
|
WHERE user_id = ? AND log_name = ? AND timestamp <= DATETIME("now", "-${keepDays} day");`,
|
2018-06-22 05:15:04 +00:00
|
|
|
[ user.userId, logName ],
|
|
|
|
err => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// cb optional - callers may fire & forget
|
2018-06-22 05:15:04 +00:00
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-07-21 20:32:06 +00:00
|
|
|
|
|
|
|
initUserEvents(cb) {
|
|
|
|
//
|
|
|
|
// We map some user events directly to user stat log entries such that they
|
|
|
|
// are persisted for a time.
|
|
|
|
//
|
|
|
|
const Events = require('./events.js');
|
|
|
|
const systemEvents = Events.getSystemEvents();
|
|
|
|
|
|
|
|
const interestedEvents = [
|
|
|
|
systemEvents.NewUser,
|
|
|
|
systemEvents.UserUpload, systemEvents.UserDownload,
|
|
|
|
systemEvents.UserPostMessage, systemEvents.UserSendMail,
|
2018-12-02 00:00:07 +00:00
|
|
|
systemEvents.UserRunDoor, systemEvents.UserSendNodeMsg,
|
2018-07-21 20:32:06 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
Events.addListenerMultipleEvents(interestedEvents, (eventName, event) => {
|
|
|
|
this.appendUserLogEntry(
|
|
|
|
event.user,
|
|
|
|
'system_event',
|
|
|
|
eventName.replace(/^codes\.l33t\.enigma\.system\./, ''), // strip package name prefix
|
|
|
|
90
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
}
|
2016-07-28 03:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new StatLog();
|