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.

3.4 KB
Raw Blame
import { MakeLogicType, actions, afterMount, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'

import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'

import { getCurrentTeamId } from '~/lib/utils/getAppContext'

import { precomputeDebugInvalidate, precomputeDebugState } from './generated/api'
import type { PrecomputeDebugResponseApi } from './generated/api.schemas'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface precomputeDebugLogicValues {
    debugState: PrecomputeDebugResponseApi | null
    debugStateLoading: boolean
    invalidatingHash: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface precomputeDebugLogicActions {
    invalidate: (queryHash: string | null) => {
        queryHash: string | null
    }
    invalidateFinished: () => {
        value: true
    }
    loadDebugState: () => any
    loadDebugStateFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadDebugStateSuccess: (
        debugState: PrecomputeDebugResponseApi,
        payload?: any
    ) => {
        debugState: PrecomputeDebugResponseApi
        payload?: any
    }
}

export type precomputeDebugLogicType = MakeLogicType<precomputeDebugLogicValues, precomputeDebugLogicActions>

export const precomputeDebugLogic = kea<precomputeDebugLogicType>([
    path(['products', 'analytics_platform', 'frontend', 'precomputeDebugLogic']),

    actions({
        invalidate: (queryHash: string | null) => ({ queryHash }),
        invalidateFinished: true,
    }),

    loaders({
        debugState: [
            null as PrecomputeDebugResponseApi | null,
            {
                loadDebugState: async () => {
                    return await precomputeDebugState(String(getCurrentTeamId()))
                },
            },
        ],
    }),

    reducers({
        // The hash being invalidated, or 'all' — drives per-button loading and
        // blocks concurrent invalidations.
        invalidatingHash: [
            null as string | null,
            {
                invalidate: (_, { queryHash }) => queryHash ?? 'all',
                invalidateFinished: () => null,
            },
        ],
    }),

    listeners(({ actions }) => ({
        invalidate: async ({ queryHash }) => {
            try {
                const result = await precomputeDebugInvalidate(String(getCurrentTeamId()), {
                    query_hash: queryHash,
                })
                const pendingNote =
                    result.pending_count > 0
                        ? ` (${result.pending_count} in-flight ${
                              result.pending_count === 1 ? 'job' : 'jobs'
                          } untouched, invalidate again once ${result.pending_count === 1 ? 'it' : 'they'} settle)`
                        : ''
                lemonToast.success(
                    `Marked ${result.updated_count} precompute ${
                        result.updated_count === 1 ? 'job' : 'jobs'
                    } stale${pendingNote}`
                )
                actions.loadDebugState()
            } catch {
                lemonToast.error('Failed to invalidate precompute jobs')
            } finally {
                actions.invalidateFinished()
            }
        },
    })),

    afterMount(({ actions }) => {
        actions.loadDebugState()
    }),
])