From ca867539983aae87dfb64301c03108b1a4846b19 Mon Sep 17 00:00:00 2001 From: mervyn Date: Thu, 23 Jun 2022 23:52:22 +0800 Subject: [PATCH] #15 show the process name --- main.js | 12 ++++--- monitor.js | 10 +++--- package-lock.json | 80 ++++++++++++++++++++++++++------------------ package.json | 2 +- test/monitor.test.js | 10 +++--- 5 files changed, 65 insertions(+), 49 deletions(-) diff --git a/main.js b/main.js index bcc2a63..f57abf1 100644 --- a/main.js +++ b/main.js @@ -8,21 +8,23 @@ const monitorConfig = isDev() ? { windowSize: 10 } : {} const changedActions = {} changedActions[CHANGED_TYPES.ADD] = ({ pid, stat }) => { - logger.warn(`pid: ${pid} drains the battery fast`) - new Notification({ title: 'ooops', body: `There is some process draining the battery fast, pid is ${pid}` }).show() + const command = path.basename(stat.command) + logger.warn(`pid: ${pid}, command: ${stat.command} drains the battery fast`) + new Notification({ title: 'ooops', body: `The command "${command}" is draining the battery fast and pid is ${pid}` }).show() const statusItem = cachedMenus.find(item => item.id === STATUS_ID) statusItem.label = STATUS_HAS_PID_LABEL cachedMenus.push({ - label: `kill ${pid}`, + label: `kill ${command} (${pid})`, + toolTip: `the path is "${stat.command}"`, type: 'normal', isPid: true, id: pid, before: [QUIT_ID], click: () => { process.kill(pid) - logger.info(`sent SIGTERM to pid ${pid}`) + logger.info(`sent SIGTERM to command ${stat.command} pid ${pid}`) cachedMenus = cachedMenus.filter(item => item.id !== pid) const hasPid = cachedMenus.some(item => item.isPid) if (!hasPid) statusItem.label = STATUS_NO_PID_LABEL @@ -35,7 +37,7 @@ changedActions[CHANGED_TYPES.ADD] = ({ pid, stat }) => { } changedActions[CHANGED_TYPES.REMOVE] = ({ pid, stat }) => { - logger.warn(`pid: ${pid} becomes normal`) + logger.warn(`command: ${stat.command} pid: ${pid} becomes normal`) const statusItem = cachedMenus.find(item => item.id === STATUS_ID) cachedMenus = cachedMenus.filter(item => item.id !== pid) const hasPid = cachedMenus.some(item => item.isPid) diff --git a/monitor.js b/monitor.js index ef289cb..cf4e593 100644 --- a/monitor.js +++ b/monitor.js @@ -1,5 +1,5 @@ const pidtree = require('pidtree') -const pidusage = require('pidusage') +const pidusage = require('@reply2future/pidusage') const ALL_PROCESSES = -1 const DEFAULT_INTERVAL_MS = 1000 @@ -87,8 +87,10 @@ class Statistics { } addPidStat (pid, stat) { - if (!this.map.has(pid)) this.map.set(pid, { sum: 0, data: [] }) + if (!this.map.has(pid)) this.map.set(pid, { sum: 0, command: stat.command, data: [] }) const _stat = this.map.get(pid) + // duplicated info + delete stat.command _stat.data.push(stat) _stat.sum += stat.cpu @@ -101,10 +103,6 @@ class Statistics { this.slidingCallback({ pid, stat: _stat }) } - reset (pid) { - this.map.set(pid, { sum: 0, data: [] }) - } - clear (pid) { this.map.delete(pid) } diff --git a/package-lock.json b/package-lock.json index f2dc2e6..f0feee4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,9 +10,9 @@ "hasInstallScript": true, "license": "GPL-2.0-only", "dependencies": { + "@reply2future/pidusage": "^3.1.0", "electron-log": "^4.4.7", - "pidtree": "^0.5.0", - "pidusage": "^3.0.0" + "pidtree": "^0.5.0" }, "devDependencies": { "chai": "^4.3.6", @@ -216,6 +216,36 @@ "node": ">= 10.0.0" } }, + "node_modules/@reply2future/pidusage": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/@reply2future/pidusage/-/pidusage-3.1.0.tgz", + "integrity": "sha512-jf9hOeaXTspeVhj9naJ/TwqiLg0pDXn1jO6AauUidwptEBvawXatxTwlRJLy33n2T2YaqVRMd9dxWEnDZPLvxg==", + "dependencies": { + "safe-buffer": "^5.2.1" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@reply2future/pidusage/node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/@sindresorhus/is": { "version": "0.14.0", "resolved": "https://registry.npmmirror.com/@sindresorhus/is/-/is-0.14.0.tgz", @@ -4223,22 +4253,6 @@ "node": ">=0.10" } }, - "node_modules/pidusage": { - "version": "3.0.0", - "resolved": "https://registry.npmmirror.com/pidusage/-/pidusage-3.0.0.tgz", - "integrity": "sha512-8VJLToXhj+RYZGNVw8oxc7dS54iCQXUJ+MDFHezQ/fwF5B8W4OWodAMboc1wb08S/4LiHwAmkT4ohf/d3YPPsw==", - "dependencies": { - "safe-buffer": "^5.2.1" - }, - "engines": { - "node": ">=10" - } - }, - "node_modules/pidusage/node_modules/safe-buffer": { - "version": "5.2.1", - "resolved": "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.2.1.tgz", - "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==" - }, "node_modules/pify": { "version": "3.0.0", "resolved": "https://registry.npmmirror.com/pify/-/pify-3.0.0.tgz", @@ -5670,6 +5684,21 @@ } } }, + "@reply2future/pidusage": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/@reply2future/pidusage/-/pidusage-3.1.0.tgz", + "integrity": "sha512-jf9hOeaXTspeVhj9naJ/TwqiLg0pDXn1jO6AauUidwptEBvawXatxTwlRJLy33n2T2YaqVRMd9dxWEnDZPLvxg==", + "requires": { + "safe-buffer": "^5.2.1" + }, + "dependencies": { + "safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==" + } + } + }, "@sindresorhus/is": { "version": "0.14.0", "resolved": "https://registry.npmmirror.com/@sindresorhus/is/-/is-0.14.0.tgz", @@ -8931,21 +8960,6 @@ "resolved": "https://registry.npmmirror.com/pidtree/-/pidtree-0.5.0.tgz", "integrity": "sha512-9nxspIM7OpZuhBxPg73Zvyq7j1QMPMPsGKTqRc2XOaFQauDvoNz9fM1Wdkjmeo7l9GXOZiRs97sPkuayl39wjA==" }, - "pidusage": { - "version": "3.0.0", - "resolved": "https://registry.npmmirror.com/pidusage/-/pidusage-3.0.0.tgz", - "integrity": "sha512-8VJLToXhj+RYZGNVw8oxc7dS54iCQXUJ+MDFHezQ/fwF5B8W4OWodAMboc1wb08S/4LiHwAmkT4ohf/d3YPPsw==", - "requires": { - "safe-buffer": "^5.2.1" - }, - "dependencies": { - "safe-buffer": { - "version": "5.2.1", - "resolved": "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.2.1.tgz", - "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==" - } - } - }, "pify": { "version": "3.0.0", "resolved": "https://registry.npmmirror.com/pify/-/pify-3.0.0.tgz", diff --git a/package.json b/package.json index 7ee71f5..0e564c2 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,6 @@ "dependencies": { "electron-log": "^4.4.7", "pidtree": "^0.5.0", - "pidusage": "^3.0.0" + "@reply2future/pidusage": "^3.1.0" } } diff --git a/test/monitor.test.js b/test/monitor.test.js index aa50135..ffdb5c8 100644 --- a/test/monitor.test.js +++ b/test/monitor.test.js @@ -7,7 +7,7 @@ const cpus = [110, 120, 130, 50, 112, 130, 0, 0, 10] let i = 0 const { Monitor, Statistics, CHANGED_TYPES } = proxyquire('../monitor', { pidtree: async () => [1], - pidusage: async () => ({ 1: { cpu: cpus[i++ % cpus.length], ppid: 1 } }) + '@reply2future/pidusage': async () => ({ 1: { cpu: cpus[i++ % cpus.length], ppid: 1 } }) }) describe('monitor module', () => { @@ -70,16 +70,18 @@ describe('monitor module', () => { }) it('should invoke callback because there is some data greater than threshold', (done) => { + const mockStat = { cpu: 110, command: '/bin/bash' } const s = new Statistics({ windowSize: 2, - slidingCallback: ({ pid }) => { + slidingCallback: ({ pid, stat }) => { pid.should.equal(1) + stat.command.should.equal(mockStat.command) done() } }) - s.addPidStat(1, { cpu: 110 }) - s.addPidStat(1, { cpu: 110 }) + s.addPidStat(1, { ...mockStat }) + s.addPidStat(1, { ...mockStat }) }) }) })