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.

3.5 KB
Raw Blame
import { MakeLogicType, afterMount, connect, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'

import { ApiConfig } from 'lib/api'

import { engineeringAnalyticsRepoOverview } from '../generated/api'
import type { RepoOverviewApi } from '../generated/api.schemas'
import { engineeringAnalyticsFiltersLogic } from './engineeringAnalyticsFiltersLogic'
import { awaitsRepoForward, engineeringAnalyticsLogic } from './engineeringAnalyticsLogic'

const projectId = (): string => String(ApiConfig.getCurrentProjectId())

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface timeToProductionLogicValues {
    dateFrom: string | null // engineeringAnalyticsFiltersLogic
    dateTo: string | null // engineeringAnalyticsFiltersLogic
    scopeRepo: string | null // engineeringAnalyticsLogic
    sourceId: string | null // engineeringAnalyticsLogic
    timing: RepoOverviewApi | null
    timingFailed: boolean
    timingLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface timeToProductionLogicActions {
    loadTiming: () => any
    loadTimingFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadTimingSuccess: (
        timing: RepoOverviewApi,
        payload?: any
    ) => {
        timing: RepoOverviewApi
        payload?: any
    }
}

export type timeToProductionLogicType = MakeLogicType<timeToProductionLogicValues, timeToProductionLogicActions>

// The windowed PR-lifecycle timings behind the pull-requests page: cycle time, the pre-merge
// pipeline legs, and the merge queue's outcomes. The repo hub reads the same endpoint for its
// CI-health numbers, so the two pages share a payload shape but never share state.
export const timeToProductionLogic = kea<timeToProductionLogicType>([
    path(['products', 'engineering_analytics', 'frontend', 'scenes', 'timeToProductionLogic']),

    connect(() => ({
        values: [
            engineeringAnalyticsFiltersLogic,
            ['dateFrom', 'dateTo'],
            engineeringAnalyticsLogic,
            ['sourceId', 'scopeRepo'],
        ],
    })),

    loaders(({ values }) => ({
        timing: [
            null as RepoOverviewApi | null,
            {
                // The series are the expensive half of this endpoint and nothing here charts them.
                loadTiming: async (): Promise<RepoOverviewApi> =>
                    await engineeringAnalyticsRepoOverview(projectId(), {
                        date_from: values.dateFrom ?? undefined,
                        date_to: values.dateTo ?? undefined,
                        include_series: false,
                        source_id: values.sourceId ?? undefined,
                        repo: values.scopeRepo ?? undefined,
                    }),
            },
        ],
    })),

    reducers({
        timingFailed: [
            false,
            {
                loadTiming: () => false,
                loadTimingSuccess: () => false,
                loadTimingFailure: () => true,
            },
        ],
    }),

    listeners(({ actions }) => ({
        [engineeringAnalyticsFiltersLogic.actionTypes.setDateRange]: () => actions.loadTiming(),
        [engineeringAnalyticsLogic.actionTypes.setSourceId]: () => actions.loadTiming(),
        [engineeringAnalyticsLogic.actionTypes.refresh]: () => actions.loadTiming(),
    })),

    afterMount(({ actions }) => {
        if (!awaitsRepoForward()) {
            actions.loadTiming()
        }
    }),
])