implement purging old environment metrics after configured duration

This commit is contained in:
liamcottle
2024-06-07 10:52:23 +12:00
parent e37d386d1a
commit e5cc893449

View File

@ -83,6 +83,11 @@ const optionsList = [
type: Number, type: Number,
description: "Device Metrics older than this many seconds will be purged from the database.", description: "Device Metrics older than this many seconds will be purged from the database.",
}, },
{
name: "purge-environment-metrics-after-seconds",
type: Number,
description: "Environment Metrics older than this many seconds will be purged from the database.",
},
{ {
name: "purge-nodes-unheard-for-seconds", name: "purge-nodes-unheard-for-seconds",
type: Number, type: Number,
@ -131,6 +136,7 @@ const decryptionKeys = options["decryption-keys"] ?? [
const purgeIntervalSeconds = options["purge-interval-seconds"] ?? 10; const purgeIntervalSeconds = options["purge-interval-seconds"] ?? 10;
const purgeNodesUnheardForSeconds = options["purge-nodes-unheard-for-seconds"] ?? null; const purgeNodesUnheardForSeconds = options["purge-nodes-unheard-for-seconds"] ?? null;
const purgeDeviceMetricsAfterSeconds = options["purge-device-metrics-after-seconds"] ?? null; const purgeDeviceMetricsAfterSeconds = options["purge-device-metrics-after-seconds"] ?? null;
const purgeEnvironmentMetricsAfterSeconds = options["purge-environment-metrics-after-seconds"] ?? null;
const purgePositionsAfterSeconds = options["purge-positions-after-seconds"] ?? null; const purgePositionsAfterSeconds = options["purge-positions-after-seconds"] ?? null;
// create mqtt client // create mqtt client
@ -158,6 +164,7 @@ if(purgeIntervalSeconds){
setInterval(async () => { setInterval(async () => {
await purgeUnheardNodes(); await purgeUnheardNodes();
await purgeOldDeviceMetrics(); await purgeOldDeviceMetrics();
await purgeOldEnvironmentMetrics();
await purgeOldPositions(); await purgeOldPositions();
}, purgeIntervalSeconds * 1000); }, purgeIntervalSeconds * 1000);
} }
@ -214,6 +221,32 @@ async function purgeOldDeviceMetrics() {
} }
/**
* Purges all environment metrics from the database that are older than the configured timeframe.
*/
async function purgeOldEnvironmentMetrics() {
// make sure seconds provided
if(!purgeEnvironmentMetricsAfterSeconds){
return;
}
// delete all environment metrics that are older than the configured purge time
try {
await prisma.environmentMetric.deleteMany({
where: {
created_at: {
// last updated before x seconds ago
lt: new Date(Date.now() - purgeEnvironmentMetricsAfterSeconds * 1000),
},
}
});
} catch(e) {
// do nothing
}
}
/** /**
* Purges all positions from the database that are older than the configured timeframe. * Purges all positions from the database that are older than the configured timeframe.
*/ */