collect power metrics
This commit is contained in:
@ -0,0 +1,20 @@
|
|||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE `power_metrics` (
|
||||||
|
`id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||||
|
`node_id` BIGINT NOT NULL,
|
||||||
|
`packet_id` BIGINT NULL,
|
||||||
|
`ch1_voltage` DECIMAL(65, 30) NULL,
|
||||||
|
`ch1_current` DECIMAL(65, 30) NULL,
|
||||||
|
`ch2_voltage` DECIMAL(65, 30) NULL,
|
||||||
|
`ch2_current` DECIMAL(65, 30) NULL,
|
||||||
|
`ch3_voltage` DECIMAL(65, 30) NULL,
|
||||||
|
`ch3_current` DECIMAL(65, 30) NULL,
|
||||||
|
`created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||||
|
`updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||||
|
|
||||||
|
INDEX `power_metrics_created_at_idx`(`created_at`),
|
||||||
|
INDEX `power_metrics_updated_at_idx`(`updated_at`),
|
||||||
|
INDEX `power_metrics_node_id_idx`(`node_id`),
|
||||||
|
INDEX `power_metrics_packet_id_idx`(`packet_id`),
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
@ -139,6 +139,28 @@ model EnvironmentMetric {
|
|||||||
@@map("environment_metrics")
|
@@map("environment_metrics")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
model PowerMetric {
|
||||||
|
id BigInt @id @default(autoincrement())
|
||||||
|
node_id BigInt
|
||||||
|
packet_id BigInt?
|
||||||
|
|
||||||
|
ch1_voltage Decimal?
|
||||||
|
ch1_current Decimal?
|
||||||
|
ch2_voltage Decimal?
|
||||||
|
ch2_current Decimal?
|
||||||
|
ch3_voltage Decimal?
|
||||||
|
ch3_current Decimal?
|
||||||
|
|
||||||
|
created_at DateTime @default(now())
|
||||||
|
updated_at DateTime @default(now()) @updatedAt
|
||||||
|
|
||||||
|
@@index(created_at)
|
||||||
|
@@index(updated_at)
|
||||||
|
@@index(node_id)
|
||||||
|
@@index(packet_id)
|
||||||
|
@@map("power_metrics")
|
||||||
|
}
|
||||||
|
|
||||||
model Position {
|
model Position {
|
||||||
id BigInt @id @default(autoincrement())
|
id BigInt @id @default(autoincrement())
|
||||||
node_id BigInt
|
node_id BigInt
|
||||||
|
47
src/mqtt.js
47
src/mqtt.js
@ -703,6 +703,53 @@ client.on("message", async (topic, message) => {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle power metrics
|
||||||
|
if(telemetry.powerMetrics){
|
||||||
|
|
||||||
|
// get metric values
|
||||||
|
const ch1Voltage = telemetry.powerMetrics.ch1Voltage !== 0 ? telemetry.powerMetrics.ch1Voltage : null;
|
||||||
|
const ch1Current = telemetry.powerMetrics.ch1Current !== 0 ? telemetry.powerMetrics.ch1Current : null;
|
||||||
|
const ch2Voltage = telemetry.powerMetrics.ch2Voltage !== 0 ? telemetry.powerMetrics.ch2Voltage : null;
|
||||||
|
const ch2Current = telemetry.powerMetrics.ch2Current !== 0 ? telemetry.powerMetrics.ch2Current : null;
|
||||||
|
const ch3Voltage = telemetry.powerMetrics.ch3Voltage !== 0 ? telemetry.powerMetrics.ch3Voltage : null;
|
||||||
|
const ch3Current = telemetry.powerMetrics.ch3Current !== 0 ? telemetry.powerMetrics.ch3Current : null;
|
||||||
|
|
||||||
|
// create power metric
|
||||||
|
try {
|
||||||
|
|
||||||
|
// find an existing metric with duplicate information created in the last 15 seconds
|
||||||
|
const existingDuplicatePowerMetric = await prisma.powerMetric.findFirst({
|
||||||
|
where: {
|
||||||
|
node_id: envelope.packet.from,
|
||||||
|
packet_id: envelope.packet.id,
|
||||||
|
created_at: {
|
||||||
|
gte: new Date(Date.now() - 15000), // created in the last 15 seconds
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// create metric if no duplicates found
|
||||||
|
if(!existingDuplicatePowerMetric){
|
||||||
|
await prisma.powerMetric.create({
|
||||||
|
data: {
|
||||||
|
node_id: envelope.packet.from,
|
||||||
|
packet_id: envelope.packet.id,
|
||||||
|
ch1_voltage: ch1Voltage,
|
||||||
|
ch1_current: ch1Current,
|
||||||
|
ch2_voltage: ch2Voltage,
|
||||||
|
ch2_current: ch2Current,
|
||||||
|
ch3_voltage: ch3Voltage,
|
||||||
|
ch3_current: ch3Current,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// update node telemetry in db
|
// update node telemetry in db
|
||||||
if(Object.keys(data).length > 0){
|
if(Object.keys(data).length > 0){
|
||||||
try {
|
try {
|
||||||
|
Reference in New Issue
Block a user