implement purging old device metrics after configured duration
This commit is contained in:
33
src/mqtt.js
33
src/mqtt.js
@ -78,6 +78,11 @@ const optionsList = [
|
||||
type: Number,
|
||||
description: "How long to wait between each automatic database purge.",
|
||||
},
|
||||
{
|
||||
name: "purge-device-metrics-after-seconds",
|
||||
type: Number,
|
||||
description: "Device Metrics older than this many seconds will be purged from the database.",
|
||||
},
|
||||
{
|
||||
name: "purge-nodes-unheard-for-seconds",
|
||||
type: Number,
|
||||
@ -125,6 +130,7 @@ const decryptionKeys = options["decryption-keys"] ?? [
|
||||
];
|
||||
const purgeIntervalSeconds = options["purge-interval-seconds"] ?? 10;
|
||||
const purgeNodesUnheardForSeconds = options["purge-nodes-unheard-for-seconds"] ?? null;
|
||||
const purgeDeviceMetricsAfterSeconds = options["purge-device-metrics-after-seconds"] ?? null;
|
||||
const purgePositionsAfterSeconds = options["purge-positions-after-seconds"] ?? null;
|
||||
|
||||
// create mqtt client
|
||||
@ -151,6 +157,7 @@ const Waypoint = root.lookupType("Waypoint");
|
||||
if(purgeIntervalSeconds){
|
||||
setInterval(async () => {
|
||||
await purgeUnheardNodes();
|
||||
await purgeOldDeviceMetrics();
|
||||
await purgeOldPositions();
|
||||
}, purgeIntervalSeconds * 1000);
|
||||
}
|
||||
@ -181,6 +188,32 @@ async function purgeUnheardNodes() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges all device metrics from the database that are older than the configured timeframe.
|
||||
*/
|
||||
async function purgeOldDeviceMetrics() {
|
||||
|
||||
// make sure seconds provided
|
||||
if(!purgeDeviceMetricsAfterSeconds){
|
||||
return;
|
||||
}
|
||||
|
||||
// delete all device metrics that are older than the configured purge time
|
||||
try {
|
||||
await prisma.deviceMetric.deleteMany({
|
||||
where: {
|
||||
created_at: {
|
||||
// last updated before x seconds ago
|
||||
lt: new Date(Date.now() - purgeDeviceMetricsAfterSeconds * 1000),
|
||||
},
|
||||
}
|
||||
});
|
||||
} catch(e) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges all positions from the database that are older than the configured timeframe.
|
||||
*/
|
||||
|
Reference in New Issue
Block a user