feat(daemon): sample host cpu, memory and disk for /info and /resources

The status page had no host figures: `/api/v1/info` only carried identity and
`/api/v1/resources` only carried instance RSS, so the CPU/mem/disk tiles fell
back to "—" and the memory tile faked its percentage from RSS over physical
memory.

src/core/sysinfo.ts is a zero-dependency sampler: CPU from the /proc/stat delta
(null on the first read, since it needs two samples), load average from
os.loadavg(), memory from os.freemem/os.totalmem, and disk via statfs on the
daemon's state dir. One sample is cached for ~1 s and shared between /info and
/resources, so polling both does not double the syscalls. /info gains additive
`cpu`, `mem`, `disk` percentages plus `cpuCores` and `loadAvg`; /resources gains
a `system` object with the byte counts, the disk path and `sampledAt`.

Every field is optional on the client side: an older daemon simply omits them.
This commit is contained in:
yuez committed 2026-10-28 08:13:58 +08:00
1 parent 1664227c3b
commit 88354ec078
7 files changed
+492 -5

No files matched your search

+26
View File
@@ -425,6 +425,32 @@ describe("HTTP API", () => {
expect(res.status).toBe(401);
});
it("includes injected system stats in resources", async () => {
const custom = await createHarness({ sysinfo: (await import("./harness.js")).makeFakeSysinfoSampler({ cpuPercent: 42 }) });
try {
const customToken = custom.createToken();
const response = await fetch(`${custom.url}/api/v1/resources`, {
headers: { Authorization: `Bearer ${customToken}` },
});
expect(response.status).toBe(200);
expect((await response.json()).system).toEqual(expect.objectContaining({ cpuPercent: 42 }));
} finally {
await custom.cleanup();
}
});
it("includes system stats in info", async () => {
const response = await fetch(`${h.url}/api/v1/info`);
expect(response.status).toBe(200);
expect(await response.json()).toEqual(expect.objectContaining({
cpuCores: 4,
loadAvg: [1.5, 1.2, 0.9],
cpu: 42,
mem: 25,
disk: 25,
}));
});
it("redirects a scanned /pair/<code> link to the zero-input connect page", async () => {
const res = await fetch(`${h.url}/pair/abcd2345`, { redirect: "manual" });
expect(res.status).toBe(302);