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.

13.3 KB
Raw Blame
import {
    BuiltLogic,
    MakeLogicType,
    actions,
    connect,
    isBreakpoint,
    kea,
    key,
    listeners,
    path,
    props,
    reducers,
    selectors,
} from 'kea'

import api from 'lib/api'
import { insightDataLogic } from 'scenes/insights/insightDataLogic'
import { insightsApi } from 'scenes/insights/utils/api'
import {
    collectNotebookDataframeNodes,
    isReferenceableSqlV2FrameName,
} from 'scenes/notebooks/Nodes/notebookNodeContent'
import type { NotebookNodeSQLV2Result } from 'scenes/notebooks/Nodes/NotebookNodeSQLV2'
import { sqlV2RunErrorMessage } from 'scenes/notebooks/Nodes/notebookNodeSQLV2Logic'
import type { notebookLogicType } from 'scenes/notebooks/Notebook/notebookLogic'
import type { NotebookNodeRunTerminalStatus } from 'scenes/notebooks/Notebook/notebookNodeStalenessLogic'
import { notebookOperationsLogic } from 'scenes/notebooks/Notebook/notebookOperationsLogic'

import { performQuery } from '~/queries/query'
import { DataNode, NodeKind, QuerySchema } from '~/queries/schema/schema-general'
import type { InsightLogicProps, InsightShortId } from '~/types'

import type { JSONContent } from '../../../frontend/src/lib/components/RichContentEditor/types'

export interface InsightDataframeAttributes {
    nodeId?: string
    id?: InsightShortId
    query?: QuerySchema
    returnVariable?: string
    dataframeSource?: string
    dataframeQuery?: string
    runId?: string | null
    result?: NotebookNodeSQLV2Result | null
    runStatus?: NotebookNodeRunTerminalStatus | null
}

export interface NotebookInsightDataframeLogicProps {
    notebookLogic: BuiltLogic<notebookLogicType>
    nodeId: string
    insightProps: InsightLogicProps
    attributes: InsightDataframeAttributes
    updateAttributes: (attributes: Partial<InsightDataframeAttributes>) => void
    enabled: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookInsightDataframeLogicValues {
    isBusy: boolean // notebookOperationsLogic
    content: JSONContent // props.notebookLogic
    error: string | null
    isPreparing: boolean
    returnVariable: string
    unsupported: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookInsightDataframeLogicActions {
    prepareDataframe: (force: boolean) => {
        force: boolean
    }
    retry: () => {
        value: true
    }
    setError: (
        error: string | null,
        unsupported?: boolean
    ) => {
        error: string | null
        unsupported: boolean
    }
    setReturnVariable: (returnVariable: string) => {
        returnVariable: string
    }
    setSyncing: (syncing: boolean) => {
        syncing: boolean
    }
    syncDataframe: (force?: boolean) => {
        force: boolean
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookInsightDataframeLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        returnVariable: (content: JSONContent, arg: string, arg2: string | undefined) => string
    }
}

export type notebookInsightDataframeLogicType = MakeLogicType<
    notebookInsightDataframeLogicValues,
    notebookInsightDataframeLogicActions,
    NotebookInsightDataframeLogicProps,
    notebookInsightDataframeLogicMeta
>

export const notebookInsightDataframeLogic = kea<notebookInsightDataframeLogicType>([
    props({} as NotebookInsightDataframeLogicProps),
    key((props) => `${props.notebookLogic.key}-${props.nodeId}`),
    path((key) => ['products', 'notebooks', 'notebookInsightDataframeLogic', key]),
    connect((props: NotebookInsightDataframeLogicProps) => ({
        values: [
            props.notebookLogic,
            ['content'],
            notebookOperationsLogic({ shortId: props.notebookLogic.props.shortId }),
            ['isBusy'],
        ],
    })),
    actions({
        syncDataframe: (force: boolean = false) => ({ force }),
        prepareDataframe: (force: boolean) => ({ force }),
        retry: true,
        setReturnVariable: (returnVariable: string) => ({ returnVariable }),
        setSyncing: (syncing: boolean) => ({ syncing }),
        setError: (error: string | null, unsupported: boolean = false) => ({ error, unsupported }),
    }),
    reducers({
        isPreparing: [false, { setSyncing: (_, { syncing }) => syncing }],
        error: [null as string | null, { setError: (_, { error }) => error }],
        unsupported: [false, { setError: (_, { unsupported }) => unsupported }],
    }),
    selectors({
        returnVariable: [
            (s) => [
                s.content,
                (_, p: NotebookInsightDataframeLogicProps) => p.nodeId,
                (_, p: NotebookInsightDataframeLogicProps) => p.attributes.returnVariable,
            ],
            (content: JSONContent | null, nodeId: string, name: string | undefined): string =>
                collectNotebookDataframeNodes(content).find((node) => node.nodeId === nodeId)?.returnVariable ??
                name ??
                'insight_df',
        ],
    }),
    listeners(({ actions, asyncActions, values, props, cache }) => ({
        setReturnVariable: ({ returnVariable }) => props.updateAttributes({ nodeId: props.nodeId, returnVariable }),
        retry: () => actions.syncDataframe(true),
        syncDataframe: async ({ force }) => {
            if (!cache.preparation) {
                cache.preparation = asyncActions.prepareDataframe(force)
            }
            try {
                await cache.preparation
            } finally {
                cache.preparation = null
            }
        },
        prepareDataframe: async ({ force }, breakpoint) => {
            if (!props.enabled || props.notebookLogic.values.isShared || !props.notebookLogic.values.canEditNotebook) {
                return
            }
            if (!isReferenceableSqlV2FrameName(values.returnVariable)) {
                actions.setError('Enter a valid dataframe name before using this insight.')
                return
            }
            if (values.isBusy) {
                actions.setError('Wait for the current notebook operation to finish, then try again.')
                return
            }
            const prepare = async (): Promise<void> => {
                actions.setSyncing(true)
                actions.setError(null)
                const operations = notebookOperationsLogic({ shortId: props.notebookLogic.props.shortId })
                const operationId = `${props.nodeId}:prepare`
                operations.actions.startOperation({
                    id: operationId,
                    nodeId: props.nodeId,
                    kind: 'run',
                    label: `Preparing ${values.returnVariable}`,
                })
                try {
                    const mounted = insightDataLogic.findMounted(props.insightProps)
                    const nodeSource = (): string => {
                        const attributes = collectNotebookDataframeNodes(values.content).find(
                            (node) => node.nodeId === props.nodeId
                        )?.node.attrs
                        return JSON.stringify([attributes?.id, attributes?.query])
                    }
                    const originalNodeSource = nodeSource()
                    const originalMountedQuery = JSON.stringify(mounted?.values.query)
                    let query = (mounted?.values.query ?? props.insightProps.query ?? props.attributes.query) as
                        | QuerySchema
                        | undefined
                    const shortId =
                        props.attributes.id ?? (query?.kind === NodeKind.SavedInsightNode ? query.shortId : undefined)
                    if (shortId) {
                        const insight = await insightsApi.getByShortId(shortId as InsightShortId)
                        query = (insight?.query ?? undefined) as QuerySchema | undefined
                    }
                    if (!query) {
                        actions.setError('The insight query is unavailable. Try again after the insight loads.')
                        return
                    }
                    const source = JSON.stringify(query)
                    const attributes = props.attributes
                    if (
                        !force &&
                        attributes.runId &&
                        attributes.dataframeQuery &&
                        attributes.dataframeSource === source
                    ) {
                        if (cache.completedRunId === attributes.runId) {
                            return
                        }
                        try {
                            const saved = await api.notebooks.sqlV2RunResult(
                                props.notebookLogic.props.shortId,
                                attributes.runId
                            )
                            if (saved.status === 'done') {
                                cache.completedRunId = attributes.runId
                                return
                            }
                        } catch {
                            /* An expired result can be prepared again on demand. */
                        }
                    }
                    let code =
                        mounted?.values.query && JSON.stringify(mounted.values.query) === source
                            ? mounted.values.hogQL
                            : null
                    if (!code) {
                        const sourceQuery = ('source' in query ? query.source : query) as DataNode
                        const response = await performQuery(sourceQuery, undefined, 'async')
                        code =
                            response && 'hogql' in response && typeof response.hogql === 'string'
                                ? response.hogql
                                : null
                    }
                    if (!code) {
                        actions.setError("This insight can't be used as a dataframe.", true)
                        return
                    }
                    const run = await api.notebooks.sqlV2Run(props.notebookLogic.props.shortId, {
                        node_id: props.nodeId,
                        code,
                        refs: {},
                        reuse_results: !force,
                    })
                    for (let attempt = 0; attempt < 600; attempt++) {
                        breakpoint()
                        const response = await api.notebooks.sqlV2RunResult(
                            props.notebookLogic.props.shortId,
                            run.run_id
                        )
                        breakpoint()
                        if (response.status === 'done') {
                            const savedQuery = shortId
                                ? (await insightsApi.getByShortId(shortId as InsightShortId))?.query
                                : query
                            breakpoint()
                            if (
                                nodeSource() !== originalNodeSource ||
                                JSON.stringify(mounted?.values.query) !== originalMountedQuery ||
                                JSON.stringify(savedQuery) !== source
                            ) {
                                actions.setError('The insight changed while preparing its dataframe. Try again.')
                                return
                            }
                            const result = response.result
                            props.updateAttributes({
                                nodeId: props.nodeId,
                                returnVariable: values.returnVariable,
                                runId: run.run_id,
                                runStatus: 'done',
                                dataframeSource: source,
                                dataframeQuery: code,
                                result: result
                                    ? {
                                          columns: result.columns ?? [],
                                          types: result.types ?? [],
                                          row_count: result.row_count ?? 0,
                                          has_more: result.has_more ?? false,
                                      }
                                    : null,
                            })
                            cache.completedRunId = run.run_id
                            return
                        }
                        if (response.status === 'failed' || response.status === 'interrupted') {
                            actions.setError(response.error || 'The dataframe query did not finish. Try again.')
                            return
                        }
                        await breakpoint(1000)
                    }
                    actions.setError('The dataframe is taking too long to prepare. Try again.')
                } catch (error) {
                    if (error instanceof Error && isBreakpoint(error)) {
                        throw error
                    }
                    actions.setError(sqlV2RunErrorMessage(error, 'Could not prepare this dataframe'))
                } finally {
                    actions.setSyncing(false)
                    operations.actions.finishOperation(operationId)
                }
            }
            await prepare()
        },
    })),
])