Skip to content

Commit a571a8c

Browse files
saphidclaude
andcommitted
refactor(usage): share the duplicate project title id
Web and mobile each worked out which id prefix tells same-titled projects apart. The shared usage merge now does it once, with a test, and both clients read the result. Project rows also use the shared unpriced-cost check. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent d98155e commit a571a8c

4 files changed

Lines changed: 75 additions & 52 deletions

File tree

‎apps/mobile/src/features/usage/UsageRouteScreen.tsx‎

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -850,19 +850,11 @@ function ProjectRows(props: {
850850
? b.costUsd - a.costUsd || b.totalTokens - a.totalTokens
851851
: b.totalTokens - a.totalTokens || b.costUsd - a.costUsd,
852852
);
853-
const groups = new Map<string, string[]>();
854-
for (const row of ordered) {
855-
if (row.kind !== "project") continue;
856-
const key = `${row.environmentId}\0${row.title}`;
857-
const peers = groups.get(key) ?? [];
858-
peers.push(row.projectId);
859-
groups.set(key, peers);
860-
}
861853
return (
862854
<View>
863855
<View className="flex-row flex-wrap">
864856
{ordered.map((row, index) => {
865-
const unknown = row.records > 0 && row.unpricedRecords >= row.records;
857+
const unknown = isModelCostUnknown(row);
866858
const label =
867859
row.kind === "project"
868860
? row.title
@@ -875,7 +867,7 @@ function ProjectRows(props: {
875867
: unknown
876868
? "Unpriced"
877869
: formatUsd(row.costUsd);
878-
const detail = projectDetail(row, metric, unknown, groups, props.showEnvironment);
870+
const detail = projectDetail(row, metric, unknown, props.showEnvironment);
879871
return (
880872
<View
881873
key={row.key}
@@ -922,22 +914,12 @@ function projectDetail(
922914
row: ProjectTotals,
923915
metric: UsageChartMetric,
924916
unknown: boolean,
925-
groups: ReadonlyMap<string, readonly string[]>,
926917
showEnvironment: boolean,
927918
) {
928919
const qualifiers: string[] = [];
929920
if (row.kind === "project") {
930921
if (showEnvironment) qualifiers.push(row.environmentLabel);
931-
const peers = groups.get(`${row.environmentId}\0${row.title}`) ?? [];
932-
if (peers.length > 1) {
933-
let length = 8;
934-
while (length < row.projectId.length) {
935-
const prefix = row.projectId.slice(0, length);
936-
if (!peers.some((id) => id !== row.projectId && id.startsWith(prefix))) break;
937-
length += 1;
938-
}
939-
qualifiers.push(`ID ${row.projectId.slice(0, length)}`);
940-
}
922+
if (row.duplicateTitleId !== null) qualifiers.push(`ID ${row.duplicateTitleId}`);
941923
} else {
942924
qualifiers.push(
943925
row.kind === "unlinked" ? "No single T3 project" : "Project details unavailable",

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

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -652,14 +652,6 @@ function UsageOverview({
652652
(left, right) => right.totalTokens - left.totalTokens || right.costUsd - left.costUsd,
653653
)
654654
: merged.projects;
655-
const projectGroups = new Map<string, string[]>();
656-
for (const project of breakdownProjects) {
657-
if (project.kind !== "project") continue;
658-
const key = `${project.environmentId}\0${project.title}`;
659-
const peers = projectGroups.get(key) ?? [];
660-
peers.push(project.projectId);
661-
projectGroups.set(key, peers);
662-
}
663655
const projectPeak = breakdownProjects.reduce(
664656
(peak, project) => Math.max(peak, metric === "tokens" ? project.totalTokens : project.costUsd),
665657
0,
@@ -984,25 +976,7 @@ function UsageOverview({
984976
) : (
985977
breakdownProjects.map((project, index) => {
986978
const value = metric === "tokens" ? project.totalTokens : project.costUsd;
987-
const costUnknown =
988-
project.records > 0 && project.unpricedRecords >= project.records;
989-
let projectIdLabel: string | null = null;
990-
if (project.kind === "project") {
991-
const peers =
992-
projectGroups.get(`${project.environmentId}\0${project.title}`) ?? [];
993-
if (peers.length > 1) {
994-
let length = 8;
995-
while (length < project.projectId.length) {
996-
const prefix = project.projectId.slice(0, length);
997-
if (
998-
!peers.some((id) => id !== project.projectId && id.startsWith(prefix))
999-
)
1000-
break;
1001-
length += 1;
1002-
}
1003-
projectIdLabel = `ID ${project.projectId.slice(0, length)}`;
1004-
}
1005-
}
979+
const costUnknown = isModelCostUnknown(project);
1006980
return (
1007981
<tr
1008982
key={project.key}
@@ -1020,8 +994,8 @@ function UsageOverview({
1020994
{project.kind === "project" && selectedEnvironments.length > 1 ? (
1021995
<span className="ml-1.5 text-xs">{project.environmentLabel}</span>
1022996
) : null}
1023-
{projectIdLabel !== null ? (
1024-
<span className="block text-xs">{projectIdLabel}</span>
997+
{project.duplicateTitleId !== null ? (
998+
<span className="block text-xs">ID {project.duplicateTitleId}</span>
1025999
) : null}
10261000
{value > 0 && projectPeak > 0 ? (
10271001
<div aria-hidden className="mt-1.5 h-0.5 max-w-48">

‎packages/shared/src/usageMerge.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,44 @@ describe("mergeUsage", () => {
207207
]);
208208
});
209209

210+
it("gives same-titled projects in one environment a distinguishing id prefix", () => {
211+
const source = { provider: "claude" as const, hostId: "mac", homePath: "/claude" };
212+
const projectRow = (projectId: string, costUsd: number) => ({
213+
projectId: projectId as ProjectId,
214+
provider: "claude" as const,
215+
sourcePath: "/claude",
216+
totals: bucket().totals,
217+
costUsd,
218+
records: 1,
219+
unpricedRecords: 0,
220+
sessions: 1,
221+
});
222+
const merged = mergeUsage(
223+
[
224+
environment("env-a", {
225+
...summary([bucket({ sourcePath: "/claude", costUsd: 6 })], [source]),
226+
projectTotals: [
227+
projectRow("project-aaaa-1", 3),
228+
projectRow("project-aaaa-2", 2),
229+
projectRow("project-bbbb-1", 1),
230+
],
231+
projects: [
232+
{ projectId: "project-aaaa-1" as ProjectId, title: "App" },
233+
{ projectId: "project-aaaa-2" as ProjectId, title: "App" },
234+
{ projectId: "project-bbbb-1" as ProjectId, title: "Site" },
235+
],
236+
}),
237+
],
238+
USAGE_CONTRACT_VERSION,
239+
);
240+
241+
expect(merged.projects.map((project) => project.duplicateTitleId)).toEqual([
242+
"project-aaaa-1",
243+
"project-aaaa-2",
244+
null,
245+
]);
246+
});
247+
210248
it("drops only the duplicated provider, keeping the environment's other one", () => {
211249
const sharedClaude = {
212250
provider: "claude" as const,

‎packages/shared/src/usageMerge.ts‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ export interface ModelTotals {
4040
}
4141

4242
/** When every record lacks rates, costUsd is unknown and must not be displayed as a dollar figure. */
43-
export function isModelCostUnknown(model: ModelTotals): boolean {
44-
return model.records > 0 && model.unpricedRecords >= model.records;
43+
export function isModelCostUnknown(
44+
totals: Pick<ModelTotals, "records" | "unpricedRecords">,
45+
): boolean {
46+
return totals.records > 0 && totals.unpricedRecords >= totals.records;
4547
}
4648

4749
export interface DailyTotals {
@@ -75,6 +77,8 @@ export type ProjectIdentity =
7577

7678
export type ProjectTotals = ProjectIdentity & {
7779
readonly key: string;
80+
/** Shortest id prefix that tells this project from same-titled ones in its environment. */
81+
readonly duplicateTitleId: string | null;
7882
readonly costUsd: number;
7983
readonly totalTokens: number;
8084
readonly records: number;
@@ -619,10 +623,35 @@ export function mergeUsage(
619623
}))
620624
.sort((a, b) => b.costUsd - a.costUsd || b.totalTokens - a.totalTokens);
621625

626+
const projectIdsByTitle = new Map<string, ProjectId[]>();
627+
const titleKey = (identity: ProjectIdentity & { readonly kind: "project" }) =>
628+
JSON.stringify([identity.environmentId, identity.title]);
629+
for (const { identity } of projectAccumulator.values()) {
630+
if (identity.kind !== "project") continue;
631+
const peers = projectIdsByTitle.get(titleKey(identity)) ?? [];
632+
peers.push(identity.projectId);
633+
projectIdsByTitle.set(titleKey(identity), peers);
634+
}
635+
const duplicateTitleId = (identity: ProjectIdentity): string | null => {
636+
if (identity.kind !== "project") return null;
637+
const peers = projectIdsByTitle.get(titleKey(identity)) ?? [];
638+
if (peers.length < 2) return null;
639+
const id = identity.projectId;
640+
let length = Math.min(8, id.length);
641+
while (
642+
length < id.length &&
643+
peers.some((peer) => peer !== id && peer.startsWith(id.slice(0, length)))
644+
) {
645+
length += 1;
646+
}
647+
return id.slice(0, length);
648+
};
649+
622650
const projects: ProjectTotals[] = [...projectAccumulator.entries()]
623651
.map(([key, { identity, ...totals }]) => ({
624652
...identity,
625653
key,
654+
duplicateTitleId: duplicateTitleId(identity),
626655
...totals,
627656
costShare: costUsd === 0 ? 0 : totals.costUsd / costUsd,
628657
tokenShare: totalTokens === 0 ? 0 : totals.totalTokens / totalTokens,

0 commit comments

Comments
 (0)