implement purging old power metrics after configured duration
This commit is contained in:
33
src/mqtt.js
33
src/mqtt.js
@ -88,6 +88,11 @@ const optionsList = [
|
||||
type: Number,
|
||||
description: "Environment Metrics older than this many seconds will be purged from the database.",
|
||||
},
|
||||
{
|
||||
name: "purge-power-metrics-after-seconds",
|
||||
type: Number,
|
||||
description: "Power Metrics older than this many seconds will be purged from the database.",
|
||||
},
|
||||
{
|
||||
name: "purge-nodes-unheard-for-seconds",
|
||||
type: Number,
|
||||
@ -137,6 +142,7 @@ 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 purgeEnvironmentMetricsAfterSeconds = options["purge-environment-metrics-after-seconds"] ?? null;
|
||||
const purgePowerMetricsAfterSeconds = options["purge-power-metrics-after-seconds"] ?? null;
|
||||
const purgePositionsAfterSeconds = options["purge-positions-after-seconds"] ?? null;
|
||||
|
||||
// create mqtt client
|
||||
@ -165,6 +171,7 @@ if(purgeIntervalSeconds){
|
||||
await purgeUnheardNodes();
|
||||
await purgeOldDeviceMetrics();
|
||||
await purgeOldEnvironmentMetrics();
|
||||
await purgeOldPowerMetrics();
|
||||
await purgeOldPositions();
|
||||
}, purgeIntervalSeconds * 1000);
|
||||
}
|
||||
@ -247,6 +254,32 @@ async function purgeOldEnvironmentMetrics() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges all power metrics from the database that are older than the configured timeframe.
|
||||
*/
|
||||
async function purgeOldPowerMetrics() {
|
||||
|
||||
// make sure seconds provided
|
||||
if(!purgePowerMetricsAfterSeconds){
|
||||
return;
|
||||
}
|
||||
|
||||
// delete all power metrics that are older than the configured purge time
|
||||
try {
|
||||
await prisma.powerMetric.deleteMany({
|
||||
where: {
|
||||
created_at: {
|
||||
// last updated before x seconds ago
|
||||
lt: new Date(Date.now() - purgePowerMetricsAfterSeconds * 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