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.

9.9 KB
Raw Blame
import { convertPropertyGroupToProperties } from 'lib/components/PropertyFilters/utils'
import { objectCleanWithEmpty } from 'lib/utils/objects'
import { MathAvailability } from 'scenes/insights/filters/ActionFilter/ActionFilterRow/types'
import {
    actorsOnlyMathTypes,
    calendarHeatmapMathTypes,
    funnelsMathTypes,
} from 'scenes/insights/filters/ActionFilter/cleanSeriesNodes'
import {
    SeriesNode,
    isActionsSeriesNode,
    isEventsSeriesNode,
    isGroupSeriesNode,
    isWarehouseSeriesNode,
    seriesNodeEntityType,
    toActionId,
} from 'scenes/insights/filters/ActionFilter/seriesNode'

import { cleanEntityProperties } from '~/queries/nodes/InsightQuery/utils/cleanProperties'
import { setLatestVersionsOnQuery } from '~/queries/schema/query-versions'
import {
    ActionsNode,
    AnyDataWarehouseNode,
    AnyEntityNode,
    DataWarehouseNode,
    EventsNode,
    FunnelsDataWarehouseNode,
    GroupNode,
    LifecycleDataWarehouseNode,
    MathType,
    NodeKind,
} from '~/queries/schema/schema-general'
import { BaseMathType, EntityTypes, FilterLogicalOperator, RetentionEntity, TemplateVariableStep } from '~/types'

type DataWarehouseNodeKind =
    | NodeKind.DataWarehouseNode
    | NodeKind.FunnelsDataWarehouseNode
    | NodeKind.LifecycleDataWarehouseNode

/** A stored template variable default. The template editor writes series fields into it that `TemplateVariableStep` does not declare. */
type TemplateStep = TemplateVariableStep & {
    math_property?: string | null
    math_property_type?: string | null
    math_hogql?: string | null
    math_group_type_index?: number | null
    optionalInFunnel?: boolean
    operator?: FilterLogicalOperator | null
    nestedFilters?: TemplateStep[] | null
    table_name?: string
    id_field?: string
    timestamp_field?: string
    distinct_id_field?: string
    aggregation_target_field?: string
    created_at_field?: string
}

function isDataWarehouseStep(step: TemplateStep): boolean {
    return step.type === EntityTypes.DATA_WAREHOUSE
}

function isGroupStep(step: TemplateStep): boolean {
    return step.type === EntityTypes.GROUPS
}

export const legacyEntityToNode = (
    entity: TemplateStep,
    includeProperties: boolean,
    mathAvailability: MathAvailability,
    dataWarehouseNodeKind: DataWarehouseNodeKind = NodeKind.DataWarehouseNode
): AnyEntityNode<AnyDataWarehouseNode> | GroupNode<AnyDataWarehouseNode> => {
    let shared: Partial<
        EventsNode | ActionsNode | DataWarehouseNode | FunnelsDataWarehouseNode | LifecycleDataWarehouseNode | GroupNode
    > = {
        name: entity.name || undefined,
        custom_name: entity.custom_name || undefined,
    }

    if (isDataWarehouseStep(entity)) {
        shared = {
            ...shared,
            timestamp_field: entity.timestamp_field || undefined,
            table_name: entity.table_name || undefined,
            ...(dataWarehouseNodeKind === NodeKind.LifecycleDataWarehouseNode
                ? {
                      aggregation_target_field: entity.aggregation_target_field,
                      created_at_field: entity.created_at_field,
                  }
                : dataWarehouseNodeKind === NodeKind.FunnelsDataWarehouseNode
                  ? {
                        id_field: entity.id_field || undefined,
                        aggregation_target_field: entity.aggregation_target_field || undefined,
                    }
                  : {
                        id_field: entity.id_field || undefined,
                        distinct_id_field: entity.distinct_id_field || undefined,
                    }),
        } as DataWarehouseNode | FunnelsDataWarehouseNode | LifecycleDataWarehouseNode
    }

    if (isGroupStep(entity)) {
        shared = {
            ...shared,
            operator: entity.operator || undefined,
            nodes: (entity.nestedFilters || []).map((v) =>
                legacyEntityToNode(v, includeProperties, mathAvailability, dataWarehouseNodeKind)
            ),
        } as GroupNode
    }

    if (includeProperties) {
        shared = { ...shared, properties: cleanEntityProperties(entity.properties) } as any
    }

    if (mathAvailability !== MathAvailability.None) {
        // only trends, funnels, and stickiness insights support math.
        // transition to then default math for stickiness, when an unsupported math type is encountered.
        if (mathAvailability === MathAvailability.ActorsOnly && !actorsOnlyMathTypes.includes(entity.math as any)) {
            shared = {
                ...shared,
                math: BaseMathType.UniqueUsers,
            }
        } else if (mathAvailability === MathAvailability.FunnelsOnly) {
            if (funnelsMathTypes.includes(entity.math as any)) {
                shared = {
                    ...shared,
                    math: entity.math as MathType,
                }
            }
            if (entity.optionalInFunnel) {
                shared = {
                    ...shared,
                    optionalInFunnel: true,
                }
            }
        } else if (mathAvailability === MathAvailability.CalendarHeatmapOnly) {
            if (calendarHeatmapMathTypes.includes(entity.math as any)) {
                shared = {
                    ...shared,
                    math: entity.math as MathType,
                }
            }
        } else {
            shared = {
                ...shared,
                math: entity.math || 'total',
                math_property: entity.math_property,
                math_property_type: entity.math_property_type,
                math_hogql: entity.math_hogql,
                math_group_type_index: entity.math_group_type_index,
            } as any
        }
    }

    if (entity.type === 'actions') {
        return setLatestVersionsOnQuery(
            objectCleanWithEmpty({
                kind: NodeKind.ActionsNode,
                id: entity.id,
                ...shared,
            })
        ) as any
    } else if (entity.type === 'data_warehouse') {
        return setLatestVersionsOnQuery(
            objectCleanWithEmpty({
                kind: dataWarehouseNodeKind,
                id: entity.id,
                ...shared,
            })
        ) as any
    } else if (entity.type === EntityTypes.GROUPS) {
        return setLatestVersionsOnQuery(
            objectCleanWithEmpty({
                kind: NodeKind.GroupNode,
                ...shared,
            })
        ) as any
    }
    return setLatestVersionsOnQuery(
        objectCleanWithEmpty({
            kind: NodeKind.EventsNode,
            event: entity.id,
            ...shared,
        })
    ) as any
}

export const sanitizeRetentionEntity = (entity: RetentionEntity | undefined): RetentionEntity | undefined => {
    if (!entity) {
        return undefined
    }

    const { id, kind, name, type, order, uuid, custom_name } = entity
    return {
        ...('id' in entity ? { id } : {}),
        ...('kind' in entity ? { kind } : {}),
        ...('name' in entity ? { name } : {}),
        ...('type' in entity ? { type } : {}),
        ...('order' in entity ? { order } : {}),
        ...('uuid' in entity ? { uuid } : {}),
        ...('custom_name' in entity ? { custom_name } : {}),
    }
}

const STEP_IDENTITY_KEYS = ['type', 'id', 'order', 'index', 'nestedFilters', 'uuid']
const NODE_IDENTITY_KEYS = ['kind', 'version', 'event', 'id', 'nodes']

/** When the picker holds more than one row, the first row of the earliest type here is the one stored. */
const TEMPLATE_STEP_TYPES = [EntityTypes.ACTIONS, EntityTypes.EVENTS, EntityTypes.DATA_WAREHOUSE, EntityTypes.GROUPS]

function omitKeys(source: Record<string, any>, keys: string[]): Record<string, any> {
    return Object.fromEntries(Object.entries(source).filter(([key]) => !keys.includes(key)))
}

export function templateVariableStepToSeries(step: TemplateVariableStep | undefined): SeriesNode[] {
    return step ? [templateStepToSeriesNode(step)] : []
}

function templateStepToSeriesNode(step: TemplateStep): SeriesNode {
    const shared = omitKeys(step, STEP_IDENTITY_KEYS)
    if (Array.isArray(shared.properties)) {
        shared.properties = convertPropertyGroupToProperties(shared.properties)
    }

    if (step.type === EntityTypes.ACTIONS) {
        return { ...shared, kind: NodeKind.ActionsNode, id: toActionId(step.id) } as SeriesNode
    }
    if (step.type === EntityTypes.GROUPS) {
        return {
            ...shared,
            kind: NodeKind.GroupNode,
            nodes: (step.nestedFilters ?? []).map(templateStepToSeriesNode),
        } as SeriesNode
    }
    if (step.type === EntityTypes.DATA_WAREHOUSE) {
        // The table name is the node's key, and a step may carry it only as its `id`.
        const tableName = shared.table_name ?? (typeof step.id === 'string' ? step.id : undefined)
        return {
            ...shared,
            kind: NodeKind.DataWarehouseNode,
            table_name: tableName,
            id: tableName ?? step.id,
        } as SeriesNode
    }
    return { ...shared, kind: NodeKind.EventsNode, event: step.id } as SeriesNode
}

export function seriesToTemplateVariableStep(series: SeriesNode[]): TemplateVariableStep | undefined {
    for (const type of TEMPLATE_STEP_TYPES) {
        const order = series.findIndex((node) => seriesNodeEntityType(node) === type)
        if (order >= 0) {
            return seriesNodeToTemplateStep(series[order], order)
        }
    }
    return undefined
}

function seriesNodeToTemplateStep(node: SeriesNode, order: number): TemplateStep {
    let id: string | number | null = null
    if (isEventsSeriesNode(node)) {
        id = node.event ?? null
    } else if (isActionsSeriesNode(node)) {
        id = node.id
    } else if (isWarehouseSeriesNode(node)) {
        id = node.table_name
    }

    return {
        ...omitKeys(node, NODE_IDENTITY_KEYS),
        ...(isGroupSeriesNode(node)
            ? { nestedFilters: node.nodes.map((nested, nestedOrder) => seriesNodeToTemplateStep(nested, nestedOrder)) }
            : {}),
        id,
        type: seriesNodeEntityType(node),
        order,
    } as TemplateStep
}