implement purging old text messages after configured duration

This commit is contained in:
liamcottle
2024-07-05 23:48:52 +12:00
parent c34c56fd41
commit 957c349870

View File

@ -103,6 +103,11 @@ const optionsList = [
type: Number, type: Number,
description: "Positions older than this many seconds will be purged from the database.", description: "Positions older than this many seconds will be purged from the database.",
}, },
{
name: "purge-text-messages-after-seconds",
type: Number,
description: "Text Messages older than this many seconds will be purged from the database.",
},
]; ];
// parse command line args // parse command line args
@ -144,6 +149,7 @@ const purgeDeviceMetricsAfterSeconds = options["purge-device-metrics-after-secon
const purgeEnvironmentMetricsAfterSeconds = options["purge-environment-metrics-after-seconds"] ?? null; const purgeEnvironmentMetricsAfterSeconds = options["purge-environment-metrics-after-seconds"] ?? null;
const purgePowerMetricsAfterSeconds = options["purge-power-metrics-after-seconds"] ?? null; const purgePowerMetricsAfterSeconds = options["purge-power-metrics-after-seconds"] ?? null;
const purgePositionsAfterSeconds = options["purge-positions-after-seconds"] ?? null; const purgePositionsAfterSeconds = options["purge-positions-after-seconds"] ?? null;
const purgeTextMessagesAfterSeconds = options["purge-text-messages-after-seconds"] ?? null;
// create mqtt client // create mqtt client
const client = mqtt.connect(mqttBrokerUrl, { const client = mqtt.connect(mqttBrokerUrl, {
@ -173,6 +179,7 @@ if(purgeIntervalSeconds){
await purgeOldEnvironmentMetrics(); await purgeOldEnvironmentMetrics();
await purgeOldPowerMetrics(); await purgeOldPowerMetrics();
await purgeOldPositions(); await purgeOldPositions();
await purgeOldTextMessages();
}, purgeIntervalSeconds * 1000); }, purgeIntervalSeconds * 1000);
} }
@ -306,6 +313,32 @@ async function purgeOldPositions() {
} }
/**
* Purges all text messages from the database that are older than the configured timeframe.
*/
async function purgeOldTextMessages() {
// make sure seconds provided
if(!purgeTextMessagesAfterSeconds){
return;
}
// delete all text messages that are older than the configured purge time
try {
await prisma.textMessage.deleteMany({
where: {
created_at: {
// last updated before x seconds ago
lt: new Date(Date.now() - purgeTextMessagesAfterSeconds * 1000),
},
}
});
} catch(e) {
// do nothing
}
}
function createNonce(packetId, fromNode) { function createNonce(packetId, fromNode) {
// Expand packetId to 64 bits // Expand packetId to 64 bits