diff --git a/packages/backend/package.json b/packages/backend/package.json index a1ee57d4d..b6168c03c 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -76,7 +76,6 @@ "nested-property": "4.0.0", "node-fetch": "3.3.2", "nodemailer": "6.9.13", - "os-utils": "0.0.14", "otpauth": "9.2.4", "parse5": "7.1.2", "pg": "8.11.5", diff --git a/packages/backend/src/daemons/server-stats.ts b/packages/backend/src/daemons/server-stats.ts index c70ccb66a..335229ff6 100644 --- a/packages/backend/src/daemons/server-stats.ts +++ b/packages/backend/src/daemons/server-stats.ts @@ -1,7 +1,7 @@ import Xev from "xev"; -import * as osUtils from "os-utils"; import * as process from "node:process"; import { readFile } from "node:fs"; +import * as os from "os"; const ev = new Xev(); @@ -20,15 +20,15 @@ export default function() { ev.emit(`serverStatsLog:${x.id}`, log.slice(0, x.length || 50)); }); - async function tick() { - const cpu = await cpuUsage(); + async function tick(): Promise { + const cpuUsage = await getCpuUsage(); const memUsage = (await getMemoryUsage() || 0) / 1024 / 1024; const stats = { - cpu: roundCpu(cpu), + cpu: roundCpu(cpuUsage), mem: { used: round(memUsage), - usage: round((memUsage / osUtils.totalmem()) * 100), + usage: round((memUsage / os.totalmem()) * 100), }, }; ev.emit("serverStats", stats); @@ -42,14 +42,57 @@ export default function() { } // CPU STAT -function cpuUsage(): Promise { +function getCpuUsage(): Promise { return new Promise((res, rej) => { - osUtils.cpuUsage((cpuUsage) => { - res(cpuUsage); - }); + try { + const stats1 = getCpu(); + setTimeout(() => { + const stats2 = getCpu(); + const idleDiff = stats2.idle - stats1.idle; + const totalDiff = stats2.total - stats1.total; + + // Prevent division by zero + if (totalDiff === 0) { + res(0); + return; + } + + const usagePercent = 1 - idleDiff / totalDiff; + res(usagePercent); + }, 1000); + } catch (e) { + rej(e); + } }); } +function getCpu(): { idle: number; total: number } { + const cpus = os.cpus(); + let user = 0; + let nice = 0; + let sys = 0; + let idle = 0; + let irq = 0; + + for (const cpu of cpus) { + if (!cpu.times) { + continue; + } + + user += cpu.times.user; + nice += cpu.times.nice; + sys += cpu.times.sys; + idle += cpu.times.idle; + irq += cpu.times.irq; + } + + const total = user + nice + sys + idle + irq; + return { + idle, + total, + }; +} + function getMemoryUsage(): Promise { if (process.platform === "linux") { return getAvailableMemoryFromProc(); diff --git a/packages/backend/yarn.lock b/packages/backend/yarn.lock index 40da00261..3b142a98a 100644 --- a/packages/backend/yarn.lock +++ b/packages/backend/yarn.lock @@ -5272,11 +5272,6 @@ os-filter-obj@^2.0.0: dependencies: arch "^2.1.0" -os-utils@0.0.14: - version "0.0.14" - resolved "https://registry.yarnpkg.com/os-utils/-/os-utils-0.0.14.tgz#29e511697b1982b8c627722175fe39797ef64156" - integrity sha512-ajB8csaHLBvJOYsHJkp8YdO2FvlBbf/ZxaYQwXXRDyQ84UoE+uTuLXxqd0shekXMX6Qr/pt/DDyLMRAMsgfWzg== - otpauth@9.2.4: version "9.2.4" resolved "https://registry.yarnpkg.com/otpauth/-/otpauth-9.2.4.tgz#3b7941a689f63c31db43ab2494d3c2d90bc1f150"