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.

12.0 KB
Raw Blame
import { useActions, useValues } from 'kea'

import { LemonBanner, LemonButton, LemonInput, LemonModal, LemonSegmentedButton, Spinner } from '@posthog/lemon-ui'

import { Label } from 'lib/ui/Label/Label'
import { ManualCalculatorMetricType } from 'scenes/experiments/RunningTimeCalculator/calculations'
import { runningTimeLogic } from 'scenes/experiments/RunningTimeCalculator/runningTimeLogic'

import { Experiment } from '~/types'

import { isLaunched } from 'products/experiments/frontend/experimentStatus'

import { DetectableEffectTable } from './DetectableEffectTable'
import { MinimumDetectableEffectField } from './MinimumDetectableEffectField'
import { MinimumDetectableEffectHelp } from './MinimumDetectableEffectHelp'

export interface RunningTimeConfigModalProps {
    experiment: Experiment
}

const METRIC_TYPE_OPTIONS: { value: ManualCalculatorMetricType; label: string }[] = [
    { value: 'funnel', label: 'Funnel' },
    { value: 'mean_count', label: 'Count' },
    { value: 'mean_sum_or_avg', label: 'Sum/Avg' },
]

function getBaselineLabel(metricType: ManualCalculatorMetricType): string {
    switch (metricType) {
        case 'funnel':
            return 'Baseline conversion rate'
        case 'mean_count':
            return 'Avg events per user'
        case 'mean_sum_or_avg':
            return 'Avg property value per user'
    }
}

function getBaselineHelp(metricType: ManualCalculatorMetricType): string {
    switch (metricType) {
        case 'funnel':
            return 'Expected conversion rate for the control group (0-100%)'
        case 'mean_count':
            return 'Average number of events per user in the control group'
        case 'mean_sum_or_avg':
            return 'Average property value per user in the control group'
    }
}

export function RunningTimeConfigModal({ experiment: experimentProp }: RunningTimeConfigModalProps): JSX.Element {
    const {
        config,
        manualFormPreview,
        isRunningTimeConfigModalOpen,
        experiment,
        isSaving,
        outlook,
        isCalculating,
        automaticTargetFailed,
        automaticTargetRequest,
    } = useValues(runningTimeLogic({ experiment: experimentProp }))
    const { setConfig, save, cancel, loadAutomaticTarget } = useActions(
        runningTimeLogic({ experiment: experimentProp })
    )

    const isPreLaunch = !isLaunched(experiment)
    const effectField = <MinimumDetectableEffectField value={config.mde} onChange={(mde) => setConfig({ mde })} />

    return (
        <LemonModal isOpen={isRunningTimeConfigModalOpen} onClose={cancel} width={560} simple>
            <LemonModal.Header>
                <h3>{isPreLaunch ? 'Estimate running time' : 'Running time'}</h3>
            </LemonModal.Header>
            <LemonModal.Content className="flex-1 min-h-0">
                <div className="space-y-6">
                    <div>
                        <Label intent="menu">Calculation mode</Label>
                        <LemonSegmentedButton
                            className="mt-1"
                            size="small"
                            fullWidth
                            options={[
                                {
                                    value: 'automatic',
                                    label: 'Automatic',
                                    disabledReason: isPreLaunch ? 'Available after launching experiment' : undefined,
                                },
                                { value: 'manual', label: 'Manual' },
                            ]}
                            value={config.mode}
                            onChange={(value) => setConfig({ mode: value as 'automatic' | 'manual' })}
                        />
                        {config.mode === 'manual' && (
                            <div className="text-xs text-muted mt-1">
                                Enter your own values to calculate sample size and running time.
                            </div>
                        )}
                    </div>

                    {config.mode === 'manual' ? (
                        <>
                            <div>
                                <Label intent="menu">Metric type</Label>
                                <LemonSegmentedButton
                                    className="mt-1"
                                    size="small"
                                    fullWidth
                                    options={METRIC_TYPE_OPTIONS}
                                    value={config.metricType}
                                    onChange={(value) => {
                                        const metricType = value as ManualCalculatorMetricType
                                        // Switching to funnel reinterprets baselineValue as a conversion rate, so carry
                                        // the same 100% clamp the baseline input applies. Otherwise a large mean value
                                        // survives the switch as an out-of-range rate.
                                        setConfig({
                                            metricType,
                                            ...(metricType === 'funnel' && config.baselineValue > 100
                                                ? { baselineValue: 100 }
                                                : {}),
                                        })
                                    }}
                                />
                            </div>

                            <div>
                                <Label intent="menu">{getBaselineLabel(config.metricType)}</Label>
                                <div className="flex items-center gap-1 mt-1">
                                    <LemonInput
                                        type="number"
                                        value={config.baselineValue}
                                        onChange={(value) => {
                                            const next = value as number
                                            // A funnel conversion rate above 100% is impossible and makes the
                                            // sample-size math produce a negative estimate. Clamp it for real,
                                            // since the `max` prop below only guides the stepper.
                                            setConfig({
                                                baselineValue:
                                                    config.metricType === 'funnel' && next > 100 ? 100 : next,
                                            })
                                        }}
                                        min={0}
                                        max={config.metricType === 'funnel' ? 100 : undefined}
                                        step={config.metricType === 'funnel' ? 0.1 : 1}
                                        className="flex-1"
                                    />
                                    {config.metricType === 'funnel' && <span className="text-muted">%</span>}
                                </div>
                                <div className="text-xs text-muted mt-1">{getBaselineHelp(config.metricType)}</div>
                            </div>

                            <div>
                                {effectField}
                                <MinimumDetectableEffectHelp className="text-muted mt-1" />
                            </div>

                            <div>
                                <Label intent="menu">Expected exposures per day</Label>
                                <LemonInput
                                    type="number"
                                    value={config.exposureRate}
                                    onChange={(value) => setConfig({ exposureRate: value as number })}
                                    min={0}
                                    step={100}
                                    className="w-32 mt-1"
                                />
                                <div className="text-xs text-muted mt-1">
                                    Total users entering the experiment per day (across all variants).
                                </div>
                            </div>
                        </>
                    ) : (
                        <>
                            {effectField}
                            <div className="space-y-2">
                                {!outlook && isCalculating ? (
                                    <div className="flex items-center gap-2 text-secondary">
                                        <Spinner />
                                        <span>Calculating the running time…</span>
                                    </div>
                                ) : automaticTargetFailed ? (
                                    <LemonBanner
                                        type="error"
                                        action={
                                            automaticTargetRequest
                                                ? {
                                                      children: 'Try again',
                                                      onClick: () => loadAutomaticTarget(automaticTargetRequest),
                                                  }
                                                : undefined
                                        }
                                    >
                                        Couldn't calculate the running time.
                                    </LemonBanner>
                                ) : !outlook ? (
                                    <LemonBanner type="info">
                                        Not enough data to estimate the running time yet. It needs exposures, and some
                                        activity on the primary metric, in each variant.
                                    </LemonBanner>
                                ) : (
                                    <DetectableEffectTable experiment={experimentProp} />
                                )}
                                <MinimumDetectableEffectHelp className="text-secondary" />
                            </div>
                        </>
                    )}
                </div>
            </LemonModal.Content>
            <LemonModal.Footer className="flex-col items-stretch gap-4">
                {config.mode === 'manual' && (
                    <div>
                        <Label intent="menu">Calculated results</Label>
                        <div className="grid grid-cols-2 gap-4 mt-2">
                            <div>
                                <div className="text-xs text-muted">Recommended sample size</div>
                                <div className="font-semibold">
                                    {manualFormPreview.sampleSize ? manualFormPreview.sampleSize.toLocaleString() : '—'}
                                </div>
                            </div>
                            <div>
                                <div className="text-xs text-muted">Estimated running time</div>
                                <div className="font-semibold">
                                    {manualFormPreview.runningTime
                                        ? `~${manualFormPreview.runningTime} day${manualFormPreview.runningTime !== 1 ? 's' : ''}`
                                        : '—'}
                                </div>
                            </div>
                        </div>
                    </div>
                )}
                <div className="flex items-center gap-2 justify-end w-full">
                    <LemonButton type="secondary" onClick={cancel} disabledReason={isSaving ? 'Saving…' : undefined}>
                        Cancel
                    </LemonButton>
                    <LemonButton type="primary" onClick={save} loading={isSaving}>
                        Save
                    </LemonButton>
                </div>
            </LemonModal.Footer>
        </LemonModal>
    )
}