Skip to content

: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.

feat(replay-vision): connect estimate API to UI (#61175)

Hayne Ryue88f8f46e408
diff --git a/products/replay_vision/backend/queries/scanner_volume_estimate.py b/products/replay_vision/backend/queries/scanner_volume_estimate.py
index 79429ba122d2..961240c4d672 100644
--- a/products/replay_vision/backend/queries/scanner_volume_estimate.py
+++ b/products/replay_vision/backend/queries/scanner_volume_estimate.py
@@ -7,6 +7,7 @@ from posthog.hogql import ast
from posthog.hogql.constants import HogQLGlobalSettings
from posthog.hogql.query import execute_hogql_query
+from posthog.clickhouse.query_tagging import Feature, Product, tag_queries
from posthog.models import Team
from posthog.session_recordings.queries.session_recording_list_from_query import SessionRecordingListFromQuery
@@ -69,6 +70,7 @@ def estimate_scanner_session_volume(*, team: Team, query: RecordingsQuery) -> Sc
),
)
+ tag_queries(team_id=team.id, product=Product.REPLAY_VISION, feature=Feature.QUERY)
response = execute_hogql_query(
query=combined_query,
team=team,
diff --git a/products/replay_vision/frontend/replay_scanners/ReplayScanner.tsx b/products/replay_vision/frontend/replay_scanners/ReplayScanner.tsx
index 3e7b751f5611..db536357c145 100644
--- a/products/replay_vision/frontend/replay_scanners/ReplayScanner.tsx
+++ b/products/replay_vision/frontend/replay_scanners/ReplayScanner.tsx
@@ -75,7 +75,7 @@ export function ReplayScannerSceneComponent({ tabId }: { tabId: string }): JSX.E
key: 'configuration',
label: 'Configuration',
content: (
- <div className="space-y-6 max-w-3xl">
+ <div className="space-y-6 max-w-3xl pb-8">
<div>
<h3 className="text-base font-semibold mb-1">Details</h3>
<p className="text-sm text-muted m-0">
diff --git a/products/replay_vision/frontend/replay_scanners/components/ScannerQuotaForecast.tsx b/products/replay_vision/frontend/replay_scanners/components/ScannerQuotaForecast.tsx
index 878d838f97c4..62ea170a3d5b 100644
--- a/products/replay_vision/frontend/replay_scanners/components/ScannerQuotaForecast.tsx
+++ b/products/replay_vision/frontend/replay_scanners/components/ScannerQuotaForecast.tsx
@@ -1,5 +1,7 @@
import { useValues } from 'kea'
+import { Spinner } from '@posthog/lemon-ui'
+
import { LemonProgress } from 'lib/lemon-ui/LemonProgress'
import { replayScannerLogic } from '../replayScannerLogic'
@@ -10,7 +12,7 @@ interface Props {
}
export function ScannerQuotaForecast({ scannerId, tabId }: Props): JSX.Element | null {
- const { scanner } = useValues(replayScannerLogic({ id: scannerId, tabId }))
+ const { scanner, scannerEstimate, scannerEstimateLoading } = useValues(replayScannerLogic({ id: scannerId, tabId }))
if (!scanner) {
return null
@@ -18,30 +20,33 @@ export function ScannerQuotaForecast({ scannerId, tabId }: Props): JSX.Element |
const samplingRatio = Math.max(0, Math.min(scanner.sampling_rate, 1))
const samplingPercent = Math.round(samplingRatio * 1000) / 10
- const oneInN = samplingRatio > 0 ? Math.max(1, Math.round(1 / samplingRatio)) : null
+ const samplingLabel = samplingPercent.toFixed(samplingPercent < 1 ? 2 : 1)
return (
<div className="border rounded p-3 bg-bg-light space-y-2">
<div className="flex items-center justify-between">
- <span className="text-sm font-medium">Quota impact</span>
- <span className="text-sm tabular-nums">{samplingPercent.toFixed(samplingPercent < 1 ? 2 : 1)}%</span>
+ <span className="text-sm font-medium">Estimated impact</span>
+ <span className="text-sm tabular-nums text-muted">{samplingLabel}% sampling</span>
</div>
<LemonProgress percent={Math.round(samplingRatio * 100)} />
- <div className="text-xs text-muted">
- {oneInN === null ? (
- <span className="text-danger">Sampling is 0%. This scanner will not produce any observations.</span>
- ) : oneInN === 1 ? (
- <>
- Every matching recording produces one observation. Each one counts against your monthly
- observation quota.
- </>
- ) : (
- <>
- About <strong>1 in {oneInN.toLocaleString()}</strong> matching recordings will be observed.
- Lower this slider to reduce monthly quota consumption; raise it for denser coverage.
- </>
- )}
- </div>
+ {samplingRatio === 0 ? (
+ <div className="text-xs text-danger">
+ Sampling is 0%. This scanner will not produce any observations.
+ </div>
+ ) : scannerEstimateLoading && !scannerEstimate ? (
+ <div className="text-xs text-muted flex items-center gap-2">
+ <Spinner /> Estimating from your filters…
+ </div>
+ ) : scannerEstimate ? (
+ <div className="text-xs text-muted">
+ About <strong>{scannerEstimate.estimated_observations_per_month.toLocaleString()}</strong>{' '}
+ observations a month at this sampling. Based on{' '}
+ <strong>{scannerEstimate.matched_sessions_in_window.toLocaleString()}</strong> matching recordings
+ in the last {scannerEstimate.window_days} days.
+ </div>
+ ) : (
+ <div className="text-xs text-muted">Estimate unavailable — try adjusting your filters.</div>
+ )}
</div>
)
}
diff --git a/products/replay_vision/frontend/replay_scanners/components/ScannerTriggers.tsx b/products/replay_vision/frontend/replay_scanners/components/ScannerTriggers.tsx
index be6ad076adab..14ec78586591 100644
--- a/products/replay_vision/frontend/replay_scanners/components/ScannerTriggers.tsx
+++ b/products/replay_vision/frontend/replay_scanners/components/ScannerTriggers.tsx
@@ -1,7 +1,7 @@
import { useValues } from 'kea'
import { Field } from 'kea-forms'
-import { LemonInput } from '@posthog/lemon-ui'
+import { LemonDivider, LemonInput } from '@posthog/lemon-ui'
import { PropertyFilters } from 'lib/components/PropertyFilters/PropertyFilters'
import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'
@@ -97,6 +97,7 @@ export function ScannerTriggers({ scannerId, tabId }: { scannerId: string; tabId
}}
</Field>
+ <LemonDivider className="my-0" />
<ScannerQuotaForecast scannerId={scannerId} tabId={tabId} />
</div>
)
diff --git a/products/replay_vision/frontend/replay_scanners/replayScannerLogic.ts b/products/replay_vision/frontend/replay_scanners/replayScannerLogic.ts
index de5a8001d3e2..582b5e4aaad8 100644
--- a/products/replay_vision/frontend/replay_scanners/replayScannerLogic.ts
+++ b/products/replay_vision/frontend/replay_scanners/replayScannerLogic.ts
@@ -1,4 +1,4 @@
-import { actions, afterMount, kea, key, listeners, path, props, reducers, selectors } from 'kea'
+import { actions, afterMount, isBreakpoint, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { forms } from 'kea-forms'
import { router } from 'kea-router'
@@ -13,11 +13,12 @@ import { NodeKind } from '~/queries/schema/schema-general'
import {
visionScannersCreate,
visionScannersDestroy,
+ visionScannersEstimateCreate,
visionScannersObservationsList,
visionScannersPartialUpdate,
visionScannersRetrieve,
} from '../generated/api'
-import type { ReplayObservationApi } from '../generated/api.schemas'
+import type { EstimateResponseApi, ReplayObservationApi } from '../generated/api.schemas'
import { scheduleObservationPoll } from '../logics/observationPolling'
import { readFixedTags, readFreeformTags, readModelOutput, readTags, readVerdict } from '../utils/observation'
import type { replayScannerLogicType } from './replayScannerLogicType'
@@ -130,6 +131,10 @@ export const replayScannerLogic = kea<replayScannerLogicType>([
setObservationTagFilter: (values: string[]) => ({ values }),
clearObservationFilters: true,
setChartDateRange: (dateFrom: string | null, dateTo: string | null) => ({ dateFrom, dateTo }),
+ requestScannerEstimate: true,
+ loadScannerEstimate: true,
+ loadScannerEstimateSuccess: (estimate: EstimateResponseApi) => ({ estimate }),
+ loadScannerEstimateFailure: true,
}),
forms(({ props }) => ({
@@ -208,6 +213,28 @@ export const replayScannerLogic = kea<replayScannerLogicType>([
loadObservationsFailure: () => false,
},
],
+ scannerEstimate: [
+ null as EstimateResponseApi | null,
+ {
+ loadScannerEstimateSuccess: (_, { estimate }) => estimate,
+ loadScannerEstimateFailure: () => null,
+ },
+ ],
+ scannerEstimateLoading: [
+ false,
+ {
+ requestScannerEstimate: () => true,
+ loadScannerEstimate: () => true,
+ loadScannerEstimateSuccess: () => false,
+ loadScannerEstimateFailure: () => false,
+ },
+ ],
+ estimateRequestVersion: [
+ 0,
+ {
+ requestScannerEstimate: (state: number) => state + 1,
+ },
+ ],
observationStatusFilter: [
[] as ObservationStatusValue[],
{
@@ -529,12 +556,57 @@ export const replayScannerLogic = kea<replayScannerLogicType>([
loadScannerSuccess: ({ scanner }) => {
actions.setScannerValues(scanner)
+ actions.requestScannerEstimate()
},
setScannerType: ({ scannerType }) => {
actions.setScannerValues({ scanner_type: scannerType, scanner_config: defaultConfigForType(scannerType) })
},
+ // kea-forms fires setScannerValue(s) on every field change. Debounce the estimate so slider drags
+ // and rapid filter edits don't fire one request per tick.
+ setScannerValue: () => actions.requestScannerEstimate(),
+ setScannerValues: () => actions.requestScannerEstimate(),
+ submitScannerSuccess: () => actions.requestScannerEstimate(),
+
+ requestScannerEstimate: () => {
+ cache.disposables.add(() => {
+ const id = setTimeout(() => actions.loadScannerEstimate(), 300)
+ return () => clearTimeout(id)
+ }, 'scannerEstimateDebounce')
+ },
+
+ loadScannerEstimate: async (_, breakpoint) => {
+ const teamId = teamLogic.values.currentTeamId
+ const scanner = values.scanner
+ if (!teamId || !scanner) {
+ actions.loadScannerEstimateFailure()
+ return
+ }
+ const version = values.estimateRequestVersion
+ try {
+ const response = await visionScannersEstimateCreate(String(teamId), {
+ query: scanner.query ?? undefined,
+ sampling_rate: scanner.sampling_rate,
+ })
+ breakpoint()
+ if (values.estimateRequestVersion !== version) {
+ return
+ }
+ actions.loadScannerEstimateSuccess(response)
+ } catch (error) {
+ if (error instanceof Error && isBreakpoint(error)) {
+ throw error
+ }
+ // eslint-disable-next-line no-console
+ console.warn('[replay-vision] scanner estimate failed', error)
+ if (values.estimateRequestVersion !== version) {
+ return
+ }
+ actions.loadScannerEstimateFailure()
+ }
+ },
+
deleteScanner: async () => {
if (props.id === 'new') {
return