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.

21.2 KB
Raw Blame
import { MakeLogicType, actions, afterMount, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'

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

import { DataQualitySubjectRef, DataQualitySubjectType, apiErrorDetail, checksApi } from './checksApi'
import { checkDisplayName } from './checksConstants'
import type {
    DataQualityCheckApi,
    DataQualityCheckRunApi,
    DataQualitySubjectHealthApi,
    DataQualitySuiteRunApi,
} from './generated/api.schemas'
import type { DataQualityOverviewCheckApi } from './generated/api.schemas'
import { openFailingRowsInSqlEditor } from './openFailingRows'
import { isTerminalSuiteRun, suiteRunOutcome, suiteRunPollListeners, suiteRunSummary } from './suiteRuns'

const CHECKS_LIMIT = 100
const SUITE_RUNS_LIMIT = 20
const FIRST_METRIC_CHECK_ADOPTION_ATTEMPTS = 5
const FIRST_METRIC_CHECK_ADOPTION_DELAY_MS = 2000

export type CheckPendingKind = 'running' | 'deleting' | 'toggling' | 'loadingRuns' | 'loadingSuiteRunRuns'

// Annotated rather than asserted at the use site: the annotation still fails the typecheck if a
// CheckPendingKind is added without seeding its map, and kea-typegen can read the type from here.
// Inlining it with `satisfies` makes typegen infer `{ running: {}, ... }`, which nothing can index.
const EMPTY_PENDING_CHECK_ACTIONS: Record<CheckPendingKind, Record<string, boolean>> = {
    running: {},
    deleting: {},
    toggling: {},
    loadingRuns: {},
    loadingSuiteRunRuns: {},
}

export interface DataQualityChecksLogicProps {
    subjectType: DataQualitySubjectType
    subjectId: string
}

function subjectRef(props: DataQualityChecksLogicProps): DataQualitySubjectRef {
    return { subjectType: props.subjectType, subjectId: props.subjectId }
}

function isForbidden(error: unknown): boolean {
    return error instanceof ApiError && error.status === 403
}

function checkSortRank(check: DataQualityCheckApi): number {
    if (check.last_status === 'failed' || check.last_status === 'errored') {
        return 0
    }
    return check.enabled === false ? 2 : 1
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dataQualityChecksLogicValues {
    accessDenied: boolean
    activeSuiteRun: DataQualitySuiteRunApi | null
    checkRunsByCheckId: Record<string, DataQualityCheckRunApi[]>
    checks: DataQualityCheckApi[]
    checksLoadError: string | null
    checksLoaded: boolean
    checksLoading: boolean
    enabledChecksCount: number
    health: DataQualitySubjectHealthApi | null
    healthLoading: boolean
    isSuiteRunning: boolean
    pendingCheckActions: Record<CheckPendingKind, Record<string, boolean>>
    pollTimedOut: boolean
    runAllInFlight: boolean
    sortedChecks: DataQualityCheckApi[]
    suiteRunCheckRunsBySuiteRunId: Record<string, DataQualityCheckRunApi[]>
    suiteRuns: DataQualitySuiteRunApi[]
    suiteRunsError: boolean
    suiteRunsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dataQualityChecksLogicActions {
    deleteCheck: (checkId: string) => {
        checkId: string
    }
    finishSuiteRun: (suiteRun: DataQualitySuiteRunApi) => {
        suiteRun: DataQualitySuiteRunApi
    }
    loadCheckRuns: (checkId: string) => {
        checkId: string
    }
    loadChecks: () => any
    loadChecksFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadChecksSuccess: (
        checks: DataQualityOverviewCheckApi[],
        payload?: any
    ) => {
        checks: DataQualityOverviewCheckApi[]
        payload?: any
    }
    loadHealth: () => any
    loadHealthFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadHealthSuccess: (
        health: DataQualitySubjectHealthApi,
        payload?: any
    ) => {
        health: DataQualitySubjectHealthApi
        payload?: any
    }
    loadSuiteRunCheckRuns: (suiteRunId: string) => {
        suiteRunId: string
    }
    loadSuiteRuns: () => any
    loadSuiteRunsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadSuiteRunsSuccess: (
        suiteRuns: DataQualitySuiteRunApi[],
        payload?: any
    ) => {
        suiteRuns: DataQualitySuiteRunApi[]
        payload?: any
    }
    openFailingRows: (checkId: string) => {
        checkId: string
    }
    pollActiveSuiteRun: () => {
        value: true
    }
    removeCheck: (checkId: string) => {
        checkId: string
    }
    runAll: () => {
        value: true
    }
    runCheck: (checkId: string) => {
        checkId: string
    }
    scheduleSuiteRunPoll: () => {
        value: true
    }
    setAccessDenied: () => {
        value: true
    }
    setActiveSuiteRun: (suiteRun: DataQualitySuiteRunApi | null) => {
        suiteRun: DataQualitySuiteRunApi | null
    }
    setCheckPending: (
        kind: CheckPendingKind,
        checkId: string,
        pending: boolean
    ) => {
        checkId: string
        kind: CheckPendingKind
        pending: boolean
    }
    setCheckRuns: (
        checkId: string,
        runs: DataQualityCheckRunApi[]
    ) => {
        checkId: string
        runs: DataQualityCheckRunApi[]
    }
    setPollTimedOut: () => {
        value: true
    }
    setRunAllInFlight: (inFlight: boolean) => {
        inFlight: boolean
    }
    setSuiteRunCheckRuns: (
        suiteRunId: string,
        runs: DataQualityCheckRunApi[]
    ) => {
        runs: DataQualityCheckRunApi[]
        suiteRunId: string
    }
    toggleCheckEnabled: (
        checkId: string,
        enabled: boolean
    ) => {
        checkId: string
        enabled: boolean
    }
    upsertCheck: (check: DataQualityCheckApi) => {
        check: DataQualityCheckApi
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dataQualityChecksLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        enabledChecksCount: (checks: DataQualityCheckApi[]) => number
        isSuiteRunning: (activeSuiteRun: DataQualitySuiteRunApi | null, pollTimedOut: boolean) => boolean
        sortedChecks: (checks: DataQualityCheckApi[]) => DataQualityCheckApi[]
    }
}

export type dataQualityChecksLogicType = MakeLogicType<
    dataQualityChecksLogicValues,
    dataQualityChecksLogicActions,
    DataQualityChecksLogicProps,
    dataQualityChecksLogicMeta
>

export const dataQualityChecksLogic = kea<dataQualityChecksLogicType>([
    props({} as DataQualityChecksLogicProps),
    key((props: DataQualityChecksLogicProps) => `${props.subjectType}:${props.subjectId}`),
    path((key) => ['products', 'data_quality', 'frontend', 'dataQualityChecksLogic', key]),
    actions({
        upsertCheck: (check: DataQualityCheckApi) => ({ check }),
        removeCheck: (checkId: string) => ({ checkId }),
        deleteCheck: (checkId: string) => ({ checkId }),
        toggleCheckEnabled: (checkId: string, enabled: boolean) => ({ checkId, enabled }),
        runCheck: (checkId: string) => ({ checkId }),
        runAll: true,
        setRunAllInFlight: (inFlight: boolean) => ({ inFlight }),
        setCheckPending: (kind: CheckPendingKind, checkId: string, pending: boolean) => ({ kind, checkId, pending }),
        setActiveSuiteRun: (suiteRun: DataQualitySuiteRunApi | null) => ({ suiteRun }),
        scheduleSuiteRunPoll: true,
        pollActiveSuiteRun: true,
        finishSuiteRun: (suiteRun: DataQualitySuiteRunApi) => ({ suiteRun }),
        setPollTimedOut: true,
        loadCheckRuns: (checkId: string) => ({ checkId }),
        openFailingRows: (checkId: string) => ({ checkId }),
        setCheckRuns: (checkId: string, runs: DataQualityCheckRunApi[]) => ({ checkId, runs }),
        loadSuiteRunCheckRuns: (suiteRunId: string) => ({ suiteRunId }),
        setSuiteRunCheckRuns: (suiteRunId: string, runs: DataQualityCheckRunApi[]) => ({ suiteRunId, runs }),
        setAccessDenied: true,
    }),
    loaders(({ props }) => ({
        checks: [
            [] as DataQualityCheckApi[],
            {
                loadChecks: async () => (await checksApi.list(subjectRef(props), CHECKS_LIMIT)).results,
            },
        ],
        health: [
            null as DataQualitySubjectHealthApi | null,
            {
                loadHealth: async () => await checksApi.health(subjectRef(props)),
            },
        ],
        suiteRuns: [
            [] as DataQualitySuiteRunApi[],
            {
                loadSuiteRuns: async () => (await checksApi.suiteRuns(subjectRef(props), SUITE_RUNS_LIMIT)).results,
            },
        ],
    })),
    reducers({
        checksLoadError: [
            null as string | null,
            {
                loadChecks: () => null,
                loadChecksFailure: (_, { error }) => error,
            },
        ],
        // A refresh runs after every suite run, so a later failure must not take the last good
        // list off the screen: the panel keeps it and warns instead.
        checksLoaded: [
            false,
            {
                loadChecksSuccess: () => true,
            },
        ],
        checks: {
            upsertCheck: (state: DataQualityCheckApi[], { check }: { check: DataQualityCheckApi }) =>
                state.some((existing) => existing.id === check.id)
                    ? state.map((existing) => (existing.id === check.id ? check : existing))
                    : [check, ...state],
            removeCheck: (state: DataQualityCheckApi[], { checkId }: { checkId: string }) =>
                state.filter((check) => check.id !== checkId),
        },
        pendingCheckActions: [
            EMPTY_PENDING_CHECK_ACTIONS,
            {
                setCheckPending: (state, { kind, checkId, pending }) => ({
                    ...state,
                    [kind]: { ...state[kind], [checkId]: pending },
                }),
            },
        ],
        runAllInFlight: [
            false,
            {
                runAll: () => true,
                setRunAllInFlight: (_, { inFlight }) => inFlight,
            },
        ],
        activeSuiteRun: [
            null as DataQualitySuiteRunApi | null,
            {
                setActiveSuiteRun: (_, { suiteRun }) => suiteRun,
                finishSuiteRun: (_, { suiteRun }) => suiteRun,
            },
        ],
        pollTimedOut: [
            false,
            {
                setPollTimedOut: () => true,
                setActiveSuiteRun: () => false,
            },
        ],
        checkRunsByCheckId: [
            {} as Record<string, DataQualityCheckRunApi[]>,
            {
                setCheckRuns: (state, { checkId, runs }) => ({ ...state, [checkId]: runs }),
            },
        ],
        suiteRunCheckRunsBySuiteRunId: [
            {} as Record<string, DataQualityCheckRunApi[]>,
            {
                setSuiteRunCheckRuns: (state, { suiteRunId, runs }) => ({ ...state, [suiteRunId]: runs }),
            },
        ],
        // Without this the table would claim no runs exist whenever the request fails, since a
        // failed load leaves the empty default behind.
        suiteRunsError: [
            false,
            {
                loadSuiteRuns: () => false,
                loadSuiteRunsSuccess: () => false,
                loadSuiteRunsFailure: () => true,
            },
        ],
        accessDenied: [
            false,
            {
                setAccessDenied: () => true,
            },
        ],
    }),
    selectors({
        enabledChecksCount: [
            (s) => [s.checks],
            (checks: DataQualityCheckApi[]) => checks.filter((check) => check.enabled !== false).length,
        ],
        isSuiteRunning: [
            (s) => [s.activeSuiteRun, s.pollTimedOut],
            (activeSuiteRun: DataQualitySuiteRunApi | null, pollTimedOut: boolean) =>
                !!activeSuiteRun && !isTerminalSuiteRun(activeSuiteRun) && !pollTimedOut,
        ],
        sortedChecks: [
            (s) => [s.checks],
            (checks: DataQualityCheckApi[]) =>
                [...checks].sort(
                    (left, right) =>
                        checkSortRank(left) - checkSortRank(right) ||
                        checkDisplayName(left).localeCompare(checkDisplayName(right))
                ),
        ],
    }),
    listeners(({ props, values, actions, cache, selectors }) => {
        // Assigned one by one rather than spread: kea-typegen walks this object literal and crashes
        // on a spread element, which has no property name.
        const poll = suiteRunPollListeners({
            retrieve: (suiteRunId) => checksApi.suiteRunRetrieve(suiteRunId),
            // A permanent 403 -- the flag turned off mid-run, or query access revoked -- takes the
            // panel's access-denied path immediately, like every other request here.
            onPollError: (error) => {
                if (!isForbidden(error)) {
                    return false
                }
                actions.setAccessDenied()
                return true
            },
            cache,
            values,
            actions,
        })
        return {
            setActiveSuiteRun: poll.setActiveSuiteRun,
            scheduleSuiteRunPoll: poll.scheduleSuiteRunPoll,
            pollActiveSuiteRun: poll.pollActiveSuiteRun,
            loadChecksFailure: ({ errorObject }) => {
                if (isForbidden(errorObject)) {
                    actions.setAccessDenied()
                }
            },
            loadHealthFailure: ({ errorObject }) => {
                if (isForbidden(errorObject)) {
                    actions.setAccessDenied()
                }
            },
            deleteCheck: async ({ checkId }) => {
                if (values.pendingCheckActions.deleting[checkId]) {
                    return
                }
                actions.setCheckPending('deleting', checkId, true)
                try {
                    await checksApi.destroy(checkId)
                    actions.removeCheck(checkId)
                    actions.loadHealth()
                } catch (error) {
                    lemonToast.error(apiErrorDetail(error) ?? 'Could not delete the check. Try again.')
                } finally {
                    actions.setCheckPending('deleting', checkId, false)
                }
            },
            toggleCheckEnabled: async ({ checkId, enabled }) => {
                if (values.pendingCheckActions.toggling[checkId]) {
                    return
                }
                actions.setCheckPending('toggling', checkId, true)
                try {
                    actions.upsertCheck(await checksApi.partialUpdate(checkId, { enabled }))
                    actions.loadHealth()
                } catch (error) {
                    lemonToast.error(apiErrorDetail(error) ?? 'Could not update the check. Try again.')
                } finally {
                    actions.setCheckPending('toggling', checkId, false)
                }
            },
            loadCheckRuns: async ({ checkId }) => {
                if (values.pendingCheckActions.loadingRuns[checkId]) {
                    return
                }
                actions.setCheckPending('loadingRuns', checkId, true)
                try {
                    actions.setCheckRuns(checkId, await checksApi.runs(checkId))
                } catch (error) {
                    lemonToast.error(apiErrorDetail(error) ?? 'Could not load the run history. Try again.')
                } finally {
                    actions.setCheckPending('loadingRuns', checkId, false)
                }
            },
            openFailingRows: async ({ checkId }) => {
                await openFailingRowsInSqlEditor({
                    cachedRuns: values.checkRunsByCheckId[checkId],
                    fetchRuns: () => checksApi.runs(checkId),
                    onRunsFetched: (runs) => actions.setCheckRuns(checkId, runs),
                })
            },
            loadSuiteRunCheckRuns: async ({ suiteRunId }) => {
                if (values.pendingCheckActions.loadingSuiteRunRuns[suiteRunId]) {
                    return
                }
                actions.setCheckPending('loadingSuiteRunRuns', suiteRunId, true)
                try {
                    actions.setSuiteRunCheckRuns(suiteRunId, await checksApi.suiteRunCheckRuns(suiteRunId))
                } catch (error) {
                    lemonToast.error(apiErrorDetail(error) ?? 'Could not load the run details. Try again.')
                } finally {
                    actions.setCheckPending('loadingSuiteRunRuns', suiteRunId, false)
                }
            },
            runCheck: async ({ checkId }) => {
                if (values.pendingCheckActions.running[checkId]) {
                    return
                }
                actions.setCheckPending('running', checkId, true)
                // The pending guard is per check, and "Run all" is not blocked by it either, so two
                // starts can be in flight at once. Only the newest one's run may become active:
                // otherwise a slower earlier request lands last and the panel polls the older run.
                cache.suiteStartToken = (cache.suiteStartToken ?? 0) + 1
                const startToken = cache.suiteStartToken
                try {
                    const started = await checksApi.run(checkId)
                    if (cache.suiteStartToken === startToken) {
                        actions.setActiveSuiteRun(started)
                    }
                } catch (error) {
                    lemonToast.error(apiErrorDetail(error) ?? 'Could not start the check. Try again.')
                } finally {
                    actions.setCheckPending('running', checkId, false)
                }
            },
            runAll: async () => {
                cache.suiteStartToken = (cache.suiteStartToken ?? 0) + 1
                const startToken = cache.suiteStartToken
                try {
                    const started = await checksApi.runAll(subjectRef(props))
                    if (cache.suiteStartToken === startToken) {
                        actions.setActiveSuiteRun(started)
                    }
                } catch (error) {
                    lemonToast.error(apiErrorDetail(error) ?? 'Could not start the checks. Try again.')
                } finally {
                    actions.setRunAllInFlight(false)
                }
            },
            finishSuiteRun: ({ suiteRun }) => {
                cache.disposables.dispose('suiteRunPoll')
                actions.loadChecks()
                actions.loadHealth()
                Object.keys(values.checkRunsByCheckId).forEach((checkId) => actions.loadCheckRuns(checkId))
                // An adopted or MCP-triggered suite can be expanded while it is still running, so its
                // rows are cached mid-flight. Without this they stay stale until it is reopened.
                Object.keys(values.suiteRunCheckRunsBySuiteRunId).forEach((suiteRunId) =>
                    actions.loadSuiteRunCheckRuns(suiteRunId)
                )
                actions.loadSuiteRuns()
                const outcome = suiteRunOutcome(suiteRun)
                if (outcome === 'empty') {
                    lemonToast.info('No enabled checks to run')
                } else if (outcome === 'errored') {
                    lemonToast.error(suiteRun.error || "Couldn't run checks. Try again.")
                } else if (outcome === 'warning') {
                    lemonToast.warning(suiteRunSummary(suiteRun))
                } else {
                    lemonToast.success(suiteRunSummary(suiteRun))
                }
            },
            upsertCheck: (_, __, ___, previousState) => {
                if (props.subjectType !== 'metric' || selectors.checks(previousState, props).length > 0) {
                    return
                }
                cache.scheduledRunAttemptsLeft = FIRST_METRIC_CHECK_ADOPTION_ATTEMPTS
                actions.loadSuiteRuns()
            },
            // A suite started elsewhere, or before this panel opened, is the newest row of the
            // history this logic already loads. Reading it from there costs no extra request.
            loadSuiteRunsSuccess: ({ suiteRuns }) => {
                const [newest] = suiteRuns
                if (newest && !isTerminalSuiteRun(newest) && !values.activeSuiteRun) {
                    cache.scheduledRunAttemptsLeft = 0
                    actions.setActiveSuiteRun(newest)
                    return
                }
                if (values.activeSuiteRun || !cache.scheduledRunAttemptsLeft) {
                    return
                }
                cache.scheduledRunAttemptsLeft -= 1
                cache.disposables.add(() => {
                    const timeoutId = setTimeout(() => actions.loadSuiteRuns(), FIRST_METRIC_CHECK_ADOPTION_DELAY_MS)
                    return () => clearTimeout(timeoutId)
                }, 'adoptScheduledRun')
            },
            loadSuiteRunsFailure: ({ errorObject }) => {
                if (isForbidden(errorObject)) {
                    actions.setAccessDenied()
                }
            },
        }
    }),
    afterMount(({ actions }) => {
        actions.loadChecks()
        actions.loadHealth()
        actions.loadSuiteRuns()
    }),
])