:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(replay-vision): add banners for billing and no recording match (#79589)
Hayne Ryudc7ce1f6dbd9
diff --git a/products/replay_vision/frontend/components/QuotaImminentBanner.tsx b/products/replay_vision/frontend/components/QuotaImminentBanner.tsxnew file mode 100644index 000000000000..a9180ff8af4b--- /dev/null+++ b/products/replay_vision/frontend/components/QuotaImminentBanner.tsx@@ -0,0 +1,33 @@+import { LemonBanner } from '@posthog/lemon-ui'++import { dayjs } from 'lib/dayjs'+import { pluralize } from 'lib/utils/strings'+import { urls } from 'scenes/urls'++import { ProductKey } from '~/queries/schema/schema-general'++interface Props {+ capReachDate: dayjs.Dayjs+ /** Free-plan orgs have no limit to raise; they need billing instead. */+ onFreePlan: boolean+}++export function QuotaImminentBanner({ capReachDate, onFreePlan }: Props): JSX.Element {+ // Calendar days off the rendered date, so the two can't disagree.+ const days = capReachDate.startOf('day').diff(dayjs().startOf('day'), 'day')+ const timing = days <= 0 ? 'today' : `in ${pluralize(days, 'day')} (${capReachDate.format('MMMM D')})`+ const outcome = onFreePlan ? 'use up your free Vision credits' : 'hit your monthly Vision spend limit'+ return (+ <LemonBanner+ type="warning"+ action={{+ children: onFreePlan ? 'Add billing' : 'Raise your billing limit',+ to: urls.organizationBilling([ProductKey.REPLAY_VISION]),+ }}+ >+ <span className="text-xs">+ Enabled scanners are on track to {outcome} {timing}. Lower the sampling rate to slow spend.+ </span>+ </LemonBanner>+ )+}diff --git a/products/replay_vision/frontend/replay_scanners/components/ScannerQuotaForecast.tsx b/products/replay_vision/frontend/replay_scanners/components/ScannerQuotaForecast.tsxindex 8bf2b57753e6..cb6a04aee19e 100644--- a/products/replay_vision/frontend/replay_scanners/components/ScannerQuotaForecast.tsx+++ b/products/replay_vision/frontend/replay_scanners/components/ScannerQuotaForecast.tsx@@ -6,11 +6,13 @@ import { LemonLabel } from 'lib/lemon-ui/LemonLabel'import { NoBillingLimitNote } from '../../components/NoBillingLimitNote'import { QuotaExhaustedNote } from '../../components/QuotaExhaustedNote'+import { QuotaImminentBanner } from '../../components/QuotaImminentBanner'import { visionQuotaLogic } from '../../logics/visionQuotaLogic'import { creditsToUsd, formatCreditCount } from '../../utils/credits'import {QUOTA_STATUS_STYLES,type QuotaStatus,+ daysUntilCapReached,hasCreditLimit,projectQuota,splitProjectedPct,@@ -60,6 +62,9 @@ export function ScannerQuotaForecast({ scannerId }: Props): JSX.Element | null {const effectiveStatus: QuotaStatus = projectedCredits === null ? 'safe' : statusconst styles = QUOTA_STATUS_STYLES[effectiveStatus]+ // No estimate means the projection isn't about the scanner being edited.+ const imminentDays = projectedCredits !== null ? daysUntilCapReached(projection) : null+const { thisScannerPct, othersPct } = splitProjectedPct(projectedPct, projectedCredits ?? 0, othersMonthly)const [freeWidth, billedWidth, othersWidth, thisWidth] = quotaMeterWidths(usedPct, usedFreePct, [othersPct,@@ -126,8 +131,8 @@ export function ScannerQuotaForecast({ scannerId }: Props): JSX.Element | null {) : (<div className="text-sm text-muted">—</div>)}- {/* The exhausted note below carries this status, so don't say it twice. */}- {hasCap && !projection.exhausted && (+ {/* The exhausted note and the imminent banner below carry this status, so don't say it twice. */}+ {hasCap && !projection.exhausted && imminentDays === null && (<span className="text-xs tabular-nums"><QuotaStatusLine projection={projection} onFreePlan={onFreePlan} /></span>@@ -146,6 +151,10 @@ export function ScannerQuotaForecast({ scannerId }: Props): JSX.Element | null {{hasCap && projection.exhausted && <QuotaExhaustedNote onFreePlan={onFreePlan} />}+ {imminentDays !== null && projection.capReachDate && (+ <QuotaImminentBanner capReachDate={projection.capReachDate} onFreePlan={onFreePlan} />+ )}+{hasCap && projectedCredits !== null && (<><Tooltip title={breakdown}>diff --git a/products/replay_vision/frontend/replay_scanners/components/ScannerTriggers.scss b/products/replay_vision/frontend/replay_scanners/components/ScannerTriggers.scssnew file mode 100644index 000000000000..69b1626e0545--- /dev/null+++ b/products/replay_vision/frontend/replay_scanners/components/ScannerTriggers.scss@@ -0,0 +1,37 @@+// The three option labels total 389px and never wrap.+.ScannerSessionCoverage.LemonSegmentedButton {+ @container (max-width: 400px) {+ width: 100%;++ > ul {+ flex-direction: column;+ }++ .LemonSegmentedButton__option {+ &:first-child:not(:last-child) {+ &,+ .LemonButton,+ .LemonButton > .LemonButton__chrome::after,+ .LemonButton > .LemonButton__chrome::before {+ border-top-right-radius: var(--radius) !important;+ border-bottom-left-radius: 0 !important;+ }+ }++ &:last-child:not(:first-child) {+ &,+ .LemonButton,+ .LemonButton > .LemonButton__chrome::after,+ .LemonButton > .LemonButton__chrome::before {+ border-top-right-radius: 0 !important;+ border-bottom-left-radius: var(--radius) !important;+ }+ }++ &:not(:first-child) .LemonButton__chrome {+ margin-top: -1px;+ margin-left: 0;+ }+ }+ }+}diff --git a/products/replay_vision/frontend/replay_scanners/components/ScannerTriggers.tsx b/products/replay_vision/frontend/replay_scanners/components/ScannerTriggers.tsxindex 9a5c685050ef..3d18ba5a7136 100644--- a/products/replay_vision/frontend/replay_scanners/components/ScannerTriggers.tsx+++ b/products/replay_vision/frontend/replay_scanners/components/ScannerTriggers.tsx@@ -1,3 +1,5 @@+import './ScannerTriggers.scss'+import { useActions, useValues } from 'kea'import { LemonBanner, LemonCard, LemonInput, LemonSegmentedButton, LemonTag } from '@posthog/lemon-ui'@@ -90,13 +92,18 @@ function ScannerFilterGroup(): JSX.Element {}export function ScannerTriggers({ scannerId }: { scannerId: string }): JSX.Element {- const { scanner } = useValues(replayScannerLogic({ id: scannerId }))+ const { scanner, scannerEstimate, scannerEstimateLoading } = useValues(replayScannerLogic({ id: scannerId }))const { featureFlags } = useValues(featureFlagLogic)const { groupsTaxonomicTypes } = useValues(groupsModel)const categoryDropdownVariant = resolveCategoryDropdownVariant(featureFlags[FEATURE_FLAGS.TAXONOMIC_FILTER_CATEGORY_DROPDOWN])const scannerFilterTypes = [...SCANNER_BASE_FILTER_TYPES, ...groupsTaxonomicTypes]+ // Waits for the in-flight estimate so an edit can't report on the previous filters.+ const noMatchWindowDays =+ !scannerEstimateLoading && scannerEstimate?.matched_sessions_in_window === 0+ ? scannerEstimate.window_days+ : nullif (!scanner) {return <div className="text-muted">Loading…</div>@@ -164,14 +171,25 @@ export function ScannerTriggers({ scannerId }: { scannerId: string }): JSX.Elemesize="small"/></div>- {groupHasNoFilters(universal.filter_group) && (+ {noMatchWindowDays !== null ? (<LemonBanner type="warning"><span className="text-xs">- No recording filters set. Your prompt describes what to look for, but it doesn't- limit which recordings get scanned, so this scanner spends credits scanning- recordings that aren't relevant to your prompt. Add a filter to narrow it down.+ No recordings from the last {noMatchWindowDays} days match these conditions, so+ this scanner has nothing to scan. Widen the filters, or pick a broader option+ under Session coverage.</span></LemonBanner>+ ) : (+ groupHasNoFilters(universal.filter_group) && (+ <LemonBanner type="warning">+ <span className="text-xs">+ No recording filters set. Your prompt describes what to look for, but it+ doesn't limit which recordings get scanned, so this scanner spends credits+ scanning recordings that aren't relevant to your prompt. Add a filter to+ narrow it down.+ </span>+ </LemonBanner>+ ))}{groupHasEventProperty(universal.filter_group) && (<LemonBanner type="info" dismissKey="replay-vision-event-vs-person-property-hint">@@ -305,9 +323,9 @@ export function ScannerTriggers({ scannerId }: { scannerId: string }): JSX.ElemeSession coverage</LemonLabel></div>- <div className="space-y-1">+ <div className="space-y-1 @container"><LemonSegmentedButton- className="max-w-full overflow-x-auto"+ className="ScannerSessionCoverage"value={mode}onChange={onChange}options={SAMPLING_MODE_OPTIONS.map((o) => ({ value: o.value, label: o.label }))}diff --git a/products/replay_vision/frontend/utils/quotaProjection.test.ts b/products/replay_vision/frontend/utils/quotaProjection.test.tsindex aa0ca732aaf8..ce30a9f35b1b 100644--- a/products/replay_vision/frontend/utils/quotaProjection.test.ts+++ b/products/replay_vision/frontend/utils/quotaProjection.test.ts@@ -1,4 +1,11 @@-import { exhaustionForecast, hasBillableSpend, projectQuota, quotaUx, splitProjectedPct } from './quotaProjection'+import {+ daysUntilCapReached,+ exhaustionForecast,+ hasBillableSpend,+ projectQuota,+ quotaUx,+ splitProjectedPct,+} from './quotaProjection'import { makeQuota } from './quotaTestUtils'describe('hasBillableSpend', () => {@@ -110,6 +117,20 @@ describe('projectQuota', () => {})})+describe('daysUntilCapReached', () => {+ it.each([+ // 1,000 left of the 10,000 cap, burned at monthly/30 per day.+ ['inside the window', { credits_used: 9_000, projected_monthly_credits: 15_000 }, 2],+ ['further out than the window', { credits_used: 9_000, projected_monthly_credits: 3_000 }, null],+ ['already exhausted', { credits_used: 9_000, projected_monthly_credits: 15_000, exhausted: true }, null],+ ['no fleet spend, so the cap is never reached', { credits_used: 9_000 }, null],+ ])('%s', (_name, overrides, expected) => {+ const days = daysUntilCapReached(projectQuota(makeQuota(overrides)))+ // Rounded: the two `dayjs()` calls behind the diff are milliseconds apart.+ expect(days === null ? null : Math.round(days * 100) / 100).toBe(expected)+ })+})+describe('splitProjectedPct', () => {it('apportions by monthly volume', () => {expect(splitProjectedPct(30, 100, 200)).toEqual({ thisScannerPct: 10, othersPct: 20 })diff --git a/products/replay_vision/frontend/utils/quotaProjection.ts b/products/replay_vision/frontend/utils/quotaProjection.tsindex be620c985a4f..c98f2865f15a 100644--- a/products/replay_vision/frontend/utils/quotaProjection.ts+++ b/products/replay_vision/frontend/utils/quotaProjection.ts@@ -5,6 +5,8 @@ import { billableCredits, formatCreditCount } from './credits'export const QUOTA_WARN_THRESHOLD = 0.85+export const IMMINENT_CAP_DAYS = 3+export type QuotaStatus = 'safe' | 'warning' | 'danger'export const QUOTA_STATUS_STYLES: Record<QuotaStatus, { bar: string; text: string }> = {@@ -130,6 +132,15 @@ export function projectQuota(}}+/** Null unless the limit lands within IMMINENT_CAP_DAYS and scanning hasn't already stopped. */+export function daysUntilCapReached(projection: QuotaProjection): number | null {+ if (!projection.capReachDate || projection.exhausted) {+ return null+ }+ const days = Math.max(projection.capReachDate.diff(dayjs(), 'day', true), 0)+ return days <= IMMINENT_CAP_DAYS ? days : null+}+/** Apportion a projected percentage between this scanner and the rest of the fleet by monthly credit volume. */export function splitProjectedPct(projectedPct: number,