-
Notifications
You must be signed in to change notification settings - Fork 562
feat(usage): Show usage against the plan limit for the current billing period #8320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,365
−47
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0538190
fix(format): shortenNumber returned NaN for zero and missing values
talissoncosta 4379ec4
refactor(usage-bar): split UsageBar up and give it thresholds
talissoncosta 639c1e4
feat(charts): let LineChart draw a reference line
talissoncosta 70ebadd
refactor(types): name the period lists for what they hold
talissoncosta f8a6995
feat(usage): show usage against the plan limit for the current billin…
talissoncosta 154be1b
docs(usage): cover the dashboard states in Storybook
talissoncosta 77ceb96
fix(usage): wait for the plan before requesting usage
talissoncosta 2a14e7b
feat(usage-bar): ease the fill when the figures change
talissoncosta 17d509f
refactor(usage): fold the dashboard body back into the view
talissoncosta 7c6cd1d
refactor(usage-bar): inline the header
talissoncosta 03a6135
fix(usage): label the filters, and stop waiting on the optional limit
talissoncosta ccafb1c
fix(usage): wait for the plan limit before drawing the meter
talissoncosta 11b593d
refactor(usage): move the dashboard to pages/usage
talissoncosta a3b7104
test(usage): share the usage fixtures and derive their totals
talissoncosta 8367d43
fix(usage): draw against the limit only when a billing period is sele…
talissoncosta 9476a6d
chore(usage): keep the reasoning in the decisions doc, not the diff
talissoncosta 47b213f
fix(usage-bar): more air between the threshold labels and the track
talissoncosta ae5d163
chore(usage): take the explanations out of the code
talissoncosta ef7fdcb
chore(format): drop the type comment too
talissoncosta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import type { Meta, StoryObj } from 'storybook' | ||
| import UsageBar from 'components/shared/UsageBar' | ||
|
|
||
| const meta: Meta<typeof UsageBar> = { | ||
| component: UsageBar, | ||
| parameters: { layout: 'padded' }, | ||
| title: 'Components/Data Display/UsageBar', | ||
| } | ||
| export default meta | ||
|
|
||
| type Story = StoryObj<typeof UsageBar> | ||
|
|
||
| export const WithLabel: Story = { | ||
| args: { label: 'Segment overrides', limit: 100, usage: 42 }, | ||
| } | ||
|
|
||
| export const Warning: Story = { | ||
| args: { label: 'Segment overrides', limit: 100, usage: 91 }, | ||
| } | ||
|
|
||
| export const OverTheLimit: Story = { | ||
| args: { label: 'Segment overrides', limit: 100, usage: 118 }, | ||
| } | ||
|
|
||
| // Thresholds are marked on the bar, for usage that is notified at set points. | ||
| export const WithThresholds: Story = { | ||
| args: { | ||
| ariaLabel: 'Plan usage this period', | ||
| limit: 2000000, | ||
| thresholds: [75, 100], | ||
| usage: 1240000, | ||
| warnAt: 75, | ||
| }, | ||
| } | ||
|
|
||
| export const WithThresholdsOverTheLimit: Story = { | ||
| args: { | ||
| ariaLabel: 'Plan usage this period', | ||
| limit: 50000, | ||
| thresholds: [75, 100], | ||
| usage: 68400, | ||
| warnAt: 75, | ||
| }, | ||
| } | ||
|
|
||
| export const NoUsageYet: Story = { | ||
| args: { label: 'Segment overrides', limit: 100, usage: 0 }, | ||
| } |
140 changes: 140 additions & 0 deletions
140
frontend/documentation/components/UsageDashboard.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import type { Meta, StoryObj } from 'storybook' | ||
| import { UsageDashboard } from 'components/pages/usage' | ||
| import { Res } from 'common/types/responses' | ||
|
|
||
| const meta: Meta<typeof UsageDashboard> = { | ||
| component: UsageDashboard, | ||
| parameters: { layout: 'fullscreen' }, | ||
| title: 'Pages/Usage Dashboard/Page', | ||
| } | ||
| export default meta | ||
|
|
||
| type Story = StoryObj<typeof UsageDashboard> | ||
|
|
||
| const DAY_WEIGHTS = [1.08, 1.12, 1.05, 1.1, 0.98, 0.62, 0.58] | ||
|
|
||
| const usage = (days: number, perDay: number): Res['organisationUsage'] => { | ||
| const events = Array.from({ length: days }).map((_, index) => { | ||
| const weight = DAY_WEIGHTS[index % DAY_WEIGHTS.length] | ||
| return { | ||
| day: `2026-08-${`${index + 1}`.padStart(2, '0')}`, | ||
| environment_document: Math.round(perDay * weight * 0.04), | ||
| flags: Math.round(perDay * weight * 0.63), | ||
| identities: Math.round(perDay * weight * 0.24), | ||
| labels: { user_agent: null }, | ||
| traits: Math.round(perDay * weight * 0.09), | ||
| } | ||
| }) | ||
| const sum = ( | ||
| key: 'flags' | 'identities' | 'traits' | 'environment_document', | ||
| ) => events.reduce((acc, event) => acc + event[key], 0) | ||
|
|
||
| return { | ||
| events_list: events, | ||
| totals: { | ||
| environmentDocument: sum('environment_document'), | ||
| flags: sum('flags'), | ||
| identities: sum('identities'), | ||
| total: | ||
| sum('flags') + | ||
| sum('identities') + | ||
| sum('traits') + | ||
| sum('environment_document'), | ||
| traits: sum('traits'), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| const paid = usage(18, 70000) | ||
| const free = usage(30, 1800) | ||
| const paidApproaching = usage(26, 75000) | ||
| const paidOver = usage(28, 92000) | ||
|
|
||
| // A plan with a billing term: usage climbs towards a reset, so it is drawn | ||
| // cumulatively against the ceiling. | ||
| export const PaidWithABillingPeriod: Story = { | ||
| args: { | ||
| data: paid, | ||
| hasBillingPeriod: true, | ||
| limit: 2000000, | ||
| total: paid.totals.total, | ||
| }, | ||
| } | ||
|
|
||
| export const PaidApproachingTheLimit: Story = { | ||
| args: { | ||
| data: paidApproaching, | ||
| hasBillingPeriod: true, | ||
| limit: 2000000, | ||
| total: paidApproaching.totals.total, | ||
| }, | ||
| } | ||
|
|
||
| export const PaidOverTheLimit: Story = { | ||
| args: { | ||
| data: paidOver, | ||
| hasBillingPeriod: true, | ||
| limit: 2000000, | ||
| total: paidOver.totals.total, | ||
| }, | ||
| } | ||
|
|
||
| // No billing term, so no reset to accumulate towards: daily volume instead. | ||
| export const FreeOnARollingWindow: Story = { | ||
| args: { | ||
| data: free, | ||
| hasBillingPeriod: false, | ||
| limit: 50000, | ||
| total: free.totals.total, | ||
| }, | ||
| } | ||
|
|
||
| // Enterprise agreements are not billed through Chargebee, so they have a real | ||
| // limit and no period. The meter still works; the chart falls back to volume. | ||
| export const EnterpriseWithoutABillingPeriod: Story = { | ||
| args: { | ||
| data: paid, | ||
| hasBillingPeriod: false, | ||
| limit: 50000000, | ||
| total: paid.totals.total, | ||
| }, | ||
| } | ||
|
|
||
| // Self-hosted has no subscription data at all, so there is nothing to be a | ||
| // percentage of. | ||
| export const WithoutAPlanLimit: Story = { | ||
| args: { | ||
| data: paid, | ||
| hasBillingPeriod: false, | ||
| limit: null, | ||
| total: paid.totals.total, | ||
| }, | ||
| } | ||
|
|
||
| export const NoUsageYet: Story = { | ||
| args: { | ||
| data: usage(0, 0), | ||
| hasBillingPeriod: true, | ||
| limit: 2000000, | ||
| total: 0, | ||
| }, | ||
| } | ||
|
|
||
| export const Loading: Story = { | ||
| args: { | ||
| hasBillingPeriod: true, | ||
| isLoading: true, | ||
| limit: 2000000, | ||
| total: 0, | ||
| }, | ||
| } | ||
|
|
||
| /** Distinct from NoUsageYet, which would otherwise look identical. */ | ||
| export const FailedToLoad: Story = { | ||
| args: { | ||
| hasBillingPeriod: true, | ||
| isError: true, | ||
| limit: 2000000, | ||
| total: 0, | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import type { Meta, StoryObj } from 'storybook' | ||
| import UsageMeter from 'components/pages/usage/components/UsageMeter' | ||
|
|
||
| const meta: Meta<typeof UsageMeter> = { | ||
| component: UsageMeter, | ||
| parameters: { layout: 'padded' }, | ||
| title: 'Pages/Usage Dashboard/Components/UsageMeter', | ||
| } | ||
| export default meta | ||
|
|
||
| type Story = StoryObj<typeof UsageMeter> | ||
|
|
||
| export const UnderTheLimit: Story = { | ||
| args: { limit: 2000000, total: 1240000 }, | ||
| } | ||
|
|
||
| export const ApproachingTheLimit: Story = { | ||
| args: { limit: 2000000, total: 1760000 }, | ||
| } | ||
|
|
||
| export const AtTheLimit: Story = { | ||
| args: { limit: 2000000, total: 2000000 }, | ||
| } | ||
|
|
||
| export const OverTheLimit: Story = { | ||
| args: { limit: 50000, total: 68400 }, | ||
| } | ||
|
|
||
| // Nothing to be a percentage of, so the total stands on its own. | ||
| export const WithoutAPlanLimit: Story = { | ||
| args: { limit: null, total: 340000 }, | ||
| } | ||
|
|
||
| export const NoUsageYet: Story = { | ||
| args: { limit: 50000, total: 0 }, | ||
| } | ||
|
|
||
| export const WithANote: Story = { | ||
| args: { | ||
| limit: 2000000, | ||
| note: ( | ||
| <p className='mt-3 mb-0 text-muted fs-small'> | ||
| On track to use ~1.9M calls by the end of the period. | ||
| </p> | ||
| ), | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| total: 1240000, | ||
| }, | ||
| } | ||
88 changes: 88 additions & 0 deletions
88
frontend/documentation/components/UsageOverTime.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import type { Meta, StoryObj } from 'storybook' | ||
| import UsageOverTime from 'components/pages/usage/components/UsageOverTime' | ||
| import { Res } from 'common/types/responses' | ||
|
|
||
| const meta: Meta<typeof UsageOverTime> = { | ||
| component: UsageOverTime, | ||
| parameters: { layout: 'padded' }, | ||
| title: 'Pages/Usage Dashboard/Components/UsageOverTime', | ||
| } | ||
| export default meta | ||
|
|
||
| type Story = StoryObj<typeof UsageOverTime> | ||
|
|
||
| // Weekends lighter, so the shape reads like real traffic rather than a ramp. | ||
| const DAY_WEIGHTS = [1.08, 1.12, 1.05, 1.1, 0.98, 0.62, 0.58] | ||
|
|
||
| const usage = (days: number, perDay: number): Res['organisationUsage'] => { | ||
| const events = Array.from({ length: days }).map((_, index) => { | ||
| const weight = DAY_WEIGHTS[index % DAY_WEIGHTS.length] | ||
| return { | ||
| day: `2026-08-${`${index + 1}`.padStart(2, '0')}`, | ||
| environment_document: Math.round(perDay * weight * 0.04), | ||
| flags: Math.round(perDay * weight * 0.63), | ||
| identities: Math.round(perDay * weight * 0.24), | ||
| labels: { user_agent: null }, | ||
| traits: Math.round(perDay * weight * 0.09), | ||
| } | ||
| }) | ||
| const sum = (key: keyof (typeof events)[number]) => | ||
| events.reduce((acc, event) => acc + Number(event[key] ?? 0), 0) | ||
|
|
||
| return { | ||
| events_list: events, | ||
| totals: { | ||
| environmentDocument: sum('environment_document'), | ||
| flags: sum('flags'), | ||
| identities: sum('identities'), | ||
| total: | ||
| sum('flags') + | ||
| sum('identities') + | ||
| sum('traits') + | ||
| sum('environment_document'), | ||
| traits: sum('traits'), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| export const CumulativeUnderTheCeiling: Story = { | ||
| args: { | ||
| data: usage(18, 70000), | ||
| isBillingPeriod: true, | ||
| limit: 2000000, | ||
| }, | ||
| } | ||
|
|
||
| export const CumulativeCrossingTheCeiling: Story = { | ||
| args: { | ||
| data: usage(24, 110000), | ||
| isBillingPeriod: true, | ||
| limit: 2000000, | ||
| }, | ||
| } | ||
|
|
||
| // A rolling window's total falls as old days drop out, so it gets daily volume | ||
| // rather than a line that only ever climbs. | ||
| export const DailyVolumeOnARollingWindow: Story = { | ||
| args: { | ||
| data: usage(30, 2000), | ||
| isBillingPeriod: false, | ||
| limit: 50000, | ||
| }, | ||
| } | ||
|
|
||
| export const NoLimitToDrawAgainst: Story = { | ||
| args: { | ||
| data: usage(18, 70000), | ||
| isBillingPeriod: true, | ||
| limit: null, | ||
| }, | ||
| } | ||
|
|
||
| export const NoUsageRecorded: Story = { | ||
| args: { | ||
| data: usage(0, 0), | ||
| isBillingPeriod: true, | ||
| limit: 2000000, | ||
| }, | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.