feat(daemon): report host uptime, not only the daemon's process age

The status tile labelled "uptime" showed the age of the daemon *process*
(`/info.uptimeMs`), and `/info` was fetched once with a 30 s staleTime and no
refetch interval, so the figure also froze: on a box five days up, a
`pi2 update` restart made the tile read "3m" and it never moved again.

`os.uptime()` is now sampled with the other host figures and served as
`system.hostUptimeSec` — which the 3 s `/resources` poll already refreshes —
plus an additive `/info.hostUptimeMs`. The tile renders it through a
`LiveUptime` label that anchors the reported snapshot once and ticks locally,
so the counter advances without a refetch and cannot rewind on a re-render.
The daemon's own process age stays in the server card, relabelled
`status.daemonUptime`, next to the `pid`/`daemonId` that give it meaning.
This commit is contained in:
yuez committed 2026-10-30 16:18:48 +08:00
1 parent 01fd7b9922
commit 2f2f1bb518
16 files changed
+204 -10

No files matched your search

+4 -1
View File
@@ -64,6 +64,8 @@ const resourcesBody = {
physicalMemoryBytes: 2048 * 1024 * 1024,
system: {
sampledAt: Date.now(),
// Host uptime, deliberately different from the daemon's own age in `infoBody`.
hostUptimeSec: 3 * 86_400,
cpuPercent: 37,
cpuCores: 8,
loadAvg: [1.23, 0.98, 0.75],
@@ -264,7 +266,8 @@ describe("StatusPage", () => {
expect(document.querySelector('.status-tile[data-res="disk"] .tile__meta')).toHaveTextContent("/var/lib/pi2");
expect(document.querySelector('.status-tile[data-res="cpu"] .tile__meta')).toHaveTextContent("8");
expect(document.querySelector('.status-tile[data-res="cpu"] .tile__meta')).toHaveTextContent("1.23");
expect(document.querySelector('.status-tile[data-res="uptime"] .tile__value')).toHaveTextContent("2h 5m");
// The tile shows the host, not the daemon process (`/info.uptimeMs` is 2h 5m).
expect(document.querySelector('.status-tile[data-res="uptime"] .tile__value')).toHaveTextContent("3d 0h 0m");
const counts = document.querySelector(".status-counts");
expect(counts).not.toBeNull();
+51
View File
@@ -0,0 +1,51 @@
import { act, render } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { LiveUptime, uptimeAnchor, uptimeSecondsFrom } from "@/features/status/uptime";
afterEach(() => {
vi.useRealTimers();
});
describe("uptime anchoring", () => {
it("anchors a reported duration at the matching epoch instant", () => {
expect(uptimeAnchor(5_000, 10_000)).toBe(5_000);
expect(uptimeAnchor(3 * 86_400_000, 1_000)).toBe(1_000 - 3 * 86_400_000);
});
it("rejects values a clock cannot start from", () => {
expect(uptimeAnchor(null)).toBeNull();
expect(uptimeAnchor(undefined)).toBeNull();
expect(uptimeAnchor(Number.NaN)).toBeNull();
expect(uptimeAnchor(Number.POSITIVE_INFINITY)).toBeNull();
// A negative duration would render as a rewind, not as uptime.
expect(uptimeAnchor(-1)).toBeNull();
});
it("counts forward and never below zero", () => {
expect(uptimeSecondsFrom(1_000, 62_000)).toBe(61);
expect(uptimeSecondsFrom(1_000, 1_000)).toBe(0);
expect(uptimeSecondsFrom(1_000, 0)).toBe(0);
});
});
describe("LiveUptime", () => {
it("renders an em dash when the daemon reports nothing", () => {
const { container } = render(<LiveUptime ms={null} />);
expect(container.textContent).toBe("—");
});
it("ticks without needing a fresh /info sample", () => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-01-01T00:00:00Z"));
// `/info` was only fetched once; the label must keep counting locally.
const { container } = render(<LiveUptime ms={5 * 60_000} />);
expect(container.textContent).toBe("0h 5m");
act(() => {
vi.advanceTimersByTime(60_000);
});
expect(container.textContent).toBe("0h 6m");
});
});