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.

16.8 KB
Raw Blame
import {
    MakeLogicType,
    actions,
    afterMount,
    beforeUnmount,
    kea,
    key,
    listeners,
    path,
    props,
    reducers,
    selectors,
} from 'kea'
import posthog, { CaptureOptions } from 'posthog-js'

import { PageLoadTimeToSeeData } from 'lib/internalMetrics'
import { uuid } from 'lib/utils/dom'
import { sceneLogic } from 'scenes/sceneLogic'

import { isPageCollection } from '~/queries/nodes/DataNode/pageCollections'
import { RefreshType } from '~/queries/schema/schema-general'

export interface DataNodeRegisteredProps {
    id: string
    loadData: (refresh?: RefreshType) => void
    cancelQuery: () => void
}

export interface CollectionNodeLoadMeta {
    isCached?: boolean
    lastRefresh?: string | null
}

export interface DataNodeCollectionProps {
    key: string
}

export interface DataNodeStatus {
    isLoading: boolean
    hasError: boolean
    lastRefresh?: string | null
}

export type DataNodeStatusMap = Record<string, DataNodeStatus | undefined>

export type DataCollectionLoadTrigger = 'initial_load' | 'refresh' | 'update'

interface DataCollectionLoadTile {
    kind: string | null
    status: 'loading' | 'success' | 'failure'
    cached: boolean
}

interface DataCollectionLoadCycle {
    id: string
    startedAt: number
    scene: string | null
    trigger: DataCollectionLoadTrigger
    tiles: Map<string, DataCollectionLoadTile>
    lastTileId: string | null
    visitId: string | null
    hiddenMs: number
    reportedHidden: boolean
    firstTileMs: number | null
    kindReadyMs: Record<string, number>
    requeriedWhileLoading: number
}

interface DataCollectionPageVisit {
    id: string
    startedAt: number
    cycles: number
    tilesMounted: number
    reported: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dataNodeCollectionLogicValues {
    areAnyLoading: boolean
    collectionNodeStatus: DataNodeStatusMap
    mountedDataNodes: DataNodeRegisteredProps[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dataNodeCollectionLogicActions {
    cancelAllLoading: () => {}
    collectionNodeLoadData: (
        id: string,
        kind?: string
    ) => {
        id: string
        kind: string | undefined
    }
    collectionNodeLoadDataFailure: (id: string) => {
        id: string
    }
    collectionNodeLoadDataSuccess: (
        id: string,
        meta?: CollectionNodeLoadMeta
    ) => {
        id: string
        meta: CollectionNodeLoadMeta | undefined
    }
    endPageVisit: () => {
        value: true
    }
    mountDataNode: (
        id: string,
        props: DataNodeRegisteredProps
    ) => {
        id: string
        props: DataNodeRegisteredProps
    }
    pageHidden: () => {
        value: true
    }
    reloadAll: () => {}
    startPageVisit: (visitId: string) => {
        visitId: string
    }
    unmountDataNode: (id: string) => {
        id: string
    }
    visibilityChanged: (hidden: boolean) => {
        hidden: boolean
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dataNodeCollectionLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        areAnyLoading: (collectionNodeStatus: DataNodeStatusMap) => boolean
    }
}

export type dataNodeCollectionLogicType = MakeLogicType<
    dataNodeCollectionLogicValues,
    dataNodeCollectionLogicActions,
    DataNodeCollectionProps,
    dataNodeCollectionLogicMeta
>

export const dataNodeCollectionLogic = kea<dataNodeCollectionLogicType>([
    path(['queries', 'nodes', 'dataNodeCollectionLogic']),
    props({} as DataNodeCollectionProps),
    key((props: DataNodeCollectionProps) => {
        return props.key
    }),
    actions({
        mountDataNode: (id: string, props: DataNodeRegisteredProps) => ({ id, props }),
        unmountDataNode: (id: string) => ({ id }),
        reloadAll: () => ({}),
        cancelAllLoading: () => ({}),
        collectionNodeLoadData: (id: string, kind?: string) => ({ id, kind }),
        collectionNodeLoadDataSuccess: (id: string, meta?: CollectionNodeLoadMeta) => ({ id, meta }),
        collectionNodeLoadDataFailure: (id: string) => ({ id }),
        pageHidden: true,
        visibilityChanged: (hidden: boolean) => ({ hidden }),
        startPageVisit: (visitId: string) => ({ visitId }),
        endPageVisit: true,
    }),
    reducers({
        mountedDataNodes: [
            [] as DataNodeRegisteredProps[],
            {
                mountDataNode: (state, payload) => {
                    const filtered = state.filter((node) => node.id !== payload.id)
                    return [...filtered, payload.props]
                },
                unmountDataNode: (state, payload) => state.filter((node) => node.id !== payload.id),
            },
        ],
        collectionNodeStatus: [
            {} as DataNodeStatusMap,
            {
                mountDataNode: (state, payload) => {
                    return { ...state, [payload.id]: state[payload.id] ?? { isLoading: false, hasError: false } }
                },
                unmountDataNode: (state, payload) => {
                    const { [payload.id]: _discard, ...rest } = state
                    return rest
                },
                collectionNodeLoadData: (state, payload) => {
                    return { ...state, [payload.id]: { isLoading: true, hasError: false } }
                },
                collectionNodeLoadDataSuccess: (state, payload) => {
                    return {
                        ...state,
                        [payload.id]: { isLoading: false, hasError: false, lastRefresh: payload.meta?.lastRefresh },
                    }
                },
                collectionNodeLoadDataFailure: (state, payload) => {
                    return { ...state, [payload.id]: { isLoading: false, hasError: true } }
                },
            },
        ],
    }),
    selectors({
        areAnyLoading: [
            (s) => [s.collectionNodeStatus],
            (collectionNodeStatus: DataNodeStatusMap) => {
                return Object.values(collectionNodeStatus).some((status) => {
                    return status?.isLoading
                })
            },
        ],
    }),
    listeners(({ values, props, cache }) => {
        const countTiles = (
            cycle: DataCollectionLoadCycle,
            matches: (tile: DataCollectionLoadTile) => boolean
        ): number => [...cycle.tiles.values()].filter(matches).length

        const report = (
            cycle: DataCollectionLoadCycle,
            outcome: Partial<PageLoadTimeToSeeData>,
            options?: CaptureOptions
        ): void => {
            if (!isPageCollection(props.key) || !posthog.capture) {
                return
            }
            const elapsedMs = Math.round(performance.now() - cycle.startedAt)
            const hiddenMs = Math.min(elapsedMs, Math.round(hiddenMsSoFar(cycle)))
            const payload: PageLoadTimeToSeeData = {
                type: 'page_load',
                context: props.key,
                action: cycle.trigger,
                primary_interaction_id: cycle.id,
                time_to_see_data_ms: elapsedMs,
                visible_ms: elapsedMs - hiddenMs,
                was_hidden: hiddenMs > 0 || cache.hiddenSince !== null,
                insights_fetched: cycle.tiles.size,
                insights_fetched_cached: countTiles(cycle, (tile) => tile.cached),
                tiles_succeeded: countTiles(cycle, (tile) => tile.status === 'success'),
                tiles_requeried_while_loading: cycle.requeriedWhileLoading,
                first_tile_ms: cycle.firstTileMs,
                kind_ready_ms: cycle.kindReadyMs,
                visit_id: cycle.visitId,
                scene: cycle.scene,
                time_since_mount_ms: Math.round(performance.now() - cache.mountedAt),
                ...outcome,
            }
            posthog.capture('time to see data', payload, options)
        }

        const hiddenMsSoFar = (cycle: DataCollectionLoadCycle): number =>
            cycle.hiddenMs +
            (cache.hiddenSince !== null ? performance.now() - Math.max(cache.hiddenSince, cycle.startedAt) : 0)

        const finishVisit = (reason: 'navigated_away' | 'left_app', options?: CaptureOptions): void => {
            const visit: DataCollectionPageVisit | null = cache.visit
            if (!visit) {
                return
            }
            if (cache.cycle) {
                abandon(cache.cycle, reason, options)
            } else if (!visit.reported && visit.cycles === 0 && isPageCollection(props.key) && posthog.capture) {
                const payload: PageLoadTimeToSeeData = {
                    type: 'page_load',
                    context: props.key,
                    action: 'initial_load',
                    status: 'no_load',
                    cancel_reason: reason,
                    primary_interaction_id: uuid(),
                    time_to_see_data_ms: Math.round(performance.now() - visit.startedAt),
                    insights_fetched: 0,
                    tiles_mounted: visit.tilesMounted,
                    visit_id: visit.id,
                    scene: sceneLogic.findMounted()?.values.activeSceneId ?? null,
                    time_since_mount_ms: Math.round(performance.now() - cache.mountedAt),
                }
                posthog.capture('time to see data', payload, options)
            }
            visit.reported = true
        }
        cache.finishVisit = finishVisit

        const finishTile = (id: string, status: 'success' | 'failure', cached = false): void => {
            const cycle = cache.cycle
            if (!cycle) {
                return
            }
            const kind = cycle.tiles.get(id)?.kind ?? null
            cycle.tiles.set(id, { kind, status, cached })
            cycle.lastTileId = id
            if (status === 'success') {
                const readyMs = Math.round(performance.now() - cycle.startedAt)
                cycle.firstTileMs ??= readyMs
                if (kind && !(kind in cycle.kindReadyMs)) {
                    cycle.kindReadyMs[kind] = readyMs
                }
            }
            if (!values.areAnyLoading) {
                settle(cycle)
            }
        }

        const closeCycle = (): void => {
            cache.cycle = null
            cache.completedCycles += 1
        }

        const settle = (cycle: DataCollectionLoadCycle): void => {
            const failedTileCount = countTiles(cycle, (tile) => tile.status === 'failure')
            const lastTile = cycle.lastTileId ? cycle.tiles.get(cycle.lastTileId) : undefined
            report(cycle, {
                status: failedTileCount > 0 ? 'failure' : 'success',
                failed_tile_count: failedTileCount,
                last_tile_id: cycle.lastTileId,
                last_tile_kind: lastTile?.kind ?? null,
                last_tile_status: lastTile?.status ?? null,
            })
            closeCycle()
        }

        const abandon = (
            cycle: DataCollectionLoadCycle,
            reason: NonNullable<PageLoadTimeToSeeData['cancel_reason']>,
            options?: CaptureOptions
        ): void => {
            report(
                cycle,
                {
                    status: 'cancelled',
                    cancel_reason: reason,
                    tiles_still_loading: countTiles(cycle, (tile) => tile.status === 'loading'),
                },
                options
            )
            closeCycle()
        }

        return {
            reloadAll: () => {
                if (cache.cycle) {
                    abandon(cache.cycle, 'refreshed')
                }
                cache.pendingTrigger = 'refresh'
                // We want to force refresh all nodes, so we use 'force_async' to bypass cache
                // The loadData function in each node will handle converting this to the appropriate type
                // based on whether the node is an insight or not
                values.mountedDataNodes.forEach((node) => node.loadData('force_async'))
                cache.pendingTrigger = null
            },
            cancelAllLoading: () => {
                values.mountedDataNodes.forEach((node) => {
                    const status = values.collectionNodeStatus[node.id]
                    if (status?.isLoading) {
                        node.cancelQuery()
                    }
                })
            },
            collectionNodeLoadData: ({ id, kind }) => {
                if (!cache.cycle) {
                    cache.cycle = {
                        id: uuid(),
                        startedAt: performance.now(),
                        scene: sceneLogic.findMounted()?.values.activeSceneId ?? null,
                        trigger: cache.pendingTrigger ?? (cache.completedCycles === 0 ? 'initial_load' : 'update'),
                        tiles: new Map(),
                        lastTileId: null,
                        visitId: cache.visit?.id ?? null,
                        hiddenMs: 0,
                        reportedHidden: false,
                        firstTileMs: null,
                        kindReadyMs: {},
                        requeriedWhileLoading: 0,
                    } satisfies DataCollectionLoadCycle
                    if (cache.visit) {
                        cache.visit.cycles += 1
                    }
                }
                if (cache.cycle.tiles.get(id)?.status === 'loading') {
                    cache.cycle.requeriedWhileLoading += 1
                }
                cache.cycle.tiles.set(id, { kind: kind ?? null, status: 'loading', cached: false })
            },
            collectionNodeLoadDataSuccess: ({ id, meta }) => {
                finishTile(id, 'success', !!meta?.isCached)
            },
            collectionNodeLoadDataFailure: ({ id }) => {
                finishTile(id, 'failure')
            },
            mountDataNode: () => {
                if (cache.visit) {
                    cache.visit.tilesMounted += 1
                }
            },
            unmountDataNode: ({ id }) => {
                if (cache.cycle?.tiles.get(id)?.status === 'loading') {
                    abandon(cache.cycle, 'navigated_away')
                }
            },
            pageHidden: () => {
                if (cache.visit) {
                    finishVisit('left_app', { transport: 'sendBeacon' })
                } else if (cache.cycle) {
                    abandon(cache.cycle, 'left_app', { transport: 'sendBeacon' })
                }
            },
            visibilityChanged: ({ hidden }) => {
                const cycle: DataCollectionLoadCycle | null = cache.cycle
                if (hidden) {
                    cache.hiddenSince ??= performance.now()
                    if (cycle && !cycle.reportedHidden) {
                        cycle.reportedHidden = true
                        report(cycle, { status: 'hidden' }, { transport: 'sendBeacon' })
                    }
                    return
                }
                if (cycle && cache.hiddenSince !== null) {
                    cycle.hiddenMs += performance.now() - Math.max(cache.hiddenSince, cycle.startedAt)
                }
                cache.hiddenSince = null
            },
            startPageVisit: ({ visitId }) => {
                if (cache.cycle) {
                    abandon(cache.cycle, 'navigated_away')
                }
                cache.visit = {
                    id: visitId,
                    startedAt: performance.now(),
                    cycles: 0,
                    tilesMounted: 0,
                    reported: false,
                } satisfies DataCollectionPageVisit
            },
            endPageVisit: () => {
                finishVisit('navigated_away')
                cache.visit = null
            },
        }
    }),
    afterMount(({ actions, cache }) => {
        cache.mountedAt = performance.now()
        cache.cycle = null
        cache.completedCycles = 0
        cache.pendingTrigger = null
        cache.visit = null
        cache.hiddenSince = document.visibilityState === 'hidden' ? performance.now() : null
        cache.disposables.add(
            () => {
                window.addEventListener('pagehide', actions.pageHidden)
                return () => window.removeEventListener('pagehide', actions.pageHidden)
            },
            'pagehide',
            { pauseOnPageHidden: false }
        )
        const onVisibilityChange = (): void => actions.visibilityChanged(document.visibilityState === 'hidden')
        cache.disposables.add(
            () => {
                document.addEventListener('visibilitychange', onVisibilityChange)
                return () => document.removeEventListener('visibilitychange', onVisibilityChange)
            },
            'visibilitychange',
            { pauseOnPageHidden: false }
        )
    }),
    beforeUnmount(({ cache }) => {
        cache.finishVisit?.('navigated_away')
    }),
])