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.

1.8 KB
Raw Blame
import { router } from 'kea-router'

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

import { apiErrorDetail } from './checksApi'
import { UNPUBLISHED_REFRESH_EXPLANATION, UNPUBLISHED_REFRESH_QUERY_UNAVAILABLE } from './checksConstants'
import type { DataQualityCheckRunApi } from './generated/api.schemas'
import { latestRunWithQuery } from './suiteRuns'

/**
 * Send the caller to this check's failing rows in the SQL editor.
 *
 * The rows that failed only exist in the run's compiled query, and runs load lazily, so a caller
 * that has not expanded the check yet passes `fetchRuns` and gets them loaded here.
 */
export async function openFailingRowsInSqlEditor({
    cachedRuns,
    fetchRuns,
    onRunsFetched,
}: {
    cachedRuns: DataQualityCheckRunApi[] | undefined
    fetchRuns: () => Promise<DataQualityCheckRunApi[]>
    onRunsFetched: (runs: DataQualityCheckRunApi[]) => void
}): Promise<void> {
    let runs = cachedRuns
    if (!runs) {
        try {
            runs = await fetchRuns()
            onRunsFetched(runs)
        } catch (error) {
            lemonToast.error(apiErrorDetail(error) ?? 'Could not load the run history. Try again.')
            return
        }
    }
    const run = latestRunWithQuery(runs)
    if (!run) {
        lemonToast.info(
            runs.length
                ? 'The query for this check is no longer kept. Run the check to see its failing rows.'
                : "This check hasn't run yet. Run it to see its failing rows."
        )
        return
    }
    if (!run.compiled_query) {
        lemonToast.info(UNPUBLISHED_REFRESH_QUERY_UNAVAILABLE)
        return
    }
    if (run.audited_staged_refresh) {
        lemonToast.info(UNPUBLISHED_REFRESH_EXPLANATION)
    }
    router.actions.push(urls.sqlEditor({ query: run.compiled_query }))
}