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.

3.3 KB
Raw Blame
import { defaultDataTableColumns } from '~/queries/nodes/DataTable/utils'
import { DataTableNode, EventsQuery, NodeKind } from '~/queries/schema/schema-general'
import { AnyPropertyFilter, PropertyFilterType, TeamPublicType, TeamType } from '~/types'

const EVENT_LOOKUP_FILTER = /^uuid = '[a-f0-9-]+'$/

export const getDefaultEventsSceneQuery = (properties?: AnyPropertyFilter[]): DataTableNode => ({
    kind: NodeKind.DataTableNode,
    full: true,
    source: {
        kind: NodeKind.EventsQuery,
        select: defaultDataTableColumns(NodeKind.EventsQuery),
        orderBy: ['timestamp DESC'],
        after: '-1h',
        ...(properties ? { properties } : {}),
    },
    propertiesViaUrl: true,
    showSavedQueries: true,
    showPersistentColumnConfigurator: true,
})

const WIDER_ACTIVITY_RANGES: Record<string, { after: '-24h' | '-7d' | '-30d'; label: string }> = {
    '-1h': { after: '-24h', label: 'Show last 24 hours' },
    '-24h': { after: '-7d', label: 'Show last 7 days' },
    '-7d': { after: '-30d', label: 'Show last 30 days' },
}

/**
 * The next wider range to offer when an activity list is empty, stepping 1h → 24h → 7d → 30d.
 * The scenes default to one hour on purpose (#35425, query memory limits), so an empty list is
 * often just a narrow window. Null for custom or bounded ranges, which the user chose themselves.
 */
export function widerActivityRange(
    after: string | null | undefined,
    before: string | null | undefined
): { after: '-24h' | '-7d' | '-30d'; label: string } | null {
    if (before || !after) {
        return null
    }
    return WIDER_ACTIVITY_RANGES[after] ?? null
}

// An event link opens the events scene with this query. isUnnamedEventLookup recognizes its filter. Change the two together.
export const getEventLookupQuery = (id: string): DataTableNode =>
    getDefaultEventsSceneQuery([
        { type: PropertyFilterType.HogQL, key: `uuid = '${id.replaceAll(/[^a-f0-9-]/g, '')}'`, value: null },
    ])

export function isUnnamedEventLookup(source: EventsQuery): boolean {
    const [filter, ...otherFilters] = source.properties ?? []
    return (
        !source.event &&
        !source.events?.length &&
        otherFilters.length === 0 &&
        filter?.type === PropertyFilterType.HogQL &&
        EVENT_LOOKUP_FILTER.test(filter.key)
    )
}

export function applyTestAccountFilter<T extends DataTableNode>(
    base: T,
    currentTeam: TeamType | TeamPublicType | null | undefined,
    filterTestAccountsDefault: boolean
): T {
    const hasTestAccountFilters = (currentTeam?.test_account_filters ?? []).length > 0
    return {
        ...base,
        source: {
            ...base.source,
            ...(hasTestAccountFilters ? { filterTestAccounts: filterTestAccountsDefault } : {}),
        },
    }
}

export const getDefaultSessionsSceneQuery = (properties?: AnyPropertyFilter[]): DataTableNode => ({
    kind: NodeKind.DataTableNode,
    full: true,
    source: {
        kind: NodeKind.SessionsQuery,
        select: defaultDataTableColumns(NodeKind.SessionsQuery),
        orderBy: ['$end_timestamp DESC NULLS FIRST'],
        after: '-1h',
        limit: 100,
        ...(properties ? { properties } : {}),
    },
    propertiesViaUrl: true,
    showSavedQueries: true,
    showPersistentColumnConfigurator: true,
    contextKey: 'activity-sessions',
})