diff options
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | src/tasks/cpuUsage.ts | 50 | ||||
-rw-r--r-- | yarn.lock | 16 |
3 files changed, 48 insertions, 20 deletions
diff --git a/package.json b/package.json index c89457a..bdaf624 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,6 @@ "lodash": "^4.17.21", "mathjs": "^10.6.1", "nanoid": "^4.0.0", - "node-os-utils": "^1.3.7", "numeral": "^2.0.6", "pg": "^8.7.3", "pg-hstore": "^2.3.4", @@ -100,7 +99,6 @@ "@types/express": "^4.17.13", "@types/lodash": "^4.14.182", "@types/node": "^18.0.0", - "@types/node-os-utils": "^1.3.0", "@types/numeral": "^2.0.2", "@types/pg": "^8.6.5", "@types/prettier": "^2.6.3", diff --git a/src/tasks/cpuUsage.ts b/src/tasks/cpuUsage.ts index f456c31..61e7a54 100644 --- a/src/tasks/cpuUsage.ts +++ b/src/tasks/cpuUsage.ts @@ -1,5 +1,5 @@ import { BushTask, Time } from '#lib'; -import osu from 'node-os-utils'; +import os from 'node:os'; export default class CpuUsageTask extends BushTask { public constructor() { @@ -10,7 +10,53 @@ export default class CpuUsageTask extends BushTask { } public async exec() { - const cpuStats = await osu.cpu.usage(this.client.stats.cpu === undefined ? 100 * Time.Millisecond : Time.Minute); + const cpuStats = await cpu.usage(this.client.stats.cpu === undefined ? 100 * Time.Millisecond : Time.Minute); this.client.stats.cpu = cpuStats; } } + +/* Everything inside the cpu namespace is adapted from the "node-os-utils" npm package which is licensed under a MIT license by Sunil Wang */ +namespace cpu { + export function usage(interval = Time.Second): Promise<number> { + return new Promise((resolve) => { + const startMeasure = average(); + + setTimeout(() => { + const endMeasure = average(); + const idleDifference = endMeasure.avgIdle - startMeasure.avgIdle; + const totalDifference = endMeasure.avgTotal - startMeasure.avgTotal; + const cpuPercentage = (10000 - Math.round((10000 * idleDifference) / totalDifference)) / 100; + + return resolve(cpuPercentage); + }, interval); + }); + } + + export function average(): CpuAverageInfo { + let totalIdle = 0; + let totalTick = 0; + const cpus = os.cpus(); + + for (let i = 0, len = cpus.length; i < len; i++) { + const cpu = cpus[i]; + for (const type in cpu.times) { + totalTick += cpu.times[type as keyof typeof cpu.times]; + } + totalIdle += cpu.times.idle; + } + + return { + totalIdle: totalIdle, + totalTick: totalTick, + avgIdle: totalIdle / cpus.length, + avgTotal: totalTick / cpus.length + }; + } + + export interface CpuAverageInfo { + totalIdle: number; + totalTick: number; + avgIdle: number; + avgTotal: number; + } +} @@ -522,13 +522,6 @@ __metadata: languageName: node linkType: hard -"@types/node-os-utils@npm:^1.3.0": - version: 1.3.0 - resolution: "@types/node-os-utils@npm:1.3.0" - checksum: bf000919593e56a65568a1ab3c3d57f31bc34c9eca59b89df0662767cf46ebdd5d8844310ca930d800bf7dd1d4564c406f27823c1c4fb521e648292c65a1b067 - languageName: node - linkType: hard - "@types/node@npm:*, @types/node@npm:^18.0.0": version: 18.0.0 resolution: "@types/node@npm:18.0.0" @@ -1068,7 +1061,6 @@ __metadata: "@types/express": ^4.17.13 "@types/lodash": ^4.14.182 "@types/node": ^18.0.0 - "@types/node-os-utils": ^1.3.0 "@types/numeral": ^2.0.2 "@types/pg": ^8.6.5 "@types/prettier": ^2.6.3 @@ -1095,7 +1087,6 @@ __metadata: mathjs: ^10.6.1 nanoid: ^4.0.0 node-fetch: ^3.2.6 - node-os-utils: ^1.3.7 numeral: ^2.0.6 pg: ^8.7.3 pg-hstore: ^2.3.4 @@ -3643,13 +3634,6 @@ __metadata: languageName: node linkType: hard -"node-os-utils@npm:^1.3.7": - version: 1.3.7 - resolution: "node-os-utils@npm:1.3.7" - checksum: 4059d74def168e2ffcd28e879731fd68f6e9b1e623804224e34bf0ca3c8968070e9e2cbc0cb57d80dd05d27552495a65c865b93ae5f54e61389e5af0b92ee818 - languageName: node - linkType: hard - "nopt@npm:^5.0.0": version: 5.0.0 resolution: "nopt@npm:5.0.0" |