Skip to content

Commit

Permalink
Backend: os-utilsやめる
Browse files Browse the repository at this point in the history
related: #1049
  • Loading branch information
nexryai committed Jul 12, 2024
1 parent 972bb8c commit 0f80ed6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
1 change: 0 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
61 changes: 52 additions & 9 deletions packages/backend/src/daemons/server-stats.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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<void> {
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);
Expand All @@ -42,14 +42,57 @@ export default function() {
}

// CPU STAT
function cpuUsage(): Promise<number> {
function getCpuUsage(): Promise<number> {
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<number | null> {
if (process.platform === "linux") {
return getAvailableMemoryFromProc();
Expand Down
5 changes: 0 additions & 5 deletions packages/backend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 0f80ed6

Please sign in to comment.