Skip to content

Commit f3963d1

Browse files
fix(web): search reaches models from every view; active days are local
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent cc615a2 commit f3963d1

3 files changed

Lines changed: 70 additions & 24 deletions

File tree

‎apps/web/src/components/usage/UsageExplorer.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ export function UsageExplorer(props: UsageExplorerProps) {
812812
</div>
813813
</section>
814814

815-
<Tiles total={total} facts={visible} timeline={timeline} />
815+
<Tiles total={total} facts={visible} timeline={timeline} timeZone={timeZone} />
816816

817817
{tokensOf(total) > 0 ? (
818818
<section className="grid gap-x-12 gap-y-8 lg:grid-cols-2">
@@ -1295,14 +1295,18 @@ function Tiles({
12951295
total,
12961296
facts,
12971297
timeline,
1298+
timeZone,
12981299
}: {
12991300
readonly total: UsageTotals;
13001301
readonly facts: readonly UsageFact[];
13011302
readonly timeline: WindowTimeline;
1303+
readonly timeZone: string;
13021304
}) {
1303-
// Short spans read better per hour than per partial day.
1305+
// Short spans read better per hour than per partial day. Days are local.
13041306
const hourly = timeline.hours.length > 0 && timeline.hours.length <= 48;
1305-
const active = new Set(facts.map((fact) => (hourly ? fact.time : fact.time.slice(0, 10)))).size;
1307+
const active = new Set(
1308+
facts.map((fact) => (hourly ? fact.time : binKey(fact.time, 1440, timeZone))),
1309+
).size;
13061310
const processed = tokensOf(total);
13071311
const input = total.input + total.cacheRead + total.cacheWrite;
13081312
const uncached = total.costUsd + total.cacheSavingsUsd;

‎apps/web/src/components/usage/usageBreakdownRows.ts‎

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,20 @@ export function buildBreakdownRows(input: BreakdownInput): readonly BreakdownRow
139139
metric,
140140
);
141141
const search = query.trim();
142-
// Threads matching the search, and every thread above them.
142+
// Threads whose title, or a model they used, matches the search, and every
143+
// thread above them.
143144
const matchedThreads = new Set<string>();
144145
if (search !== "") {
145-
for (const [key, info] of tree.info) {
146-
if (info.title !== null && matchesQuery(info.title, search)) {
147-
for (let up: string | undefined = key; up !== undefined; up = tree.parentOf.get(up)) {
148-
matchedThreads.add(up);
149-
}
146+
const mark = (key: string) => {
147+
for (let up: string | undefined = key; up !== undefined; up = tree.parentOf.get(up)) {
148+
matchedThreads.add(up);
150149
}
150+
};
151+
for (const [key, info] of tree.info) {
152+
if (info.title !== null && matchesQuery(info.title, search)) mark(key);
153+
}
154+
for (const fact of facts) {
155+
if (fact.thread !== null && matchesQuery(fact.model, search)) mark(fact.thread);
151156
}
152157
}
153158
const threadMatches = (key: string) => search === "" || matchedThreads.has(key);
@@ -167,19 +172,34 @@ export function buildBreakdownRows(input: BreakdownInput): readonly BreakdownRow
167172
return cached;
168173
};
169174

170-
const itemMatches = (dim: BreakdownDimension, key: string, scope: readonly UsageFact[]) => {
171-
if (search === "") return true;
172-
if (matchesQuery(nameOf(dim, key), search)) return true;
173-
if (dim === "thread") return threadMatches(key);
174-
// A project matches when one of its threads does.
175-
return (
176-
dim === "project" &&
177-
scope.some(
178-
(fact) =>
179-
fact.project === key && fact.thread !== null && threadMatches(tree.rootOf(fact.thread)),
180-
)
181-
);
182-
};
175+
/**
176+
* Whether something an item lists under it matches the search: a project's
177+
* threads, a provider's accounts and models, an account's models, a model's
178+
* projects. Such an item stays and opens, so the match is reachable.
179+
*/
180+
const childMatches = (dim: BreakdownDimension, key: string, scope: readonly UsageFact[]) =>
181+
scope.some((fact) => {
182+
if (keyFor(dim, fact, tree) !== key) return false;
183+
switch (dim) {
184+
case "project":
185+
return fact.thread !== null && threadMatches(tree.rootOf(fact.thread));
186+
case "provider":
187+
return (
188+
matchesQuery(fact.model, search) ||
189+
matchesQuery(nameOf("account", fact.account), search)
190+
);
191+
case "account":
192+
return matchesQuery(fact.model, search);
193+
case "model":
194+
return matchesQuery(nameOf("project", fact.project), search);
195+
case "thread":
196+
return false;
197+
}
198+
});
199+
const ownMatch = (dim: BreakdownDimension, key: string) =>
200+
matchesQuery(nameOf(dim, key), search) || (dim === "thread" && threadMatches(key));
201+
const itemMatches = (dim: BreakdownDimension, key: string, scope: readonly UsageFact[]) =>
202+
search === "" || ownMatch(dim, key) || childMatches(dim, key, scope);
183203

184204
const ranked = (
185205
dim: BreakdownDimension,
@@ -225,7 +245,11 @@ export function buildBreakdownRows(input: BreakdownInput): readonly BreakdownRow
225245
: childDim !== null && depth < 3 && !(dim === "project" && !scopeThreads?.any);
226246
// A search opens the path to its matches.
227247
const isOpen =
228-
expandable && (open.has(path) || (search !== "" && depth === 0 && dim === "project"));
248+
expandable &&
249+
(open.has(path) ||
250+
(search !== "" &&
251+
(isThread ? liveChildren.some(threadMatches) : !ownMatch(dim, entry.key)) &&
252+
(isThread || childMatches(dim, entry.key, scope))));
229253
const familyCount = isThread ? (scopeThreads?.families.get(entry.key)?.descendants ?? 0) : 0;
230254
rows.push({
231255
kind: "item",

‎apps/web/src/components/usage/usageExplorerModel.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,29 @@ describe("buildBreakdownRows", () => {
327327
expect(rows.at(-1)).toMatchObject({ kind: "fewer", target: "\u0000top" });
328328
});
329329

330-
it("opens projects whose threads match the search", () => {
330+
it("opens the path down to a sub-agent that matches the search", () => {
331331
const rows = buildBreakdownRows(input({ query: "explore" }));
332332
expect(rows.map((row) => (row.kind === "item" ? row.key : row.kind))).toEqual([
333333
"env-a\u001fapp",
334334
"env-a\u001ft3:parent",
335+
"leaf",
336+
"env-a\u001fagent:claude:s:a",
337+
]);
338+
});
339+
340+
it("finds a model by name from the Projects and Providers views", () => {
341+
const byProject = buildBreakdownRows(input({ query: "gpt-6" }));
342+
// The Codex session is unattributed (outside projects), so only its project row stays.
343+
expect(byProject.filter((row) => row.kind === "item").map((row) => row.key)).toEqual([
344+
OUTSIDE_PROJECTS,
345+
"env-a\u001fsession:codex:x",
346+
]);
347+
const byProvider = buildBreakdownRows(
348+
input({ dimension: "provider", query: "gpt-6", nameOf: (_: string, key: string) => key }),
349+
);
350+
expect(byProvider.filter((row) => row.kind === "item").map((row) => row.key)).toEqual([
351+
"codex",
352+
"codex\u001fgpt-6-sol",
335353
]);
336354
});
337355
});

0 commit comments

Comments
 (0)