dont save duplicate device metrics received in the last 15 seconds
This commit is contained in:
parent
b812d730dc
commit
9bceb14c4a
1 changed files with 23 additions and 4 deletions
27
src/mqtt.js
27
src/mqtt.js
|
|
@ -223,15 +223,34 @@ client.on("message", async (topic, message) => {
|
|||
|
||||
// create device metric
|
||||
try {
|
||||
await prisma.deviceMetric.create({
|
||||
data: {
|
||||
|
||||
// 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({
|
||||
data: {
|
||||
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,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue