improve ping db pruning

This commit is contained in:
Josh Harvey 2016-12-06 02:57:40 +02:00
parent d38ee58ec4
commit 0936874c78

View file

@ -244,16 +244,20 @@ exports.cartridgeCounts = function cartridgeCounts (deviceId) {
})
}
// Note: since we only prune on insert, we'll always have
// last known state.
exports.machineEvent = function machineEvent (rec) {
const TTL = 2 * 60 * 60 * 1000
const fields = ['id', 'device_id', 'event_type', 'note', 'device_time']
const sql = getInsertQuery('machine_events', fields)
const values = [rec.id, rec.deviceId, rec.eventType, rec.note, rec.deviceTime]
const deleteSql = 'DELETE FROM machine_events WHERE (EXTRACT(EPOCH FROM (now() - created))) * 1000 > $1'
const deleteValues = [TTL]
const deleteSql = `delete from machine_events
where device_id=$1
and event_type=$2
and created < now() - interval '2 hours'`
return db.none(sql, values)
.then(() => db.none(deleteSql, deleteValues))
.then(() => db.none(deleteSql, [rec.deviceId, rec.eventType]))
}
exports.devices = function devices () {