feat(web): filter the Usage page to just the providers you want (#16970)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
10e29a26c2
commit
13c5328dd0
7 files changed
+204
-20
No files matched your search
@@ -1,4 +1,9 @@
|
||||
import { EnvironmentId, UsageDay, USAGE_CONTRACT_VERSION } from "@t3tools/contracts";
|
||||
import {
|
||||
EnvironmentId,
|
||||
UsageDay,
|
||||
USAGE_CONTRACT_VERSION,
|
||||
type UsageProviderKind,
|
||||
} from "@t3tools/contracts";
|
||||
import { act, useLayoutEffect } from "react";
|
||||
import { create, type ReactTestRenderer } from "react-test-renderer";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
|
||||
@@ -17,7 +22,12 @@ const input = {
|
||||
timeZone: "UTC",
|
||||
};
|
||||
|
||||
function environment(id: string, cost: number | null, hostId = id): EnvironmentUsageStatus {
|
||||
function environment(
|
||||
id: string,
|
||||
cost: number | null,
|
||||
hostId = id,
|
||||
provider: UsageProviderKind = "codex",
|
||||
): EnvironmentUsageStatus {
|
||||
return {
|
||||
environmentId: EnvironmentId.make(id),
|
||||
label: id,
|
||||
@@ -35,7 +45,7 @@ function environment(id: string, cost: number | null, hostId = id): EnvironmentU
|
||||
buckets: [
|
||||
{
|
||||
day: input.sinceDay,
|
||||
provider: "codex",
|
||||
provider,
|
||||
model: id,
|
||||
totals: {
|
||||
uncachedInputTokens: 100,
|
||||
@@ -56,7 +66,7 @@ function environment(id: string, cost: number | null, hostId = id): EnvironmentU
|
||||
{
|
||||
fingerprint: {
|
||||
hostId,
|
||||
provider: "codex",
|
||||
provider,
|
||||
resolvedHomePath: "/sessions",
|
||||
volumeId: hostId,
|
||||
},
|
||||
@@ -77,8 +87,14 @@ function environment(id: string, cost: number | null, hostId = id): EnvironmentU
|
||||
let renderer: ReactTestRenderer | undefined;
|
||||
let latest: UsageView;
|
||||
|
||||
function Probe({ selected }: { selected: ReadonlySet<EnvironmentId> | null }) {
|
||||
const usage = useUsage(input, selected);
|
||||
function Probe({
|
||||
selected,
|
||||
hidden,
|
||||
}: {
|
||||
selected: ReadonlySet<EnvironmentId> | null;
|
||||
hidden?: ReadonlySet<UsageProviderKind>;
|
||||
}) {
|
||||
const usage = useUsage(input, selected, hidden);
|
||||
useLayoutEffect(() => {
|
||||
latest = usage;
|
||||
}, [usage]);
|
||||
@@ -166,3 +182,17 @@ describe("usage environment selection", () => {
|
||||
expect(latest.isPartial).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("usage provider filter", () => {
|
||||
it("drops hidden providers from totals and sessions, then restores them", async () => {
|
||||
testState.environments = [environment("a", 10), environment("c", 5, "c", "claude")];
|
||||
await act(() => renderer?.update(<Probe selected={null} hidden={new Set(["codex"])} />));
|
||||
expect(latest.merged.costUsd).toBe(5);
|
||||
expect(latest.merged.sessions).toBe(1);
|
||||
expect(latest.merged.providers.map((entry) => entry.provider)).toEqual(["claude"]);
|
||||
|
||||
await act(() => renderer?.update(<Probe selected={null} />));
|
||||
expect(latest.merged.costUsd).toBe(15);
|
||||
expect(latest.merged.sessions).toBe(2);
|
||||
});
|
||||
});
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
type EnvironmentId,
|
||||
type UsageBucket,
|
||||
type UsageSummary,
|
||||
type UsageProviderKind,
|
||||
type UsageSummaryInput,
|
||||
} from "@t3tools/contracts";
|
||||
import { needsCursorKeychainAccess, refreshUsage } from "@t3tools/client-runtime/state/usage";
|
||||
@@ -128,9 +129,39 @@ export function mergeAnsweredUsage(
|
||||
return mergeUsage(answered, USAGE_CONTRACT_VERSION);
|
||||
}
|
||||
|
||||
const NO_HIDDEN_PROVIDERS: ReadonlySet<UsageProviderKind> = new Set();
|
||||
|
||||
/**
|
||||
* Drops hidden providers' buckets and sources before merging, so totals,
|
||||
* shares, and session counts all describe only the visible providers.
|
||||
*/
|
||||
function withoutProviders(
|
||||
environments: readonly EnvironmentUsageStatus[],
|
||||
hiddenProviders: ReadonlySet<UsageProviderKind>,
|
||||
): readonly EnvironmentUsageStatus[] {
|
||||
if (hiddenProviders.size === 0) return environments;
|
||||
return environments.map((environment) =>
|
||||
environment.summary === null
|
||||
? environment
|
||||
: {
|
||||
...environment,
|
||||
summary: {
|
||||
...environment.summary,
|
||||
buckets: environment.summary.buckets.filter(
|
||||
(bucket) => !hiddenProviders.has(bucket.provider),
|
||||
),
|
||||
sources: environment.summary.sources.filter(
|
||||
(source) => !hiddenProviders.has(source.fingerprint.provider),
|
||||
),
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export function useUsage(
|
||||
input: UsageSummaryInput,
|
||||
selectedEnvironmentIds: ReadonlySet<EnvironmentId> | null = null,
|
||||
hiddenProviders: ReadonlySet<UsageProviderKind> = NO_HIDDEN_PROVIDERS,
|
||||
): UsageView {
|
||||
const windowKey = useMemo(
|
||||
() =>
|
||||
@@ -183,7 +214,10 @@ export function useUsage(
|
||||
[selectedEnvironments, windowKey],
|
||||
);
|
||||
|
||||
const merged = useMemo(() => mergeAnsweredUsage(selectedEnvironments), [selectedEnvironments]);
|
||||
const merged = useMemo(
|
||||
() => mergeAnsweredUsage(withoutProviders(selectedEnvironments, hiddenProviders)),
|
||||
[selectedEnvironments, hiddenProviders],
|
||||
);
|
||||
|
||||
const answeredCount = selectedEnvironments.filter(
|
||||
(environment) => environment.summary !== null,
|
||||
|
||||
Reference in new issue
Block a user