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.

39.8 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { subscriptions } from 'kea-subscriptions'
import posthog from 'posthog-js'

import api from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { objectsEqual } from 'lib/utils/objects'

import { experimentsConfigLogic } from '~/scenes/settings/environment/experimentsConfigLogic'
import { ConversionRateInputType, Experiment } from '~/types'

import { isExperimentExposureFrozen, isLaunched } from 'products/experiments/frontend/experimentStatus'
import {
    experimentsCalculateRunningTimeCreate,
    experimentsRunningTimeTargetRetrieve,
} from 'products/experiments/frontend/generated/api'
import type {
    RunningTimeCalculationInputApi,
    RunningTimeTargetApi,
} from 'products/experiments/frontend/generated/api.schemas'

import type { CachedNewExperimentQueryResponse } from '../../../queries/schema/schema-general'
import { experimentLogic, saveExperimentUpdate } from '../experimentLogic'
import { experimentMetricsLogic } from '../experimentMetricsLogic'
import { getDefaultMetricTitle } from '../MetricsView/shared/utils'
import { modalsLogic } from '../modalsLogic'
import { getFlagVariants, metricResults, isExperimentConflictError, toConcurrencyPayload } from '../utils'
import type { MetricWithResult } from '../utils'
import {
    ManualCalculatorMetricType,
    calculateCurrentExposures,
    buildRunningTimeOutlook,
    calculateDaysElapsed,
    daysFromToday,
    rescaleSampleSize,
    RunningTimeOutlook,
} from './calculations'

export interface RunningTimeLogicProps {
    experiment: Experiment
}

export interface RunningTimeConfig {
    mode: 'automatic' | 'manual'
    mde: number
    metricType: ManualCalculatorMetricType
    baselineValue: number
    exposureRate: number
}

export interface ManualPreview {
    sampleSize: number | null
    runningTime: number | null
}

// The metric the automatic estimate is sized from.
export interface EstimateMetric {
    name: string
    // The experiment has more than one primary metric, and the estimate reads the first.
    isFirstOfSeveral: boolean
    hasOtherMetrics: boolean
}

// The backend sizes the target for the saved minimum detectable effect. The settings rescale it for an edited one, so an
// edit sends no request.
export interface AutomaticTargetRequest {
    // Never sent. A new result of the primary metric changes it, so that the backend sizes the target again. The sums
    // count too, because more metric events from the same exposed users change the variance.
    resultStats: number[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface runningTimeLogicValues {
    currentProjectId: number | null // experimentLogic
    experiment: Experiment // experimentLogic
    unmodifiedExperiment: Experiment | null // experimentLogic
    isRecalculating: boolean // experimentMetricsLogic
    primaryMetricsResults: CachedNewExperimentQueryResponse[] // experimentMetricsLogic
    primaryMetricsResultsErrors: (unknown | null)[] // experimentMetricsLogic
    defaultMinimumDetectableEffect: number // experimentsConfigLogic
    isRunningTimeConfigModalOpen: boolean // modalsLogic
    automaticTarget: RunningTimeTargetApi | null
    automaticTargetFailed: boolean
    automaticTargetLoading: boolean
    automaticTargetRequest: AutomaticTargetRequest | null
    canIncludeMoreUsers: boolean
    config: RunningTimeConfig
    configOverrides: Partial<RunningTimeConfig> | null
    currentExposures: number | null
    dailyExposureRate: number | null
    editedMde: number | null
    enteredMde: number | null
    estimateMetric: EstimateMetric | null
    initialConfig: RunningTimeConfig
    isCalculating: boolean
    isComplete: boolean
    isManualEstimate: boolean
    isManualMode: boolean
    isSaving: boolean
    manualFormPreview: ManualPreview
    manualPreview: ManualPreview
    manualPreviewInput: RunningTimeCalculationInputApi | null
    manualPreviewLoading: boolean
    mde: number
    metricsWithResults: MetricWithResult[]
    mode: RunningTimeConfig['mode']
    numberOfVariants: number
    outlook: RunningTimeOutlook | null
    remainingDays: number | null
    selectedCheckpointKey: string | null
    targetEffect: number
    targetSampleSize: number | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface runningTimeLogicActions {
    setExperiment: (experiment: Partial<Experiment>) => {
        experiment: Partial<Experiment>
    } // experimentLogic
    setUnmodifiedExperiment: (experiment: Experiment) => {
        experiment: Experiment
    } // experimentLogic
    closeRunningTimeConfigModal: () => {
        value: true
    } // modalsLogic
    openRunningTimeConfigModal: () => {
        value: true
    } // modalsLogic
    cancel: () => {
        value: true
    }
    loadAutomaticTarget: (_: AutomaticTargetRequest) => AutomaticTargetRequest
    loadAutomaticTargetFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadAutomaticTargetSuccess: (
        automaticTarget: RunningTimeTargetApi | null,
        payload?: AutomaticTargetRequest
    ) => {
        automaticTarget: RunningTimeTargetApi | null
        payload?: AutomaticTargetRequest
    }
    loadManualPreview: (input: RunningTimeCalculationInputApi) => RunningTimeCalculationInputApi
    loadManualPreviewFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadManualPreviewSuccess: (
        manualPreview: {
            runningTime: number | null
            sampleSize: number | null
        },
        payload?: RunningTimeCalculationInputApi
    ) => {
        manualPreview: {
            runningTime: number | null
            sampleSize: number | null
        }
        payload?: RunningTimeCalculationInputApi
    }
    persistRunningTimeEstimate: () => {
        value: true
    }
    resetConfig: () => {
        value: true
    }
    save: () => {
        value: true
    }
    selectCheckpoint: (
        key: string,
        effect: number
    ) => {
        effect: number
        key: string
    }
    setConfig: (config: Partial<RunningTimeConfig>) => {
        config: Partial<RunningTimeConfig>
    }
    setSaving: (saving: boolean) => {
        saving: boolean
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface runningTimeLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        numberOfVariants: (experiment: Experiment) => number
        isManualMode: (experiment: Experiment) => boolean
        initialConfig: (
            experiment: Experiment,
            isManualMode: boolean,
            defaultMinimumDetectableEffect: number
        ) => RunningTimeConfig
        config: (
            initialConfig: RunningTimeConfig,
            configOverrides: Partial<RunningTimeConfig> | null
        ) => RunningTimeConfig
        mde: (config: RunningTimeConfig) => number
        editedMde: (configOverrides: Partial<RunningTimeConfig> | null) => number | null
        targetEffect: (editedMde: number | null, automaticTarget: RunningTimeTargetApi | null, mde: number) => number
        mode: (config: RunningTimeConfig) => RunningTimeConfig['mode']
        metricsWithResults: (
            experiment: Experiment,
            primaryMetricsResults: CachedNewExperimentQueryResponse[],
            primaryMetricsResultsErrors: unknown[]
        ) => MetricWithResult[]
        isManualEstimate: (isManualMode: boolean, mode: 'automatic' | 'manual') => boolean
        currentExposures: (
            isManualEstimate: boolean,
            metricsWithResults: MetricWithResult[],
            automaticTarget: RunningTimeTargetApi | null
        ) => number | null
        automaticTargetRequest: (
            mode: 'automatic' | 'manual',
            metricsWithResults: MetricWithResult[]
        ) => AutomaticTargetRequest | null
        manualPreviewInput: (
            config: RunningTimeConfig,
            numberOfVariants: number
        ) => RunningTimeCalculationInputApi | null
        targetSampleSize: (
            isManualEstimate: boolean,
            targetEffect: number,
            experiment: Experiment,
            automaticTargetRequest: AutomaticTargetRequest | null,
            automaticTarget: RunningTimeTargetApi | null
        ) => number | null
        dailyExposureRate: (
            isManualEstimate: boolean,
            experiment: Experiment,
            automaticTarget: RunningTimeTargetApi | null
        ) => number | null
        remainingDays: (
            targetSampleSize: number | null,
            currentExposures: number | null,
            dailyExposureRate: number | null,
            experiment: Experiment,
            isManualEstimate: boolean,
            outlook: RunningTimeOutlook | null
        ) => number | null
        outlook: (
            isManualEstimate: boolean,
            automaticTarget: RunningTimeTargetApi | null,
            targetEffect: number,
            defaultMinimumDetectableEffect: number,
            enteredMde: number | null,
            selectedCheckpointKey: string | null
        ) => RunningTimeOutlook | null
        canIncludeMoreUsers: (experiment: Experiment) => boolean
        estimateMetric: (experiment: Experiment, metricsWithResults: MetricWithResult[]) => EstimateMetric | null
        isComplete: (currentExposures: number | null, targetSampleSize: number | null) => boolean
        isCalculating: (isManualEstimate: boolean, isRecalculating: boolean, automaticTargetLoading: boolean) => boolean
        manualFormPreview: (
            manualPreviewInput: RunningTimeCalculationInputApi | null,
            manualPreview: ManualPreview
        ) => ManualPreview
    }
}

export type runningTimeLogicType = MakeLogicType<
    runningTimeLogicValues,
    runningTimeLogicActions,
    RunningTimeLogicProps,
    runningTimeLogicMeta
>

export const runningTimeLogic = kea<runningTimeLogicType>([
    path(['scenes', 'experiments', 'RunningTimeCalculator', 'runningTimeLogic']),
    props({} as RunningTimeLogicProps),
    key((props) => `${props.experiment.id}`),

    connect((props: RunningTimeLogicProps) => {
        const experimentId = props.experiment.id
        return {
            values: [
                experimentLogic({ experimentId }),
                ['experiment', 'unmodifiedExperiment', 'currentProjectId'],
                experimentMetricsLogic({ experiment: props.experiment }),
                ['primaryMetricsResults', 'primaryMetricsResultsErrors', 'isRecalculating'],
                modalsLogic,
                ['isRunningTimeConfigModalOpen'],
                experimentsConfigLogic,
                ['defaultMinimumDetectableEffect'],
            ],
            actions: [
                experimentLogic({ experimentId }),
                ['setExperiment', 'setUnmodifiedExperiment'],
                modalsLogic,
                ['openRunningTimeConfigModal', 'closeRunningTimeConfigModal'],
            ],
        }
    }),

    actions({
        setConfig: (config: Partial<RunningTimeConfig>) => ({ config }),
        selectCheckpoint: (key: string, effect: number) => ({ key, effect }),
        resetConfig: true,
        save: true,
        cancel: true,
        persistRunningTimeEstimate: true,
        setSaving: (saving: boolean) => ({ saving }),
    }),

    reducers({
        configOverrides: [
            null as Partial<RunningTimeConfig> | null,
            {
                setConfig: (state, { config }) => ({ ...state, ...config }),
                selectCheckpoint: (state, { effect }) => ({ ...state, mde: effect }),
                resetConfig: () => null,
            },
        ],
        enteredMde: [
            null as number | null,
            {
                setConfig: (state, { config }) => (config.mde === undefined ? state : config.mde),
                resetConfig: () => null,
            },
        ],
        // The row chosen with "Use". The slider and the input make the custom row the target again.
        selectedCheckpointKey: [
            null as string | null,
            {
                selectCheckpoint: (_, { key }) => key,
                setConfig: (state, { config }) => (config.mde === undefined ? state : null),
                resetConfig: () => null,
            },
        ],
        isSaving: [
            false,
            {
                setSaving: (_, { saving }) => saving,
            },
        ],
        automaticTargetFailed: [
            false,
            {
                loadAutomaticTarget: () => false,
                loadAutomaticTargetSuccess: () => false,
                loadAutomaticTargetFailure: () => true,
            },
        ],
    }),

    loaders(({ values, props }) => ({
        // Automatic mode: the target the backend sizes from the primary metric's stored result and the flag's split.
        automaticTarget: [
            null as RunningTimeTargetApi | null,
            {
                loadAutomaticTarget: async (_: AutomaticTargetRequest, breakpoint) => {
                    const experimentId = props.experiment.id
                    if (typeof experimentId !== 'number') {
                        return null
                    }
                    const result = await experimentsRunningTimeTargetRetrieve(
                        String(values.currentProjectId),
                        experimentId,
                        // Sized from the results also while a manual experiment is being switched to automatic.
                        { mode: 'automatic' }
                    )
                    breakpoint()
                    return result
                },
            },
        ],
        // Manual mode: live preview of sample size / running time for the values being edited in the form.
        manualPreview: [
            { sampleSize: null, runningTime: null } as ManualPreview,
            {
                loadManualPreview: async (input: RunningTimeCalculationInputApi, breakpoint) => {
                    // Debounce burst typing in the form so we don't fire a request per keypress.
                    await breakpoint(300)
                    const result = await experimentsCalculateRunningTimeCreate(String(values.currentProjectId), input)
                    breakpoint()
                    return {
                        sampleSize: result.recommended_sample_size,
                        runningTime: result.recommended_running_time_days,
                    }
                },
            },
        ],
    })),

    selectors({
        numberOfVariants: [
            (s) => [s.experiment],
            (experiment: Experiment): number => getFlagVariants(experiment?.feature_flag).length || 2,
        ],

        isManualMode: [
            (s) => [s.experiment],
            (experiment: Experiment): boolean =>
                experiment?.running_time_calculation?.exposure_estimate_config?.conversionRateInputType ===
                ConversionRateInputType.MANUAL,
        ],

        initialConfig: [
            (s) => [s.experiment, s.isManualMode, s.defaultMinimumDetectableEffect],
            (
                experiment: Experiment,
                isManualMode: boolean,
                defaultMinimumDetectableEffect: number
            ): RunningTimeConfig => {
                // Pre-launch experiments must use manual mode (no data available for automatic)
                const isPreLaunch = !isLaunched(experiment)
                const exposureEstimateConfig = experiment?.running_time_calculation?.exposure_estimate_config
                return {
                    mode: isPreLaunch || isManualMode ? 'manual' : 'automatic',
                    mde:
                        experiment?.running_time_calculation?.minimum_detectable_effect ??
                        defaultMinimumDetectableEffect,
                    metricType: (exposureEstimateConfig?.manualMetricType as ManualCalculatorMetricType) ?? 'funnel',
                    baselineValue: exposureEstimateConfig?.manualBaselineValue ?? 5,
                    exposureRate: exposureEstimateConfig?.manualExposureRate ?? 100,
                }
            },
        ],

        config: [
            (s) => [s.initialConfig, s.configOverrides],
            (initialConfig: RunningTimeConfig, configOverrides: Partial<RunningTimeConfig> | null): RunningTimeConfig =>
                configOverrides ? { ...initialConfig, ...configOverrides } : initialConfig,
        ],

        mde: [(s) => [s.config], (config: RunningTimeConfig): number => config.mde],
        editedMde: [
            (s) => [s.configOverrides],
            (configOverrides: Partial<RunningTimeConfig> | null): number | null => configOverrides?.mde ?? null,
        ],
        // The effect the automatic target is sized for: an edited one, else the one the backend sized for. The backend's
        // can differ from `mde` before the team default loads, or when the saved effect isn't a positive number.
        targetEffect: [
            (s) => [s.editedMde, s.automaticTarget, s.mde],
            (editedMde: number | null, automaticTarget: RunningTimeTargetApi | null, mde: number): number =>
                editedMde ?? automaticTarget?.minimum_detectable_effect ?? mde,
        ],
        // A primitive, so that automaticTargetRequest stays stable when the experiment object is replaced (e.g. after
        // persisting an estimate) but the mode is unchanged.
        mode: [(s) => [s.config], (config: RunningTimeConfig): RunningTimeConfig['mode'] => config.mode],

        // Primary metrics zipped with their results, so automatic mode always has a baseline to read.
        metricsWithResults: [
            (s) => [s.experiment, s.primaryMetricsResults, s.primaryMetricsResultsErrors],
            (
                experiment: Experiment,
                results: import('../../../queries/schema').CachedNewExperimentQueryResponse[],
                errors: (unknown | null)[]
            ) => (experiment ? metricResults(experiment)(results, errors, 'primary') : []),
        ],

        // Switching a manual experiment to automatic in the settings previews the automatic estimate, so the manual one
        // applies only while both the saved and the edited mode are manual.
        isManualEstimate: [
            (s) => [s.isManualMode, s.mode],
            (isManualMode: boolean, mode: RunningTimeConfig['mode']): boolean => isManualMode && mode === 'manual',
        ],

        currentExposures: [
            (s) => [s.isManualEstimate, s.metricsWithResults, s.automaticTarget],
            (
                isManualEstimate: boolean,
                results: {
                    displayIndex: number
                    error: any
                    metric: import('../../../queries/schema').ExperimentMetricUnion
                    metricIndex: number
                    result: any
                }[],
                automaticTarget: RunningTimeTargetApi | null
            ): number | null => {
                // The backend counts the exposures the automatic target sizes, so a variant at 0% does not count.
                if (!isManualEstimate && automaticTarget?.current_sample_size != null) {
                    return automaticTarget.current_sample_size
                }
                return calculateCurrentExposures(results?.[0]?.result ?? null)
            },
        ],

        // Request for the automatic-mode target, or null when not applicable (manual mode / no results yet).
        automaticTargetRequest: [
            (s) => [s.mode, s.metricsWithResults],
            (
                mode: RunningTimeConfig['mode'],
                results: {
                    displayIndex: number
                    error: any
                    metric: import('../../../queries/schema').ExperimentMetricUnion
                    metricIndex: number
                    result: any
                }[]
            ): AutomaticTargetRequest | null => {
                // Gate on the in-form mode rather than the persisted mode. While the form is in manual mode
                // (even if the experiment is still saved as automatic), the automatic estimate is neither
                // shown nor persisted, so it shouldn't be requested.
                if (mode !== 'automatic') {
                    return null
                }
                const result = results?.[0]?.result as CachedNewExperimentQueryResponse | undefined
                if (!result?.baseline) {
                    return null
                }
                return {
                    resultStats: [result.baseline, ...result.variant_results].flatMap((stats) => [
                        stats.number_of_samples,
                        stats.sum,
                        stats.sum_squares,
                    ]),
                }
            },
        ],

        // Request body for the live manual-mode preview, or null when the form inputs aren't usable yet.
        manualPreviewInput: [
            (s) => [s.config, s.numberOfVariants],
            (config: RunningTimeConfig, numberOfVariants: number): RunningTimeCalculationInputApi | null => {
                // Use !(x > 0) to catch NaN, 0, negative, and undefined
                if (config.mode !== 'manual' || !(config.baselineValue > 0) || !(config.mde > 0)) {
                    return null
                }
                const baselineValue = config.metricType === 'funnel' ? config.baselineValue / 100 : config.baselineValue
                return {
                    metric_type: config.metricType,
                    minimum_detectable_effect: config.mde,
                    number_of_variants: numberOfVariants,
                    baseline_value: baselineValue,
                    exposure_rate_per_day: config.exposureRate > 0 ? config.exposureRate : undefined,
                }
            },
        ],

        targetSampleSize: [
            (s) => [s.isManualEstimate, s.targetEffect, s.experiment, s.automaticTargetRequest, s.automaticTarget],
            (
                isManualEstimate: boolean,
                targetEffect: number,
                experiment: Experiment,
                automaticInput: AutomaticTargetRequest | null,
                automaticTarget: RunningTimeTargetApi | null
            ): number | null => {
                if (isManualEstimate) {
                    // Persisted by the save listener from the same backend calculation.
                    return experiment?.running_time_calculation?.recommended_sample_size ?? null
                }
                if (!automaticInput || !automaticTarget?.recommended_sample_size) {
                    return null
                }
                return rescaleSampleSize(
                    automaticTarget.recommended_sample_size,
                    automaticTarget.minimum_detectable_effect,
                    targetEffect
                )
            },
        ],

        dailyExposureRate: [
            (s) => [s.isManualEstimate, s.experiment, s.automaticTarget],
            (
                isManualEstimate: boolean,
                experiment: Experiment,
                automaticTarget: RunningTimeTargetApi | null
            ): number | null => {
                if (isManualEstimate) {
                    return experiment?.running_time_calculation?.exposure_estimate_config?.manualExposureRate ?? null
                }
                return automaticTarget?.outlook?.exposures_per_day ?? null
            },
        ],

        // Days until we reach target sample size
        remainingDays: [
            (s) => [
                s.targetSampleSize,
                s.currentExposures,
                s.dailyExposureRate,
                s.experiment,
                s.isManualEstimate,
                s.outlook,
            ],
            (
                target: number | null,
                current: number | null,
                rate: number | null,
                experiment: Experiment,
                isManualEstimate: boolean,
                outlook: RunningTimeOutlook | null
            ): number | null => {
                if (!target || target <= 0 || !rate || rate <= 0) {
                    return null
                }
                // The automatic rate comes from the backend's outlook, which projects from the first 0.1 days on.
                if (!isManualEstimate) {
                    if (current !== null && current >= target) {
                        return 0
                    }
                    if (!experiment?.start_date) {
                        return Math.ceil(target / rate)
                    }
                    // The projection reaches the target `target / rate` days after launch, or at the checkpoint whose
                    // effect the target took. Calendar days to that date keep the header, the table's date and its
                    // "in N days" in step, also when the results end before today.
                    return daysFromToday(experiment.start_date, outlook?.daysToTarget ?? target / rate)
                }

                // The manual estimate counts down from the exposures so far once there are a day and 100 of them.
                const daysElapsed = calculateDaysElapsed(experiment?.start_date ?? null)
                if (!daysElapsed || daysElapsed < 1 || !current || current < 100) {
                    // Before that, the whole planned run.
                    if (rate > 0) {
                        return Math.ceil(target / rate)
                    }
                    return null
                }

                if (current >= target) {
                    return 0
                }

                const remaining = target - current
                return Math.ceil(remaining / rate)
            },
        ],
        // What the experiment can detect over time, for the automatic estimate.
        outlook: [
            (s) => [
                s.isManualEstimate,
                s.automaticTarget,
                s.targetEffect,
                s.defaultMinimumDetectableEffect,
                s.enteredMde,
                s.selectedCheckpointKey,
            ],
            (
                isManualEstimate: boolean,
                automaticTarget: RunningTimeTargetApi | null,
                targetEffect: number,
                defaultMinimumDetectableEffect: number,
                enteredMde: number | null,
                selectedCheckpointKey: string | null
            ): RunningTimeOutlook | null =>
                isManualEstimate
                    ? null
                    : buildRunningTimeOutlook({
                          projection: automaticTarget?.outlook ?? null,
                          currentSampleSize: automaticTarget?.current_sample_size ?? null,
                          savedEffect: automaticTarget?.minimum_detectable_effect ?? null,
                          savedSampleSize: automaticTarget?.recommended_sample_size ?? null,
                          defaultEffect: defaultMinimumDetectableEffect,
                          enteredEffect: enteredMde,
                          selectedKey: selectedCheckpointKey,
                          minimumDetectableEffect: targetEffect,
                      }),
        ],
        canIncludeMoreUsers: [
            (s) => [s.experiment],
            (experiment: Experiment): boolean => {
                // A group without conditions at 100% already includes every user.
                const includesEveryone = (experiment?.feature_flag?.filters?.groups ?? []).some(
                    (group) => !group.properties?.length && (group.rollout_percentage ?? 100) >= 100
                )
                return !includesEveryone && !isExperimentExposureFrozen(experiment)
            },
        ],
        // The first primary metric in display order, which is the one the backend sizes the target from.
        estimateMetric: [
            (s) => [s.experiment, s.metricsWithResults],
            (experiment: Experiment, metricsWithResults: MetricWithResult[]): EstimateMetric | null => {
                const metric = metricsWithResults?.[0]?.metric
                if (!metric) {
                    return null
                }
                const secondaryCount =
                    (experiment?.metrics_secondary?.length ?? 0) +
                    (experiment?.saved_metrics ?? []).filter((saved) => saved.metadata?.type === 'secondary').length
                return {
                    name: metric.name || getDefaultMetricTitle(metric),
                    isFirstOfSeveral: metricsWithResults.length > 1,
                    hasOtherMetrics: metricsWithResults.length > 1 || secondaryCount > 0,
                }
            },
        ],
        isComplete: [
            (s) => [s.currentExposures, s.targetSampleSize],
            (current: number | null, target: number | null): boolean =>
                current !== null && target !== null && current >= target,
        ],
        // True while the estimate is still resolving: metric results loading or the automatic calculation
        // request in flight. Lets the UI distinguish "still loading" from "settled but can't estimate"
        // instead of flashing a pending state during the gap between the two. Manual mode has no async
        // estimate to wait on, so it never counts as calculating.
        isCalculating: [
            (s) => [s.isManualEstimate, s.isRecalculating, s.automaticTargetLoading],
            (isManualEstimate: boolean, isRecalculating: boolean, automaticTargetLoading: boolean): boolean => {
                if (isManualEstimate) {
                    return false
                }
                return isRecalculating || automaticTargetLoading
            },
        ],
        manualFormPreview: [
            (s) => [s.manualPreviewInput, s.manualPreview],
            (input: RunningTimeCalculationInputApi | null, preview: ManualPreview): ManualPreview =>
                input ? preview : { sampleSize: null, runningTime: null },
        ],
    }),

    subscriptions(({ actions }) => ({
        // kea-subscriptions fires on reference change. The recalc poll replaces the metric-results array every
        // tick, so these inputs get a fresh object reference even when their values are identical, which would
        // re-fire the request on every poll. Skip the dispatch unless the value actually changed.
        automaticTargetRequest: (input: AutomaticTargetRequest | null, oldInput: AutomaticTargetRequest | null) => {
            if (input && !objectsEqual(input, oldInput)) {
                actions.loadAutomaticTarget(input)
            }
        },
        manualPreviewInput: (
            input: RunningTimeCalculationInputApi | null,
            oldInput: RunningTimeCalculationInputApi | null
        ) => {
            if (input && !objectsEqual(input, oldInput)) {
                actions.loadManualPreview(input)
            }
        },
    })),

    listeners(({ actions, values, props }) => ({
        // Persist the automatic estimate once the backend calculation lands and the selectors are fresh.
        loadAutomaticTargetSuccess: () => {
            actions.persistRunningTimeEstimate()
        },

        persistRunningTimeEstimate: async () => {
            const { isManualMode, remainingDays, targetSampleSize, experiment, currentProjectId, configOverrides } =
                values

            // Skip while the user has unsaved edits — `save` owns persistence then. Auto-persisting a
            // transient value here (e.g. right after `resetConfig`) would race with the save and revert it.
            if (
                configOverrides ||
                isManualMode ||
                !isLaunched(experiment) ||
                remainingDays === null ||
                targetSampleSize === null ||
                // Never store a non-positive estimate — a negative sample size or running time is
                // meaningless and would surface in the header and list view.
                remainingDays < 0 ||
                targetSampleSize <= 0
            ) {
                return
            }

            const savedRunningTime = experiment?.running_time_calculation?.recommended_running_time
            const savedSampleSize = experiment?.running_time_calculation?.recommended_sample_size
            if (savedRunningTime === remainingDays && savedSampleSize === targetSampleSize) {
                return
            }

            const updatedRunningTimeCalculation = {
                ...experiment?.running_time_calculation,
                recommended_running_time: remainingDays,
                recommended_sample_size: targetSampleSize,
            }

            // This write bumps the experiment's concurrency version like any other, so it must
            // carry the handshake and absorb the response — otherwise every open tab of a running
            // experiment goes silently stale on results load and later scalar saves 409.
            try {
                // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use experimentsPartialUpdate() from 'products/experiments/frontend/generated/api' instead.
                const response: Experiment = await api.update(
                    `api/projects/${currentProjectId}/experiments/${props.experiment.id}`,
                    {
                        ...toConcurrencyPayload(values.unmodifiedExperiment),
                        running_time_calculation: updatedRunningTimeCalculation,
                    }
                )
                actions.setUnmodifiedExperiment(structuredClone(response))
                actions.setExperiment({
                    running_time_calculation: response.running_time_calculation ?? updatedRunningTimeCalculation,
                })
            } catch (error: any) {
                if (!isExperimentConflictError(error)) {
                    throw error
                }
                // A machine-written estimate losing a race is not a user problem: drop it silently
                // and resync the snapshot so this tab stops being stale (and the guard above stops
                // re-persisting the same values against fresh server state).
                try {
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use experimentsRetrieve() from 'products/experiments/frontend/generated/api' instead.
                    const fresh: Experiment = await api.get(
                        `api/projects/${currentProjectId}/experiments/${props.experiment.id}`
                    )
                    actions.setUnmodifiedExperiment(structuredClone(fresh))
                    actions.setExperiment({ running_time_calculation: fresh.running_time_calculation })
                } catch {
                    // Leave state as-is; the next full experiment load resyncs the snapshot.
                }
            }
        },

        save: async () => {
            const { config, numberOfVariants, experiment, currentProjectId } = values
            // Read before the save, which replaces the experiment and with it the initial config.
            const previousMde = values.initialConfig.mde

            actions.setSaving(true)
            let saved = false
            try {
                let update: Partial<Experiment> & { update_feature_flag_params?: boolean }
                if (config.mode === 'manual') {
                    // Convert NaN (from empty input) to 0
                    const manualBaselineValue = Number.isNaN(config.baselineValue) ? 0 : config.baselineValue
                    const manualExposureRate = Number.isNaN(config.exposureRate) ? 0 : config.exposureRate

                    const baselineValue =
                        config.metricType === 'funnel' ? manualBaselineValue / 100 : manualBaselineValue

                    let sampleSize: number | null = null
                    let runningTime: number | null = null
                    if (baselineValue > 0) {
                        const result = await experimentsCalculateRunningTimeCreate(String(currentProjectId), {
                            metric_type: config.metricType,
                            minimum_detectable_effect: config.mde,
                            number_of_variants: numberOfVariants,
                            baseline_value: baselineValue,
                            exposure_rate_per_day: manualExposureRate > 0 ? manualExposureRate : undefined,
                        })
                        sampleSize = result.recommended_sample_size
                        runningTime = result.recommended_running_time_days
                    }

                    update = {
                        running_time_calculation: {
                            ...experiment?.running_time_calculation,
                            minimum_detectable_effect: config.mde,
                            recommended_sample_size: sampleSize ?? undefined,
                            recommended_running_time: runningTime ?? undefined,
                            exposure_estimate_config: {
                                conversionRateInputType: ConversionRateInputType.MANUAL,
                                manualMetricType: config.metricType,
                                manualBaselineValue,
                                manualExposureRate,
                            },
                        },
                        update_feature_flag_params: false,
                    }
                } else {
                    update = {
                        running_time_calculation: {
                            ...experiment?.running_time_calculation,
                            minimum_detectable_effect: config.mde,
                            exposure_estimate_config: {
                                conversionRateInputType: ConversionRateInputType.AUTOMATIC,
                            },
                        },
                        update_feature_flag_params: false,
                    }
                }

                // Await so the experiment reflects the saved config before resetConfig below. Otherwise the
                // transient (overrides cleared, experiment not yet updated) would re-trigger the automatic
                // auto-persist with stale values and revert the save.
                saved = await saveExperimentUpdate(props.experiment.id, update)
            } catch {
                // Keep the modal open (don't close/reset below) so the user can retry.
                lemonToast.error('Failed to save running time settings. Please try again.')
                return
            } finally {
                actions.setSaving(false)
            }
            // A failed experiment save has already shown its error. Keep the modal open so the user can retry.
            if (!saved) {
                return
            }
            // pinned: analytics event name and properties.
            posthog.capture('experiment running time settings saved', {
                experiment_id: props.experiment.id,
                mode: config.mode,
                minimum_detectable_effect: config.mde,
                previous_minimum_detectable_effect: previousMde,
            })
            actions.closeRunningTimeConfigModal()
            actions.resetConfig()
            // The target on hand is sized for the previous effect. The backend sizes it for the saved one, and its
            // success persists the estimate.
            if (config.mode === 'automatic' && values.automaticTargetRequest) {
                actions.loadAutomaticTarget(values.automaticTargetRequest)
            }
        },

        openRunningTimeConfigModal: () => {
            const { config, outlook } = values
            // pinned: analytics event name and properties.
            posthog.capture('experiment running time settings opened', {
                experiment_id: props.experiment.id,
                mode: config.mode,
                out_of_reach: outlook?.isOutOfReach ?? null,
                days_to_target: outlook ? Math.round(outlook.daysToTarget) : null,
            })
        },

        cancel: () => {
            actions.closeRunningTimeConfigModal()
            actions.resetConfig()
        },
    })),
])