mirror of
https://github.com/NuSkooler/enigma-bbs.git
synced 2025-07-22 10:45:56 +02:00
Finally implement "Total Calls Today" MCI: TT
+ Add findSystemLogEntries() to StatLog amongst others
This commit is contained in:
parent
ec97b3e8d4
commit
f471fd0ebe
9 changed files with 121 additions and 43 deletions
130
core/stat_log.js
130
core/stat_log.js
|
@ -5,9 +5,11 @@ const sysDb = require('./database.js').dbs.system;
|
|||
const {
|
||||
getISOTimestampString
|
||||
} = require('./database.js');
|
||||
const Errors = require('./enig_error.js');
|
||||
|
||||
// deps
|
||||
const _ = require('lodash');
|
||||
const moment = require('moment');
|
||||
|
||||
/*
|
||||
System Event Log & Stats
|
||||
|
@ -74,6 +76,19 @@ class StatLog {
|
|||
this.systemStats[statName] = statValue;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
setSystemStat(statName, statValue, cb) {
|
||||
// live stats
|
||||
this.systemStats[statName] = statValue;
|
||||
|
@ -99,19 +114,7 @@ class StatLog {
|
|||
}
|
||||
|
||||
incrementSystemStat(statName, incrementBy, cb) {
|
||||
incrementBy = incrementBy || 1;
|
||||
|
||||
let newValue = parseInt(this.systemStats[statName]);
|
||||
if(newValue) {
|
||||
if(!_.isNumber(newValue)) {
|
||||
return cb(new Error(`Value for ${statName} is not a number!`));
|
||||
}
|
||||
|
||||
newValue += incrementBy;
|
||||
} else {
|
||||
newValue = incrementBy;
|
||||
}
|
||||
|
||||
const newValue = this.incrementNonPersistentSystemStat(statName, incrementBy);
|
||||
return this.setSystemStat(statName, newValue, cb);
|
||||
}
|
||||
|
||||
|
@ -216,26 +219,78 @@ class StatLog {
|
|||
);
|
||||
}
|
||||
|
||||
getSystemLogEntries(logName, order, limit, cb) {
|
||||
let sql =
|
||||
`SELECT timestamp, log_value
|
||||
FROM system_event_log
|
||||
WHERE log_name = ?`;
|
||||
/*
|
||||
Find System Log entries by |filter|:
|
||||
|
||||
switch(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()';
|
||||
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'));
|
||||
}
|
||||
|
||||
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`;
|
||||
} else {
|
||||
sql =
|
||||
`SELECT timestamp, log_value
|
||||
FROM system_event_log`;
|
||||
}
|
||||
|
||||
sql += ' WHERE log_name = ?';
|
||||
|
||||
if(filter.date) {
|
||||
filter.date = moment(filter.date);
|
||||
sql += ` AND DATE(timestamp) = DATE("${filter.date.format('YYYY-MM-DD')}")`;
|
||||
}
|
||||
|
||||
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}`;
|
||||
}
|
||||
|
||||
sql += ';';
|
||||
|
||||
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;
|
||||
|
@ -243,15 +298,12 @@ class StatLog {
|
|||
limit = limit || 0;
|
||||
}
|
||||
|
||||
if(0 !== limit) {
|
||||
sql += ` LIMIT ${limit}`;
|
||||
}
|
||||
|
||||
sql += ';';
|
||||
|
||||
sysDb.all(sql, [ logName ], (err, rows) => {
|
||||
return cb(err, rows);
|
||||
});
|
||||
const filter = {
|
||||
logName,
|
||||
order,
|
||||
limit,
|
||||
};
|
||||
return this.findSystemLogEntries(filter, cb);
|
||||
}
|
||||
|
||||
appendUserLogEntry(user, logName, logValue, keepDays, cb) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue