Truncate/trim server tables

This commit is contained in:
Rafael Taranto 2019-04-24 23:05:39 -03:00 committed by Josh Harvey
parent 4640b4a774
commit 67919892e8
5 changed files with 35 additions and 17 deletions

View file

@ -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 }

View file

@ -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,

View file

@ -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)

View file

@ -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 () {

View file

@ -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()
}