feat: system status helper

- remove pidusage
This commit is contained in:
pk5ls20
2024-12-22 02:24:10 +08:00
parent fb1f122ef7
commit 202129d491
2 changed files with 32 additions and 13 deletions

View File

@@ -31,7 +31,6 @@
"@types/express": "^5.0.0", "@types/express": "^5.0.0",
"@types/fluent-ffmpeg": "^2.1.24", "@types/fluent-ffmpeg": "^2.1.24",
"@types/node": "^22.0.1", "@types/node": "^22.0.1",
"@types/pidusage": "^2.0.5",
"@types/qrcode-terminal": "^0.12.2", "@types/qrcode-terminal": "^0.12.2",
"@types/ws": "^8.5.12", "@types/ws": "^8.5.12",
"@typescript-eslint/eslint-plugin": "^8.3.0", "@typescript-eslint/eslint-plugin": "^8.3.0",
@@ -57,7 +56,6 @@
"dependencies": { "dependencies": {
"express": "^5.0.0", "express": "^5.0.0",
"fluent-ffmpeg": "^2.1.2", "fluent-ffmpeg": "^2.1.2",
"pidusage": "^3.0.2",
"piscina": "^4.7.0", "piscina": "^4.7.0",
"qrcode-terminal": "^0.12.0", "qrcode-terminal": "^0.12.0",
"silk-wasm": "^3.6.1", "silk-wasm": "^3.6.1",

View File

@@ -1,6 +1,6 @@
import os from "node:os"; import os from "node:os";
import pidusage from 'pidusage';
import EventEmitter from "node:events"; import EventEmitter from "node:events";
import { cpus } from "node:os";
export interface SystemStatus { export interface SystemStatus {
cpu: { cpu: {
@@ -10,8 +10,10 @@ export interface SystemStatus {
system: string system: string
qq: string qq: string
}, },
core: number
}, },
memory: { memory: {
total: string
usage: { usage: {
system: string system: string
qq: string qq: string
@@ -20,6 +22,9 @@ export interface SystemStatus {
} }
export class StatusHelper { export class StatusHelper {
private currentUsage = process.cpuUsage();
private currentTime = process.hrtime();
private get sysCpuInfo() { private get sysCpuInfo() {
const { total, active } = os.cpus().map(cpu => { const { total, active } = os.cpus().map(cpu => {
const times = cpu.times; const times = cpu.times;
@@ -33,34 +38,50 @@ export class StatusHelper {
return { return {
usage: ((active / total) * 100).toFixed(2), usage: ((active / total) * 100).toFixed(2),
model: os.cpus()[0].model, model: os.cpus()[0].model,
speed: os.cpus()[0].speed speed: os.cpus()[0].speed,
core: cpus().length
}; };
} }
private get sysMemoryUsage() { private get sysMemoryUsage() {
const { total, free } = { total: os.totalmem(), free: os.freemem() }; const { total, free } = { total: os.totalmem(), free: os.freemem() };
return ((total - free) / total * 100).toFixed(2); return ((total - free) / 1024 / 1024).toFixed(2);
} }
private async qqUsage() { private qqUsage() {
return await pidusage(process.pid); const mem = process.memoryUsage();
console.log(JSON.stringify(mem));
const numCpus = cpus().length;
const usageDiff = process.cpuUsage(this.currentUsage);
const endTime = process.hrtime(this.currentTime);
this.currentUsage = process.cpuUsage();
this.currentTime = process.hrtime();
const usageMS = (usageDiff.user + usageDiff.system) / 1e3;
const totalMS = endTime[0] * 1e3 + endTime[1] / 1e6;
const normPercent = (usageMS / totalMS / numCpus) * 100;
return {
cpu: normPercent.toFixed(2),
memory: ((mem.heapTotal + mem.external + mem.arrayBuffers) / 1024 / 1024).toFixed(2)
};
} }
async systemStatus(): Promise<SystemStatus> { systemStatus(): SystemStatus {
const qqUsage = await this.qqUsage(); const qqUsage = this.qqUsage();
return { return {
cpu: { cpu: {
core: this.sysCpuInfo.core,
model: this.sysCpuInfo.model, model: this.sysCpuInfo.model,
speed: (this.sysCpuInfo.speed / 1000).toFixed(2), speed: (this.sysCpuInfo.speed / 1000).toFixed(2),
usage: { usage: {
system: this.sysCpuInfo.usage, system: this.sysCpuInfo.usage,
qq: qqUsage.cpu.toFixed(2) qq: qqUsage.cpu
}, },
}, },
memory: { memory: {
total: (os.totalmem() / 1024 / 1024).toFixed(2),
usage: { usage: {
system: this.sysMemoryUsage, system: this.sysMemoryUsage,
qq: (qqUsage.memory / os.totalmem() * 100).toFixed(2) qq: qqUsage.memory
} }
}, },
}; };
@@ -87,8 +108,8 @@ class StatusHelperSubscription extends EventEmitter {
} }
private startInterval(time: number) { private startInterval(time: number) {
this.interval ??= setInterval(async () => { this.interval ??= setInterval(() => {
const status = await this.statusHelper.systemStatus(); const status = this.statusHelper.systemStatus();
this.emit('statusUpdate', status); this.emit('statusUpdate', status);
}, time); }, time);
} }