diff --git a/lib/logs.js b/lib/logs.js index 52073ad4..11bcee12 100644 --- a/lib/logs.js +++ b/lib/logs.js @@ -54,6 +54,14 @@ function update (deviceId, logLines) { return _.mapKeys(_.snakeCase, formatted) }, logLines) const sql = pgp.helpers.insert(logs, cs) + 'on conflict do nothing' + + return db.none(sql) +} + +function clearOldLogs () { + const sql = `delete from logs + where timestamp < now() - interval '3 days'` + return db.none(sql) } @@ -87,4 +95,4 @@ function getMachineLogs (deviceId, until = new Date().toISOString()) { })) } -module.exports = { getUnlimitedMachineLogs, getMachineLogs, update, getLastSeen } +module.exports = { getUnlimitedMachineLogs, getMachineLogs, update, getLastSeen, clearOldLogs } diff --git a/lib/plugins.js b/lib/plugins.js index 473140a8..1e2b274c 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -9,6 +9,7 @@ const BN = require('./bn') const dbm = require('./postgresql_interface') const db = require('./db') const logger = require('./logger') +const logs = require('./logs') const T = require('./time') const configManager = require('./config-manager') const ticker = require('./ticker') @@ -412,17 +413,15 @@ function plugins (settings, deviceId) { return sendTransactionMessage(rec) } - function pong () { - db.none('insert into server_events (event_type) values ($1)', ['ping']) + function clearOldLogs () { + return logs.clearOldLogs() .catch(logger.error) } - function pongClear () { - const sql = `delete from server_events - where event_type=$1 - and created < now() - interval $2` - - db.none(sql, ['ping', PONG_TTL]) + function pong () { + return db.none(`UPDATE server_events SET created=now() WHERE event_type=$1; + INSERT INTO server_events (event_type) SELECT $1 + WHERE NOT EXISTS (SELECT 1 FROM server_events WHERE event_type=$1);`, ['ping']) .catch(logger.error) } @@ -778,7 +777,7 @@ function plugins (settings, deviceId) { getPhoneCode, executeTrades, pong, - pongClear, + clearOldLogs, notifyConfirmation, sweepHd, sendMessage, diff --git a/lib/poller.js b/lib/poller.js index 68038bcd..d7795e54 100644 --- a/lib/poller.js +++ b/lib/poller.js @@ -17,7 +17,7 @@ const UNNOTIFIED_INTERVAL = 10 * T.seconds const SWEEP_HD_INTERVAL = T.minute const TRADE_INTERVAL = 60 * T.seconds const PONG_INTERVAL = 10 * T.seconds -const PONG_CLEAR_INTERVAL = 1 * T.day +const LOGS_CLEAR_INTERVAL = 1 * T.day const SANCTIONS_INITIAL_DOWNLOAD_INTERVAL = 5 * T.minutes const SANCTIONS_UPDATE_INTERVAL = 1 * T.week const RADAR_UPDATE_INTERVAL = 5 * T.minutes @@ -70,7 +70,7 @@ function start (__settings) { pi().executeTrades() pi().pong() - pi().pongClear() + pi().clearOldLogs() cashOutTx.monitorLiveIncoming(settings()) cashOutTx.monitorStaleIncoming(settings()) cashOutTx.monitorUnnotified(settings()) @@ -85,7 +85,7 @@ function start (__settings) { setInterval(() => cashInTx.monitorPending(settings()), PENDING_INTERVAL) setInterval(() => pi().sweepHd(), SWEEP_HD_INTERVAL) setInterval(() => pi().pong(), PONG_INTERVAL) - setInterval(() => pi().pongClear(), PONG_CLEAR_INTERVAL) + setInterval(() => pi().clearOldLogs(), LOGS_CLEAR_INTERVAL) setInterval(() => notifier.checkNotification(pi()), CHECK_NOTIFICATION_INTERVAL) setInterval(initialSanctionsDownload, SANCTIONS_INITIAL_DOWNLOAD_INTERVAL) setInterval(updateAndLoadSanctions, SANCTIONS_UPDATE_INTERVAL) diff --git a/lib/postgresql_interface.js b/lib/postgresql_interface.js index c6e523b2..aa8cc126 100644 --- a/lib/postgresql_interface.js +++ b/lib/postgresql_interface.js @@ -42,12 +42,10 @@ exports.machineEvent = function machineEvent (rec) { const values = [rec.id, rec.deviceId, rec.eventType, rec.note, rec.deviceTime] const deleteSql = `delete from machine_events - where device_id=$1 - and event_type=$2 - and created < now() - interval '2 days'` + where created < now() - interval '1 days'` return db.none(sql, values) - .then(() => db.none(deleteSql, [rec.deviceId, rec.eventType])) + .then(() => db.none(deleteSql)) } exports.machineEvents = function machineEvents () { diff --git a/migrations/1556157018569-truncate-server-events.js b/migrations/1556157018569-truncate-server-events.js new file mode 100644 index 00000000..cf62bf47 --- /dev/null +++ b/migrations/1556157018569-truncate-server-events.js @@ -0,0 +1,13 @@ +const db = require('./db') + +exports.up = function (next) { + var sql = [ + 'TRUNCATE TABLE server_events' + ] + + db.multi(sql, next) +} + +exports.down = function (next) { + next() +}