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.

4.1 KB
Raw Blame
import type { Mocks } from '~/mocks/utils'

import type {
    ExperimentMetricsRecalculationJobApi,
    ExperimentMetricsRecalculationLatestApi,
    ExperimentMetricsRecalculationRunApi,
    MetricRecalculationResultApi,
} from 'products/experiments/frontend/generated/api.schemas'

/** The parts of an experiment fixture the run reads: every metric uuid, inline and shared, in both sections. */
type ExperimentFixture = {
    id: number | string
    metrics?: { uuid?: string }[]
    metrics_secondary?: { uuid?: string }[]
    saved_metrics?: { query?: { uuid?: string } }[]
}

/** A result fixture is a cached experiment query response; `last_refresh` stamps the run's window. */
type ResultFixture = { last_refresh?: string | null } & Record<string, unknown>

const RUN_ID = 'storybook-recalculation'

type MetricType = 'mean' | 'funnel' | 'ratio'
type ExperimentWithMetricTypes = ExperimentFixture & {
    metrics?: { uuid?: string; metric_type?: string }[]
    metrics_secondary?: { uuid?: string; metric_type?: string }[]
}

/** Resolves each metric of the experiment to the result fixture for its metric type. */
export function resultsByMetricType(
    experiment: ExperimentWithMetricTypes,
    fixtures: Partial<Record<MetricType, ResultFixture>>
): (metricUuid: string) => ResultFixture | null {
    const metrics = [...(experiment.metrics ?? []), ...(experiment.metrics_secondary ?? [])]
    return (metricUuid) => {
        const metricType = metrics.find((metric) => metric.uuid === metricUuid)?.metric_type as MetricType | undefined
        return (metricType && fixtures[metricType]) || null
    }
}

export function recalculationMocks(
    experiment: ExperimentFixture,
    resultFor: (metricUuid: string) => ResultFixture | null
): Mocks {
    const metricUuids = [
        ...(experiment.metrics ?? []).map((metric) => metric.uuid),
        ...(experiment.metrics_secondary ?? []).map((metric) => metric.uuid),
        ...(experiment.saved_metrics ?? []).map((savedMetric) => savedMetric.query?.uuid),
    ].filter((uuid): uuid is string => !!uuid)

    const results: MetricRecalculationResultApi[] = metricUuids.map((metricUuid) => {
        const result = resultFor(metricUuid)
        return result
            ? { metric_uuid: metricUuid, status: 'completed', result, error_message: null }
            : { metric_uuid: metricUuid, status: 'failed', result: null, error_message: 'Metric failed to compute' }
    })
    const failedUuids = results.filter(({ status }) => status === 'failed').map(({ metric_uuid }) => metric_uuid)

    const queryTo =
        results
            .map(({ result }) => (result as ResultFixture | null)?.last_refresh ?? null)
            .filter((stamp): stamp is string => !!stamp)
            .sort()
            .at(-1) ?? null

    const run: ExperimentMetricsRecalculationRunApi = {
        id: RUN_ID,
        experiment_id: Number(experiment.id),
        status: failedUuids.length === metricUuids.length && metricUuids.length > 0 ? 'failed' : 'completed',
        total_metrics: metricUuids.length,
        completed_metrics: metricUuids.length - failedUuids.length,
        failed_metrics: failedUuids.length,
        metric_errors: Object.fromEntries(
            failedUuids.map((uuid) => [
                uuid,
                { step: 'calculate', message: 'Metric failed to compute', retriable: false },
            ])
        ),
        created_at: queryTo ?? new Date(0).toISOString(),
        started_at: queryTo,
        completed_at: queryTo,
        query_to: queryTo,
        metric_retries: {},
        results,
        rows_read: null,
        estimated_rows_total: null,
    }
    const latest: ExperimentMetricsRecalculationLatestApi = { ...run, active_run: null, result_source: 'recalculation' }
    const job: ExperimentMetricsRecalculationJobApi = { ...run, is_existing: true }
    const base = `/api/projects/:team_id/experiments/${experiment.id}/metrics_recalculation`

    return {
        get: {
            [`${base}/latest/`]: latest,
            [`${base}/:recalculationId/`]: run,
        },
        post: {
            [`${base}/`]: [201, job],
        },
    }
}