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.

chore(web-analytics): Add live refresh grace period (#86751)

Jordan Mryyanc2b2786a994f
diff --git a/frontend/src/scenes/web-analytics/LiveMetricsDashboard/LiveWebAnalyticsMetrics.tsx b/frontend/src/scenes/web-analytics/LiveMetricsDashboard/LiveWebAnalyticsMetrics.tsx
index e3de9969c5ba..7c2242a39740 100644
--- a/frontend/src/scenes/web-analytics/LiveMetricsDashboard/LiveWebAnalyticsMetrics.tsx
+++ b/frontend/src/scenes/web-analytics/LiveMetricsDashboard/LiveWebAnalyticsMetrics.tsx
@@ -13,7 +13,7 @@ import { useActions, useValues } from 'kea'
import { ReactNode, useEffect, useMemo, useState } from 'react'
import { IconDrag, IconFilter, IconGlobe, IconPencil, IconX } from '@posthog/icons'
-import { LemonBanner, LemonButton, LemonDivider, Popover } from '@posthog/lemon-ui'
+import { LemonBanner, LemonButton, LemonDivider, Popover, Spinner, Tooltip } from '@posthog/lemon-ui'
import { FilterBar } from 'lib/components/FilterBar'
import { liveUserCountLogic } from 'lib/components/LiveUserCount/liveUserCountLogic'
@@ -97,6 +97,7 @@ const LiveDashboardFilterRow = ({
const { rawWebAnalyticsFilters, deviceTypeFilter, validatedDomainFilter } = useValues(webAnalyticsLogic)
const { setDeviceTypeFilter, setWebAnalyticsFilters } = useActions(webAnalyticsLogic)
const { clearFilters } = useActions(webAnalyticsFilterLogic)
+ const { isRefreshing } = useValues(liveWebAnalyticsMetricsLogic)
const hasDomainFilter = !!validatedDomainFilter && validatedDomainFilter !== 'all'
const livePropertyFilters = rawWebAnalyticsFilters.filter(isLiveStreamFilter)
@@ -168,6 +169,11 @@ const LiveDashboardFilterRow = ({
}
right={
<>
+ {isRefreshing && (
+ <Tooltip title="Refreshing live data">
+ <Spinner className="text-lg text-muted" />
+ </Tooltip>
+ )}
{isEditing ? (
<>
<LemonButton type="secondary" size="small" onClick={() => resetLayout()}>
diff --git a/frontend/src/scenes/web-analytics/LiveMetricsDashboard/liveWebAnalyticsMetricsLogic.tsx b/frontend/src/scenes/web-analytics/LiveMetricsDashboard/liveWebAnalyticsMetricsLogic.tsx
index c5cc608bda74..4e4aa63aa8b9 100644
--- a/frontend/src/scenes/web-analytics/LiveMetricsDashboard/liveWebAnalyticsMetricsLogic.tsx
+++ b/frontend/src/scenes/web-analytics/LiveMetricsDashboard/liveWebAnalyticsMetricsLogic.tsx
@@ -60,7 +60,6 @@ import {
} from './recentUsersByLastSeen'
const ERROR_TOAST_ID = 'live-pageviews-error'
-const RECONNECT_TOAST_ID = 'live-pageviews-reconnect'
const PARTIAL_FAILURE_TOAST_ID = 'live-pageviews-partial-failure'
const BUCKET_WINDOW_MINUTES = 30
const FLUSH_INTERVAL_MS = 300
@@ -73,6 +72,7 @@ const LIVE_QUERY_CONCURRENCY = 4
const LIVE_QUERY_MAX_ATTEMPTS = 3
const LIVE_QUERY_RETRY_DELAY_MS = 250
const RELOAD_DEBOUNCE_MS = 300
+const PAUSE_GRACE_MS = 2000
const TRANSIENT_QUERY_STATUSES = new Set([502, 503, 504])
// Bound live-dashboard query fan-out to protect ClickHouse capacity.
@@ -141,6 +141,7 @@ export interface liveWebAnalyticsMetricsLogicValues {
geoVersion: number
hasActiveFilters: boolean
isLoading: boolean
+ isRefreshing: boolean
liveFilters: WebAnalyticsPropertyFilter[]
liveUserCount: number
recentEvents: LiveEvent[]
@@ -179,8 +180,8 @@ export interface liveWebAnalyticsMetricsLogicActions {
clearRecentEvents: () => {
value: true
}
- loadInitialData: () => {
- value: true
+ loadInitialData: (isBackground?: boolean) => {
+ isBackground: boolean
}
pauseStream: () => {
value: true
@@ -207,6 +208,9 @@ export interface liveWebAnalyticsMetricsLogicActions {
setIsLoading: (loading: boolean) => {
loading: boolean
}
+ setIsRefreshing: (refreshing: boolean) => {
+ refreshing: boolean
+ }
tickCurrentMinute: () => {
value: true
}
@@ -288,7 +292,8 @@ export const liveWebAnalyticsMetricsLogic = kea<liveWebAnalyticsMetricsLogicType
recentUsersByLastSeen: [string, number][]
) => ({ buckets, recentUsersByLastSeen }),
setIsLoading: (loading: boolean) => ({ loading }),
- loadInitialData: true,
+ setIsRefreshing: (refreshing: boolean) => ({ refreshing }),
+ loadInitialData: (isBackground: boolean = false) => ({ isBackground }),
scheduleReload: true,
updateConnection: true,
updateGeoConnection: true,
@@ -427,6 +432,12 @@ export const liveWebAnalyticsMetricsLogic = kea<liveWebAnalyticsMetricsLogicType
setIsLoading: (_, { loading }) => loading,
},
],
+ isRefreshing: [
+ false,
+ {
+ setIsRefreshing: (_, { refreshing }) => refreshing,
+ },
+ ],
recentEvents: [
[] as LiveEvent[],
{
@@ -592,33 +603,46 @@ export const liveWebAnalyticsMetricsLogic = kea<liveWebAnalyticsMetricsLogicType
listeners(({ actions, values, cache }) => ({
pauseStream: () => {
cache.isPaused = true
- cache.loadAbortController?.abort()
- cache.eventsConnection?.abort()
- cache.geoConnection?.abort()
- cache.batch = []
- cache.geoBatch = []
- stopFlushInterval(cache as FlushCache)
- cache.disposables.dispose('liveUserCountInterval')
+ cache.disposables.add(
+ () => {
+ const timeoutId = setTimeout(() => {
+ cache.loadAbortController?.abort()
+ cache.eventsConnection?.abort()
+ cache.geoConnection?.abort()
+ cache.batch = []
+ cache.geoBatch = []
+ stopFlushInterval(cache as FlushCache)
+ cache.disposables.dispose('liveUserCountInterval')
+ cache.isTornDown = true
+ }, PAUSE_GRACE_MS)
+ return () => clearTimeout(timeoutId)
+ },
+ 'pauseGrace',
+ { pauseOnPageHidden: false }
+ )
},
resumeStream: () => {
cache.isPaused = false
- if (cache.hasInitialized) {
- lemonToast.info('Refreshing live data...', {
- toastId: RECONNECT_TOAST_ID,
- autoClose: 2000,
- })
+ cache.disposables.dispose('pauseGrace')
+ if (!cache.isTornDown) {
+ return
}
+ cache.isTornDown = false
startFlushInterval(cache as FlushCache, actions as FlushActions)
startLiveUserCountInterval(cache as FlushCache, actions as FlushActions)
- actions.loadInitialData()
+ actions.loadInitialData(true)
},
- loadInitialData: async () => {
+ loadInitialData: async ({ isBackground }) => {
cache.loadAbortController?.abort()
const abortController = new AbortController()
cache.loadAbortController = abortController
const { signal } = abortController
- actions.setIsLoading(true)
+ if (!cache.hasLoadedData || !isBackground) {
+ actions.setIsLoading(true)
+ } else {
+ actions.setIsRefreshing(true)
+ }
try {
const now = Date.now()
@@ -666,6 +690,7 @@ export const liveWebAnalyticsMetricsLogic = kea<liveWebAnalyticsMetricsLogicType
[...bucketMap.entries()].map(([timestamp, bucket]) => ({ timestamp, bucket })),
data.recentUsers ? getRecentUsersByLastSeenEntries(data.recentUsers) : []
)
+ cache.hasLoadedData = true
if (data.failedQueries.length > 0) {
lemonToast.warning(`Some live metrics failed to load: ${data.failedQueries.join(', ')}`, {
@@ -683,6 +708,7 @@ export const liveWebAnalyticsMetricsLogic = kea<liveWebAnalyticsMetricsLogicType
}
if (!signal.aborted) {
actions.setIsLoading(false)
+ actions.setIsRefreshing(false)
}
cache.hasInitialized = true
}
@@ -825,6 +851,7 @@ export const liveWebAnalyticsMetricsLogic = kea<liveWebAnalyticsMetricsLogicType
startLiveUserCountInterval(cache as FlushCache, actions as FlushActions)
},
beforeUnmount: () => {
+ cache.disposables.dispose('pauseGrace')
cache.loadAbortController?.abort()
cache.eventsConnection?.abort()
cache.geoConnection?.abort()