From eb2cf89d77d0bee70c59bbd8e95ee58450d8066e Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sat, 16 Mar 2024 19:07:02 +1300 Subject: [PATCH] add api to fetch mqtt topics published to by node --- src/index.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/index.js b/src/index.js index 236d8dd..95b42fd 100644 --- a/src/index.js +++ b/src/index.js @@ -127,6 +127,61 @@ app.get('/api/v1/nodes/:nodeId/device-metrics', async (req, res) => { } }); +app.get('/api/v1/nodes/:nodeId/mqtt-topics', async (req, res) => { + try { + + const nodeId = parseInt(req.params.nodeId); + + // find node + const node = await prisma.node.findFirst({ + where: { + node_id: nodeId, + }, + }); + + // make sure node exists + if(!node){ + res.status(404).json({ + message: "Not Found", + }); + } + + // get list of unique mqtt topics published to by this node + const queryResult = await prisma.$queryRaw`SELECT + gateway_id, + JSON_ARRAYAGG(mqtt_topic) AS unique_mqtt_topics + FROM ( + SELECT + gateway_id, + mqtt_topic + FROM + service_envelopes + GROUP BY + gateway_id, mqtt_topic + ) AS subquery + WHERE + gateway_id is not null + and gateway_id = ${nodeId} + GROUP BY + gateway_id + ORDER BY + COUNT(*) DESC;`; + + // get result from query + const uniqueMqttTopics = queryResult[0]?.unique_mqtt_topics ?? []; + + res.json({ + mqtt_topics: uniqueMqttTopics, + }); + + } catch(err) { + console.error(err); + res.status(500).json({ + message: "Something went wrong, try again later.", + }); + } +}); + app.get('/api/v1/stats/hardware-models', async (req, res) => { try {