Update Chanelutilization

This commit is contained in:
Anton Roslund 2025-09-25 20:17:42 +02:00
parent 7df3908c4b
commit 6682131cf1
3 changed files with 47 additions and 13 deletions

View file

@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE `channel_utilization_stats` ADD COLUMN `channel_id` VARCHAR(191) NULL;
-- CreateIndex
CREATE INDEX `channel_utilization_stats_channel_id_idx` ON `channel_utilization_stats`(`channel_id`);

View file

@ -341,7 +341,9 @@ model ChannelUtilizationStats {
id BigInt @id @default(autoincrement()) id BigInt @id @default(autoincrement())
recorded_at DateTime? @default(now()) recorded_at DateTime? @default(now())
avg_channel_utilization Decimal? avg_channel_utilization Decimal?
channel_id String?
@@index([channel_id])
@@index([recorded_at]) @@index([recorded_at])
@@map("channel_utilization_stats") @@map("channel_utilization_stats")
} }

View file

@ -185,21 +185,48 @@ router.get('/battery-stats', async (req, res) => {
}); });
router.get('/channel-utilization-stats', async (req, res) => { router.get('/channel-utilization-stats', async (req, res) => {
const days = parseInt(req.query.days || '1', 10); const days = parseInt(req.query.days || '1', 10);
const channelId = req.query.channel_id; // optional string
try { try {
const stats = await prisma.$queryRaw` const stats = await prisma.$queryRaw(
SELECT recorded_at, avg_channel_utilization Prisma.sql`
FROM channel_utilization_stats SELECT recorded_at, channel_id, avg_channel_utilization
WHERE recorded_at >= NOW() - INTERVAL ${days} DAY FROM channel_utilization_stats
ORDER BY recorded_at DESC; WHERE recorded_at >= NOW() - INTERVAL ${days} DAY
`; ${channelId ? Prisma.sql`AND channel_id = ${channelId}` : Prisma.sql``}
ORDER BY recorded_at DESC;
res.json(stats); `
);
res.json(stats);
} catch (err) { } catch (err) {
console.error('Error fetching channel utilization stats:', err); console.error('Error fetching channel utilization stats:', err);
res.status(500).json({ error: 'Internal server error' }); res.status(500).json({ error: 'Internal server error' });
} }
}); });
router.get('/channel-utilization', async (req, res) => {
const channelId = req.query.channel_id;
try {
const snapshot = await prisma.$queryRaw(
Prisma.sql`
SELECT recorded_at, channel_id, avg_channel_utilization
FROM channel_utilization_stats
WHERE recorded_at = (
SELECT MAX(recorded_at) FROM channel_utilization_stats
)
${channelId ? Prisma.sql`AND channel_id = ${channelId}` : Prisma.sql``}
ORDER BY channel_id;
`
);
res.json(snapshot);
} catch (err) {
console.error('Error fetching latest channel utilization:', err);
res.status(500).json({ error: 'Internal server error' });
}
});
module.exports = router; module.exports = router;