diff --git a/src/index.js b/src/index.js index 02c77cf..236d8dd 100644 --- a/src/index.js +++ b/src/index.js @@ -84,6 +84,49 @@ app.get('/api/v1/nodes', async (req, res) => { } }); +app.get('/api/v1/nodes/:nodeId/device-metrics', async (req, res) => { + try { + + const nodeId = parseInt(req.params.nodeId); + const count = req.query.count ? parseInt(req.query.count) : undefined; + + // 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 latest device metrics + const deviceMetrics = await prisma.deviceMetric.findMany({ + where: { + node_id: node.node_id, + }, + orderBy: { + id: 'desc', + }, + take: count, + }); + + res.json({ + device_metrics: deviceMetrics, + }); + + } 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 { diff --git a/src/public/index.html b/src/public/index.html index b9da579..591754b 100644 --- a/src/public/index.html +++ b/src/public/index.html @@ -28,6 +28,9 @@ + + +