implement purging waypoints

This commit is contained in:
liamcottle
2024-08-21 00:33:14 +12:00
parent 32d8019d61
commit d4b67aebde

View File

@ -113,6 +113,11 @@ const optionsList = [
type: Number, type: Number,
description: "Text Messages older than this many seconds will be purged from the database.", description: "Text Messages older than this many seconds will be purged from the database.",
}, },
{
name: "purge-waypoints-after-seconds",
type: Number,
description: "Waypoints older than this many seconds will be purged from the database.",
},
]; ];
// parse command line args // parse command line args
@ -156,6 +161,7 @@ const purgeMapReportsAfterSeconds = options["purge-map-reports-after-seconds"] ?
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; const purgeTextMessagesAfterSeconds = options["purge-text-messages-after-seconds"] ?? null;
const purgeWaypointsAfterSeconds = options["purge-waypoints-after-seconds"] ?? null;
// create mqtt client // create mqtt client
const client = mqtt.connect(mqttBrokerUrl, { const client = mqtt.connect(mqttBrokerUrl, {
@ -187,6 +193,7 @@ if(purgeIntervalSeconds){
await purgeOldPowerMetrics(); await purgeOldPowerMetrics();
await purgeOldPositions(); await purgeOldPositions();
await purgeOldTextMessages(); await purgeOldTextMessages();
await purgeOldWaypoints();
}, purgeIntervalSeconds * 1000); }, purgeIntervalSeconds * 1000);
} }
@ -231,7 +238,7 @@ async function purgeOldDeviceMetrics() {
await prisma.deviceMetric.deleteMany({ await prisma.deviceMetric.deleteMany({
where: { where: {
created_at: { created_at: {
// last updated before x seconds ago // created before x seconds ago
lt: new Date(Date.now() - purgeDeviceMetricsAfterSeconds * 1000), lt: new Date(Date.now() - purgeDeviceMetricsAfterSeconds * 1000),
}, },
} }
@ -257,7 +264,7 @@ async function purgeOldEnvironmentMetrics() {
await prisma.environmentMetric.deleteMany({ await prisma.environmentMetric.deleteMany({
where: { where: {
created_at: { created_at: {
// last updated before x seconds ago // created before x seconds ago
lt: new Date(Date.now() - purgeEnvironmentMetricsAfterSeconds * 1000), lt: new Date(Date.now() - purgeEnvironmentMetricsAfterSeconds * 1000),
}, },
} }
@ -309,7 +316,7 @@ async function purgeOldPowerMetrics() {
await prisma.powerMetric.deleteMany({ await prisma.powerMetric.deleteMany({
where: { where: {
created_at: { created_at: {
// last updated before x seconds ago // created before x seconds ago
lt: new Date(Date.now() - purgePowerMetricsAfterSeconds * 1000), lt: new Date(Date.now() - purgePowerMetricsAfterSeconds * 1000),
}, },
} }
@ -335,7 +342,7 @@ async function purgeOldPositions() {
await prisma.position.deleteMany({ await prisma.position.deleteMany({
where: { where: {
created_at: { created_at: {
// last updated before x seconds ago // created before x seconds ago
lt: new Date(Date.now() - purgePositionsAfterSeconds * 1000), lt: new Date(Date.now() - purgePositionsAfterSeconds * 1000),
}, },
} }
@ -361,7 +368,7 @@ async function purgeOldTextMessages() {
await prisma.textMessage.deleteMany({ await prisma.textMessage.deleteMany({
where: { where: {
created_at: { created_at: {
// last updated before x seconds ago // created before x seconds ago
lt: new Date(Date.now() - purgeTextMessagesAfterSeconds * 1000), lt: new Date(Date.now() - purgeTextMessagesAfterSeconds * 1000),
}, },
} }
@ -372,6 +379,32 @@ async function purgeOldTextMessages() {
} }
/**
* Purges all waypoints from the database that are older than the configured timeframe.
*/
async function purgeOldWaypoints() {
// make sure seconds provided
if(!purgeWaypointsAfterSeconds){
return;
}
// delete all waypoints that are older than the configured purge time
try {
await prisma.waypoint.deleteMany({
where: {
created_at: {
// created before x seconds ago
lt: new Date(Date.now() - purgeWaypointsAfterSeconds * 1000),
},
}
});
} catch(e) {
// do nothing
}
}
function createNonce(packetId, fromNode) { function createNonce(packetId, fromNode) {
// Expand packetId to 64 bits // Expand packetId to 64 bits