Skip to content

Commit bee411d

Browse files
talissoncostaclaude
andcommitted
fix(usage): compare the project with the same period it is shown against
The contribution note divided a project's usage over the period on screen by the organisation's usage over the allowance window. Different windows, so on Last 90 days with a project chosen it could read as more than 100% of the organisation. It now takes the unfiltered total for the period being viewed. With no project chosen the arguments match the query already running and RTK serves both from one request, so the extra call only happens while a filter is applied. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 86f189b commit bee411d

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

frontend/documentation/components/UsageDashboard.stories.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,18 @@ const UsagePage: FC<HarnessProps> = ({
100100
scenarioFor(billingPeriod, !!empty, isFreePlan),
101101
share * scale,
102102
)
103-
const organisationTotal = toUsageResponse(
103+
const allowanceTotal = toUsageResponse(
104104
scenarioFor(allowanceWindow(basis), !!empty, isFreePlan),
105105
scale,
106106
).totals.total
107107

108+
// The note needs the organisation over the period on screen, not over the
109+
// allowance window, or a project can read as more than all of it.
110+
const periodTotal = toUsageResponse(
111+
scenarioFor(billingPeriod, !!empty, isFreePlan),
112+
scale,
113+
).totals.total
114+
108115
const { setDimension, ...breakdown } = useUsageBreakdown({ data: scoped })
109116

110117
const scope = `${filtered ? project : 'All projects'} · ${periodLabel(
@@ -149,7 +156,7 @@ const UsagePage: FC<HarnessProps> = ({
149156
limit={limit}
150157
meterNote={
151158
filtered
152-
? contributionNote(project, scoped.totals.total, organisationTotal)
159+
? contributionNote(project, scoped.totals.total, periodTotal)
153160
: undefined
154161
}
155162
onRetry={() => {}}
@@ -159,7 +166,7 @@ const UsagePage: FC<HarnessProps> = ({
159166
billingPeriod,
160167
filtered ? 1 : undefined,
161168
)}
162-
total={organisationTotal}
169+
total={allowanceTotal}
163170
/>
164171
)
165172
}

frontend/web/components/pages/usage/UsageDashboardPage.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ const UsageDashboardPage: FC<UsageDashboardPageProps> = ({
8484
{ refetchOnFocus: false },
8585
)
8686

87+
// The note compares a project with the organisation over the same period, so
88+
// it needs the unfiltered total for the period on screen, not the allowance
89+
// one. With no project chosen the arguments match the query above and RTK
90+
// serves both from one request.
91+
const { data: periodData } = useGetOrganisationUsageQuery(
92+
organisationId && organisation && selectedProjectId
93+
? { billing_period: billingPeriod, organisationId }
94+
: skipToken,
95+
{ refetchOnFocus: false },
96+
)
97+
8798
const {
8899
data: subscriptionMeta,
89100
isLoading: loadingLimit,
@@ -122,7 +133,7 @@ const UsageDashboardPage: FC<UsageDashboardPageProps> = ({
122133
? contributionNote(
123134
projectName,
124135
data?.totals?.total ?? 0,
125-
organisationData?.totals?.total ?? 0,
136+
periodData?.totals?.total ?? 0,
126137
)
127138
: undefined
128139
}

frontend/web/components/pages/usage/__tests__/utils.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ describe('UsageDashboard utils', () => {
6262
it('has nothing to say when the organisation used nothing', () => {
6363
expect(contributionNote('Checkout', 0, 0)).toBeUndefined()
6464
})
65+
66+
it('never reports a project as more than all of the usage', () => {
67+
expect(contributionNote('Checkout', 1000000, 1000000)).toBe(
68+
'Checkout accounts for 100% of that usage.',
69+
)
70+
})
6571
})
6672

6773
describe('planSectionCopy', () => {

0 commit comments

Comments
 (0)