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.

1.6 KB
Raw Blame
import { HogFlow } from '../schema/hogflow'
import { HogFunctionType, MinimalAppMetric } from '../types'
import { filteredMetric, rejectsEventName } from '../utils/hog-function-filtering'

export const runsOnEvents = (fn: HogFunctionType): boolean => (fn.filters?.source ?? 'events') === 'events'

export const triggersOnEvents = (
    flow: HogFlow
): flow is HogFlow & { trigger: Extract<HogFlow['trigger'], { type: 'event' }> } => flow.trigger.type === 'event'

export type EventNameGate = {
    admits: (eventName: string) => boolean
    /** The metrics the pre-filter records for an event no source admits, so skipping it records the same. */
    filteredMetrics: () => { hogFunctions: MinimalAppMetric[]; hogFlows: MinimalAppMetric[] }
}

export function buildEventNameGate({
    hogFunctions,
    hogFlows,
    alsoAdmitted,
}: {
    hogFunctions: HogFunctionType[]
    hogFlows: HogFlow[]
    alsoAdmitted: string[]
}): EventNameGate {
    const eventFunctions = hogFunctions.filter(runsOnEvents)
    const eventFlows = hogFlows.filter(triggersOnEvents)
    const filters = [...eventFunctions.map((fn) => fn.filters), ...eventFlows.map((flow) => flow.trigger.filters)]

    return {
        admits: (eventName) =>
            alsoAdmitted.includes(eventName) || filters.some((filter) => !rejectsEventName(filter, eventName)),
        filteredMetrics: () => ({
            hogFunctions: eventFunctions.map(filteredMetric),
            hogFlows: eventFlows.map((flow) => ({
                ...filteredMetric(flow),
                app_source_version: { id: flow.id, version: flow.version },
            })),
        }),
    }
}