: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
import { MakeLogicType, actions, connect, events, kea, listeners, path, reducers } from 'kea'
import { LIVE_COUNT_REFRESH_MS, loadCachedLiveCount } from 'lib/components/LiveUserCount/liveCountCache'
import { hogql } from 'lib/utils/hogql'
import { teamLogic } from 'scenes/teamLogic'
import { QueryLogTags } from '~/queries/schema/schema-general'
const QUERY_TAGS: QueryLogTags = { scene: 'Replay', productKey: 'session_replay' }
// The min_first_timestamp bound lets ClickHouse prune on the sort key; a session cannot run longer than a day.
const LIVE_RECORDINGS_COUNT_QUERY = hogql`
SELECT count()
FROM (
SELECT session_id
FROM raw_session_replay_events
WHERE _timestamp >= now() - INTERVAL 5 MINUTE
AND min_first_timestamp >= now() - INTERVAL 1 DAY
GROUP BY session_id
HAVING max(is_deleted) = 0
)`
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface liveRecordingsCountLogicValues {
currentTeamId: number | null // teamLogic
activeRecordings: number | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface liveRecordingsCountLogicActions {
loadCurrentTeamSuccess: (
currentTeam: null | import('../../../types').TeamPublicType,
payload?: any
) => {
currentTeam: null | import('../../../types').TeamPublicType
payload?: any
} // teamLogic
pollActiveRecordings: () => {
value: true
}
setActiveRecordings: (activeRecordings: number) => {
activeRecordings: number
}
}
export type liveRecordingsCountLogicType = MakeLogicType<
liveRecordingsCountLogicValues,
liveRecordingsCountLogicActions
>
export const liveRecordingsCountLogic = kea<liveRecordingsCountLogicType>([
path(['scenes', 'session-recordings', 'components', 'liveRecordingsCountLogic']),
connect(() => ({
values: [teamLogic, ['currentTeamId']],
actions: [teamLogic, ['loadCurrentTeamSuccess']],
})),
actions({
pollActiveRecordings: true,
setActiveRecordings: (activeRecordings: number) => ({ activeRecordings }),
}),
reducers({
activeRecordings: [
null as number | null,
{
setActiveRecordings: (_, { activeRecordings }) => activeRecordings,
},
],
}),
listeners(({ actions, values, cache }) => ({
loadCurrentTeamSuccess: () => {
// Without a team, a poll skips the query and waits a full refresh interval, so poll again when the team loads.
if (values.activeRecordings === null) {
actions.pollActiveRecordings()
}
},
pollActiveRecordings: async () => {
let nextPollMs = LIVE_COUNT_REFRESH_MS
try {
if (values.currentTeamId) {
const { value, fetchedAt } = await loadCachedLiveCount(
'recordings',
values.currentTeamId,
LIVE_RECORDINGS_COUNT_QUERY,
QUERY_TAGS
)
if (cache.disposables.isDisposed) {
return
}
actions.setActiveRecordings(value)
nextPollMs = Math.max(0, fetchedAt + LIVE_COUNT_REFRESH_MS - Date.now())
}
} catch {
// A failed poll keeps the last count instead of toasting every interval.
}
// Scheduling after the poll settles means two polls never overlap.
cache.disposables.add(() => {
const timeoutId = setTimeout(() => actions.pollActiveRecordings(), nextPollMs)
return () => clearTimeout(timeoutId)
}, 'pollTimeout')
},
})),
events(({ actions }) => ({
afterMount: () => {
actions.pollActiveRecordings()
},
})),
])