Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class FixtureQueryClient extends QueryClient {
const {remaining, total} = this.wallet;
return {
balance: remaining,
dailyLimit: null,
percentUsed: 1 - remaining / total,
included: {remaining, resetsAt: null, total},
purchased: {remaining: 0, expiresAt: null, total: 0},
Expand Down
78 changes: 60 additions & 18 deletions static/gsApp/components/ai/seerCreditUsageMeter.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,23 @@ function mockWallet(
{
autoReload = false,
balance = 250,
dailyLimit = null,
percentUsed = 0.75,
included = 1000,
purchased = 0,
}: {
autoReload?: boolean;
balance?: number;
included?: number;
dailyLimit?: {remaining: number; total: number} | null;
percentUsed?: number;
purchased?: number;
} = {}
) {
const wallet = MockApiClient.addMockResponse({
url: `/customers/${organization.slug}/seer-wallet/`,
body: {
balance,
dailyLimit,
percentUsed,
included: {remaining: balance, resetsAt: null, total: included},
purchased: {remaining: 0, expiresAt: null, total: purchased},
included: {remaining: balance, resetsAt: null, total: 1000},
purchased: {remaining: 0, expiresAt: null, total: 0},
creditPriceCents: 1,
},
});
Expand Down Expand Up @@ -92,7 +91,7 @@ describe('SeerCreditUsageMeter', () => {
'Your organization has used 75% of your available monthly credits.'
)
).toBeInTheDocument();
expect(screen.getByText('250/1,000 credits remaining.')).toBeInTheDocument();
expect(screen.getByText('250 credits remaining.')).toBeInTheDocument();
// The out-of-credits alert, not the meter, is where auto-reload gets turned on.
expect(screen.queryByRole('link')).not.toBeInTheDocument();
});
Expand Down Expand Up @@ -154,23 +153,66 @@ describe('SeerCreditUsageMeter', () => {
expect(await screen.findByText('50%')).toBeInTheDocument();
});

it('rounds the percent and counts purchased credits in the total', async () => {
mockWallet(billingOrganization, {
balance: 1250,
percentUsed: 0.8754,
included: 8000,
purchased: 2000,
});
it('rounds the percent and formats the credits remaining', async () => {
mockWallet(billingOrganization, {balance: 1250, percentUsed: 0.8754});

render(<SeerCreditUsageMeter hasEnoughCredits />, {
organization: billingOrganization,
});

await userEvent.hover(await screen.findByText('88%'));

expect(
await screen.findByText('1,250/10,000 credits remaining.')
).toBeInTheDocument();
expect(await screen.findByText('1,250 credits remaining.')).toBeInTheDocument();
});

it('says credit, not credits, when one is left', async () => {
mockWallet(billingOrganization, {balance: 1, percentUsed: 0.999});

render(<SeerCreditUsageMeter hasEnoughCredits />, {
organization: billingOrganization,
});

await userEvent.hover(await screen.findByText('100%'));

expect(await screen.findByText('1 credit remaining.')).toBeInTheDocument();
});

it.each([
[
'what is left of the daily usage limit when it runs out first',
{autoReload: false, balance: 2000, dailyLimit: {total: 2500, remaining: 700}},
'Your organization has used 72% of its daily usage limit.',
'700 credits remaining today.',
],
[
'the wallet when it runs out first',
{autoReload: false, balance: 250, dailyLimit: {total: 2500, remaining: 700}},
'Your organization has used 75% of your available monthly credits.',
'250 credits remaining.',
],
[
'the daily usage limit when auto-reload refills the wallet',
{autoReload: true, balance: 250, dailyLimit: {total: 2500, remaining: 700}},
'Your organization has used 72% of its daily usage limit.',
'700 credits remaining today.',
],
[
'a $0 daily usage limit as fully used',
{autoReload: false, balance: 2000, dailyLimit: {total: 0, remaining: 0}},
'Your organization has used 100% of its daily usage limit.',
'0 credits remaining today.',
],
] as const)('counts down %s', async (_, wallet, usedText, remainingText) => {
mockWallet(billingOrganization, wallet);

render(<SeerCreditUsageMeter hasEnoughCredits />, {
organization: billingOrganization,
});

await userEvent.hover(await screen.findByTestId('seer-credit-usage-meter'));

expect(await screen.findByText(usedText)).toBeInTheDocument();
expect(screen.getByText(remainingText)).toBeInTheDocument();
});

it.each([
Expand Down Expand Up @@ -205,7 +247,7 @@ describe('SeerCreditUsageMeter', () => {
expect(
await screen.findByText('Your organization has used 75% of your available credits.')
).toBeInTheDocument();
expect(screen.getByText('250/1,000 credits remaining.')).toBeInTheDocument();
expect(screen.getByText('250 credits remaining.')).toBeInTheDocument();
expect(
screen.queryByText('Credits will reload automatically.')
).not.toBeInTheDocument();
Expand Down
34 changes: 29 additions & 5 deletions static/gsApp/components/ai/seerCreditUsageMeter.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default Storybook.story('SeerCreditUsageMeter', story => {
<SeerCreditUsageMeterView
percentUsed={0.75}
remaining={250}
total={1000}
countsDailyLimit={false}
autoReloadEnabled={false}
hasEnoughCredits
configureAutoReloadTo={`/settings/${organization.slug}/billing/overview/`}
Expand All @@ -91,7 +91,7 @@ export default Storybook.story('SeerCreditUsageMeter', story => {
<SeerCreditUsageMeterView
percentUsed={0.75}
remaining={250}
total={1000}
countsDailyLimit={false}
autoReloadEnabled
hasEnoughCredits
configureAutoReloadTo={`/settings/${organization.slug}/billing/overview/`}
Expand All @@ -116,7 +116,7 @@ export default Storybook.story('SeerCreditUsageMeter', story => {
<SeerCreditUsageMeterView
percentUsed={0.95}
remaining={50}
total={1000}
countsDailyLimit={false}
autoReloadEnabled
hasEnoughCredits={false}
configureAutoReloadTo={`/settings/${organization.slug}/billing/overview/`}
Expand All @@ -140,7 +140,7 @@ export default Storybook.story('SeerCreditUsageMeter', story => {
<SeerCreditUsageMeterView
percentUsed={0.95}
remaining={50}
total={1000}
countsDailyLimit={false}
autoReloadEnabled={false}
hasEnoughCredits={false}
configureAutoReloadTo={`/settings/${organization.slug}/billing/overview/`}
Expand All @@ -151,6 +151,30 @@ export default Storybook.story('SeerCreditUsageMeter', story => {
);
});

story('Daily usage limit', () => {
const organization = useOrganization();
return (
<Fragment>
<Text as="p">
With a daily usage limit, the meter counts down what's left of it today once it
would run out before the wallet. With auto-reload on, reloads refill the wallet
until the limit stops them, so the meter always counts the limit.
</Text>
<NextToSendButton>
<SeerCreditUsageMeterView
percentUsed={0.72}
remaining={700}
countsDailyLimit
autoReloadEnabled
hasEnoughCredits
configureAutoReloadTo={`/settings/${organization.slug}/billing/overview/`}
forceTooltipVisible
/>
</NextToSendButton>
</Fragment>
);
});

story('Colors', () => {
const organization = useOrganization();
const configureAutoReloadTo = `/settings/${organization.slug}/billing/overview/`;
Expand All @@ -173,7 +197,7 @@ export default Storybook.story('SeerCreditUsageMeter', story => {
<SeerCreditUsageMeterView
percentUsed={percentUsed}
remaining={Math.round((1 - percentUsed) * 1000)}
total={1000}
countsDailyLimit={false}
autoReloadEnabled={false}
hasEnoughCredits={hasEnoughCredits}
configureAutoReloadTo={configureAutoReloadTo}
Expand Down
82 changes: 63 additions & 19 deletions static/gsApp/components/ai/seerCreditUsageMeter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {Tooltip} from '@sentry/scraps/tooltip';

import {ProgressBar} from 'sentry/components/progressBar';
import type {SeerCreditUsageMeterProps} from 'sentry/components/seer/outOfCredits/seerCreditUsageMeter';
import {t, tct} from 'sentry/locale';
import {t, tct, tn} from 'sentry/locale';
import {useOrganization} from 'sentry/utils/useOrganization';

import {
SEER_AUTO_RELOAD_MODAL_HASH,
seerWalletAutoTopUpOptions,
seerWalletBalanceOptions,
type SeerWalletBalance,
} from 'getsentry/views/subscriptionPage/seerWallet/seerWalletOptions';

/**
Expand Down Expand Up @@ -52,18 +53,42 @@ export function SeerCreditUsageMeter({hasEnoughCredits}: SeerCreditUsageMeterPro
return null;
}

const autoReloadEnabled = autoTopUp?.enabled ?? null;
return (
<SeerCreditUsageMeterView
percentUsed={wallet.percentUsed}
remaining={wallet.balance}
total={wallet.included.total + wallet.purchased.total}
autoReloadEnabled={autoTopUp?.enabled ?? null}
{...getUsage(wallet, autoReloadEnabled)}
autoReloadEnabled={autoReloadEnabled}
hasEnoughCredits={hasEnoughCredits}
configureAutoReloadTo={`/settings/${organization.slug}/billing/overview/${SEER_AUTO_RELOAD_MODAL_HASH}`}
/>
);
}

/**
* Counts down whichever runs out first: the wallet or today's daily usage
* limit. With auto-reload on, reloads refill the wallet until the daily limit
* stops them, so only the limit runs out.
*/
function getUsage(
wallet: SeerWalletBalance,
autoReloadEnabled: boolean | null
): Pick<SeerCreditUsageMeterViewProps, 'countsDailyLimit' | 'percentUsed' | 'remaining'> {
const {dailyLimit} = wallet;
if (dailyLimit && (autoReloadEnabled || dailyLimit.remaining <= wallet.balance)) {
return {
countsDailyLimit: true,
// A $0 limit blocks all usage, so it's fully used.
percentUsed: dailyLimit.total > 0 ? 1 - dailyLimit.remaining / dailyLimit.total : 1,
remaining: dailyLimit.remaining,
};
}
return {
countsDailyLimit: false,
percentUsed: wallet.percentUsed,
remaining: wallet.balance,
};
}

interface SeerCreditUsageMeterViewProps {
/**
* `null` for members without `org:billing`, who can't read the auto-reload
Expand All @@ -74,17 +99,26 @@ interface SeerCreditUsageMeterViewProps {
* Where the tooltip links once auto-reload has run out of auto-reloads.
*/
configureAutoReloadTo: LinkProps['to'];
/**
* Whether the meter counts today's daily usage limit rather than the
* wallet's included plus purchased credits.
*/
countsDailyLimit: boolean;
/**
* Whether the organization can afford the operation next to the meter.
* With auto-reload on, it can't only once the auto-reloads have run out.
*/
hasEnoughCredits: boolean;
/**
* Share of included plus purchased credits used, from 0 to 1.
* Share used, from 0 to 1, of whatever `countsDailyLimit` says the meter
* counts.
*/
percentUsed: number;
/**
* Credits left of whatever `countsDailyLimit` says the meter counts. Can go
* negative.
*/
remaining: number;
total: number;
/**
* Keeps the tooltip open, for stories and screenshots.
*/
Expand All @@ -98,15 +132,16 @@ interface SeerCreditUsageMeterViewProps {
export function SeerCreditUsageMeterView({
autoReloadEnabled,
configureAutoReloadTo,
countsDailyLimit,
hasEnoughCredits,
percentUsed,
remaining,
total,
forceTooltipVisible,
}: SeerCreditUsageMeterViewProps) {
// The balance can go negative, which would put the share used past 100%.
const percent = Math.round(Math.min(Math.max(percentUsed, 0), 1) * 100);
const isWarning = percent >= WARNING_PERCENT || !hasEnoughCredits;
const shownRemaining = Math.max(remaining, 0);

return (
<Tooltip
Expand All @@ -116,20 +151,29 @@ export function SeerCreditUsageMeterView({
title={
<Stack gap="lg">
<Text>
{autoReloadEnabled === false
? tct(
'Your organization has used [percent] of your available monthly credits.',
{percent: `${percent}%`}
)
: tct('Your organization has used [percent] of your available credits.', {
{countsDailyLimit
? tct('Your organization has used [percent] of its daily usage limit.', {
percent: `${percent}%`,
})}
})
: autoReloadEnabled === false
? tct(
'Your organization has used [percent] of your available monthly credits.',
{percent: `${percent}%`}
)
: tct('Your organization has used [percent] of your available credits.', {
percent: `${percent}%`,
})}
</Text>
<Text>
{tct('[remaining]/[total] credits remaining.', {
remaining: Math.max(remaining, 0).toLocaleString(),
total: total.toLocaleString(),
})}
{/* No total: the wallet only knows what's left of the purchased credits,
so a total would shrink as they're used, down to the included credits. */}
{countsDailyLimit
? tn(
'%s credit remaining today.',
'%s credits remaining today.',
shownRemaining
)
: tn('%s credit remaining.', '%s credits remaining.', shownRemaining)}
</Text>
<AutoReloadStatus
autoReloadEnabled={autoReloadEnabled}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function WalletFixture(params: Partial<SeerWalletBalance> = {}): SeerWalletBalan
return {
balance: 320,
creditPriceCents: 1,
dailyLimit: null,
included: {total: 500, remaining: 320, resetsAt: '2026-11-01'},
purchased: {total: 0, remaining: 0, expiresAt: null},
percentUsed: 0.36,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export type SeerWalletBalance = {
* Remaining included plus purchased credits. Can go negative.
*/
balance: number;
/**
* The daily usage limit and what's left of it today, in credits. `remaining`
* can go negative. Null means no daily usage limit.
*/
dailyLimit: {remaining: number; total: number} | null;
included: {remaining: number; resetsAt: string | null; total: number};
/**
* Share of included plus purchased credits used, from 0 to 1.
Expand Down
Loading