From 6e9d425bda700fbbf96022f5e57e3a0f7c0c6103 Mon Sep 17 00:00:00 2001 From: Anton Roslund Date: Mon, 31 Mar 2025 21:11:49 +0200 Subject: [PATCH] add most active nodes stats --- src/stats.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/stats.js b/src/stats.js index c45418c..47c8e05 100644 --- a/src/stats.js +++ b/src/stats.js @@ -117,4 +117,28 @@ router.get('/position-precision', async (req, res) => { } }); +router.get('/most-active-nodes', async (req, res) => { + try { + const result = await prisma.$queryRaw` + SELECT n.long_name, COUNT(*) AS count + FROM service_envelopes se + JOIN nodes n ON se.from = n.node_id + WHERE + se.created_at >= NOW() - INTERVAL 1 DAY + AND se.mqtt_topic NOT LIKE '%/map/' + GROUP BY n.long_name + ORDER BY count DESC + LIMIT 25; + `; + + res.set('Cache-Control', 'public, max-age=600'); // 10 min cache + res.json(result); + + } catch (error) { + console.error('Error fetching data:', error); + res.status(500).json({ error: 'Internal Server Error' }); + } +}); + + module.exports = router; \ No newline at end of file