Repository navigation
Show the free disk space and the CPU load of the machine in the daemon status popover #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
packages/cli/src/features/machine-usage/machine-usage.test.ts
This file contains hidden or 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,24 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import os from 'node:os'; | ||
| import { test } from 'node:test'; | ||
|
|
||
| import { getCpuLoad, readDiskSpace } from './machine-usage'; | ||
|
|
||
| test('the cpu load is the busy share of the time between two readings', () => { | ||
| assert.equal(getCpuLoad({ idle: 1000, total: 2000 }, { idle: 1300, total: 3000 }), 0.7); | ||
| }); | ||
|
|
||
| test('the cpu load is unknown when no time passed between two readings', () => { | ||
| assert.equal(getCpuLoad({ idle: 1000, total: 2000 }, { idle: 1000, total: 2000 }), null); | ||
| }); | ||
|
|
||
| test('the disk space of a folder has free bytes within the total', () => { | ||
| const disk = readDiskSpace(os.tmpdir()); | ||
| assert.ok(disk !== null); | ||
| assert.ok(disk.totalBytes > 0); | ||
| assert.ok(disk.freeBytes >= 0 && disk.freeBytes <= disk.totalBytes); | ||
| }); | ||
|
|
||
| test('the disk space of a missing folder is unknown', () => { | ||
| assert.equal(readDiskSpace('/no/such/kombinat/folder'), null); | ||
| }); |
This file contains hidden or 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,54 @@ | ||
| import fs from 'node:fs'; | ||
| import os from 'node:os'; | ||
|
|
||
| import type { DiskSpace } from 'domains/daemon/types'; | ||
|
|
||
| const CPU_SAMPLE_MS = 5_000; | ||
|
|
||
| export interface CpuTimes { | ||
| idle: number; | ||
| total: number; | ||
| } | ||
|
|
||
| let cpuLoad: number | null = null; | ||
|
|
||
| function readCpuTimes(): CpuTimes { | ||
| return os.cpus().reduce( | ||
| (sum, { times }) => ({ | ||
| idle: sum.idle + times.idle, | ||
| total: sum.total + times.user + times.nice + times.sys + times.idle + times.irq, | ||
| }), | ||
| { idle: 0, total: 0 }, | ||
| ); | ||
| } | ||
|
|
||
| export function getCpuLoad(previous: CpuTimes, next: CpuTimes): number | null { | ||
| const total = next.total - previous.total; | ||
| if (total <= 0) return null; | ||
| return Math.min(1, Math.max(0, 1 - (next.idle - previous.idle) / total)); | ||
| } | ||
|
|
||
| // CPU times only count up, so the load is the busy share between two readings. | ||
| export function startCpuSampling(): () => void { | ||
| let previous = readCpuTimes(); | ||
| const timer = setInterval(() => { | ||
| const next = readCpuTimes(); | ||
| cpuLoad = getCpuLoad(previous, next); | ||
| previous = next; | ||
| }, CPU_SAMPLE_MS); | ||
| timer.unref(); | ||
| return () => clearInterval(timer); | ||
| } | ||
|
|
||
| export function readCpuLoad(): number | null { | ||
| return cpuLoad; | ||
| } | ||
|
|
||
| export function readDiskSpace(dir: string): DiskSpace | null { | ||
| try { | ||
| const { bavail, blocks, bsize } = fs.statfsSync(dir); | ||
| return { freeBytes: bavail * bsize, totalBytes: blocks * bsize }; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export const STALE_TICK_MS = 5 * 60_000; | ||
| export const LOW_DISK_BYTES = 10 * 1000 ** 3; |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| import type { z } from 'zod'; | ||
|
|
||
| import type { daemonStatusSchema } from './schemas'; | ||
| import type { daemonStatusSchema, diskSpaceSchema } from './schemas'; | ||
|
|
||
| export type DaemonStatus = z.infer<typeof daemonStatusSchema>; | ||
|
|
||
| export type DiskSpace = z.infer<typeof diskSpaceSchema>; | ||
|
|
||
| export type DaemonHealth = 'healthy' | 'stale' | 'unreachable'; |
This file contains hidden or 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,30 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { test } from 'node:test'; | ||
|
|
||
| import { checkIsDiskLow, formatBytes, formatCpuLoad, formatDiskSpace } from './utils'; | ||
|
|
||
| test('bytes read in the largest decimal unit, with a decimal only below ten', () => { | ||
| assert.equal(formatBytes(1_834_000_000), '1.8 GB'); | ||
| assert.equal(formatBytes(494_380_000_000), '494 GB'); | ||
| assert.equal(formatBytes(2_000_000_000_000), '2 TB'); | ||
| assert.equal(formatBytes(512_000_000), '512 MB'); | ||
| assert.equal(formatBytes(4_000_000_000_000_000), '4,000 TB'); | ||
| assert.equal(formatBytes(0), '0 B'); | ||
| }); | ||
|
|
||
| test('disk space reads as free of total', () => { | ||
| assert.equal( | ||
| formatDiskSpace({ freeBytes: 182_400_000_000, totalBytes: 494_380_000_000 }), | ||
| '182 GB free of 494 GB', | ||
| ); | ||
| }); | ||
|
|
||
| test('a disk with less than 10 GB free is low', () => { | ||
| assert.equal(checkIsDiskLow({ freeBytes: 1_834_000_000, totalBytes: 494_380_000_000 }), true); | ||
| assert.equal(checkIsDiskLow({ freeBytes: 182_400_000_000, totalBytes: 494_380_000_000 }), false); | ||
| }); | ||
|
|
||
| test('the cpu load reads as a whole percentage, or a dash before the first sample', () => { | ||
| assert.equal(formatCpuLoad(0.234), '23%'); | ||
| assert.equal(formatCpuLoad(null), '-'); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.