Skip to content
Merged
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
5 changes: 5 additions & 0 deletions packages/worker/client/routes/account-billing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,11 @@ export function AccountBillingRoute(handle: Handle) {
{tier.price}
</span>
<p mix={css(descriptionCss)}>{tier.description}</p>
<p mix={css({ margin: 0 })}>
<a href="/account/usage" mix={css(primaryLinkCss)}>
See your current usage
</a>
</p>
{isCurrent || isIncluded ? (
<div>
<button
Expand Down
251 changes: 192 additions & 59 deletions packages/worker/client/routes/account-usage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type Handle, css } from 'remix/ui'
import {
type AccountUsageEntitlementConsumption,
type AccountUsageLoaderData,
type AdminPlanName,
} from '#app/loader-data.ts'
Expand All @@ -26,14 +27,35 @@ import {
accountManagementTableNumericCellCss,
} from '#client/routes/account-management-components.tsx'
import { chartColor, formatIntegerNumber } from '#client/charts/chart-theme.ts'
import { colors, spacing, typography } from '#client/styles/tokens.ts'
import { colors, radius, spacing, typography } from '#client/styles/tokens.ts'
import {
descriptionCss,
primaryLinkCss,
} from '#client/styles/style-primitives.ts'

const usageApiPath = '/account/usage.json'
const usagePath = '/account/usage'
const billingPath = '/account/billing'

const entitlementGroupOrder: Array<
AccountUsageEntitlementConsumption['group']
> = ['daily', 'counts', 'storage', 'limits']

const entitlementGroupLabels: Record<
AccountUsageEntitlementConsumption['group'],
string
> = {
daily: 'Daily rates',
counts: 'Resource counts',
storage: 'Storage',
limits: 'Per-item limits',
}

const entitlementGroupNotes: Partial<
Record<AccountUsageEntitlementConsumption['group'], string>
> = {
daily: 'Counters reset at UTC midnight.',
}

function formatUsagePercent(value: number | null) {
if (value === null) return '—'
Expand All @@ -54,14 +76,82 @@ function formatBytes(value: number) {
}

function formatUsageValue(resource: string, value: number) {
if (resource === 'storage_bytes') return formatBytes(value)
if (resource === 'storage_bytes' || resource === 'email_message_bytes') {
return formatBytes(value)
}
return formatIntegerNumber(value)
}

function formatCurrentValue(item: AccountUsageEntitlementConsumption) {
if (item.kind === 'per_unit_max') return '—'
if (item.kind === 'boolean_allowance') {
return item.limit > 0 ? 'Allowed' : 'Not on plan'
}
return formatUsageValue(item.resource, item.current)
}

function formatLimitValue(item: AccountUsageEntitlementConsumption) {
if (item.kind === 'boolean_allowance') {
return item.limit > 0 ? 'Yes' : 'No'
}
return formatUsageValue(item.resource, item.limit)
}

function usageProgressPercent(item: AccountUsageEntitlementConsumption) {
if (item.percentOfLimit === null) return null
return Math.min(100, Math.round(item.percentOfLimit * 100))
}

function isUsagePath(href: string) {
return new URL(href, 'http://localhost').pathname === usagePath
}

function groupEntitlementRows(rows: Array<AccountUsageEntitlementConsumption>) {
const grouped = new Map<
AccountUsageEntitlementConsumption['group'],
Array<AccountUsageEntitlementConsumption>
>()
for (const group of entitlementGroupOrder) grouped.set(group, [])
for (const row of rows) {
const bucket = grouped.get(row.group) ?? []
bucket.push(row)
grouped.set(row.group, bucket)
}
return entitlementGroupOrder
.map((group) => ({
group,
rows: grouped.get(group) ?? [],
}))
.filter((entry) => entry.rows.length > 0)
}

function renderUsageProgressBar(item: AccountUsageEntitlementConsumption) {
const percent = usageProgressPercent(item)
if (percent === null) return null
const barColor = item.overEightyPercent ? chartColor.amber : chartColor.blue
return (
<div
role="img"
aria-label={`${item.label}: ${formatUsagePercent(item.percentOfLimit)} of plan limit`}
mix={css({
height: '8px',
borderRadius: radius.md,
background: colors.border,
overflow: 'hidden',
minWidth: '4rem',
})}
>
<div
mix={css({
height: '100%',
width: `${percent}%`,
background: barColor,
})}
/>
</div>
)
}

export async function accountUsageRouteLoader(
_url: URL,
signal: AbortSignal,
Expand Down Expand Up @@ -147,12 +237,15 @@ export function AccountUsageRoute(handle: Handle) {
}

const usage = status === 'ready' ? data : null
const groupedRows = usage
? groupEntitlementRows(usage.entitlementConsumption)
: []

return (
<AccountManagementShell>
<AccountPageHeader
title="Usage"
description="Your current plan limits and how much you are using today."
description="Plan limits, current consumption, and what counts toward each resource."
currentHref={currentHref}
/>
{message ? (
Expand All @@ -179,7 +272,7 @@ export function AccountUsageRoute(handle: Handle) {
]}
/>
<p mix={css({ margin: `${spacing.sm} 0 0` })}>
<a href="/account/billing" mix={css(primaryLinkCss)}>
<a href={billingPath} mix={css(primaryLinkCss)}>
Manage billing
</a>
</p>
Expand All @@ -194,72 +287,112 @@ export function AccountUsageRoute(handle: Handle) {
margin: 0,
paddingLeft: spacing.lg,
display: 'grid',
gap: spacing.xs,
gap: spacing.sm,
color: colors.text,
})}
>
{usage.warnings.map((item) => (
<li key={item.resource}>
{item.label}:{' '}
{formatUsageValue(item.resource, item.current)} /{' '}
{formatUsageValue(item.resource, item.limit)} (
{formatUsagePercent(item.percentOfLimit)})
<strong>{item.label}</strong>: {formatCurrentValue(item)}{' '}
/ {formatLimitValue(item)} (
{formatUsagePercent(item.percentOfLimit)}).{' '}
{item.howToReduce}{' '}
<a href={billingPath} mix={css(primaryLinkCss)}>
Upgrade your plan
</a>
</li>
))}
</ul>
</AccountManagementPanel>
) : null}
<AccountManagementPanel
title="Entitlement use"
description="Row counts and daily counters enforced against your plan. Daily counters reset at UTC midnight."
>
<div mix={css({ overflowX: 'auto' })}>
<table mix={css(accountManagementTableCss)}>
<thead>
<tr>
<th mix={css(accountManagementTableCellCss)}>Resource</th>
<th mix={css(accountManagementTableNumericCellCss)}>
In use
</th>
<th mix={css(accountManagementTableNumericCellCss)}>
Limit
</th>
<th mix={css(accountManagementTableNumericCellCss)}>
Used
</th>
</tr>
</thead>
<tbody>
{usage.entitlementConsumption.map((item) => (
<tr key={item.resource}>
<td mix={css(accountManagementTableCellCss)}>
{item.label}
</td>
<td mix={css(accountManagementTableNumericCellCss)}>
{formatUsageValue(item.resource, item.current)}
</td>
<td mix={css(accountManagementTableNumericCellCss)}>
{formatUsageValue(item.resource, item.limit)}
</td>
<td
mix={css({
...accountManagementTableNumericCellCss,
...(item.overEightyPercent
? {
color: chartColor.amber,
fontWeight: typography.fontWeight.semibold,
}
: {}),
})}
>
{formatUsagePercent(item.percentOfLimit)}
</td>
{groupedRows.map(({ group, rows }) => (
<AccountManagementPanel
key={group}
title={entitlementGroupLabels[group]}
description={
entitlementGroupNotes[group] ??
'Current use compared to your plan limit.'
}
>
<div mix={css({ overflowX: 'auto' })}>
<table mix={css(accountManagementTableCss)}>
<thead>
<tr>
<th mix={css(accountManagementTableCellCss)}>
Resource
</th>
<th mix={css(accountManagementTableNumericCellCss)}>
In use
</th>
<th mix={css(accountManagementTableNumericCellCss)}>
Limit
</th>
<th mix={css(accountManagementTableNumericCellCss)}>
Used
</th>
<th mix={css(accountManagementTableCellCss)}>
Progress
</th>
</tr>
))}
</tbody>
</table>
</div>
</AccountManagementPanel>
</thead>
<tbody>
{rows.map((item) => (
<tr key={item.resource}>
<td mix={css(accountManagementTableCellCss)}>
<div
mix={css({
display: 'grid',
gap: spacing.xs,
})}
>
<span
mix={css({
fontWeight: typography.fontWeight.medium,
color: colors.text,
})}
>
{item.label}
</span>
<span
mix={css({
fontSize: typography.fontSize.sm,
color: colors.textMuted,
lineHeight: 1.4,
})}
>
{item.whatCounts} {item.howToReduce}
</span>
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
</div>
</td>
<td mix={css(accountManagementTableNumericCellCss)}>
{formatCurrentValue(item)}
</td>
<td mix={css(accountManagementTableNumericCellCss)}>
{formatLimitValue(item)}
</td>
<td
mix={css({
...accountManagementTableNumericCellCss,
...(item.overEightyPercent
? {
color: chartColor.amber,
fontWeight: typography.fontWeight.semibold,
}
: {}),
})}
>
{formatUsagePercent(item.percentOfLimit)}
</td>
<td mix={css(accountManagementTableCellCss)}>
{renderUsageProgressBar(item)}
</td>
</tr>
))}
</tbody>
</table>
</div>
</AccountManagementPanel>
))}
</>
) : null}
</AccountManagementShell>
Expand Down
Loading
Loading