Skip to content
This repository was archived by the owner on May 21, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions monitor.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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)
}
Expand Down
80 changes: 47 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@
"dependencies": {
"electron-log": "^4.4.7",
"pidtree": "^0.5.0",
"pidusage": "^3.0.0"
"@reply2future/pidusage": "^3.1.0"
}
}
10 changes: 6 additions & 4 deletions test/monitor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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 })
})
})
})