mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
feat: better system status helper
- cpu usage diff
This commit is contained in:
@@ -21,28 +21,40 @@ export interface SystemStatus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class StatusHelper {
|
export class StatusHelper {
|
||||||
private currentUsage = process.cpuUsage();
|
private psCpuUsage = process.cpuUsage();
|
||||||
private currentTime = process.hrtime();
|
private psCurrentTime = process.hrtime();
|
||||||
|
private cpuTimes = os.cpus().map(cpu => cpu.times);
|
||||||
|
|
||||||
private get sysCpuInfo() {
|
private replaceNaN(value: number) {
|
||||||
const { total, active } = os.cpus().map(cpu => {
|
return isNaN(value) ? 0 : value;
|
||||||
const times = cpu.times;
|
}
|
||||||
const total = times.user + times.nice + times.sys + times.idle + times.irq;
|
|
||||||
const active = total - times.idle;
|
private sysCpuInfo() {
|
||||||
return { total, active };
|
const currentTimes = os.cpus().map(cpu => cpu.times);
|
||||||
|
const { total, active } = currentTimes.map((times, index) => {
|
||||||
|
const prevTimes = this.cpuTimes[index];
|
||||||
|
const totalCurrent = times.user + times.nice + times.sys + times.idle + times.irq;
|
||||||
|
const totalPrev = prevTimes.user + prevTimes.nice + prevTimes.sys + prevTimes.idle + prevTimes.irq;
|
||||||
|
const activeCurrent = totalCurrent - times.idle;
|
||||||
|
const activePrev = totalPrev - prevTimes.idle;
|
||||||
|
return {
|
||||||
|
total: totalCurrent - totalPrev,
|
||||||
|
active: activeCurrent - activePrev
|
||||||
|
};
|
||||||
}).reduce((acc, cur) => ({
|
}).reduce((acc, cur) => ({
|
||||||
total: acc.total + cur.total,
|
total: acc.total + cur.total,
|
||||||
active: acc.active + cur.active
|
active: acc.active + cur.active
|
||||||
}), { total: 0, active: 0 });
|
}), { total: 0, active: 0 });
|
||||||
|
this.cpuTimes = currentTimes;
|
||||||
return {
|
return {
|
||||||
usage: ((active / total) * 100).toFixed(2),
|
usage: this.replaceNaN(((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: os.cpus().length
|
core: os.cpus().length
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private get sysMemoryUsage() {
|
private sysMemoryUsage() {
|
||||||
const { total, free } = { total: os.totalmem(), free: os.freemem() };
|
const { total, free } = { total: os.totalmem(), free: os.freemem() };
|
||||||
return ((total - free) / 1024 / 1024).toFixed(2);
|
return ((total - free) / 1024 / 1024).toFixed(2);
|
||||||
}
|
}
|
||||||
@@ -50,35 +62,36 @@ export class StatusHelper {
|
|||||||
private qqUsage() {
|
private qqUsage() {
|
||||||
const mem = process.memoryUsage();
|
const mem = process.memoryUsage();
|
||||||
const numCpus = os.cpus().length;
|
const numCpus = os.cpus().length;
|
||||||
const usageDiff = process.cpuUsage(this.currentUsage);
|
const usageDiff = process.cpuUsage(this.psCpuUsage);
|
||||||
const endTime = process.hrtime(this.currentTime);
|
const endTime = process.hrtime(this.psCurrentTime);
|
||||||
this.currentUsage = process.cpuUsage();
|
this.psCpuUsage = process.cpuUsage();
|
||||||
this.currentTime = process.hrtime();
|
this.psCurrentTime = process.hrtime();
|
||||||
const usageMS = (usageDiff.user + usageDiff.system) / 1e3;
|
const usageMS = (usageDiff.user + usageDiff.system) / 1e3;
|
||||||
const totalMS = endTime[0] * 1e3 + endTime[1] / 1e6;
|
const totalMS = endTime[0] * 1e3 + endTime[1] / 1e6;
|
||||||
const normPercent = (usageMS / totalMS / numCpus) * 100;
|
const normPercent = (usageMS / totalMS / numCpus) * 100;
|
||||||
return {
|
return {
|
||||||
cpu: normPercent.toFixed(2),
|
cpu: this.replaceNaN(normPercent).toFixed(2),
|
||||||
memory: ((mem.heapTotal + mem.external + mem.arrayBuffers) / 1024 / 1024).toFixed(2)
|
memory: ((mem.heapTotal + mem.external + mem.arrayBuffers) / 1024 / 1024).toFixed(2)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
systemStatus(): SystemStatus {
|
systemStatus(): SystemStatus {
|
||||||
const qqUsage = this.qqUsage();
|
const qqUsage = this.qqUsage();
|
||||||
|
const sysCpuInfo = this.sysCpuInfo();
|
||||||
return {
|
return {
|
||||||
cpu: {
|
cpu: {
|
||||||
core: this.sysCpuInfo.core,
|
core: sysCpuInfo.core,
|
||||||
model: this.sysCpuInfo.model,
|
model: sysCpuInfo.model,
|
||||||
speed: (this.sysCpuInfo.speed / 1000).toFixed(2),
|
speed: (sysCpuInfo.speed / 1000).toFixed(2),
|
||||||
usage: {
|
usage: {
|
||||||
system: this.sysCpuInfo.usage,
|
system: sysCpuInfo.usage,
|
||||||
qq: qqUsage.cpu
|
qq: qqUsage.cpu
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
memory: {
|
memory: {
|
||||||
total: (os.totalmem() / 1024 / 1024).toFixed(2),
|
total: (os.totalmem() / 1024 / 1024).toFixed(2),
|
||||||
usage: {
|
usage: {
|
||||||
system: this.sysMemoryUsage,
|
system: this.sysMemoryUsage(),
|
||||||
qq: qqUsage.memory
|
qq: qqUsage.memory
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user