dont save duplicate device metrics received in the last 15 seconds

This commit is contained in:
liamcottle
2024-03-14 01:02:00 +13:00
parent b812d730dc
commit 9bceb14c4a

View File

@ -223,6 +223,23 @@ client.on("message", async (topic, message) => {
// create device metric // create device metric
try { try {
// find an existing metric with duplicate information created in the last 15 seconds
const existingDuplicateDeviceMetric = await prisma.deviceMetric.findFirst({
where: {
node_id: envelope.packet.from,
battery_level: data.battery_level,
voltage: data.voltage,
channel_utilization: data.channel_utilization,
air_util_tx: data.air_util_tx,
created_at: {
gte: new Date(Date.now() - 15000), // created in the last 15 seconds
},
}
})
// create metric if no duplicates found
if(!existingDuplicateDeviceMetric){
await prisma.deviceMetric.create({ await prisma.deviceMetric.create({
data: { data: {
node_id: envelope.packet.from, node_id: envelope.packet.from,
@ -232,6 +249,8 @@ client.on("message", async (topic, message) => {
air_util_tx: data.air_util_tx, air_util_tx: data.air_util_tx,
}, },
}); });
}
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }