-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add notification when disk is getting full (#146)
- Loading branch information
1 parent
6fdc4d4
commit 47843ba
Showing
6 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { platform } from 'os'; | ||
import { check } from 'diskusage'; | ||
import Logger from '../Logger'; | ||
import DiscordClient from './DiscordClient'; | ||
|
||
class DiskUsageChecker { | ||
private static warningThreshold = 0.9; | ||
private static rootDir = platform() !== 'win32' ? '/' : 'C:'; | ||
|
||
constructor(private logger: Logger, private discord: DiscordClient) {} | ||
|
||
public checkUsage = async () => { | ||
const { available, total } = await check(DiskUsageChecker.rootDir); | ||
|
||
const used = total - available; | ||
const usedPercentage = used / total; | ||
|
||
if (usedPercentage >= DiskUsageChecker.warningThreshold) { | ||
const message = `Disk usage is **${this.formatNumber(usedPercentage * 100)}%**: ` + | ||
`**${this.formatNumber(this.convertToGb(available))} GB** of **${this.formatNumber(this.convertToGb(total))} GB** available`; | ||
|
||
this.logger.warn(message); | ||
await this.discord.sendAlert(message); | ||
} | ||
} | ||
|
||
private formatNumber = (toFormat: number) => { | ||
return Number(toFormat.toFixed(2)); | ||
} | ||
|
||
private convertToGb = (bytes: number) => { | ||
return bytes / 1073741824; | ||
} | ||
} | ||
|
||
export default DiskUsageChecker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { platform } from 'os'; | ||
import { DiskUsage } from 'diskusage'; | ||
import Logger from '../../../lib/Logger'; | ||
import DiscordClient from '../../../lib/notifications/DiscordClient'; | ||
import DiskUsageChecker from '../../../lib/notifications/DiskUsageChecker'; | ||
|
||
const mockSendAlert = jest.fn().mockImplementation(() => Promise.resolve()); | ||
|
||
jest.mock('../../../lib/notifications/DiscordClient', () => { | ||
return jest.fn().mockImplementation(() => ({ | ||
sendAlert: mockSendAlert, | ||
})); | ||
}); | ||
|
||
const mockedDiscordClient = <jest.Mock<DiscordClient>><any>DiscordClient; | ||
|
||
const gigabyte = 1073741824; | ||
|
||
let diskUsage: DiskUsage = { | ||
free: 2 * gigabyte, | ||
total: 10 * gigabyte, | ||
available: 1 * gigabyte, | ||
}; | ||
|
||
const mockCheck = jest.fn().mockImplementation(() => diskUsage); | ||
|
||
jest.mock('diskusage', () => { | ||
return { | ||
check: async () => { | ||
return mockCheck(); | ||
}, | ||
}; | ||
}); | ||
|
||
describe('DiskUsageChecker', () => { | ||
const discordClient = mockedDiscordClient(); | ||
const checker = new DiskUsageChecker(Logger.disabledLogger, discordClient); | ||
|
||
beforeEach(() => { | ||
mockSendAlert.mockClear(); | ||
}); | ||
|
||
test('should get correct root path', () => { | ||
if (platform() !== 'win32') { | ||
expect(DiskUsageChecker['rootDir']).toEqual('/'); | ||
} else { | ||
expect(DiskUsageChecker['rootDir']).toEqual('C:'); | ||
} | ||
}); | ||
|
||
test('should format numbers', () => { | ||
const formatNumber = checker['formatNumber']; | ||
|
||
expect(formatNumber(100.123)).toEqual(100.12); | ||
expect(formatNumber(100.0000123)).toEqual(100); | ||
}); | ||
|
||
test('should convert bytes to gigabytes', () => { | ||
const convertToGb = checker['convertToGb']; | ||
|
||
expect(convertToGb(536870912)).toEqual(0.5); | ||
expect(convertToGb(1073741824)).toEqual(1); | ||
expect(convertToGb(2147483648)).toEqual(2); | ||
}); | ||
|
||
test('should send a warning if disk usage is 90% or greater', async () => { | ||
await checker.checkUsage(); | ||
|
||
expect(mockSendAlert).toHaveBeenCalledTimes(1); | ||
expect(mockSendAlert).toHaveBeenCalledWith('Disk usage is **90%**: **1 GB** of **10 GB** available'); | ||
|
||
diskUsage = { | ||
available: 0, | ||
free: 5 * gigabyte, | ||
total: 20 * gigabyte, | ||
}; | ||
|
||
await checker.checkUsage(); | ||
|
||
expect(mockSendAlert).toHaveBeenCalledTimes(2); | ||
expect(mockSendAlert).toHaveBeenCalledWith('Disk usage is **100%**: **0 GB** of **20 GB** available'); | ||
}); | ||
|
||
test('should not send a warning if disk usage is less than 90%', async () => { | ||
diskUsage = { | ||
free: 5.05 * gigabyte, | ||
available: 5 * gigabyte, | ||
total: 10 * gigabyte, | ||
}; | ||
|
||
await checker.checkUsage(); | ||
|
||
expect(mockSendAlert).toHaveBeenCalledTimes(0); | ||
}); | ||
}); |