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.

2.9 KB
Raw Blame
import { deepEqual as equal } from 'fast-equals'
import { MakeLogicType, afterMount, kea, key, path, props, propsChanged } from 'kea'
import { loaders } from 'kea-loaders'

import { performQuery } from '~/queries/query'
import type { ExperimentMetric } from '~/queries/schema/schema-general'
import { getEventCountQuery } from '~/scenes/experiments/utils'

export type MetricRecentActivityLogicProps = {
    metric: ExperimentMetric
    filterTestAccounts: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface metricRecentActivityLogicValues {
    eventCount: number | null
    eventCountLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface metricRecentActivityLogicActions {
    loadEventCount: () => any
    loadEventCountFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadEventCountSuccess: (
        eventCount: number | null,
        payload?: any
    ) => {
        eventCount: number | null
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface metricRecentActivityLogicMeta {
    key: string
}

export type metricRecentActivityLogicType = MakeLogicType<
    metricRecentActivityLogicValues,
    metricRecentActivityLogicActions,
    MetricRecentActivityLogicProps,
    metricRecentActivityLogicMeta
>

export const metricRecentActivityLogic = kea<metricRecentActivityLogicType>([
    props({} as MetricRecentActivityLogicProps),
    key((props) => `${props.metric.uuid}`),
    path((key) => ['scenes', 'experiments', 'create', 'metricRecentActivityLogic', key]),
    loaders(({ props }) => ({
        eventCount: [
            null as number | null,
            {
                loadEventCount: async () => {
                    const query = getEventCountQuery(props.metric, props.filterTestAccounts)

                    if (!query) {
                        return null
                    }

                    const response = await performQuery(query)

                    if (response.results && response.results.length > 0) {
                        const firstResult = response.results[0]
                        if (firstResult && typeof firstResult.aggregated_value === 'number') {
                            return firstResult.aggregated_value
                        }
                    }

                    return null
                },
            },
        ],
    })),
    afterMount(({ actions, props }) => {
        if (props.metric.uuid) {
            actions.loadEventCount()
        }
    }),
    propsChanged(({ actions, props }, oldProps) => {
        /**
         * this brings back memories of componentWillReceiveProps...
         */
        if (!equal(props, oldProps)) {
            actions.loadEventCount()
        }
    }),
])