Skip to content

Commit 3e19534

Browse files
committed
fix(usage): keep large dashboard logs responsive
1 parent 911373d commit 3e19534

7 files changed

Lines changed: 346 additions & 29 deletions

File tree

‎gui/src/pages/dashboard-core-poll.ts‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export type DashboardCorePoll = {
3838
startupHealthSeed: SettingsData["startupHealth"] | null | undefined;
3939
sidecar: SidecarData | null;
4040
shadowCall: ShadowCallData | null | undefined;
41-
usage30d: UsageSummary30d | null;
4241
maMode: "v1" | "default" | "v2";
4342
maModeResolved: boolean;
4443
/** Absent when the optional endpoint failed — callers must keep prior UI state. */
@@ -89,6 +88,14 @@ export async function fetchDashboardModels(apiBase: string, signal: AbortSignal)
8988
return requireJson<ModelInfo[]>(response);
9089
}
9190

91+
export async function fetchDashboardUsage(apiBase: string, signal: AbortSignal): Promise<UsageSummary30d> {
92+
const response = await fetch(`${apiBase}/api/usage?range=30d`, { signal });
93+
// Usage can be expensive on an older server. Keeping it in its own resource means
94+
// it cannot delay health/provider/settings commits, and a failed refresh retains
95+
// the last good usage snapshot.
96+
return requireJson<UsageSummary30d>(response);
97+
}
98+
9299
export async function fetchDashboardCore(
93100
apiBase: string,
94101
signal: AbortSignal,
@@ -112,7 +119,6 @@ export async function fetchDashboardCore(
112119
startupHealthSeed: undefined,
113120
sidecar: null,
114121
shadowCall: undefined,
115-
usage30d: null,
116122
maMode: "default",
117123
maModeResolved: true,
118124
injection: undefined,
@@ -121,13 +127,12 @@ export async function fetchDashboardCore(
121127
};
122128

123129
try {
124-
const [hRes, pRes, sRes, scRes, shRes, uRes] = await Promise.all([
130+
const [hRes, pRes, sRes, scRes, shRes] = await Promise.all([
125131
fetch(`${apiBase}/healthz`, { signal }),
126132
fetch(`${apiBase}/api/providers`, { signal }),
127133
fetch(`${apiBase}/api/settings`, { signal }),
128134
fetch(`${apiBase}/api/sidecar-settings`, { signal }),
129135
fetch(`${apiBase}/api/shadow-call-settings`, { signal }),
130-
fetch(`${apiBase}/api/usage?range=30d`, { signal }),
131136
]);
132137

133138
const health = await requireJson<HealthData>(hRes);
@@ -176,14 +181,6 @@ export async function fetchDashboardCore(
176181
}
177182
}
178183

179-
// Usage is best-effort: a malformed/empty body must not mark the whole dashboard offline.
180-
let usage30d: UsageSummary30d | null = null;
181-
try {
182-
usage30d = (await readJsonIfOk<UsageSummary30d>(uRes)) ?? null;
183-
} catch {
184-
usage30d = null;
185-
}
186-
187184
let maMode: "v1" | "default" | "v2" = "default";
188185
let maModeResolved = false;
189186
try {
@@ -236,7 +233,6 @@ export async function fetchDashboardCore(
236233
startupHealthSeed,
237234
sidecar,
238235
shadowCall,
239-
usage30d,
240236
maMode,
241237
maModeResolved,
242238
injection,

‎gui/src/pages/use-dashboard-data.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "../startup-health-ui";
99
import {
1010
fetchDashboardCore,
11+
fetchDashboardUsage,
1112
fetchDashboardModels,
1213
fetchProjectConfigDiagnostics,
1314
fetchStartupHealth,
@@ -147,6 +148,13 @@ export function useDashboardData(apiBase: string) {
147148
{ pollMs: 5000 },
148149
);
149150

151+
const usagePoll = useKeyedClientResource(
152+
`dashboard-usage:${apiBase}`,
153+
[apiBase],
154+
(signal) => fetchDashboardUsage(apiBase, signal),
155+
{ pollMs: 5000 },
156+
);
157+
150158
const diagnosticsPoll = useKeyedClientResource(
151159
`dashboard-diagnostics:${apiBase}`,
152160
[apiBase],
@@ -188,7 +196,6 @@ export function useDashboardData(apiBase: string) {
188196
}
189197
if (data.sidecar) setSidecar(data.sidecar);
190198
if (data.shadowCall !== undefined) setShadowCall(data.shadowCall);
191-
setUsage30d(data.usage30d);
192199
setMaMode(data.maMode);
193200
setMaModeResolved(data.maModeResolved);
194201
if (data.injection) {
@@ -205,6 +212,10 @@ export function useDashboardData(apiBase: string) {
205212
setError(data.error);
206213
}, [corePoll.data]);
207214

215+
useEffect(() => {
216+
if (usagePoll.data !== undefined) setUsage30d(usagePoll.data);
217+
}, [usagePoll.data]);
218+
208219
useEffect(() => {
209220
if (diagnosticsPoll.data) setProjectConfigWarnings(diagnosticsPoll.data);
210221
}, [diagnosticsPoll.data]);

‎gui/tests/dashboard-contracts.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ test("Dashboard wires a single project-config diagnostics owner outside the sett
1919
expect(coreBody).not.toContain("diagnostics/project-config");
2020
});
2121

22+
test("Dashboard usage polling cannot delay core health and settings", async () => {
23+
const core = await Bun.file(new URL("../src/pages/dashboard-core-poll.ts", import.meta.url)).text();
24+
const hook = await Bun.file(new URL("../src/pages/use-dashboard-data.ts", import.meta.url)).text();
25+
const coreFnStart = core.indexOf("export async function fetchDashboardCore");
26+
const usageFnStart = core.indexOf("export async function fetchDashboardUsage");
27+
expect(coreFnStart).toBeGreaterThan(-1);
28+
expect(usageFnStart).toBeGreaterThan(-1);
29+
expect(core.slice(coreFnStart)).not.toContain("/api/usage?range=30d");
30+
expect(hook).toContain("dashboard-usage:${apiBase}");
31+
expect(hook).toContain("fetchDashboardUsage(apiBase, signal)");
32+
});
33+
2234
test("Dashboard workspace pane is a labelled section, not a nested main landmark", async () => {
2335
const src = await Bun.file(new URL("../src/pages/Dashboard.tsx", import.meta.url)).text();
2436
expect(src).toContain("dashboard-workspace-main");

‎src/server/management/logs-usage-routes.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { primeCodexPoolQuotas } from "../../codex/auth-api";
3434
import { DEFAULT_PROVIDER_CONTEXT_CAP, globalContextCapValue, providerContextCap, providerContextCaps, setAllProviderContextCaps, setGlobalContextCapValue, setProviderContextCap } from "../../providers/context-cap";
3535
import { resolveCodexHomeDir } from "../../codex/home";
3636
import { scanStorage } from "../../storage/scanner";
37-
import { readUsageEntries } from "../../usage/log";
37+
import { readUsageEntriesForManagement } from "../../usage/log";
3838
import { getUsageDebugLogEntries } from "../../usage/debug";
3939
import { parseRange, parseUsageSurface, summarizeUsage } from "../../usage/summary";
4040
import { stripCodexRuntimeProviderFields } from "../../codex/auth-context";
@@ -123,7 +123,7 @@ export async function handleLogsUsageRoutes(ctx: ManagementContext): Promise<Res
123123
const surface = parseUsageSurface(url.searchParams.get("surface"));
124124
const now = Date.now();
125125
try {
126-
return jsonResponse(summarizeUsage(readUsageEntries(), range, now, surface));
126+
return jsonResponse(summarizeUsage(await readUsageEntriesForManagement(), range, now, surface));
127127
} catch {
128128
return jsonResponse({
129129
range,

0 commit comments

Comments
 (0)