: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import {
type ExperimentLogicProps,
type ExperimentWarning,
experimentLogic,
getSectionMetricUuids,
} from 'scenes/experiments/experimentLogic'
import { projectLogic } from 'scenes/projectLogic'
import type { Experiment, ExperimentIdType } from '~/types'
import { experimentsHealthInputsRetrieve } from '../../generated/api'
import type { ExperimentHealthApi, ExperimentHealthInputsApi } from '../../generated/api.schemas'
import type { SetupFact } from '../../setupInspector/SetupFactsTable'
import { type HealthPanelFinding, hoursSinceStart } from '../healthPanelFindings'
import {
type HealthDebugFlagChange,
type HealthDebugReleaseCondition,
type HealthInputsState,
type PageMetricCounts,
buildExperimentFacts,
buildExposureFacts,
buildFlagChanges,
buildFlagFacts,
buildFlagHistoryFacts,
buildMetricFacts,
buildReleaseConditions,
buildTrafficFacts,
} from './healthDebugInputs'
import {
type HealthDebugCheck,
type HealthDebugDomain,
type HealthDebugInput,
buildHealthDebugChecks,
describePanel,
describeServerHealth,
} from './healthDebugReport'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface experimentHealthDebugLogicValues {
browserExperimentWarning: ExperimentWarning | null // experimentLogic
browserNoMetricsWarning: boolean // experimentLogic
experiment: Experiment // experimentLogic
experimentHealth: ExperimentHealthApi | null // experimentLogic
experimentHealthLoading: boolean // experimentLogic
experimentLoadCount: number // experimentLogic
exposures: any // experimentLogic
exposuresLoading: boolean // experimentLogic
healthFindings: HealthPanelFinding[] | null // experimentLogic
isExperimentDraft: boolean // experimentLogic
currentProjectId: number | null // projectLogic
activeDomain: HealthDebugDomain
experimentFacts: SetupFact[]
exposureFacts: SetupFact[]
flagChanges: HealthDebugFlagChange[]
flagFacts: SetupFact[]
flagHistoryFacts: SetupFact[]
healthDebugChecks: HealthDebugCheck[]
healthDebugRawData: Record<string, unknown>
healthDebugSummary: SetupFact[]
healthInputs: ExperimentHealthInputsApi | null
healthInputsError: string | null
healthInputsLoading: boolean
healthInputsState: HealthInputsState
metricFacts: SetupFact[]
pageMetricCounts: PageMetricCounts
releaseConditions: HealthDebugReleaseCondition[]
trafficFacts: SetupFact[]
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface experimentHealthDebugLogicActions {
loadExperimentHealthSuccess: (
experimentHealth: ExperimentHealthApi | null,
payload?: ('experiment_load' | 'experiment_update') | undefined
) => {
experimentHealth: ExperimentHealthApi | null
payload?: 'experiment_load' | 'experiment_update'
} // experimentLogic
loadHealthInputs: () => any
loadHealthInputsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadHealthInputsSuccess: (
healthInputs: ExperimentHealthInputsApi | null,
payload?: any
) => {
healthInputs: ExperimentHealthInputsApi | null
payload?: any
}
setActiveDomain: (domain: HealthDebugDomain) => {
domain: HealthDebugDomain
}
setHealthInputsError: (error: string | null) => {
error: string | null
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface experimentHealthDebugLogicMeta {
key: ExperimentIdType
__keaTypeGenInternalSelectorTypes: {
pageMetricCounts: (experiment: Experiment) => PageMetricCounts
healthInputsState: (
healthInputs: ExperimentHealthInputsApi | null,
healthInputsLoading: boolean,
healthInputsError: string | null
) => HealthInputsState
healthDebugChecks: (
experimentHealth: ExperimentHealthApi | null,
browserExperimentWarning: ExperimentWarning | null,
browserNoMetricsWarning: boolean,
isExperimentDraft: boolean,
exposures: any,
exposuresLoading: boolean,
healthInputs: ExperimentHealthInputsApi | null,
healthInputsLoading: boolean
) => HealthDebugCheck[]
healthDebugSummary: (
experimentHealth: ExperimentHealthApi | null,
healthFindings: HealthPanelFinding[] | null,
healthDebugChecks: HealthDebugCheck[],
experimentLoadCount: number
) => SetupFact[]
experimentFacts: (experiment: Experiment, healthInputsState: HealthInputsState) => SetupFact[]
flagFacts: (experiment: Experiment, healthInputsState: HealthInputsState) => SetupFact[]
releaseConditions: (healthInputs: ExperimentHealthInputsApi | null) => HealthDebugReleaseCondition[]
flagHistoryFacts: (healthInputsState: HealthInputsState) => SetupFact[]
flagChanges: (healthInputs: ExperimentHealthInputsApi | null) => HealthDebugFlagChange[]
trafficFacts: (healthInputsState: HealthInputsState) => SetupFact[]
metricFacts: (
experiment: Experiment,
healthInputsState: HealthInputsState,
pageMetricCounts: PageMetricCounts
) => SetupFact[]
exposureFacts: (healthInputsState: HealthInputsState) => SetupFact[]
healthDebugRawData: (
experimentHealth: ExperimentHealthApi | null,
healthFindings: HealthPanelFinding[] | null,
healthInputs: ExperimentHealthInputsApi | null,
exposures: any
) => Record<string, unknown>
}
}
export type experimentHealthDebugLogicType = MakeLogicType<
experimentHealthDebugLogicValues,
experimentHealthDebugLogicActions,
ExperimentLogicProps,
experimentHealthDebugLogicMeta
>
function errorDetail(error: unknown): string {
const { detail } = (error ?? {}) as { detail?: string }
return detail || 'The request failed. Reload the inputs, and check the server logs if it keeps failing.'
}
/** The staff-only view of the health checks: what each check concluded, and the inputs it read, by domain. */
export const experimentHealthDebugLogic = kea<experimentHealthDebugLogicType>([
props({} as ExperimentLogicProps),
key((props) => props.experimentId ?? 'new'),
path((key) => ['products', 'experiments', 'health', 'debug', 'experimentHealthDebugLogic', key]),
connect((props: ExperimentLogicProps) => ({
values: [
experimentLogic(props),
[
'experiment',
'experimentHealth',
'experimentHealthLoading',
'healthFindings',
'browserExperimentWarning',
'browserNoMetricsWarning',
'exposures',
'exposuresLoading',
'isExperimentDraft',
'experimentLoadCount',
],
projectLogic,
['currentProjectId'],
],
actions: [experimentLogic(props), ['loadExperimentHealthSuccess']],
})),
actions({
setActiveDomain: (domain: HealthDebugDomain) => ({ domain }),
setHealthInputsError: (error: string | null) => ({ error }),
}),
loaders(({ actions, props, values }) => ({
// The error is caught and shown in the panel, because the loader's failure path would also raise a toast for
// the same message.
healthInputs: [
null as ExperimentHealthInputsApi | null,
{
loadHealthInputs: async () => {
if (typeof props.experimentId !== 'number' || values.currentProjectId === null) {
return null
}
actions.setHealthInputsError(null)
try {
return await experimentsHealthInputsRetrieve(
String(values.currentProjectId),
props.experimentId
)
} catch (error) {
actions.setHealthInputsError(errorDetail(error))
return null
}
},
},
],
})),
reducers({
activeDomain: ['experiment' as HealthDebugDomain, { setActiveDomain: (_, { domain }) => domain }],
healthInputsError: [null as string | null, { setHealthInputsError: (_, { error }) => error }],
}),
selectors({
pageMetricCounts: [
(s) => [s.experiment],
(experiment: Experiment): PageMetricCounts => ({
primary: getSectionMetricUuids(experiment, false).length,
secondary: getSectionMetricUuids(experiment, true).length,
}),
],
healthInputsState: [
(s) => [s.healthInputs, s.healthInputsLoading, s.healthInputsError],
(inputs: ExperimentHealthInputsApi | null, loading: boolean, error: string | null): HealthInputsState => ({
inputs,
loading,
error,
}),
],
healthDebugChecks: [
(s) => [
s.experimentHealth,
s.browserExperimentWarning,
s.browserNoMetricsWarning,
s.isExperimentDraft,
s.exposures,
s.exposuresLoading,
s.healthInputs,
s.healthInputsLoading,
],
(
experimentHealth: ExperimentHealthApi | null,
browserWarning: ExperimentWarning | null,
browserNoMetricsWarning: boolean,
isExperimentDraft: boolean,
exposures: HealthDebugInput['exposures'],
exposuresLoading: boolean,
healthInputs: ExperimentHealthInputsApi | null,
healthInputsLoading: boolean
): HealthDebugCheck[] =>
buildHealthDebugChecks({
health: experimentHealth,
browserWarning,
browserNoMetricsWarning,
isExperimentDraft,
exposures,
exposuresLoading,
serverExposures: healthInputs?.exposures ?? null,
serverExposuresLoading: healthInputsLoading,
}),
],
healthDebugSummary: [
(s) => [s.experimentHealth, s.healthFindings, s.healthDebugChecks, s.experimentLoadCount],
(
experimentHealth: ExperimentHealthApi | null,
healthFindings: HealthPanelFinding[] | null,
checks: HealthDebugCheck[],
experimentLoadCount: number
): SetupFact[] => {
const differing = checks
.filter(({ difference }) => difference === 'disagrees')
.map(({ check }) => check)
return [
{ label: 'Server health', value: describeServerHealth(experimentHealth) },
{ label: 'Health panel', value: describePanel(healthFindings) },
{
label: 'Browser rules',
value: differing.length ? `Differ from the server on ${differing.join(', ')}` : 'Agree',
help: "The page's older rules for the flag state and the metrics. The page shows the server's findings when it has them.",
},
{
label: 'Page load',
value: String(experimentLoadCount),
help: 'Each finding is reported as shown once per load.',
},
]
},
],
experimentFacts: [
(s) => [s.experiment, s.healthInputsState],
(experiment: Experiment, state: HealthInputsState): SetupFact[] =>
buildExperimentFacts(experiment, state, hoursSinceStart(experiment.start_date)),
],
flagFacts: [
(s) => [s.experiment, s.healthInputsState],
(experiment: Experiment, state: HealthInputsState): SetupFact[] => buildFlagFacts(experiment, state),
],
releaseConditions: [
(s) => [s.healthInputs],
(inputs: ExperimentHealthInputsApi | null): HealthDebugReleaseCondition[] => buildReleaseConditions(inputs),
],
flagHistoryFacts: [
(s) => [s.healthInputsState],
(state: HealthInputsState): SetupFact[] => buildFlagHistoryFacts(state),
],
flagChanges: [
(s) => [s.healthInputs],
(inputs: ExperimentHealthInputsApi | null): HealthDebugFlagChange[] => buildFlagChanges(inputs),
],
trafficFacts: [
(s) => [s.healthInputsState],
(state: HealthInputsState): SetupFact[] => buildTrafficFacts(state),
],
metricFacts: [
(s) => [s.experiment, s.healthInputsState, s.pageMetricCounts],
(experiment: Experiment, state: HealthInputsState, pageMetricCounts: PageMetricCounts): SetupFact[] =>
buildMetricFacts(experiment, state, pageMetricCounts),
],
exposureFacts: [
(s) => [s.healthInputsState],
(state: HealthInputsState): SetupFact[] => buildExposureFacts(state),
],
healthDebugRawData: [
(s) => [s.experimentHealth, s.healthFindings, s.healthInputs, s.exposures],
(
experimentHealth: ExperimentHealthApi | null,
healthFindings: HealthPanelFinding[] | null,
healthInputs: ExperimentHealthInputsApi | null,
exposures: any
): Record<string, unknown> => ({
health: experimentHealth,
panelFindings: healthFindings,
healthInputs,
pageExposures: exposures
? {
total_exposures: exposures.total_exposures,
sample_ratio_mismatch: exposures.sample_ratio_mismatch,
bias_risk: exposures.bias_risk,
}
: null,
}),
],
}),
listeners(({ actions }) => ({
// The inputs follow the findings, so that both describe the same save of the experiment.
loadExperimentHealthSuccess: () => {
actions.loadHealthInputs()
},
})),
afterMount(({ actions, values }) => {
// While the findings load, their success loads the inputs, so loading here too would ask twice.
if (!values.experimentHealthLoading) {
actions.loadHealthInputs()
}
}),
])