Skip to content

Commit

Permalink
feat: system status helper
Browse files Browse the repository at this point in the history
- remove pidusage
  • Loading branch information
pk5ls20 committed Dec 21, 2024
1 parent fb1f122 commit 202129d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@types/express": "^5.0.0",
"@types/fluent-ffmpeg": "^2.1.24",
"@types/node": "^22.0.1",
"@types/pidusage": "^2.0.5",
"@types/qrcode-terminal": "^0.12.2",
"@types/ws": "^8.5.12",
"@typescript-eslint/eslint-plugin": "^8.3.0",
Expand All @@ -57,7 +56,6 @@
"dependencies": {
"express": "^5.0.0",
"fluent-ffmpeg": "^2.1.2",
"pidusage": "^3.0.2",
"piscina": "^4.7.0",
"qrcode-terminal": "^0.12.0",
"silk-wasm": "^3.6.1",
Expand Down
43 changes: 32 additions & 11 deletions src/core/helper/status.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os from "node:os";
import pidusage from 'pidusage';
import EventEmitter from "node:events";
import { cpus } from "node:os";

export interface SystemStatus {
cpu: {
Expand All @@ -10,8 +10,10 @@ export interface SystemStatus {
system: string
qq: string
},
core: number
},
memory: {
total: string
usage: {
system: string
qq: string
Expand All @@ -20,6 +22,9 @@ export interface SystemStatus {
}

export class StatusHelper {
private currentUsage = process.cpuUsage();
private currentTime = process.hrtime();

private get sysCpuInfo() {
const { total, active } = os.cpus().map(cpu => {
const times = cpu.times;
Expand All @@ -33,34 +38,50 @@ export class StatusHelper {
return {
usage: ((active / total) * 100).toFixed(2),
model: os.cpus()[0].model,
speed: os.cpus()[0].speed
speed: os.cpus()[0].speed,
core: cpus().length
};
}

private get sysMemoryUsage() {
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() {
return await pidusage(process.pid);
private qqUsage() {
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> {
const qqUsage = await this.qqUsage();
systemStatus(): SystemStatus {
const qqUsage = this.qqUsage();
return {
cpu: {
core: this.sysCpuInfo.core,
model: this.sysCpuInfo.model,
speed: (this.sysCpuInfo.speed / 1000).toFixed(2),
usage: {
system: this.sysCpuInfo.usage,
qq: qqUsage.cpu.toFixed(2)
qq: qqUsage.cpu
},
},
memory: {
total: (os.totalmem() / 1024 / 1024).toFixed(2),
usage: {
system: this.sysMemoryUsage,
qq: (qqUsage.memory / os.totalmem() * 100).toFixed(2)
qq: qqUsage.memory
}
},
};
Expand All @@ -87,8 +108,8 @@ class StatusHelperSubscription extends EventEmitter {
}

private startInterval(time: number) {
this.interval ??= setInterval(async () => {
const status = await this.statusHelper.systemStatus();
this.interval ??= setInterval(() => {
const status = this.statusHelper.systemStatus();
this.emit('statusUpdate', status);
}, time);
}
Expand Down

0 comments on commit 202129d

Please sign in to comment.