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.0 KB
Raw Blame
import { LogicWrapper, MakeLogicType, actions, connect, kea, key, listeners, path, props, selectors } from 'kea'

import type { SourceSpendApi, SpendOverviewApi } from '../../generated/api.schemas'
import { SignalSourceProduct } from '../types'
import { selfDrivingSpendLogic } from './selfDrivingSpendLogic'
import { sourceSpendLogic } from './sourceSpendLogic'

export interface UsageBucket {
    sourceProduct: string
    amount: number | null
}

export interface UsageOverviewLogicProps {
    teamId: number
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface usageOverviewLogicValues {
    overview: SpendOverviewApi | null // selfDrivingSpendLogic
    scoutsFailed: boolean // selfDrivingSpendLogic
    sources: SourceSpendApi[] | null // sourceSpendLogic
    sourcesFailed: boolean // sourceSpendLogic
    spendByProduct: Map<string, number | null> // sourceSpendLogic
    buckets: UsageBucket[]
    failed: boolean
    loading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface usageOverviewLogicActions {
    loadOverview: () => any // selfDrivingSpendLogic
    loadSources: () => any // sourceSpendLogic
    loadUsage: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface usageOverviewLogicMeta {
    key: number
    __keaTypeGenInternalSelectorTypes: {
        loading: (sources: SourceSpendApi[] | null, overview: SpendOverviewApi | null) => boolean
        failed: (sourcesFailed: boolean, scoutsFailed: boolean) => boolean
        buckets: (spendByProduct: Map<string, number | null>, overview: SpendOverviewApi | null) => UsageBucket[]
    }
}

export type usageOverviewLogicType = MakeLogicType<
    usageOverviewLogicValues,
    usageOverviewLogicActions,
    UsageOverviewLogicProps,
    usageOverviewLogicMeta
>

export const usageOverviewLogic: LogicWrapper<usageOverviewLogicType> = kea<usageOverviewLogicType>([
    props({} as UsageOverviewLogicProps),
    key((props: UsageOverviewLogicProps) => props.teamId),
    path((key) => ['products', 'signals', 'usageOverviewLogic', key]),
    connect((props: UsageOverviewLogicProps) => ({
        values: [
            sourceSpendLogic(props),
            ['sources', 'spendByProduct', 'failed as sourcesFailed'],
            selfDrivingSpendLogic(props),
            ['overview', 'failed as scoutsFailed'],
        ],
        actions: [sourceSpendLogic(props), ['loadSources'], selfDrivingSpendLogic(props), ['loadOverview']],
    })),
    actions({ loadUsage: true }),
    selectors({
        loading: [
            (s) => [s.sources, s.overview],
            (sources: SourceSpendApi[] | null, overview: SpendOverviewApi | null): boolean =>
                sources === null || overview === null,
        ],
        failed: [
            (s) => [s.sourcesFailed, s.scoutsFailed],
            (sourcesFailed: boolean, scoutsFailed: boolean): boolean => sourcesFailed || scoutsFailed,
        ],
        buckets: [
            (s) => [s.spendByProduct, s.overview],
            (spendByProduct: Map<string, number | null>, overview: SpendOverviewApi | null): UsageBucket[] => {
                const amounts = new Map<string, number | null>(spendByProduct)
                const scoutSignals = amounts.get(SignalSourceProduct.SignalsScout)
                const scoutRuns = overview?.scout_spend.total_spend
                amounts.set(
                    SignalSourceProduct.SignalsScout,
                    scoutSignals == null && scoutRuns == null ? null : (scoutSignals ?? 0) + (scoutRuns ?? 0)
                )
                return Array.from(amounts, ([sourceProduct, amount]) => ({ sourceProduct, amount })).sort(
                    (a, b) => (b.amount ?? -1) - (a.amount ?? -1) || a.sourceProduct.localeCompare(b.sourceProduct)
                )
            },
        ],
    }),
    listeners(({ actions }) => ({
        loadUsage: () => {
            actions.loadSources()
            actions.loadOverview()
        },
    })),
])