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.

28.3 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { actionToUrl, combineUrl, router, urlToAction } from 'kea-router'
import posthog from 'posthog-js'

import { lemonToast } from '@posthog/lemon-ui'

import api, { PaginatedResponse } from 'lib/api'
import { convertPropertyGroupToProperties } from 'lib/components/PropertyFilters/utils'
import { EVENT_DEFINITIONS_PER_PAGE, PROPERTY_DEFINITIONS_PER_EVENT } from 'lib/constants'
import { objectsEqual } from 'lib/utils/objects'
import { parseTagsFilter } from 'lib/utils/url'
import { projectLogic } from 'scenes/projectLogic'

import { AnyPropertyFilter, EventDefinition, EventDefinitionType, PropertyDefinition } from '~/types'

import { eventDefinitionsBulkUpdateVerifiedCreate } from 'products/event_definitions/frontend/generated/api'
import type {
    EventDefinitionBulkUpdateVerifiedResponseApi,
    PaginatedEnterpriseEventDefinitionListApi,
} from 'products/event_definitions/frontend/generated/api.schemas'

import type {
    BulkUpdateTagsUUIDErrorApi,
    EventDefinitionBulkUpdateVerifiedItemApi,
} from '../../../../../products/event_definitions/frontend/generated/api.schemas'

export interface EventDefinitionsPaginatedResponse
    extends PaginatedResponse<EventDefinition>, Pick<PaginatedEnterpriseEventDefinitionListApi, 'count_is_capped'> {
    current?: string
    count?: number
    page?: number
}

export interface PropertyDefinitionsPaginatedResponse extends PaginatedResponse<PropertyDefinition> {
    current?: string
    count?: number
    page?: number
}

export interface Filters {
    event: string
    properties: AnyPropertyFilter[]
    event_type: EventDefinitionType
    ordering?: string
    tags?: string[]
    verified?: boolean
}

export interface BulkVerifiedResult extends EventDefinitionBulkUpdateVerifiedResponseApi {
    // Echoes the requested target so the success toast can describe the direction (verify vs unverify).
    verified: boolean
}

const DEFAULT_FILTERS: Filters = {
    event: '',
    properties: [],
    event_type: EventDefinitionType.Event,
    ordering: 'event',
    tags: undefined,
    verified: undefined,
}

export function createDefinitionKey(event?: EventDefinition, property?: PropertyDefinition): string {
    return `${event?.id ?? 'event'}-${property?.id ?? 'property'}`
}

export function normalizePropertyDefinitionEndpointUrl(
    url: string | null | undefined,
    searchParams: Record<string, any> = {},
    full: boolean = false
): string | null {
    if (!full && !url) {
        return null
    }
    return api.propertyDefinitions.determineListEndpoint({
        ...(url ? combineUrl(url).searchParams : {}),
        ...searchParams,
    })
}

function filtersFromSearchParams(searchParams: Record<string, any>): Filters {
    const { event, event_type, ordering, tags, verified } = searchParams
    return {
        ...DEFAULT_FILTERS,
        ...(event !== undefined && { event }),
        ...(event_type !== undefined && { event_type }),
        ...(ordering !== undefined && { ordering }),
        ...(parseTagsFilter(tags) !== undefined && { tags: parseTagsFilter(tags) }),
        ...(verified !== undefined && { verified: verified === 'true' || verified === true }),
    }
}

function normalizeEventDefinitionEndpointUrl({
    url,
    searchParams = {},
    full = false,
    eventTypeFilter = EventDefinitionType.Event,
}: {
    url?: string | null | undefined
    searchParams?: Record<string, any>
    full?: boolean
    eventTypeFilter?: EventDefinitionType
}): string | null {
    if (!full && !url) {
        return null
    }
    const params = {
        ...(url
            ? {
                  ...combineUrl(url).searchParams,
                  event_type: eventTypeFilter,
              }
            : {}),
        ...searchParams,
    }
    return api.eventDefinitions.determineListEndpoint(params)
}

export interface EventDefinitionsTableLogicProps {
    key: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface eventDefinitionsTableLogicValues {
    currentProjectId: number | null // projectLogic
    bulkVerifiedResult: BulkVerifiedResult | null
    bulkVerifiedResultLoading: boolean
    eventDefinitionPropertiesLoading: string[]
    eventDefinitions: EventDefinitionsPaginatedResponse
    eventDefinitionsLoading: boolean
    eventPropertiesCacheMap: Record<string, PropertyDefinitionsPaginatedResponse>
    eventPropertiesCacheMapLoading: boolean
    filters: Filters
    paramsFromFilters: Record<string, any>
    showVerifiedFilter: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface eventDefinitionsTableLogicActions {
    applyBulkTagUpdates: (
        updates: {
            id: number | string
            tags: string[]
        }[]
    ) => {
        updates: {
            id: number | string
            tags: string[]
        }[]
    }
    applyBulkTagUpdatesFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    applyBulkTagUpdatesSuccess: (
        eventDefinitions: EventDefinitionsPaginatedResponse,
        payload?: {
            updates: {
                id: number | string
                tags: string[]
            }[]
        }
    ) => {
        eventDefinitions: EventDefinitionsPaginatedResponse
        payload?: {
            updates: {
                id: number | string
                tags: string[]
            }[]
        }
    }
    applyBulkVerifiedUpdates: (
        updates: {
            id: string
            verified: boolean
        }[]
    ) => {
        updates: {
            id: string
            verified: boolean
        }[]
    }
    applyBulkVerifiedUpdatesFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    applyBulkVerifiedUpdatesSuccess: (
        eventDefinitions: EventDefinitionsPaginatedResponse,
        payload?: {
            updates: {
                id: string
                verified: boolean
            }[]
        }
    ) => {
        eventDefinitions: EventDefinitionsPaginatedResponse
        payload?: {
            updates: {
                id: string
                verified: boolean
            }[]
        }
    }
    bulkUpdateVerified: ({ ids, verified }: { ids: string[]; onSuccess?: () => void; verified: boolean }) => {
        ids: string[]
        verified: boolean
        onSuccess?: () => void
    }
    bulkUpdateVerifiedFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    bulkUpdateVerifiedSuccess: (
        bulkVerifiedResult: {
            skipped: BulkUpdateTagsUUIDErrorApi[]
            updated: EventDefinitionBulkUpdateVerifiedItemApi[]
            verified: boolean
        },
        payload?: {
            ids: string[]
            verified: boolean
            onSuccess?: () => void
        }
    ) => {
        bulkVerifiedResult: {
            skipped: BulkUpdateTagsUUIDErrorApi[]
            updated: EventDefinitionBulkUpdateVerifiedItemApi[]
            verified: boolean
        }
        payload?: {
            ids: string[]
            verified: boolean
            onSuccess?: () => void
        }
    }
    loadEventDefinitions: (url?: string | null) => {
        url: string | null
    }
    loadEventDefinitionsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadEventDefinitionsSuccess: (
        eventDefinitions: EventDefinitionsPaginatedResponse,
        payload?: {
            url: string | null
        }
    ) => {
        eventDefinitions: EventDefinitionsPaginatedResponse
        payload?: {
            url: string | null
        }
    }
    loadEventExample: (definition: EventDefinition) => {
        definition: EventDefinition
    }
    loadPropertiesForEvent: (
        definition: EventDefinition,
        url?: string | null
    ) => {
        definition: EventDefinition
        url: string | null
    }
    loadPropertiesForEventFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadPropertiesForEventSuccess: (
        eventPropertiesCacheMap: {
            [x: string]: any
        },
        payload?: {
            definition: EventDefinition
            url: string | null
        }
    ) => {
        eventPropertiesCacheMap: {
            [x: string]: any
        }
        payload?: {
            definition: EventDefinition
            url: string | null
        }
    }
    setEventDefinitionPropertiesLoading: (ids: string[]) => {
        ids: string[]
    }
    setFilters: (
        filters: Partial<Filters>,
        debounce?: boolean
    ) => {
        debounce: boolean
        filters: Partial<Filters>
    }
    setLocalEventDefinition: (definition: EventDefinition) => {
        definition: EventDefinition
    }
    setLocalEventDefinitionFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    setLocalEventDefinitionSuccess: (
        eventDefinitions: EventDefinitionsPaginatedResponse,
        payload?: {
            definition: EventDefinition
        }
    ) => {
        eventDefinitions: EventDefinitionsPaginatedResponse
        payload?: {
            definition: EventDefinition
        }
    }
    setLocalPropertyDefinition: (
        event: EventDefinition,
        definition: PropertyDefinition
    ) => {
        definition: PropertyDefinition
        event: EventDefinition
    }
    setLocalPropertyDefinitionFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    setLocalPropertyDefinitionSuccess: (
        eventPropertiesCacheMap: Record<string, PropertyDefinitionsPaginatedResponse>,
        payload?: {
            definition: PropertyDefinition
            event: EventDefinition
        }
    ) => {
        eventPropertiesCacheMap: Record<string, PropertyDefinitionsPaginatedResponse>
        payload?: {
            definition: PropertyDefinition
            event: EventDefinition
        }
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface eventDefinitionsTableLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        showVerifiedFilter: (eventDefinitions: EventDefinitionsPaginatedResponse, filters: Filters) => boolean
        paramsFromFilters: (filters: Filters) => Record<string, any>
    }
}

export type eventDefinitionsTableLogicType = MakeLogicType<
    eventDefinitionsTableLogicValues,
    eventDefinitionsTableLogicActions,
    EventDefinitionsTableLogicProps,
    eventDefinitionsTableLogicMeta
>

export const eventDefinitionsTableLogic = kea<eventDefinitionsTableLogicType>([
    path((key) => ['scenes', 'data-management', 'events', 'eventDefinitionsTableLogic', key]),
    props({} as EventDefinitionsTableLogicProps),
    key((props) => props.key || 'scene'),
    connect(() => ({
        values: [projectLogic, ['currentProjectId']],
    })),
    actions({
        loadEventDefinitions: (url: string | null = '') => ({ url }),
        loadEventExample: (definition: EventDefinition) => ({ definition }),
        loadPropertiesForEvent: (definition: EventDefinition, url: string | null = '') => ({ definition, url }),
        setFilters: (filters: Partial<Filters>, debounce: boolean = true) => ({ filters, debounce }),
        setLocalEventDefinition: (definition: EventDefinition) => ({ definition }),
        applyBulkTagUpdates: (updates: { id: number | string; tags: string[] }[]) => ({ updates }),
        applyBulkVerifiedUpdates: (updates: { id: string; verified: boolean }[]) => ({ updates }),
        setLocalPropertyDefinition: (event: EventDefinition, definition: PropertyDefinition) => ({ event, definition }),
        setEventDefinitionPropertiesLoading: (ids: string[]) => ({ ids }),
    }),
    reducers({
        filters: [
            DEFAULT_FILTERS,
            {
                setFilters: (state, { filters }) => ({
                    ...state,
                    ...filters,
                    properties: convertPropertyGroupToProperties(filters.properties) ?? [],
                }),
            },
        ],
        eventDefinitionPropertiesLoading: [
            [] as string[],
            {
                setEventDefinitionPropertiesLoading: (_, { ids }) => ids ?? [],
            },
        ],
    }),
    loaders(({ values, cache, actions }) => ({
        eventDefinitions: [
            {
                count: 0,
                next: undefined,
                current: undefined,
                previous: undefined,
                results: [],
            } as EventDefinitionsPaginatedResponse,
            {
                loadEventDefinitions: async ({ url: _url }, breakpoint) => {
                    let url = _url

                    if (!url) {
                        url = api.eventDefinitions.determineListEndpoint(values.paramsFromFilters)
                    }

                    if (url && url in (cache.apiCache ?? {})) {
                        return cache.apiCache[url]
                    }

                    cache.eventsStartTime = performance.now()
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.
                    const response = await api.get(url)
                    breakpoint()

                    const result = {
                        ...response,
                        previous: normalizeEventDefinitionEndpointUrl({
                            url: response.previous,
                            eventTypeFilter: values.filters.event_type,
                        }),
                        next: normalizeEventDefinitionEndpointUrl({
                            url: response.next,
                            eventTypeFilter: values.filters.event_type,
                        }),
                        current: url,
                        page: Math.floor((combineUrl(url).searchParams.offset ?? 0) / EVENT_DEFINITIONS_PER_PAGE) + 1,
                    }

                    cache.apiCache = {
                        ...cache.apiCache,
                        [url]: result,
                    }

                    return result
                },
                setLocalEventDefinition: ({ definition }) => {
                    if (!values.eventDefinitions.current) {
                        return values.eventDefinitions
                    }
                    const result = {
                        ...values.eventDefinitions,
                        results: values.eventDefinitions.results.map((d) => (d.id === definition.id ? definition : d)),
                    }

                    // Update cache
                    cache.apiCache = {
                        ...cache.apiCache,
                        [values.eventDefinitions.current]: result,
                    }

                    return result
                },
                applyBulkTagUpdates: ({ updates }) => {
                    if (!values.eventDefinitions.current || updates.length === 0) {
                        return values.eventDefinitions
                    }
                    const tagsById = new Map(updates.map((u) => [String(u.id), u.tags]))
                    const result = {
                        ...values.eventDefinitions,
                        results: values.eventDefinitions.results.map((d) =>
                            tagsById.has(String(d.id)) ? { ...d, tags: tagsById.get(String(d.id)) } : d
                        ),
                    }

                    cache.apiCache = {
                        ...cache.apiCache,
                        [values.eventDefinitions.current]: result,
                    }

                    return result
                },
                applyBulkVerifiedUpdates: ({ updates }) => {
                    // Patch the page the request started on (pinned in bulkUpdateVerified), not
                    // whatever is current now — the user may have navigated while it was in flight.
                    const cacheKey: string | undefined = cache.bulkVerifiedCacheKey
                    if (!cacheKey || updates.length === 0) {
                        return values.eventDefinitions
                    }
                    const verifiedById = new Map(updates.map((u) => [String(u.id), u.verified] as const))
                    const patch = (list: EventDefinitionsPaginatedResponse): EventDefinitionsPaginatedResponse => ({
                        ...list,
                        results: list.results.map((d) =>
                            verifiedById.has(String(d.id)) ? { ...d, verified: verifiedById.get(String(d.id)) } : d
                        ),
                    })

                    const onScreen = values.eventDefinitions.current === cacheKey
                    const source = cache.apiCache?.[cacheKey] ?? (onScreen ? values.eventDefinitions : undefined)
                    if (!source) {
                        return values.eventDefinitions
                    }
                    const patched = patch(source)
                    cache.apiCache = {
                        ...cache.apiCache,
                        [cacheKey]: patched,
                    }

                    // Only replace what's on screen if it's still that same page.
                    return onScreen ? patched : values.eventDefinitions
                },
            },
        ],
        bulkVerifiedResult: [
            null as BulkVerifiedResult | null,
            {
                bulkUpdateVerified: async ({
                    ids,
                    verified,
                }: {
                    ids: string[]
                    verified: boolean
                    onSuccess?: () => void
                }) => {
                    // Pin the page being edited now: the user may page or filter away before the
                    // request resolves, and the row patch must land on the page it started on.
                    cache.bulkVerifiedCacheKey = values.eventDefinitions.current
                    const response = await eventDefinitionsBulkUpdateVerifiedCreate(String(values.currentProjectId), {
                        ids,
                        verified,
                    })
                    return { verified, ...response }
                },
            },
        ],
        eventPropertiesCacheMap: [
            {} as Record<string, PropertyDefinitionsPaginatedResponse>,
            {
                loadPropertiesForEvent: async ({ definition, url }, breakpoint) => {
                    if (url && url in (cache.apiCache ?? {})) {
                        return {
                            ...values.eventPropertiesCacheMap,
                            [definition.id]: cache.apiCache[url],
                        }
                    }

                    if (!url) {
                        url = api.propertyDefinitions.determineListEndpoint({
                            event_names: [definition.name],
                            exclude_core_properties: true,
                            filter_by_event_names: true,
                            is_feature_flag: false,
                            limit: PROPERTY_DEFINITIONS_PER_EVENT,
                        })
                    }
                    actions.setEventDefinitionPropertiesLoading(
                        Array.from([...values.eventDefinitionPropertiesLoading, definition.id])
                    )
                    cache.propertiesStartTime = performance.now()
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.
                    const response = await api.get(url)
                    breakpoint()

                    // Fetch one event as example and cache (only if events have been captured)
                    let exampleEventProperties: Record<string, string> = {}
                    if (definition.last_seen_at) {
                        const exampleUrl = api.events.determineListEndpoint({ event: definition.name }, 1)
                        if (exampleUrl && exampleUrl in (cache.apiCache ?? {})) {
                            exampleEventProperties = cache.apiCache[exampleUrl]
                        } else {
                            // nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.
                            exampleEventProperties = (await api.get(exampleUrl))?.results?.[0].properties ?? {}
                            cache.apiCache = {
                                ...cache.apiCache,
                                [exampleUrl]: exampleEventProperties,
                            }
                        }
                    }

                    const currentUrl = `${normalizePropertyDefinitionEndpointUrl(url)}`
                    const propertyResult = {
                        count: response.count,
                        previous: normalizePropertyDefinitionEndpointUrl(response.previous),
                        next: normalizePropertyDefinitionEndpointUrl(response.next),
                        current: currentUrl,
                        page:
                            Math.floor(
                                (combineUrl(currentUrl).searchParams.offset ?? 0) / PROPERTY_DEFINITIONS_PER_EVENT
                            ) + 1,
                        results: response.results.map((prop: PropertyDefinition) => ({
                            ...prop,
                            example: exampleEventProperties?.[prop.name]?.toString(),
                        })),
                    }

                    cache.apiCache = {
                        ...cache.apiCache,
                        [currentUrl]: propertyResult,
                    }

                    actions.setEventDefinitionPropertiesLoading(
                        values.eventDefinitionPropertiesLoading.filter((loadingId) => loadingId != definition.id)
                    )
                    return {
                        ...values.eventPropertiesCacheMap,
                        [definition.id]: propertyResult,
                    }
                },
                setLocalPropertyDefinition: ({ event, definition }) => {
                    if (!values.eventPropertiesCacheMap?.[event.id]?.current) {
                        return values.eventPropertiesCacheMap
                    }
                    // Update cache as well
                    const eventCacheKey = values.eventPropertiesCacheMap[event.id].current as string
                    const updatedProperties = {
                        ...values.eventPropertiesCacheMap[event.id],
                        results: values.eventPropertiesCacheMap[event.id].results.map((p) =>
                            p.id === definition.id ? definition : p
                        ),
                    }

                    cache.apiCache = {
                        ...cache.apiCache,
                        [eventCacheKey]: updatedProperties,
                    }

                    return {
                        ...values.eventPropertiesCacheMap,
                        [event.id]: updatedProperties,
                    }
                },
            },
        ],
    })),
    selectors(() => ({
        showVerifiedFilter: [
            (s) => [s.eventDefinitions, s.filters],
            (eventDefinitions: EventDefinitionsPaginatedResponse, filters: Filters): boolean =>
                filters.verified !== undefined ||
                (eventDefinitions.results.length > 0 && 'verified' in eventDefinitions.results[0]),
        ],
        // Convert filters to API params
        paramsFromFilters: [
            (s) => [s.filters],
            (filters: Filters) => {
                const params: Record<string, any> = {
                    search: filters.event,
                    ordering: filters.ordering,
                    event_type: filters.event_type,
                }
                if (filters.tags && filters.tags.length > 0) {
                    params.tags = JSON.stringify(filters.tags)
                }
                if (filters.verified !== undefined) {
                    params.verified = filters.verified
                }
                return params
            },
        ],
    })),
    listeners(({ actions, values, cache }) => ({
        setFilters: async ({ debounce }, breakpoint) => {
            if (debounce) {
                cache.filtersLoadPending = true
                await breakpoint(200)
            }
            cache.filtersLoadPending = false
            actions.loadEventDefinitions()
        },
        bulkUpdateVerifiedSuccess: ({ bulkVerifiedResult, payload }) => {
            if (!bulkVerifiedResult) {
                return
            }
            const { verified, updated, skipped } = bulkVerifiedResult
            actions.applyBulkVerifiedUpdates(updated)
            // Clear the selection only now that the request succeeded, so a failure leaves it
            // intact for the user to retry without re-selecting everything.
            payload?.onSuccess?.()
            if (updated.length > 0) {
                lemonToast.success(
                    `${verified ? 'Verified' : 'Unverified'} ${updated.length} event${updated.length !== 1 ? 's' : ''}`
                )
            }
            if (skipped.length > 0) {
                lemonToast.warning(`${skipped.length} event${skipped.length !== 1 ? 's' : ''} skipped`)
            }
            if (updated.length === 0 && skipped.length === 0) {
                lemonToast.info('No events needed updating')
            }
        },
        bulkUpdateVerifiedFailure: () => {
            lemonToast.error('Failed to update verification status')
        },
        loadEventDefinitionsSuccess: () => {
            if (cache.eventsStartTime !== undefined) {
                posthog.capture('event definitions page load succeeded', {
                    load_time: performance.now() - cache.eventsStartTime,
                    num_results: values.eventDefinitions.results.length,
                })
                cache.eventsStartTime = undefined
            }
        },
        loadEventDefinitionsFailure: ({ error }) => {
            if (cache.eventsStartTime !== undefined) {
                posthog.capture('event definitions page load failed', {
                    load_time: performance.now() - cache.eventsStartTime,
                    error: error ?? 'There was an unknown error fetching event definitions.',
                })
                cache.eventsStartTime = undefined
            }
        },
        loadPropertiesForEventSuccess: () => {
            if (cache.propertiesStartTime !== undefined) {
                posthog.capture('event definitions page event nested properties load succeeded', {
                    load_time: performance.now() - cache.propertiesStartTime,
                })
                cache.propertiesStartTime = undefined
            }
        },
        loadPropertiesForEventFailure: ({ error }) => {
            if (cache.propertiesStartTime !== undefined) {
                posthog.capture('event definitions page event nested properties load failed', {
                    load_time: performance.now() - cache.propertiesStartTime,
                    error: error ?? 'There was an unknown error fetching nested property definitions.',
                })
                cache.propertiesStartTime = undefined
            }
        },
    })),
    urlToAction(({ actions, values, cache }) => ({
        '/data-management/events': (_, searchParams) => {
            const filtersFromUrl = filtersFromSearchParams(searchParams)

            if (!objectsEqual(values.filters, filtersFromUrl)) {
                actions.setFilters(filtersFromUrl, false)
            } else if (
                // A debounced filter change already writes this URL, so loading here would skip the debounce
                !cache.filtersLoadPending &&
                !values.eventDefinitions.results.length &&
                !values.eventDefinitionsLoading
            ) {
                actions.loadEventDefinitions()
            }
        },
    })),
    actionToUrl(({ values }) => ({
        setFilters: () => [router.values.location.pathname, values.filters],
    })),
])