23 lines
517 B
JavaScript
23 lines
517 B
JavaScript
import { cpus } from "node:os";
|
|
|
|
export async function compute() {
|
|
const cpuList = cpus();
|
|
|
|
let totalIdle = 0;
|
|
let totalTick = 0;
|
|
for (const cpu of cpuList) {
|
|
for (const [, time] of Object.entries(cpu.times)) {
|
|
totalTick += time;
|
|
}
|
|
totalIdle += cpu.times.idle;
|
|
}
|
|
|
|
const loadPercent = totalTick === 0 ? 0 : ((totalTick - totalIdle) / totalTick) * 100;
|
|
|
|
return {
|
|
model: cpuList[0]?.model ?? "unknown",
|
|
loadPercent: Math.round(loadPercent * 100) / 100,
|
|
ts: Date.now(),
|
|
};
|
|
}
|