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.

5.5 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'

import { ApiConfig } from 'lib/api'

import { engineeringAnalyticsPullRequestTimelines } from '../generated/api'
import type { PullRequestTimelinesApi } from '../generated/api.schemas'
import { DeliveryScope, deliveryScopeKey, deliveryScopeParams } from '../lib/deliveryScope'
import { DayViewAlignment, DayViewGroup, RedTimeByCause, axisDays, groupTimelines } from '../lib/pullRequestDayView'
import { engineeringAnalyticsFiltersLogic } from './engineeringAnalyticsFiltersLogic'

export interface PullRequestTimelinesLogicProps {
    scope: DeliveryScope
    sourceId: string | null
    /** The 'owner/name' in the page's path. */
    repo?: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface pullRequestTimelinesLogicValues {
    dateFrom: string | null // engineeringAnalyticsFiltersLogic
    dateTo: string | null // engineeringAnalyticsFiltersLogic
    dayViewAlignment: DayViewAlignment
    dayViewAxisDays: number
    dayViewGroups: DayViewGroup[]
    redTime: RedTimeByCause
    repoSlugs: string[]
    timelines: PullRequestTimelinesApi | null
    timelinesFailed: boolean
    timelinesLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface pullRequestTimelinesLogicActions {
    loadTimelines: () => any
    loadTimelinesFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadTimelinesSuccess: (
        timelines: PullRequestTimelinesApi,
        payload?: any
    ) => {
        timelines: PullRequestTimelinesApi
        payload?: any
    }
    setDayViewAlignment: (alignment: DayViewAlignment) => {
        alignment: DayViewAlignment
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface pullRequestTimelinesLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        dayViewGroups: (timelines: PullRequestTimelinesApi | null) => DayViewGroup[]
        dayViewAxisDays: (timelines: PullRequestTimelinesApi | null, dayViewAlignment: DayViewAlignment) => number
        redTime: (timelines: PullRequestTimelinesApi | null) => RedTimeByCause
        repoSlugs: (timelines: PullRequestTimelinesApi | null) => string[]
    }
}

export type pullRequestTimelinesLogicType = MakeLogicType<
    pullRequestTimelinesLogicValues,
    pullRequestTimelinesLogicActions,
    PullRequestTimelinesLogicProps,
    pullRequestTimelinesLogicMeta
>

export const pullRequestTimelinesLogic = kea<pullRequestTimelinesLogicType>([
    path(['products', 'engineering_analytics', 'frontend', 'scenes', 'pullRequestTimelinesLogic']),
    props({} as PullRequestTimelinesLogicProps),
    key((props) => `${deliveryScopeKey(props.scope)}@${props.repo ?? ''}@${props.sourceId ?? ''}`),

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

    actions({
        setDayViewAlignment: (alignment: DayViewAlignment) => ({ alignment }),
    }),

    loaders(({ props, values }) => ({
        timelines: [
            null as PullRequestTimelinesApi | null,
            {
                loadTimelines: async () =>
                    await engineeringAnalyticsPullRequestTimelines(String(ApiConfig.getCurrentProjectId()), {
                        ...deliveryScopeParams(props.scope),
                        date_from: values.dateFrom ?? undefined,
                        date_to: values.dateTo ?? undefined,
                        source_id: props.sourceId ?? undefined,
                        repo: props.repo ?? undefined,
                    }),
            },
        ],
    })),

    reducers({
        dayViewAlignment: ['days' as DayViewAlignment, { setDayViewAlignment: (_, { alignment }) => alignment }],
        timelinesFailed: [false, { loadTimelines: () => false, loadTimelinesFailure: () => true }],
    }),

    listeners(({ actions }) => ({
        [engineeringAnalyticsFiltersLogic.actionTypes.setDateRange]: () => actions.loadTimelines(),
    })),

    selectors({
        dayViewGroups: [
            (s) => [s.timelines],
            (timelines: PullRequestTimelinesApi | null): DayViewGroup[] => groupTimelines(timelines?.items ?? []),
        ],
        dayViewAxisDays: [
            (s) => [s.timelines, s.dayViewAlignment],
            (timelines: PullRequestTimelinesApi | null, alignment: DayViewAlignment): number =>
                axisDays(timelines?.items ?? [], alignment),
        ],
        redTime: [
            (s) => [s.timelines],
            (timelines: PullRequestTimelinesApi | null): RedTimeByCause => {
                const secondsPerMergedPr = (timelines?.red_seconds_per_merged_pr ?? []).map(
                    ({ kind, seconds_per_merged_pr }) => ({ kind, seconds: seconds_per_merged_pr })
                )
                return {
                    mergedCount: timelines?.merged_pr_count ?? 0,
                    secondsPerMergedPr,
                    totalSecondsPerMergedPr: secondsPerMergedPr.reduce((sum, entry) => sum + entry.seconds, 0),
                }
            },
        ],
        repoSlugs: [
            (s) => [s.timelines],
            (timelines: PullRequestTimelinesApi | null): string[] =>
                Array.from(new Set((timelines?.items ?? []).map((pr) => `${pr.repo.owner}/${pr.repo.name}`))),
        ],
    }),

    afterMount(({ actions }) => {
        actions.loadTimelines()
    }),
])