2016-07-28 03:48:13 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
const sysDb = require('./database.js').dbs.system;
|
|
|
|
const { getISOTimestampString } = require('./database.js');
|
|
|
|
const Errors = require('./enig_error.js');
|
|
|
|
const SysProps = require('./system_property.js');
|
2022-05-12 02:30:25 +00:00
|
|
|
const UserProps = require('./user_property');
|
|
|
|
const Message = require('./message');
|
2022-08-02 05:09:18 +00:00
|
|
|
const { getActiveConnections, AllConnections } = require('./client_connections');
|
2016-07-28 03:48:13 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
2022-06-12 20:12:03 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
const moment = require('moment');
|
|
|
|
const SysInfo = require('systeminformation');
|
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 = {};
|
2020-11-23 01:54:49 +00:00
|
|
|
this.lastSysInfoStatsRefresh = 0;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (row) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.systemStats[row.stat_name] = row.stat_value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get KeepDays() {
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
Forever: -1,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
get KeepType() {
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
Forever: 'forever',
|
|
|
|
Days: 'days',
|
|
|
|
Max: 'max',
|
|
|
|
Count: 'max',
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
get Order() {
|
|
|
|
return {
|
2022-06-05 20:04:25 +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]);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!isNaN(newValue)) {
|
2018-11-26 03:13:48 +00:00
|
|
|
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 (?, ?);`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[statName, statValue],
|
2018-06-22 05:15:04 +00:00
|
|
|
err => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// cb optional - callers may fire & forget
|
2022-06-05 20:04:25 +00:00
|
|
|
if (cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-23 01:54:49 +00:00
|
|
|
getSystemStat(statName) {
|
|
|
|
const stat = this.systemStats[statName];
|
|
|
|
|
|
|
|
// Some stats are refreshed periodically when they are
|
|
|
|
// being accessed (e.g. "looked at"). This is handled async.
|
|
|
|
this._refreshSystemStat(statName);
|
|
|
|
|
|
|
|
return stat;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-11-10 02:40:55 +00:00
|
|
|
getFriendlySystemStat(statName, defaultValue) {
|
|
|
|
return (this.getSystemStat(statName) || defaultValue).toLocaleString();
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
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
|
2019-01-03 05:13:42 +00:00
|
|
|
// These are simply convenience methods to the user's properties
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2019-01-07 04:56:12 +00:00
|
|
|
setUserStatWithOptions(user, statName, statValue, options, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// note: cb is optional in PersistUserProperty
|
2019-01-03 05:13:42 +00:00
|
|
|
user.persistProperty(statName, statValue, cb);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!options.noEvent) {
|
|
|
|
const Events = require('./events.js'); // we need to late load currently
|
|
|
|
Events.emit(Events.getSystemEvents().UserStatSet, {
|
|
|
|
user,
|
|
|
|
statName,
|
|
|
|
statValue,
|
|
|
|
});
|
2019-01-07 04:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setUserStat(user, statName, statValue, cb) {
|
|
|
|
return this.setUserStatWithOptions(user, statName, statValue, {}, cb);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getUserStat(user, statName) {
|
2022-05-12 02:30:25 +00:00
|
|
|
return user.getProperty(statName);
|
|
|
|
}
|
|
|
|
|
|
|
|
getUserStatByClient(client, statName) {
|
|
|
|
const stat = this.getUserStat(client.user, statName);
|
|
|
|
this._refreshUserStat(client, statName);
|
|
|
|
return stat;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getUserStatNum(user, statName) {
|
|
|
|
return parseInt(this.getUserStat(user, statName)) || 0;
|
|
|
|
}
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
getUserStatNumByClient(client, statName, ttlSeconds = 10) {
|
2022-05-12 02:30:25 +00:00
|
|
|
const stat = this.getUserStatNum(client.user, statName);
|
|
|
|
this._refreshUserStat(client, statName, ttlSeconds);
|
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
incrementUserStat(user, statName, incrementBy, cb) {
|
|
|
|
incrementBy = incrementBy || 1;
|
|
|
|
|
2019-01-25 04:54:16 +00:00
|
|
|
const oldValue = user.getPropertyAsNumber(statName) || 0;
|
|
|
|
const newValue = oldValue + incrementBy;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
2019-01-07 04:56:12 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (cb) {
|
|
|
|
return cb(err);
|
2019-01-07 04:56:12 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
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 (?, ?, ?);`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[this.now, logName, logValue],
|
2018-06-22 05:15:04 +00:00
|
|
|
() => {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Handle keep
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (-1 === keep) {
|
|
|
|
if (cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
switch (keepType) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// keep # of days
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'days':
|
2018-06-22 05:15:04 +00:00
|
|
|
sysDb.run(
|
|
|
|
`DELETE FROM system_event_log
|
2018-06-23 03:26:46 +00:00
|
|
|
WHERE log_name = ? AND timestamp <= DATETIME("now", "-${keep} day");`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[logName],
|
2018-06-22 05:15:04 +00:00
|
|
|
err => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// cb optional - callers may fire & forget
|
2022-06-05 20:04:25 +00:00
|
|
|
if (cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'count':
|
2022-06-05 20:04:25 +00:00
|
|
|
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(
|
2020-11-10 02:40:55 +00:00
|
|
|
SELECT id
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM system_event_log
|
|
|
|
WHERE log_name = ?
|
|
|
|
ORDER BY id DESC
|
|
|
|
LIMIT -1 OFFSET ${keep}
|
|
|
|
);`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[logName],
|
2018-06-22 05:15:04 +00:00
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'forever':
|
|
|
|
default:
|
2018-06-23 03:26:46 +00:00
|
|
|
// nop
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-26 22:53:21 +00:00
|
|
|
//
|
|
|
|
// Find System Log entry(s) by |filter|:
|
|
|
|
//
|
|
|
|
// - logName: Name of log (required)
|
|
|
|
// - resultType: 'obj' | 'count' (default='obj')
|
|
|
|
// - limit: Limit returned results
|
|
|
|
// - date: exact date to filter against
|
|
|
|
// - order: 'timestamp' | 'timestamp_asc' | 'timestamp_desc' | 'random'
|
|
|
|
// (default='timestamp')
|
|
|
|
//
|
2018-11-26 03:13:48 +00:00
|
|
|
findSystemLogEntries(filter, cb) {
|
2020-11-26 22:53:21 +00:00
|
|
|
return this._findLogEntries('system_event_log', filter, cb);
|
2018-11-26 03:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getSystemLogEntries(logName, order, limit, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!cb && _.isFunction(limit)) {
|
|
|
|
cb = limit;
|
|
|
|
limit = 0;
|
2018-11-26 03:13:48 +00:00
|
|
|
} 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 (?, ?, ?, ?, ?);`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[this.now, user.userId, user.sessionId, logName, logValue],
|
2018-07-21 20:32:06 +00:00
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
if (cb) {
|
2018-07-21 20:32:06 +00:00
|
|
|
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
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (-1 === keepDays) {
|
|
|
|
if (cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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");`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[user.userId, logName],
|
2018-06-22 05:15:04 +00:00
|
|
|
err => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// cb optional - callers may fire & forget
|
2022-06-05 20:04:25 +00:00
|
|
|
if (cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-07-21 20:32:06 +00:00
|
|
|
|
|
|
|
initUserEvents(cb) {
|
2019-01-18 03:18:02 +00:00
|
|
|
const systemEventUserLogInit = require('./sys_event_user_log.js');
|
|
|
|
systemEventUserLogInit(this);
|
2018-07-21 20:32:06 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
2020-11-23 01:54:49 +00:00
|
|
|
|
2020-11-26 22:53:21 +00:00
|
|
|
//
|
|
|
|
// Find User Log entry(s) by |filter|:
|
|
|
|
//
|
|
|
|
// - logName: Name of log (required)
|
|
|
|
// - userId: User ID in which to restrict entries to (missing=all)
|
|
|
|
// - sessionId: Session ID in which to restrict entries to (missing=any)
|
|
|
|
// - resultType: 'obj' | 'count' (default='obj')
|
|
|
|
// - limit: Limit returned results
|
|
|
|
// - date: exact date to filter against
|
|
|
|
// - order: 'timestamp' | 'timestamp_asc' | 'timestamp_desc' | 'random'
|
|
|
|
// (default='timestamp')
|
|
|
|
//
|
|
|
|
findUserLogEntries(filter, cb) {
|
|
|
|
return this._findLogEntries('user_event_log', filter, cb);
|
|
|
|
}
|
|
|
|
|
2020-11-23 01:54:49 +00:00
|
|
|
_refreshSystemStat(statName) {
|
|
|
|
switch (statName) {
|
2022-06-12 20:12:03 +00:00
|
|
|
case SysProps.SystemLoadStats:
|
|
|
|
case SysProps.SystemMemoryStats:
|
2020-11-23 01:54:49 +00:00
|
|
|
return this._refreshSysInfoStats();
|
2022-08-02 05:09:18 +00:00
|
|
|
|
|
|
|
case SysProps.ProcessTrafficStats:
|
|
|
|
return this._refreshProcessTrafficStats();
|
2020-11-23 01:54:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_refreshSysInfoStats() {
|
|
|
|
const now = Math.floor(Date.now() / 1000);
|
|
|
|
if (now < this.lastSysInfoStatsRefresh + 5) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastSysInfoStatsRefresh = now;
|
|
|
|
|
|
|
|
const basicSysInfo = {
|
2022-06-12 20:12:03 +00:00
|
|
|
mem: 'total, free',
|
|
|
|
currentLoad: 'avgLoad, currentLoad',
|
2020-11-23 01:54:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SysInfo.get(basicSysInfo)
|
|
|
|
.then(sysInfo => {
|
|
|
|
const memStats = {
|
2022-06-12 20:12:03 +00:00
|
|
|
totalBytes: sysInfo.mem.total,
|
|
|
|
freeBytes: sysInfo.mem.free,
|
2020-11-23 01:54:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.setNonPersistentSystemStat(SysProps.SystemMemoryStats, memStats);
|
|
|
|
|
|
|
|
const loadStats = {
|
|
|
|
// Not avail on BSD, yet.
|
2022-06-12 20:12:03 +00:00
|
|
|
average: parseFloat(
|
|
|
|
_.get(sysInfo, 'currentLoad.avgLoad', 0).toFixed(2)
|
|
|
|
),
|
|
|
|
current: parseFloat(
|
|
|
|
_.get(sysInfo, 'currentLoad.currentLoad', 0).toFixed(2)
|
|
|
|
),
|
2020-11-23 01:54:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.setNonPersistentSystemStat(SysProps.SystemLoadStats, loadStats);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
// :TODO: log me
|
|
|
|
});
|
|
|
|
}
|
2020-11-26 22:53:21 +00:00
|
|
|
|
2022-08-02 05:09:18 +00:00
|
|
|
_refreshProcessTrafficStats() {
|
|
|
|
const trafficStats = getActiveConnections(AllConnections).reduce(
|
|
|
|
(stats, conn) => {
|
|
|
|
stats.ingress += conn.rawSocket.bytesRead;
|
|
|
|
stats.egress += conn.rawSocket.bytesWritten;
|
|
|
|
return stats;
|
|
|
|
},
|
|
|
|
{ ingress: 0, egress: 0 }
|
|
|
|
);
|
|
|
|
|
|
|
|
this.setNonPersistentSystemStat(SysProps.ProcessTrafficStats, trafficStats);
|
|
|
|
}
|
|
|
|
|
2022-05-12 02:30:25 +00:00
|
|
|
_refreshUserStat(client, statName, ttlSeconds) {
|
2022-06-12 20:12:03 +00:00
|
|
|
switch (statName) {
|
2022-05-12 02:30:25 +00:00
|
|
|
case UserProps.NewPrivateMailCount:
|
2022-06-12 20:12:03 +00:00
|
|
|
this._wrapUserRefreshWithCachedTTL(
|
|
|
|
client,
|
|
|
|
statName,
|
|
|
|
this._refreshUserPrivateMailCount,
|
|
|
|
ttlSeconds
|
|
|
|
);
|
2022-05-12 02:30:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UserProps.NewAddressedToMessageCount:
|
2022-06-12 20:12:03 +00:00
|
|
|
this._wrapUserRefreshWithCachedTTL(
|
|
|
|
client,
|
|
|
|
statName,
|
|
|
|
this._refreshUserNewAddressedToMessageCount,
|
|
|
|
ttlSeconds
|
|
|
|
);
|
2022-05-12 02:30:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_wrapUserRefreshWithCachedTTL(client, statName, updateMethod, ttlSeconds) {
|
|
|
|
client.statLogRefreshCache = client.statLogRefreshCache || new Map();
|
|
|
|
|
|
|
|
const now = Math.floor(Date.now() / 1000);
|
|
|
|
const old = client.statLogRefreshCache.get(statName) || 0;
|
|
|
|
if (now < old + ttlSeconds) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateMethod(client);
|
|
|
|
client.statLogRefreshCache.set(statName, now);
|
|
|
|
}
|
|
|
|
|
|
|
|
_refreshUserPrivateMailCount(client) {
|
|
|
|
const MsgArea = require('./message_area');
|
2022-06-12 20:12:03 +00:00
|
|
|
MsgArea.getNewMessageCountInAreaForUser(
|
|
|
|
client.user.userId,
|
|
|
|
Message.WellKnownAreaTags.Private,
|
|
|
|
(err, count) => {
|
|
|
|
if (!err) {
|
|
|
|
client.user.setProperty(UserProps.NewPrivateMailCount, count);
|
|
|
|
}
|
2022-05-12 02:30:25 +00:00
|
|
|
}
|
2022-06-12 20:12:03 +00:00
|
|
|
);
|
2022-05-12 02:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_refreshUserNewAddressedToMessageCount(client) {
|
|
|
|
const MsgArea = require('./message_area');
|
|
|
|
MsgArea.getNewMessageCountAddressedToUser(client, (err, count) => {
|
2022-06-12 20:12:03 +00:00
|
|
|
if (!err) {
|
2022-05-12 02:30:25 +00:00
|
|
|
client.user.setProperty(UserProps.NewAddressedToMessageCount, count);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-26 22:53:21 +00:00
|
|
|
_findLogEntries(logTable, filter, cb) {
|
|
|
|
filter = filter || {};
|
2022-06-12 20:12:03 +00:00
|
|
|
if (!_.isString(filter.logName)) {
|
2020-11-26 22:53:21 +00:00
|
|
|
return cb(Errors.MissingParam('filter.logName is required'));
|
|
|
|
}
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
filter.resultType = filter.resultType || 'obj';
|
|
|
|
filter.order = filter.order || 'timestamp';
|
2020-11-26 22:53:21 +00:00
|
|
|
|
|
|
|
let sql;
|
2022-06-12 20:12:03 +00:00
|
|
|
if ('count' === filter.resultType) {
|
|
|
|
sql = `SELECT COUNT() AS count
|
2020-11-26 22:53:21 +00:00
|
|
|
FROM ${logTable}`;
|
|
|
|
} else {
|
2022-06-12 20:12:03 +00:00
|
|
|
sql = `SELECT timestamp, log_value
|
2020-11-26 22:53:21 +00:00
|
|
|
FROM ${logTable}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
sql += ' WHERE log_name = ?';
|
|
|
|
|
|
|
|
if (_.isNumber(filter.userId)) {
|
|
|
|
sql += ` AND user_id = ${filter.userId}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filter.sessionId) {
|
|
|
|
sql += ` AND session_id = ${filter.sessionId}`;
|
|
|
|
}
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
if (filter.date) {
|
2020-11-26 22:53:21 +00:00
|
|
|
filter.date = moment(filter.date);
|
2022-06-12 20:12:03 +00:00
|
|
|
sql += ` AND DATE(timestamp, "localtime") = DATE("${filter.date.format(
|
|
|
|
'YYYY-MM-DD'
|
|
|
|
)}")`;
|
2020-11-26 22:53:21 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
if ('count' !== filter.resultType) {
|
|
|
|
switch (filter.order) {
|
|
|
|
case 'timestamp':
|
|
|
|
case 'timestamp_asc':
|
2020-11-26 22:53:21 +00:00
|
|
|
sql += ' ORDER BY timestamp ASC';
|
|
|
|
break;
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
case 'timestamp_desc':
|
2020-11-26 22:53:21 +00:00
|
|
|
sql += ' ORDER BY timestamp DESC';
|
|
|
|
break;
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
case 'random':
|
2020-11-26 22:53:21 +00:00
|
|
|
sql += ' ORDER BY RANDOM()';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
if (_.isNumber(filter.limit) && 0 !== filter.limit) {
|
2020-11-26 22:53:21 +00:00
|
|
|
sql += ` LIMIT ${filter.limit}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
sql += ';';
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
if ('count' === filter.resultType) {
|
|
|
|
sysDb.get(sql, [filter.logName], (err, row) => {
|
2020-11-26 22:53:21 +00:00
|
|
|
return cb(err, row ? row.count : 0);
|
|
|
|
});
|
|
|
|
} else {
|
2022-06-12 20:12:03 +00:00
|
|
|
sysDb.all(sql, [filter.logName], (err, rows) => {
|
2020-11-26 22:53:21 +00:00
|
|
|
return cb(err, rows);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-07-28 03:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new StatLog();
|