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.

17.6 KB
Raw Blame
import {
    LogicWrapper,
    MakeLogicType,
    actions,
    afterMount,
    connect,
    kea,
    key,
    listeners,
    path,
    props,
    selectors,
    sharedListeners,
} from 'kea'
import type { BreakPointFunction } from 'kea'
import { subscriptions } from 'kea-subscriptions'

import { dayjs } from 'lib/dayjs'
import { queryUsesPlaceholder } from 'scenes/data-warehouse/editor/sql-utils'
import { sqlEditorLogic } from 'scenes/data-warehouse/editor/sqlEditorLogic'
import { SQLEditorMode } from 'scenes/data-warehouse/editor/sqlEditorModes'
import { SQLEditorPlaceholder } from 'scenes/data-warehouse/editor/sqlEditorPlaceholders'
import { teamLogic } from 'scenes/teamLogic'

import { HogQLQueryModifiers, NodeKind } from '~/queries/schema/schema-general'
import { BatchExportInterval, ChartDisplayType, TeamPublicType, TeamType } from '~/types'

import { BatchExportConfigFormLogicProps, batchExportConfigFormLogic } from './batchExportConfigFormLogic'
import { BatchExportDataInterval, lastCompleteDataInterval } from './utils'

export const DATA_INTERVAL_START_PLACEHOLDER = 'data_interval_start'
export const DATA_INTERVAL_END_PLACEHOLDER = 'data_interval_end'

const DEFAULT_HOGQL_QUERY = `SELECT
    uuid,
    event,
    timestamp,
    distinct_id,
    properties,
    properties.$current_url AS current_url
FROM events
WHERE timestamp >= {${DATA_INTERVAL_START_PLACEHOLDER}}
    AND timestamp < {${DATA_INTERVAL_END_PLACEHOLDER}}`

// ClickHouse cannot parse a datetime string with a UTC offset.
const PLACEHOLDER_VALUE_FORMAT = 'YYYY-MM-DD HH:mm:ss'

export type QueryTimezoneChoice = 'project_timezone' | 'utc'

export function batchExportHogQLEditorTabId({ id, service }: BatchExportConfigFormLogicProps): string {
    return `batch-export-hogql-${id ?? `new-${service}`}`
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface batchExportHogQLQueryLogicValues {
    configuration: Record<string, any> // batchExportConfigFormLogic
    queryInput: string | null // sqlEditorLogic
    currentTeam: TeamPublicType | TeamType | null // teamLogic
    projectTimezone: string // teamLogic
    convertsToProjectTimezone: boolean
    editorModifiers: HogQLQueryModifiers
    followsProjectModifier: boolean
    hogqlModifiers: HogQLQueryModifiers | null
    placeholders: SQLEditorPlaceholder[]
    previewEnd: string
    previewInterval: BatchExportDataInterval
    previewStart: string
    queryTimezone: string
    queryTimezoneChoice: QueryTimezoneChoice
    scheduleInterval: BatchExportInterval
    scheduleOffsetDay: number | null
    scheduleOffsetHour: number | null
    scheduleTimezone: string | null
    teamConvertToProjectTimezone: boolean | null
    usesIntervalPlaceholders: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface batchExportHogQLQueryLogicActions {
    loadBatchExportConfigSuccess: (
        batchExportConfig: null | import('~/types').BatchExportConfiguration,
        payload?: any
    ) => {
        batchExportConfig: null | import('~/types').BatchExportConfiguration
        payload?: any
    } // batchExportConfigFormLogic
    resetConfiguration: (values?: Record<string, any> | undefined) => {
        values?: Record<string, any>
    } // batchExportConfigFormLogic
    setConfigurationValue: (
        key: import('node_modules/kea-forms/lib').FieldName,
        value: any
    ) => {
        name: import('node_modules/kea-forms/lib').FieldName
        value: any
    } // batchExportConfigFormLogic
    setConfigurationValues: (values: import('node_modules/kea-forms/lib').DeepPartial<Record<string, any>>) => {
        values: import('node_modules/kea-forms/lib').DeepPartial<Record<string, any>>
    } // batchExportConfigFormLogic
    updateBatchExportConfigSuccess: (batchExportConfig: import('~/types').BatchExportConfiguration) => {
        batchExportConfig: import('~/types').BatchExportConfiguration
    } // batchExportConfigFormLogic
    setPlaceholders: (placeholders: SQLEditorPlaceholder[]) => {
        placeholders: SQLEditorPlaceholder[]
    } // sqlEditorLogic
    setQueryInput: (queryInput: string | null) => {
        queryInput: string | null
    } // sqlEditorLogic
    setQueryModifiers: (queryModifiers: HogQLQueryModifiers | null) => {
        queryModifiers: HogQLQueryModifiers | null
    } // sqlEditorLogic
    setSourceQuery: (sourceQuery: import('~/queries/schema/schema-general').DataVisualizationNode) => {
        sourceQuery: import('~/queries/schema/schema-general').DataVisualizationNode
    } // sqlEditorLogic
    setQueryTimezone: (choice: QueryTimezoneChoice) => {
        choice: QueryTimezoneChoice
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface batchExportHogQLQueryLogicMeta {
    key: string
    sharedListeners: {
        syncFormToEditor: (
            payload: any,
            breakpoint: BreakPointFunction,
            action: {
                type: string
                payload: any
            },
            previousState: any
        ) => void | Promise<void>
    }
    __keaTypeGenInternalSelectorTypes: {
        hogqlModifiers: (configuration: Record<string, any>) => HogQLQueryModifiers | null
        teamConvertToProjectTimezone: (currentTeam: TeamPublicType | TeamType | null) => boolean | null
        convertsToProjectTimezone: (
            hogqlModifiers: HogQLQueryModifiers | null,
            teamConvertToProjectTimezone: boolean | null
        ) => boolean
        followsProjectModifier: (
            hogqlModifiers: HogQLQueryModifiers | null,
            teamConvertToProjectTimezone: boolean | null
        ) => boolean
        queryTimezoneChoice: (convertsToProjectTimezone: boolean) => QueryTimezoneChoice
        queryTimezone: (convertsToProjectTimezone: boolean, projectTimezone: string) => string
        editorModifiers: (
            hogqlModifiers: HogQLQueryModifiers | null,
            convertsToProjectTimezone: boolean
        ) => HogQLQueryModifiers
        scheduleInterval: (configuration: Record<string, any>) => BatchExportInterval
        scheduleTimezone: (configuration: Record<string, any>) => string | null
        scheduleOffsetDay: (configuration: Record<string, any>) => number | null
        scheduleOffsetHour: (configuration: Record<string, any>) => number | null
        previewInterval: (
            scheduleInterval: BatchExportInterval,
            scheduleTimezone: string | null,
            scheduleOffsetDay: number | null,
            scheduleOffsetHour: number | null,
            projectTimezone: string
        ) => BatchExportDataInterval
        previewStart: (previewInterval: BatchExportDataInterval, queryTimezone: string) => string
        previewEnd: (previewInterval: BatchExportDataInterval, queryTimezone: string) => string
        placeholders: (previewStart: string, previewEnd: string) => SQLEditorPlaceholder[]
        usesIntervalPlaceholders: (queryInput: string | null) => boolean
    }
}

export type batchExportHogQLQueryLogicType = MakeLogicType<
    batchExportHogQLQueryLogicValues,
    batchExportHogQLQueryLogicActions,
    BatchExportConfigFormLogicProps,
    batchExportHogQLQueryLogicMeta
>

// Keeps the embedded SQL editor and the form's `hogql_query` in step, and gives the editor the
// interval placeholders with preview values for the export's schedule.
export const batchExportHogQLQueryLogic: LogicWrapper<batchExportHogQLQueryLogicType> =
    kea<batchExportHogQLQueryLogicType>([
        props({ id: null, service: null } as BatchExportConfigFormLogicProps),
        key(({ service, id }: BatchExportConfigFormLogicProps) => (id ? `ID:${id}` : `NEW:${service}`)),
        path((key) => ['scenes', 'data-pipelines', 'batch-exports', 'batchExportHogQLQueryLogic', key]),
        connect((props: BatchExportConfigFormLogicProps) => ({
            values: [
                batchExportConfigFormLogic(props),
                ['configuration'],
                teamLogic,
                ['timezone as projectTimezone', 'currentTeam'],
                sqlEditorLogic({ tabId: batchExportHogQLEditorTabId(props), mode: SQLEditorMode.Embedded }),
                ['queryInput'],
            ],
            actions: [
                batchExportConfigFormLogic(props),
                [
                    'setConfigurationValue',
                    'setConfigurationValues',
                    'resetConfiguration',
                    'loadBatchExportConfigSuccess',
                    'updateBatchExportConfigSuccess',
                ],
                sqlEditorLogic({ tabId: batchExportHogQLEditorTabId(props), mode: SQLEditorMode.Embedded }),
                ['setQueryInput', 'setSourceQuery', 'setPlaceholders', 'setQueryModifiers'],
            ],
        })),
        actions({
            setQueryTimezone: (choice: QueryTimezoneChoice) => ({ choice }),
        }),
        selectors({
            hogqlModifiers: [
                (s) => [s.configuration],
                (configuration: Record<string, any>): HogQLQueryModifiers | null =>
                    configuration.hogql_modifiers ?? null,
            ],
            // Only the API sets the team's modifier, so a value here means someone chose it on purpose
            teamConvertToProjectTimezone: [
                (s) => [s.currentTeam],
                (currentTeam: TeamType | TeamPublicType | null): boolean | null =>
                    currentTeam?.modifiers?.convertToProjectTimezone ?? null,
            ],
            // An export without its own value follows the project modifier, and HogQL batch exports fall back to
            // UTC, unlike other queries, which fall back to the project timezone
            convertsToProjectTimezone: [
                (s) => [s.hogqlModifiers, s.teamConvertToProjectTimezone],
                (hogqlModifiers: HogQLQueryModifiers | null, teamConvertToProjectTimezone: boolean | null): boolean =>
                    hogqlModifiers?.convertToProjectTimezone ?? teamConvertToProjectTimezone ?? false,
            ],
            followsProjectModifier: [
                (s) => [s.hogqlModifiers, s.teamConvertToProjectTimezone],
                (hogqlModifiers: HogQLQueryModifiers | null, teamConvertToProjectTimezone: boolean | null): boolean =>
                    (hogqlModifiers?.convertToProjectTimezone ?? null) === null &&
                    teamConvertToProjectTimezone !== null,
            ],
            queryTimezoneChoice: [
                (s) => [s.convertsToProjectTimezone],
                (convertsToProjectTimezone: boolean): QueryTimezoneChoice =>
                    convertsToProjectTimezone ? 'project_timezone' : 'utc',
            ],
            queryTimezone: [
                (s) => [s.convertsToProjectTimezone, s.projectTimezone],
                (convertsToProjectTimezone: boolean, projectTimezone: string): string =>
                    convertsToProjectTimezone ? projectTimezone : 'UTC',
            ],
            // The preview runs through the query API, which falls back to the project timezone, so it gets the
            // resolved value
            editorModifiers: [
                (s) => [s.hogqlModifiers, s.convertsToProjectTimezone],
                (
                    hogqlModifiers: HogQLQueryModifiers | null,
                    convertsToProjectTimezone: boolean
                ): HogQLQueryModifiers => ({
                    ...hogqlModifiers,
                    convertToProjectTimezone: convertsToProjectTimezone,
                }),
            ],
            scheduleInterval: [
                (s) => [s.configuration],
                (configuration: Record<string, any>): BatchExportInterval => configuration.interval ?? 'hour',
            ],
            scheduleTimezone: [
                (s) => [s.configuration],
                (configuration: Record<string, any>): string | null => configuration.timezone ?? null,
            ],
            scheduleOffsetDay: [
                (s) => [s.configuration],
                (configuration: Record<string, any>): number | null => configuration.offset_day ?? null,
            ],
            scheduleOffsetHour: [
                (s) => [s.configuration],
                (configuration: Record<string, any>): number | null => configuration.offset_hour ?? null,
            ],
            previewInterval: [
                (s) => [
                    s.scheduleInterval,
                    s.scheduleTimezone,
                    s.scheduleOffsetDay,
                    s.scheduleOffsetHour,
                    s.projectTimezone,
                ],
                (
                    interval: BatchExportInterval,
                    scheduleTimezone: string | null,
                    offsetDay: number | null,
                    offsetHour: number | null,
                    projectTimezone: string
                ): BatchExportDataInterval => {
                    const scheduleUsesTimezone = interval === 'day' || interval === 'week'
                    return lastCompleteDataInterval({
                        interval,
                        now: dayjs(),
                        timezone: scheduleUsesTimezone ? scheduleTimezone || projectTimezone : 'UTC',
                        offsetDay,
                        offsetHour,
                    })
                },
            ],
            // HogQL reads these strings as times in the query's timezone, so convert the bounds to it first.
            previewStart: [
                (s) => [s.previewInterval, s.queryTimezone],
                (previewInterval: BatchExportDataInterval, queryTimezone: string): string =>
                    previewInterval.start.tz(queryTimezone).format(PLACEHOLDER_VALUE_FORMAT),
            ],
            previewEnd: [
                (s) => [s.previewInterval, s.queryTimezone],
                (previewInterval: BatchExportDataInterval, queryTimezone: string): string =>
                    previewInterval.end.tz(queryTimezone).format(PLACEHOLDER_VALUE_FORMAT),
            ],
            placeholders: [
                (s) => [s.previewStart, s.previewEnd],
                (previewStart: string, previewEnd: string): SQLEditorPlaceholder[] => [
                    {
                        name: DATA_INTERVAL_START_PLACEHOLDER,
                        description: 'Start of the time range a run exports. Rows at or after this time are included.',
                        previewValue: previewStart,
                    },
                    {
                        name: DATA_INTERVAL_END_PLACEHOLDER,
                        description: 'End of the time range a run exports. Rows before this time are included.',
                        previewValue: previewEnd,
                    },
                ],
            ],
            usesIntervalPlaceholders: [
                (s) => [s.queryInput],
                (queryInput: string | null): boolean =>
                    queryInput === null ||
                    queryUsesPlaceholder(queryInput, DATA_INTERVAL_START_PLACEHOLDER) ||
                    queryUsesPlaceholder(queryInput, DATA_INTERVAL_END_PLACEHOLDER),
            ],
        }),
        sharedListeners(({ actions, values }) => ({
            syncFormToEditor: () => {
                const formQuery: string = values.configuration.hogql_query ?? ''
                // An empty form query means the config has not loaded yet, so keep what the editor has
                if (values.queryInput !== null && formQuery && formQuery !== values.queryInput) {
                    actions.setQueryInput(formQuery)
                }
            },
        })),
        listeners(({ actions, values, sharedListeners }) => ({
            setQueryTimezone: ({ choice }) => {
                // Only this key changes, so modifiers set through the API survive a save from the form
                actions.setConfigurationValue('hogql_modifiers', {
                    ...values.hogqlModifiers,
                    convertToProjectTimezone: choice === 'project_timezone',
                })
            },
            setQueryInput: ({ queryInput }) => {
                if (queryInput !== null && queryInput !== values.configuration.hogql_query) {
                    actions.setConfigurationValue('hogql_query', queryInput)
                }
            },
            // Each listener writes only when the two sides differ, so a write settles instead of bouncing back
            resetConfiguration: sharedListeners.syncFormToEditor,
            setConfigurationValues: sharedListeners.syncFormToEditor,
            loadBatchExportConfigSuccess: sharedListeners.syncFormToEditor,
            updateBatchExportConfigSuccess: sharedListeners.syncFormToEditor,
        })),
        subscriptions(({ actions }) => ({
            placeholders: (placeholders: SQLEditorPlaceholder[]) => actions.setPlaceholders(placeholders),
            editorModifiers: (editorModifiers: HogQLQueryModifiers) => actions.setQueryModifiers(editorModifiers),
        })),
        afterMount(({ actions, values, props }) => {
            // A new export saves its timezone, so the select always shows what the export does
            if (!props.id && (values.hogqlModifiers?.convertToProjectTimezone ?? null) === null) {
                actions.setConfigurationValue('hogql_modifiers', {
                    ...values.hogqlModifiers,
                    convertToProjectTimezone: false,
                })
            }
            if (values.queryInput !== null) {
                return
            }
            const initialQuery = values.configuration.hogql_query || DEFAULT_HOGQL_QUERY
            actions.setQueryInput(initialQuery)
            actions.setSourceQuery({
                kind: NodeKind.DataVisualizationNode,
                source: { kind: NodeKind.HogQLQuery, query: initialQuery },
                display: ChartDisplayType.ActionsTable,
            })
        }),
    ])