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.

2.8 KB
Raw Blame
import { MakeLogicType, afterMount, kea, key, path, props } from 'kea'

import { dashboardLogic } from 'scenes/dashboard/dashboardLogic'

import { DashboardPlacement } from '~/types'

import { DashboardPerformanceMonitor } from './dashboardPerformance'

// Embedded, exported and public placements have their own layout and load paths, which would mix into the numbers.
const MEASURED_PLACEMENTS = new Set([DashboardPlacement.Dashboard, DashboardPlacement.ProjectHomepage])

export interface DashboardPerformanceLogicProps {
    dashboardId: number
    placement: DashboardPlacement
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dashboardPerformanceLogicMeta {
    key: number
}

export type dashboardPerformanceLogicType = MakeLogicType<
    {},
    {},
    DashboardPerformanceLogicProps,
    dashboardPerformanceLogicMeta
>

export const dashboardPerformanceLogic = kea<dashboardPerformanceLogicType>([
    path(['scenes', 'dashboard', 'dashboardPerformanceLogic']),
    props({} as DashboardPerformanceLogicProps),
    key((props) => props.dashboardId),
    afterMount(({ props, cache }) => {
        if (
            !Number.isFinite(props.dashboardId) ||
            !MEASURED_PLACEMENTS.has(props.placement) ||
            !DashboardPerformanceMonitor.shouldSample()
        ) {
            return
        }
        cache.disposables.add(
            () => {
                const start = (): ReturnType<DashboardPerformanceMonitor['start']> =>
                    new DashboardPerformanceMonitor({
                        dashboardId: props.dashboardId,
                        placement: props.placement,
                        getTileCount: () =>
                            dashboardLogic.findMounted({ id: props.dashboardId })?.values.tiles.length ?? 0,
                    }).start()
                let report = start()
                const onPageHide = (): void => report({ transport: 'sendBeacon', send_instantly: true })
                // A page restored from the back-forward cache does not mount again, so it needs a new monitor.
                const onPageShow = (event: PageTransitionEvent): void => {
                    if (event.persisted) {
                        report = start()
                    }
                }
                window.addEventListener('pagehide', onPageHide)
                window.addEventListener('pageshow', onPageShow)
                return () => {
                    window.removeEventListener('pagehide', onPageHide)
                    window.removeEventListener('pageshow', onPageShow)
                    report()
                }
            },
            'performanceMonitor',
            // The monitor excludes hidden time itself, and a pause would split one view into two events.
            { pauseOnPageHidden: false }
        )
    }),
])