Improved metrics updating

This commit is contained in:
2025-12-28 04:52:44 +01:00
parent 95806f04c4
commit fe85a86276
2 changed files with 16 additions and 10 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
import type { NextConfig } from "next"; import type { NextConfig } from "next";
const nextConfig: NextConfig = { const nextConfig: NextConfig = {
/* config options here */ serverExternalPackages: ["ssh2", "docker-modem", "dockerode"],
}; };
export default nextConfig; export default nextConfig;
+15 -9
View File
@@ -25,6 +25,13 @@ const usageHours: (Usage & { time: string })[] = new Array(12).fill({
var hr = 0; 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 () => { setInterval(async () => {
const [mem, load, network] = await Promise.all([ const [mem, load, network] = await Promise.all([
si.mem(), si.mem(),
@@ -32,21 +39,19 @@ setInterval(async () => {
si.networkStats(), si.networkStats(),
]); ]);
usageSecs.push({ const sec = {
cpu: load.currentLoad, cpu: load.currentLoad,
memory: ((mem.total - mem.available) / mem.total) * 100, memory: ((mem.total - mem.available) / mem.total) * 100,
in: average(network, (n) => n.rx_bytes), in: average(network, (n) => n.rx_bytes),
out: average(network, (n) => n.tx_bytes), out: average(network, (n) => n.tx_bytes),
}); };
usageSecs.push(sec);
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}`;
if (usageSecs.length === 10) {
usageHours[hr] = { time: getTime(), ...sec };
} else if (usageSecs.length >= 60) {
usageMins.push({ usageMins.push({
time, time: getTime(),
cpu: average(usageSecs, (s) => s.cpu), cpu: average(usageSecs, (s) => s.cpu),
memory: average(usageSecs, (s) => s.memory), memory: average(usageSecs, (s) => s.memory),
in: average(usageSecs, (s) => s.in), in: average(usageSecs, (s) => s.in),
@@ -54,6 +59,7 @@ setInterval(async () => {
}); });
if (usageMins.length >= 60) { if (usageMins.length >= 60) {
const time = getTime();
if (hr >= 12) { if (hr >= 12) {
usageHours.shift(); usageHours.shift();
usageHours.push({ usageHours.push({