From fe85a86276a189b3c574d7ac904b84716de04dad Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 28 Dec 2025 04:52:44 +0100 Subject: [PATCH] Improved metrics updating --- next.config.ts | 2 +- src/app/api/metrics/route.ts | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/next.config.ts b/next.config.ts index e9ffa30..57d0761 100644 --- a/next.config.ts +++ b/next.config.ts @@ -1,7 +1,7 @@ import type { NextConfig } from "next"; const nextConfig: NextConfig = { - /* config options here */ + serverExternalPackages: ["ssh2", "docker-modem", "dockerode"], }; export default nextConfig; diff --git a/src/app/api/metrics/route.ts b/src/app/api/metrics/route.ts index 2e0681b..a727fcd 100644 --- a/src/app/api/metrics/route.ts +++ b/src/app/api/metrics/route.ts @@ -25,6 +25,13 @@ const usageHours: (Usage & { time: string })[] = new Array(12).fill({ var hr = 0; +function getTime() { + const now = new Date(); + const hours = String(now.getHours()).padStart(2, "0"); + const minutes = String(now.getMinutes()).padStart(2, "0"); + return `${hours}:${minutes}`; +} + setInterval(async () => { const [mem, load, network] = await Promise.all([ si.mem(), @@ -32,21 +39,19 @@ setInterval(async () => { si.networkStats(), ]); - usageSecs.push({ + const sec = { cpu: load.currentLoad, memory: ((mem.total - mem.available) / mem.total) * 100, in: average(network, (n) => n.rx_bytes), out: average(network, (n) => n.tx_bytes), - }); - - if (usageSecs.length >= 60) { - const now = new Date(); - const hours = String(now.getHours()).padStart(2, "0"); - const minutes = String(now.getMinutes()).padStart(2, "0"); - const time = `${hours}:${minutes}`; + }; + usageSecs.push(sec); + if (usageSecs.length === 10) { + usageHours[hr] = { time: getTime(), ...sec }; + } else if (usageSecs.length >= 60) { usageMins.push({ - time, + time: getTime(), cpu: average(usageSecs, (s) => s.cpu), memory: average(usageSecs, (s) => s.memory), in: average(usageSecs, (s) => s.in), @@ -54,6 +59,7 @@ setInterval(async () => { }); if (usageMins.length >= 60) { + const time = getTime(); if (hr >= 12) { usageHours.shift(); usageHours.push({