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
// Form logic for the backfill creation modal — date range selection, schedule display, and submission.
import { MakeLogicType, actions, connect, kea, key, path, props, reducers, selectors } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'

import { lemonToast } from '@posthog/lemon-ui'

import api from 'lib/api'
import { Dayjs, dayjs } from 'lib/dayjs'
import { addProductIntent } from 'lib/utils/product-intents'
import { teamLogic } from 'scenes/teamLogic'

import { ProductIntentContext, ProductKey } from '~/queries/schema/schema-general'
import { BatchExportConfiguration } from '~/types'

import { batchExportDataLogic } from './batchExportDataLogic'
import { BatchExportContext } from './types'
import { dayOptions } from './utils'

export interface BatchExportBackfillModalLogicProps {
    id: string
    context?: BatchExportContext
}

/**
 * Formats a date value for display in the calendar picker.
 */
export function formatDateForDisplay(value: Dayjs | null | undefined): Dayjs | null | undefined {
    if (!value) {
        return value
    }
    // replace the timezone with UTC as the calendar date picker doesn't play well with timezones
    return value.tz('UTC', true)
}

/**
 * Transforms a date when the user selects it in the calendar picker.
 * Converts to the appropriate timezone and sets hour/minute/second for day/week intervals.
 */
export function transformDateOnChange(
    date: Dayjs | null,
    interval: string | undefined,
    timezone: string,
    hourOffset: number | null
): Dayjs | null {
    if (!date) {
        return date
    }
    // replace the timezone with expected timezone
    let dateWithTz = date.tz(timezone, true)
    if (interval === 'day' || interval === 'week') {
        dateWithTz = dateWithTz
            .hour(hourOffset ?? 0)
            .minute(0)
            .second(0)
    }
    return dateWithTz
}

/**
 * Determines the calendar granularity based on the batch export interval.
 * Returns 'hour' for hour intervals, 'minute' for minute intervals, and 'day' otherwise.
 */
export function getCalendarGranularity(interval: string | undefined): 'hour' | 'minute' | 'day' {
    if (!interval) {
        return 'day'
    }
    if (interval === 'hour') {
        return 'hour'
    }
    if (interval.endsWith('minutes')) {
        return 'minute'
    }
    return 'day'
}

/**
 * Calculates the most recent valid interval boundary based on interval, timezone, and offset.
 * This ensures the end_at date aligns with the batch export's schedule.
 */
export function getMostRecentIntervalBoundary(
    interval: string | undefined,
    timezone: string,
    hourOffset: number | null,
    dayOfWeek: number | null
): Dayjs {
    const now = dayjs().tz(timezone)

    if (!interval) {
        return now.hour(0).minute(0).second(0).millisecond(0)
    }

    if (interval === 'day') {
        const hour = hourOffset ?? 0
        let boundary = now.hour(hour).minute(0).second(0).millisecond(0)
        // If we're before today's boundary, use yesterday's boundary
        if (boundary.isAfter(now)) {
            boundary = boundary.subtract(1, 'day')
        }
        return boundary
    }

    if (interval === 'week') {
        const hour = hourOffset ?? 0
        const day = dayOfWeek ?? 0

        // Find the most recent occurrence of this day of week at the specified hour
        let boundary = now.day(day).hour(hour).minute(0).second(0).millisecond(0)

        // If we're before this week's occurrence, use last week's occurrence
        if (boundary.isAfter(now)) {
            boundary = boundary.subtract(1, 'week')
        }
        return boundary
    }

    // if hourly or sub-hourly, return the current hour
    return now.minute(0).second(0).millisecond(0)
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface batchExportBackfillModalLogicValues {
    batchExportConfig: BatchExportConfiguration | null // batchExportDataLogic
    backfillForm: {
        earliest_backfill: boolean
        end_at?: Dayjs | undefined
        start_at?: Dayjs | undefined
    }
    backfillFormAllErrors: Record<string, any>
    backfillFormChanged: boolean
    backfillFormErrors: DeepPartialMap<
        {
            earliest_backfill: boolean
            end_at?: Dayjs | undefined
            start_at?: Dayjs | undefined
        },
        ValidationErrorType
    >
    backfillFormHasErrors: boolean
    backfillFormManualErrors: Record<string, any>
    backfillFormTouched: boolean
    backfillFormTouches: Record<string, boolean>
    backfillFormValidationErrors: DeepPartialMap<
        {
            earliest_backfill: boolean
            end_at?: Dayjs | undefined
            start_at?: Dayjs | undefined
        },
        ValidationErrorType
    >
    dayOfWeek: number | null
    dayOfWeekName: string | null
    defaultEndAt: Dayjs
    hourOffset: number | null
    interval: string | undefined
    isBackfillFormSubmitting: boolean
    isBackfillFormValid: boolean
    isBackfillModalOpen: boolean
    isEarliestBackfill: boolean
    isHogFunction: boolean
    showBackfillFormErrors: boolean
    timezone: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface batchExportBackfillModalLogicActions {
    backfillCreated: (backfillId: string) => {
        backfillId: string
    }
    closeBackfillModal: () => {
        value: true
    }
    openBackfillModal: () => {
        value: true
    }
    resetBackfillForm: (values?: {
        earliest_backfill: boolean
        end_at?: Dayjs | undefined
        start_at?: Dayjs | undefined
    }) => {
        values?: {
            earliest_backfill: boolean
            end_at?: Dayjs | undefined
            start_at?: Dayjs | undefined
        }
    }
    setBackfillFormManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setBackfillFormValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setBackfillFormValues: (
        values: DeepPartial<{
            earliest_backfill: boolean
            end_at?: Dayjs | undefined
            start_at?: Dayjs | undefined
        }>
    ) => {
        values: DeepPartial<{
            earliest_backfill: boolean
            end_at?: Dayjs | undefined
            start_at?: Dayjs | undefined
        }>
    }
    setEarliestBackfill: () => {
        value: true
    }
    submitBackfillForm: () => {
        value: boolean
    }
    submitBackfillFormFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitBackfillFormRequest: (backfillForm: {
        earliest_backfill: boolean
        end_at?: Dayjs | undefined
        start_at?: Dayjs | undefined
    }) => {
        backfillForm: {
            earliest_backfill: boolean
            end_at?: Dayjs | undefined
            start_at?: Dayjs | undefined
        }
    }
    submitBackfillFormSuccess: (backfillForm: {
        earliest_backfill: boolean
        end_at?: Dayjs | undefined
        start_at?: Dayjs | undefined
    }) => {
        backfillForm: {
            earliest_backfill: boolean
            end_at?: Dayjs | undefined
            start_at?: Dayjs | undefined
        }
    }
    touchBackfillFormField: (key: string) => {
        key: string
    }
    unsetEarliestBackfill: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface batchExportBackfillModalLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        isHogFunction: (arg: any) => boolean
        interval: (batchExportConfig: BatchExportConfiguration | null) => string | undefined
        timezone: (batchExportConfig: BatchExportConfiguration | null, timezone: string) => string
        dayOfWeek: (batchExportConfig: BatchExportConfiguration | null) => number | null
        dayOfWeekName: (dayOfWeek: number | null) => string | null
        hourOffset: (batchExportConfig: BatchExportConfiguration | null) => number | null
        defaultEndAt: (
            interval: string | undefined,
            timezone: string,
            hourOffset: number | null,
            dayOfWeek: number | null
        ) => Dayjs
    }
}

export type batchExportBackfillModalLogicType = MakeLogicType<
    batchExportBackfillModalLogicValues,
    batchExportBackfillModalLogicActions,
    BatchExportBackfillModalLogicProps,
    batchExportBackfillModalLogicMeta
>

export const batchExportBackfillModalLogic = kea<batchExportBackfillModalLogicType>([
    props({} as BatchExportBackfillModalLogicProps),
    key(({ id }) => id),
    path((key) => ['scenes', 'pipeline', 'batchExportBackfillModalLogic', key]),
    connect((props: BatchExportBackfillModalLogicProps) => ({
        values: [batchExportDataLogic({ id: props.id }), ['batchExportConfig']],
    })),
    actions({
        openBackfillModal: true,
        closeBackfillModal: true,
        setEarliestBackfill: true,
        unsetEarliestBackfill: true,
        backfillCreated: (backfillId: string) => ({ backfillId }),
    }),
    reducers({
        isBackfillModalOpen: [
            false,
            {
                openBackfillModal: () => true,
                closeBackfillModal: () => false,
            },
        ],
        isEarliestBackfill: [
            false,
            {
                setEarliestBackfill: () => true,
                unsetEarliestBackfill: () => false,
            },
        ],
    }),
    selectors({
        isHogFunction: [
            () => [(_, props) => props],
            (props: BatchExportBackfillModalLogicProps): boolean => props.context === 'hog_function',
        ],
        interval: [
            (s) => [s.batchExportConfig],
            (batchExportConfig: BatchExportConfiguration | null): string | undefined => batchExportConfig?.interval,
        ],
        timezone: [
            (s) => [s.batchExportConfig, teamLogic.selectors.timezone],
            (batchExportConfig: BatchExportConfiguration | null, teamTimezone: string): string => {
                // How timezones are handled:
                // - If the batch export's interval is day or week, we use the timezone from the batch export config.
                // - Otherwise, the timezone doesn't make any difference (and is set to UTC by default on the batch export)
                //   so we use the team/project's timezone when displaying the timezone in the UI.
                // Note: all dates sent to the backend are converted to UTC using toISOString(), so the timezone is purely
                // for use in the UI.
                const interval = batchExportConfig?.interval
                const useTeamTimezone = interval !== 'day' && interval !== 'week'
                return useTeamTimezone ? teamTimezone : (batchExportConfig?.timezone ?? 'UTC')
            },
        ],
        dayOfWeek: [
            (s) => [s.batchExportConfig],
            (batchExportConfig: BatchExportConfiguration | null): number | null => {
                const dayOffset = batchExportConfig?.offset_day
                return dayOffset ?? null
            },
        ],
        dayOfWeekName: [
            (s) => [s.dayOfWeek],
            (dayOfWeek: number | null): string | null => {
                if (dayOfWeek === null) {
                    return null
                }
                return dayOptions[dayOfWeek].label
            },
        ],
        hourOffset: [
            (s) => [s.batchExportConfig],
            (batchExportConfig: BatchExportConfiguration | null): number | null => {
                const hourOffset = batchExportConfig?.offset_hour
                return hourOffset ?? null
            },
        ],
        defaultEndAt: [
            (s) => [s.interval, s.timezone, s.hourOffset, s.dayOfWeek],
            (
                interval: string | undefined,
                timezone: string,
                hourOffset: number | null,
                dayOfWeek: number | null
            ): Dayjs => {
                return getMostRecentIntervalBoundary(interval, timezone, hourOffset, dayOfWeek)
            },
        ],
    }),
    forms(({ props, actions, values }) => ({
        backfillForm: {
            defaults: {
                start_at: undefined,
                end_at: getMostRecentIntervalBoundary(
                    values.interval,
                    values.timezone,
                    values.hourOffset,
                    values.dayOfWeek
                ),
                earliest_backfill: false,
            } as {
                start_at?: Dayjs
                end_at?: Dayjs
                earliest_backfill: boolean
            },

            errors: ({ start_at, end_at, earliest_backfill }) => {
                const errors: {
                    start_at?: string
                    end_at?: string
                    earliest_backfill?: string
                } = {}

                // Required field validation
                if (!start_at && !earliest_backfill) {
                    errors.start_at = 'Start date is required'
                }

                if (!end_at) {
                    errors.end_at = 'End date is required'
                }

                // Only validate format/business rules if fields are present
                if (start_at && !errors.start_at) {
                    // Validate minute intervals (e.g., 5-minute exports require multiples of 5, 15-minute require multiples of 15)
                    if (values.batchExportConfig && values.batchExportConfig.interval.endsWith('minutes')) {
                        const minuteFrequency = parseInt(values.batchExportConfig.interval.split(' ')[1], 10)
                        if (start_at.minute() !== undefined && !(start_at.minute() % minuteFrequency === 0)) {
                            errors.start_at = `Start time must be a multiple of ${minuteFrequency} minutes`
                        }
                    }
                    // validate that weekly exports are on a valid day of the week
                    if (values.batchExportConfig && values.batchExportConfig.interval === 'week') {
                        if (start_at.day() !== values.dayOfWeek) {
                            errors.start_at = `Start date must be on a valid day of the week (${values.dayOfWeekName}) for weekly batch exports`
                        }
                    }
                }

                if (end_at && !errors.end_at) {
                    // Validate minute intervals for end date
                    if (values.batchExportConfig && values.batchExportConfig.interval.endsWith('minutes')) {
                        const minuteFrequency = parseInt(values.batchExportConfig.interval.split(' ')[1], 10)
                        if (end_at.minute() !== undefined && !(end_at.minute() % minuteFrequency === 0)) {
                            errors.end_at = `End time must be a multiple of ${minuteFrequency} minutes`
                        }
                    }

                    // Validate that weekly exports are on a valid day of the week
                    if (values.batchExportConfig && values.batchExportConfig.interval === 'week') {
                        if (end_at.day() !== values.dayOfWeek) {
                            errors.end_at = 'End date must be on a valid day of the week for weekly batch exports'
                        }
                    }

                    // Validate that end date is not in the future
                    const upperBound = dayjs()
                    if (end_at > upperBound) {
                        errors.end_at = `End date (${end_at.format('MMMM D, YYYY h:mm A z')}) cannot be in the future`
                    }

                    if (start_at && end_at < start_at) {
                        errors.end_at = 'End date must be after start date'
                    }
                }

                return errors
            },

            submit: async ({ start_at, end_at, earliest_backfill }) => {
                let startAtStr
                let endAtStr
                // If it's a daily or weekly batch export, we should only send date strings rather than datetime strings.
                // (using format('YYYY-MM-DD') rather than toISOString() to return the date in the local timezone)
                if (values.batchExportConfig?.interval === 'day' || values.batchExportConfig?.interval === 'week') {
                    startAtStr = earliest_backfill ? null : (start_at?.format('YYYY-MM-DD') ?? null)
                    endAtStr = end_at?.format('YYYY-MM-DD') ?? null
                } else {
                    startAtStr = earliest_backfill ? null : (start_at?.toISOString() ?? null)
                    endAtStr = end_at?.toISOString() ?? null
                }
                const result = await api.batchExports
                    .createBackfill(props.id, { start_at: startAtStr, end_at: endAtStr })
                    .catch((e) => {
                        if (e.detail) {
                            actions.setBackfillFormManualErrors({
                                [e.attr ?? 'end_at']: e.detail,
                            })
                        } else {
                            lemonToast.error('Unknown error occurred')
                        }
                        throw e
                    })

                void addProductIntent({
                    product_type: ProductKey.PIPELINE_BATCH_EXPORTS,
                    intent_context: ProductIntentContext.BATCH_EXPORT_BACKFILL_CREATED,
                })

                actions.closeBackfillModal()
                lemonToast.success('Backfill created')
                actions.backfillCreated(result.backfill_id)
                return
            },
        },
    })),
])