StatLog will now store for N days or N max/count items

This commit is contained in:
Bryan Ashby 2016-09-01 23:41:20 -06:00
parent 9d46f23c66
commit 767bddcc4b
3 changed files with 58 additions and 17 deletions

View File

@ -50,6 +50,15 @@ class StatLog {
};
}
get KeepType() {
return {
Forever : 'forever',
Days : 'days',
Max : 'max',
Count : 'max',
};
}
get Order() {
return {
Timestamp : 'timestamp_asc',
@ -129,25 +138,28 @@ class StatLog {
// the time "now" in the ISO format we use and love :)
get now() { return moment().format('YYYY-MM-DDTHH:mm:ss.SSSZ'); }
appendSystemLogEntry(logName, logValue, keepDays, cb) {
appendSystemLogEntry(logName, logValue, keep, keepType, cb) {
sysDb.run(
`INSERT INTO system_event_log (timestamp, log_name, log_value)
VALUES (?, ?, ?);`,
[ this.now, logName, logValue ],
() => {
//
// Handle keepDays
// Handle keep
//
if(-1 === keepDays) {
if(-1 === keep) {
if(cb) {
return cb(null);
}
return;
}
switch(keepType) {
// keep # of days
case 'days' :
sysDb.run(
`DELETE FROM system_event_log
WHERE log_name = ? AND timestamp <= DATETIME("now", "-${keepDays} day");`,
WHERE log_name = ? AND timestamp <= DATETIME("now", "-${keep} day");`,
[ logName ],
err => {
// cb optional - callers may fire & forget
@ -156,6 +168,34 @@ class StatLog {
}
}
);
break;
case 'count':
case 'max' :
// keep max of N/count
sysDb.run(
`DELETE FROM system_event_log
WHERE id IN(
SELECT id
FROM system_event_log
WHERE log_name = ?
ORDER BY id DESC
LIMIT -1 OFFSET ${keep}
);`,
[ logName ],
err => {
if(cb) {
return cb(err);
}
}
);
break;
case 'forever' :
default :
// nop
break;
}
}
);
}

View File

@ -73,7 +73,8 @@ function userLogin(client, username, password, cb) {
StatLog.incrementUserStat(user, 'login_count', 1, callback);
},
function recordLoginHistory(callback) {
StatLog.appendSystemLogEntry('user_login_history', user.userId, 30, callback);
const LOGIN_HISTORY_MAX = 200; // history of up to last 200 callers
StatLog.appendSystemLogEntry('user_login_history', user.userId, LOGIN_HISTORY_MAX, StatLog.KeepType.Max, callback);
}
],
function complete(err) {

View File

@ -52,7 +52,7 @@ exports.getModule = class RumorzModule extends MenuModule {
if(_.isString(formData.value.rumor) && renderStringLength(formData.value.rumor) > 0) {
const rumor = formData.value.rumor.trim(); // remove any trailing ws
StatLog.appendSystemLogEntry(STATLOG_KEY_RUMORZ, rumor, StatLog.KeepDays.Forever, () => {
StatLog.appendSystemLogEntry(STATLOG_KEY_RUMORZ, rumor, StatLog.KeepDays.Forever, StatLog.KeepType.Forever, () => {
this.clearAddForm();
return this.displayViewScreen(true, cb); // true=cls
});