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.

9.0 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, key, listeners, path, props, reducers } from 'kea'

import { keyForInsightLogicProps } from 'lib/insights/sharedUtils'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { teamLogic } from 'scenes/teamLogic'

import { DataNodeLogicProps, dataNodeLogic } from '~/queries/nodes/DataNode/dataNodeLogic'
import { insightVizDataNodeKey } from '~/queries/nodes/InsightViz/insightVizKeys'
import { InsightLogicProps } from '~/types'

import type {
    DataNode,
    ErrorTrackingQueryResponse,
    HogQLAutocompleteResponse,
    HogQLMetadataResponse,
    HogQLQueryResponse,
    HogQueryResponse,
    LogAttributesQueryResponse,
    LogValuesQueryResponse,
    MetricsQueryResponse,
    RefreshType,
    SessionsQueryResponse,
    TraceSpansAggregationQueryResponse,
    TraceSpansAttributeBreakdownQueryResponse,
    TraceSpansQueryResponse,
} from '../../queries/schema/schema-general'
import type { TraceSpansTreeQueryResponse } from '../../queries/schema/schema-general'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface insightDataTimingLogicValues {
    query: DataNode<Record<string, any>> // dataNodeLogic
    response:
        | ErrorTrackingQueryResponse
        | HogQLAutocompleteResponse
        | HogQLMetadataResponse
        | HogQLQueryResponse<any[]>
        | HogQueryResponse
        | LogAttributesQueryResponse
        | LogValuesQueryResponse
        | MetricsQueryResponse
        | Record<string, any>
        | SessionsQueryResponse
        | TraceSpansAggregationQueryResponse
        | TraceSpansAttributeBreakdownQueryResponse
        | TraceSpansQueryResponse
        | TraceSpansTreeQueryResponse
        | null // dataNodeLogic
    currentTeamId: number | null // teamLogic
    queryStartTimes: Record<string, number>
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface insightDataTimingLogicActions {
    loadData: (
        refresh?: RefreshType | undefined,
        alreadyRunningQueryId?: string | undefined,
        overrideQuery?: DataNode<Record<string, any>> | undefined
    ) => {
        overrideQuery: DataNode<Record<string, any>> | undefined
        pollOnly: boolean
        queryId: string
        refresh: RefreshType | undefined
    } // dataNodeLogic
    loadDataCancellation: (payload: { queryId: string }) => {
        queryId: string
    } // dataNodeLogic
    loadDataFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    } // dataNodeLogic
    loadDataSuccess: (
        response:
            | ErrorTrackingQueryResponse
            | HogQLAutocompleteResponse
            | HogQLMetadataResponse
            | HogQLQueryResponse<any[]>
            | HogQueryResponse
            | LogAttributesQueryResponse
            | LogValuesQueryResponse
            | MetricsQueryResponse
            | Record<string, any>
            | SessionsQueryResponse
            | TraceSpansAggregationQueryResponse
            | TraceSpansAttributeBreakdownQueryResponse
            | TraceSpansQueryResponse
            | TraceSpansTreeQueryResponse
            | null,
        payload?:
            | {
                  overrideQuery: DataNode<Record<string, any>> | undefined
                  pollOnly: boolean
                  queryId: string
                  refresh: RefreshType | undefined
              }
            | undefined
    ) => {
        payload?: {
            overrideQuery: DataNode<Record<string, any>> | undefined
            pollOnly: boolean
            queryId: string
            refresh: RefreshType | undefined
        }
        response:
            | ErrorTrackingQueryResponse
            | HogQLAutocompleteResponse
            | HogQLMetadataResponse
            | HogQLQueryResponse<any[]>
            | HogQueryResponse
            | LogAttributesQueryResponse
            | LogValuesQueryResponse
            | MetricsQueryResponse
            | Record<string, any>
            | SessionsQueryResponse
            | TraceSpansAggregationQueryResponse
            | TraceSpansAttributeBreakdownQueryResponse
            | TraceSpansQueryResponse
            | TraceSpansTreeQueryResponse
            | null
    } // dataNodeLogic
    removeQuery: (queryId: string) => {
        queryId: string
    }
    startQuery: (queryId: string) => {
        queryId: string
    }
}

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

export type insightDataTimingLogicType = MakeLogicType<
    insightDataTimingLogicValues,
    insightDataTimingLogicActions,
    InsightLogicProps,
    insightDataTimingLogicMeta
>

export const insightDataTimingLogic = kea<insightDataTimingLogicType>([
    props({} as InsightLogicProps),
    key(keyForInsightLogicProps('new')),
    path((key) => ['scenes', 'insights', 'insightDataTimingLogic', key]),
    connect((props: InsightLogicProps) => ({
        values: [
            teamLogic,
            ['currentTeamId'],
            dataNodeLogic({ key: insightVizDataNodeKey(props) } as DataNodeLogicProps),
            ['query', 'response'],
        ],
        actions: [
            dataNodeLogic({ key: insightVizDataNodeKey(props) } as DataNodeLogicProps),
            ['loadData', 'loadDataSuccess', 'loadDataFailure', 'abortQuery as loadDataCancellation'],
        ],
        logic: [eventUsageLogic],
    })),
    actions({
        startQuery: (queryId: string) => ({ queryId }),
        removeQuery: (queryId: string) => ({ queryId }),
    }),
    reducers({
        queryStartTimes: [
            {} as Record<string, number>,
            {
                startQuery: (state, { queryId }) => ({ ...state, [queryId]: performance.now() }),
                removeQuery: (state, { queryId }) => {
                    const { [queryId]: remove, ...rest } = state
                    return rest
                },
            },
        ],
    }),
    listeners(({ actions, values }) => ({
        loadData: ({ queryId }) => {
            actions.startQuery(queryId)
        },
        loadDataSuccess: ({ payload }) => {
            // ignore initialization
            if (!payload || !values.queryStartTimes[payload.queryId]) {
                return
            }

            const duration = performance.now() - values.queryStartTimes[payload.queryId]

            eventUsageLogic.actions.reportTimeToSeeData({
                team_id: values.currentTeamId,
                type: 'insight_load',
                context: 'insight',
                primary_interaction_id: payload.queryId,
                query_id: payload.queryId,
                status: 'success',
                time_to_see_data_ms: Math.floor(duration),
                insights_fetched: 1,
                insights_fetched_cached:
                    values.response && 'is_cached' in values.response && values.response.is_cached ? 1 : 0,
                // api_response_bytes: values.response?.apiResponseBytes, getResponseB
                // api_url: values.response?.apiUrl,
                insight: values.query.kind,
                is_primary_interaction: true,
            })

            actions.removeQuery(payload.queryId)
        },
        loadDataFailure: ({ errorObject }) => {
            // ignore unexpected errors without query id
            if (!errorObject.queryId) {
                return
            }

            const duration = performance.now() - values.queryStartTimes[errorObject.queryId]
            eventUsageLogic.actions.reportTimeToSeeData({
                team_id: values.currentTeamId,
                type: 'insight_load',
                context: 'insight',
                primary_interaction_id: errorObject.queryId,
                query_id: errorObject.queryId,
                status: 'failure',
                time_to_see_data_ms: Math.floor(duration),
                insights_fetched: 1,
                insights_fetched_cached:
                    values.response && 'is_cached' in values.response && values.response.is_cached ? 1 : 0,
                // api_response_bytes: values.response?.apiResponseBytes, getResponseB
                // api_url: values.response?.apiUrl,
                insight: values.query.kind,
                is_primary_interaction: true,
            })

            actions.removeQuery(errorObject.queryId)
        },
        loadDataCancellation: (payload) => {
            const duration = performance.now() - values.queryStartTimes[payload.queryId]
            eventUsageLogic.actions.reportTimeToSeeData({
                team_id: values.currentTeamId,
                type: 'insight_load',
                context: 'insight',
                primary_interaction_id: payload.queryId,
                query_id: payload.queryId,
                status: 'cancelled',
                time_to_see_data_ms: Math.floor(duration),
                insights_fetched: 0,
                insights_fetched_cached: 0,
                api_response_bytes: 0,
                insight: values.query.kind,
            })

            actions.removeQuery(payload.queryId)
        },
    })),
])