First pass formatting with Prettier
* Added .prettierrc.json * Added .prettierignore * Formatted
This commit is contained in:
175
core/stat_log.js
175
core/stat_log.js
@@ -1,15 +1,13 @@
|
||||
/* jslint node: true */
|
||||
'use strict';
|
||||
|
||||
const sysDb = require('./database.js').dbs.system;
|
||||
const {
|
||||
getISOTimestampString
|
||||
} = require('./database.js');
|
||||
const Errors = require('./enig_error.js');
|
||||
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');
|
||||
const _ = require('lodash');
|
||||
const moment = require('moment');
|
||||
|
||||
/*
|
||||
System Event Log & Stats
|
||||
@@ -38,7 +36,7 @@ class StatLog {
|
||||
`SELECT stat_name, stat_value
|
||||
FROM system_stat;`,
|
||||
(err, row) => {
|
||||
if(row) {
|
||||
if (row) {
|
||||
self.systemStats[row.stat_name] = row.stat_value;
|
||||
}
|
||||
},
|
||||
@@ -50,25 +48,25 @@ class StatLog {
|
||||
|
||||
get KeepDays() {
|
||||
return {
|
||||
Forever : -1,
|
||||
Forever: -1,
|
||||
};
|
||||
}
|
||||
|
||||
get KeepType() {
|
||||
return {
|
||||
Forever : 'forever',
|
||||
Days : 'days',
|
||||
Max : 'max',
|
||||
Count : 'max',
|
||||
Forever: 'forever',
|
||||
Days: 'days',
|
||||
Max: 'max',
|
||||
Count: 'max',
|
||||
};
|
||||
}
|
||||
|
||||
get Order() {
|
||||
return {
|
||||
Timestamp : 'timestamp_asc',
|
||||
TimestampAsc : 'timestamp_asc',
|
||||
TimestampDesc : 'timestamp_desc',
|
||||
Random : 'random',
|
||||
Timestamp: 'timestamp_asc',
|
||||
TimestampAsc: 'timestamp_asc',
|
||||
TimestampDesc: 'timestamp_desc',
|
||||
Random: 'random',
|
||||
};
|
||||
}
|
||||
|
||||
@@ -80,7 +78,7 @@ class StatLog {
|
||||
incrementBy = incrementBy || 1;
|
||||
|
||||
let newValue = parseInt(this.systemStats[statName]);
|
||||
if(!isNaN(newValue)) {
|
||||
if (!isNaN(newValue)) {
|
||||
newValue += incrementBy;
|
||||
} else {
|
||||
newValue = incrementBy;
|
||||
@@ -97,17 +95,19 @@ class StatLog {
|
||||
sysDb.run(
|
||||
`REPLACE INTO system_stat (stat_name, stat_value)
|
||||
VALUES (?, ?);`,
|
||||
[ statName, statValue ],
|
||||
[statName, statValue],
|
||||
err => {
|
||||
// cb optional - callers may fire & forget
|
||||
if(cb) {
|
||||
if (cb) {
|
||||
return cb(err);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
getSystemStat(statName) { return this.systemStats[statName]; }
|
||||
getSystemStat(statName) {
|
||||
return this.systemStats[statName];
|
||||
}
|
||||
|
||||
getSystemStatNum(statName) {
|
||||
return parseInt(this.getSystemStat(statName)) || 0;
|
||||
@@ -126,9 +126,13 @@ class StatLog {
|
||||
// note: cb is optional in PersistUserProperty
|
||||
user.persistProperty(statName, statValue, cb);
|
||||
|
||||
if(!options.noEvent) {
|
||||
const Events = require('./events.js'); // we need to late load currently
|
||||
Events.emit(Events.getSystemEvents().UserStatSet, { user, statName, statValue } );
|
||||
if (!options.noEvent) {
|
||||
const Events = require('./events.js'); // we need to late load currently
|
||||
Events.emit(Events.getSystemEvents().UserStatSet, {
|
||||
user,
|
||||
statName,
|
||||
statValue,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,31 +154,22 @@ class StatLog {
|
||||
const oldValue = user.getPropertyAsNumber(statName) || 0;
|
||||
const newValue = oldValue + incrementBy;
|
||||
|
||||
this.setUserStatWithOptions(
|
||||
user,
|
||||
statName,
|
||||
newValue,
|
||||
{ noEvent : true },
|
||||
err => {
|
||||
if(!err) {
|
||||
const Events = require('./events.js'); // we need to late load currently
|
||||
Events.emit(
|
||||
Events.getSystemEvents().UserStatIncrement,
|
||||
{
|
||||
user,
|
||||
statName,
|
||||
oldValue,
|
||||
statIncrementBy : incrementBy,
|
||||
statValue : newValue
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if(cb) {
|
||||
return cb(err);
|
||||
}
|
||||
this.setUserStatWithOptions(user, statName, newValue, { noEvent: true }, err => {
|
||||
if (!err) {
|
||||
const Events = require('./events.js'); // we need to late load currently
|
||||
Events.emit(Events.getSystemEvents().UserStatIncrement, {
|
||||
user,
|
||||
statName,
|
||||
oldValue,
|
||||
statIncrementBy: incrementBy,
|
||||
statValue: newValue,
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
if (cb) {
|
||||
return cb(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// the time "now" in the ISO format we use and love :)
|
||||
@@ -186,28 +181,28 @@ class StatLog {
|
||||
sysDb.run(
|
||||
`INSERT INTO system_event_log (timestamp, log_name, log_value)
|
||||
VALUES (?, ?, ?);`,
|
||||
[ this.now, logName, logValue ],
|
||||
[this.now, logName, logValue],
|
||||
() => {
|
||||
//
|
||||
// Handle keep
|
||||
//
|
||||
if(-1 === keep) {
|
||||
if(cb) {
|
||||
if (-1 === keep) {
|
||||
if (cb) {
|
||||
return cb(null);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
switch(keepType) {
|
||||
switch (keepType) {
|
||||
// keep # of days
|
||||
case 'days' :
|
||||
case 'days':
|
||||
sysDb.run(
|
||||
`DELETE FROM system_event_log
|
||||
WHERE log_name = ? AND timestamp <= DATETIME("now", "-${keep} day");`,
|
||||
[ logName ],
|
||||
[logName],
|
||||
err => {
|
||||
// cb optional - callers may fire & forget
|
||||
if(cb) {
|
||||
if (cb) {
|
||||
return cb(err);
|
||||
}
|
||||
}
|
||||
@@ -215,7 +210,7 @@ class StatLog {
|
||||
break;
|
||||
|
||||
case 'count':
|
||||
case 'max' :
|
||||
case 'max':
|
||||
// keep max of N/count
|
||||
sysDb.run(
|
||||
`DELETE FROM system_event_log
|
||||
@@ -226,17 +221,17 @@ class StatLog {
|
||||
ORDER BY id DESC
|
||||
LIMIT -1 OFFSET ${keep}
|
||||
);`,
|
||||
[ logName ],
|
||||
[logName],
|
||||
err => {
|
||||
if(cb) {
|
||||
if (cb) {
|
||||
return cb(err);
|
||||
}
|
||||
}
|
||||
);
|
||||
break;
|
||||
|
||||
case 'forever' :
|
||||
default :
|
||||
case 'forever':
|
||||
default:
|
||||
// nop
|
||||
break;
|
||||
}
|
||||
@@ -256,69 +251,69 @@ class StatLog {
|
||||
*/
|
||||
findSystemLogEntries(filter, cb) {
|
||||
filter = filter || {};
|
||||
if(!_.isString(filter.logName)) {
|
||||
if (!_.isString(filter.logName)) {
|
||||
return cb(Errors.MissingParam('filter.logName is required'));
|
||||
}
|
||||
|
||||
filter.resultType = filter.resultType || 'obj';
|
||||
filter.order = filter.order || 'timestamp';
|
||||
filter.resultType = filter.resultType || 'obj';
|
||||
filter.order = filter.order || 'timestamp';
|
||||
|
||||
let sql;
|
||||
if('count' === filter.resultType) {
|
||||
sql =
|
||||
`SELECT COUNT() AS count
|
||||
if ('count' === filter.resultType) {
|
||||
sql = `SELECT COUNT() AS count
|
||||
FROM system_event_log`;
|
||||
} else {
|
||||
sql =
|
||||
`SELECT timestamp, log_value
|
||||
sql = `SELECT timestamp, log_value
|
||||
FROM system_event_log`;
|
||||
}
|
||||
|
||||
sql += ' WHERE log_name = ?';
|
||||
|
||||
if(filter.date) {
|
||||
if (filter.date) {
|
||||
filter.date = moment(filter.date);
|
||||
sql += ` AND DATE(timestamp, "localtime") = DATE("${filter.date.format('YYYY-MM-DD')}")`;
|
||||
sql += ` AND DATE(timestamp, "localtime") = DATE("${filter.date.format(
|
||||
'YYYY-MM-DD'
|
||||
)}")`;
|
||||
}
|
||||
|
||||
if('count' !== filter.resultType) {
|
||||
switch(filter.order) {
|
||||
case 'timestamp' :
|
||||
case 'timestamp_asc' :
|
||||
if ('count' !== filter.resultType) {
|
||||
switch (filter.order) {
|
||||
case 'timestamp':
|
||||
case 'timestamp_asc':
|
||||
sql += ' ORDER BY timestamp ASC';
|
||||
break;
|
||||
|
||||
case 'timestamp_desc' :
|
||||
case 'timestamp_desc':
|
||||
sql += ' ORDER BY timestamp DESC';
|
||||
break;
|
||||
|
||||
case 'random' :
|
||||
case 'random':
|
||||
sql += ' ORDER BY RANDOM()';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(_.isNumber(filter.limit) && 0 !== filter.limit) {
|
||||
if (_.isNumber(filter.limit) && 0 !== filter.limit) {
|
||||
sql += ` LIMIT ${filter.limit}`;
|
||||
}
|
||||
|
||||
sql += ';';
|
||||
|
||||
if('count' === filter.resultType) {
|
||||
sysDb.get(sql, [ filter.logName ], (err, row) => {
|
||||
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) => {
|
||||
sysDb.all(sql, [filter.logName], (err, rows) => {
|
||||
return cb(err, rows);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
getSystemLogEntries(logName, order, limit, cb) {
|
||||
if(!cb && _.isFunction(limit)) {
|
||||
cb = limit;
|
||||
limit = 0;
|
||||
if (!cb && _.isFunction(limit)) {
|
||||
cb = limit;
|
||||
limit = 0;
|
||||
} else {
|
||||
limit = limit || 0;
|
||||
}
|
||||
@@ -335,10 +330,10 @@ class StatLog {
|
||||
sysDb.run(
|
||||
`INSERT INTO user_event_log (timestamp, user_id, session_id, log_name, log_value)
|
||||
VALUES (?, ?, ?, ?, ?);`,
|
||||
[ this.now, user.userId, user.sessionId, logName, logValue ],
|
||||
[this.now, user.userId, user.sessionId, logName, logValue],
|
||||
err => {
|
||||
if(err) {
|
||||
if(cb) {
|
||||
if (err) {
|
||||
if (cb) {
|
||||
cb(err);
|
||||
}
|
||||
return;
|
||||
@@ -346,8 +341,8 @@ class StatLog {
|
||||
//
|
||||
// Handle keepDays
|
||||
//
|
||||
if(-1 === keepDays) {
|
||||
if(cb) {
|
||||
if (-1 === keepDays) {
|
||||
if (cb) {
|
||||
return cb(null);
|
||||
}
|
||||
return;
|
||||
@@ -356,10 +351,10 @@ class StatLog {
|
||||
sysDb.run(
|
||||
`DELETE FROM user_event_log
|
||||
WHERE user_id = ? AND log_name = ? AND timestamp <= DATETIME("now", "-${keepDays} day");`,
|
||||
[ user.userId, logName ],
|
||||
[user.userId, logName],
|
||||
err => {
|
||||
// cb optional - callers may fire & forget
|
||||
if(cb) {
|
||||
if (cb) {
|
||||
return cb(err);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user