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
6 changes: 5 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { store, STORE_KEY } = require('./store')
const { MenuTray, MENU_CLICK_EVENT } = require('./menu')
const logger = require('electron-log')

const monitorConfig = isDev() ? { windowSize: 5 } : {}
const monitorConfig = isDev() ? { windowSize: 5, intervalMs: 1000 } : { intervalMs: store.get(STORE_KEY.intervalTimeSd, 1000) }
const monitor = new Monitor(monitorConfig)

monitor.on(ACTION_EVENT.REMOVE, ({ pid }) => {
Expand Down Expand Up @@ -49,6 +49,10 @@ function initListeners () {
openAtLogin: value
})
})

ipcMain.on(getIpcStoreKey(STORE_KEY.intervalTimeSd), (event, intervalMs) => {
monitor.setInterval(intervalMs)
})
}

// This method will be called when Electron has done everything
Expand Down
17 changes: 13 additions & 4 deletions monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Monitor extends EventEmitter {
if (this.running) return
this.running = true
this.emit(STATUS_EVENT.START)
const interval = async (time) => {
const interval = async () => {
setTimeout(async () => {
if (!this.running) return

Expand All @@ -72,11 +72,11 @@ class Monitor extends EventEmitter {
this.stats.addPidStat(pid, stat)
}

interval(time)
}, time)
interval()
}, this.intervalMs)
}

interval(this.intervalMs)
interval()
this.checkTimer.start()
}

Expand All @@ -96,6 +96,11 @@ class Monitor extends EventEmitter {
const nums = [...data].sort((a, b) => a.cpu - b.cpu)
return dLen % 2 !== 0 ? nums[mid].cpu : (nums[mid - 1].cpu + nums[mid].cpu) / 2
}

setInterval (intervalMs) {
this.intervalMs = intervalMs
this.checkTimer.setInterval(intervalMs * this.windowSize)
}
}

class Statistics {
Expand Down Expand Up @@ -149,6 +154,10 @@ class CheckTimer {
stop () {
clearInterval(this.interval)
}

setInterval (interval) {
this.intervalMs = interval
}
}

module.exports = { Monitor, Statistics, CheckTimer, ACTION_EVENT, STATUS_EVENT }
9 changes: 7 additions & 2 deletions preference.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@

<div>
<label for="silent-time">Silent time(between 1h to 6h):</label>
<input type="range" id="silent-time" name="silent-time" value="1" min="1" max="6" step="1" oninput="this.nextElementSibling.value = `${this.value}h`">
<output>1h</output>
<input type="range" id="silent-time" name="silent-time" value="1" min="1" max="6" step="1" oninput="this.nextElementSibling.value = `${this.value} hour`">
<output>1 hour</output>
</div>
<div>
<label for="interval-time">Interval time(between 1s to 10s):</label>
<input type="range" id="interval-time" name="interval-time" value="1" min="1" max="10" step="1" oninput="this.nextElementSibling.value = `${this.value} second`">
<output>1 second</output>
</div>
<div>
<label for="auto-launch">Run on system startup</label>
Expand Down
11 changes: 10 additions & 1 deletion renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ silentTimeRange.addEventListener('change', (event) => {
})

silentTimeRange.value = window.api.getStore(window.api.STORE_KEY.silentTimeHr)
silentTimeRange.nextElementSibling.value = `${silentTimeRange.value}h`
silentTimeRange.nextElementSibling.value = `${silentTimeRange.value} hour`

const intervalTimeRange = document.querySelector('#interval-time')

intervalTimeRange.addEventListener('change', (event) => {
window.api.setStore(window.api.STORE_KEY.intervalTimeSd, parseInt(event.target.value, 10) * 1000)
})

intervalTimeRange.value = window.api.getStore(window.api.STORE_KEY.intervalTimeSd) / 1000
intervalTimeRange.nextElementSibling.value = `${intervalTimeRange.value} second`

const autoLaunch = document.querySelector('#auto-launch')

Expand Down
1 change: 1 addition & 0 deletions store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const store = new Store()

const STORE_KEY = {
silentTimeHr: 'silentTimeHr',
intervalTimeSd: 'intervalTimeSd',
autoLaunch: 'autoLaunch',
appInfo: 'appInfo'
}
Expand Down
28 changes: 28 additions & 0 deletions test/monitor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ describe('monitor module', () => {
describe('monitor', () => {
let clock
beforeEach(() => {
i = 0
clock = sinon.useFakeTimers()
})
afterEach(() => {
clock.restore()
i = 0
})
it('should monitor successfully', async () => {
let addCbCount = 0
Expand Down Expand Up @@ -62,6 +64,32 @@ describe('monitor module', () => {
done()
})
})

it('should setInterval successfully', async () => {
let addCbCount = 0
let removeCbCount = 0
const monitor = new Monitor()
monitor.on(ACTION_EVENT.ADD, ({ pid, stat, type }) => {
addCbCount += 1
})
monitor.on(ACTION_EVENT.REMOVE, ({ pid, stat, type }) => {
removeCbCount += 1
})
monitor.stats.windowSize = 2

monitor.setInterval(4000)
monitor.start()

monitor.isRunning().should.equal(true)
await clock.tickAsync(3000)
addCbCount.should.equal(0)
removeCbCount.should.equal(0)
await clock.tickAsync(5000)
addCbCount.should.equal(1)
removeCbCount.should.equal(0)
await monitor.stop()
monitor.isRunning().should.equal(false)
})
})

describe('statistics', () => {
Expand Down