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.

21.9 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'

import { dayjs } from 'lib/dayjs'
import { dateStringToDayJs } from 'lib/utils/dateFilters'
import { teamLogic } from 'scenes/teamLogic'

import {
    FilterLogicalOperator,
    LogPropertyFilter,
    PropertyFilterType,
    PropertyOperator,
    UniversalFiltersGroup,
} from '~/types'

import { logsViewerConfigLogic } from 'products/logs/frontend/components/LogsViewer/config/logsViewerConfigLogic'
import {
    SERVICE_NAME_FILTER,
    SEVERITY_LEVEL_FILTER,
    setFacetIncluded,
} from 'products/logs/frontend/components/LogsViewer/FacetRail/facetFilters'
import { logsViewerFiltersLogic } from 'products/logs/frontend/components/LogsViewer/Filters/logsViewerFiltersLogic'
import { logsPatternsCreate, logsPatternsDiffCreate } from 'products/logs/frontend/generated/api'
import type {
    _LogPatternApi,
    _LogPropertyFilterApi,
    _LogsPatternsDiffResponseApi,
    _LogsPatternsResponseApi,
} from 'products/logs/frontend/generated/api.schemas'

import type { DateRange } from '../../../../../frontend/src/queries/schema/schema-general'
import type { LogsViewerViewMode, PatternsBaselineMode } from '../LogsViewer/config/logsViewerConfigLogic'
import type { LogsViewerFilters } from '../LogsViewer/config/types'

export interface LogsPatternsLogicProps {
    id: string
}

// The severity filter is an exact match on these six buckets; a sampled severity outside them
// (e.g. "notice") can't be expressed, so applying the filter would silently exclude those lines.
const CANONICAL_SEVERITIES = ['trace', 'debug', 'info', 'warn', 'error', 'fatal']

// Mirrors the miner's LOGS_PATTERNS_MAX_SERVICES default: a services list this long may have
// been truncated at the cap, so filtering by it could exclude services the pattern also hits.
// severity_counts has no cap, so it never needs this guard.
const SERVICES_LIST_CAP = 4

// Explicit baseline window for 'preceding' mode: the same-length window ending where the
// current one starts. Computed at request time (not memoized) because a relative range like
// "-1h" resolves against "now" — a cached computation would drift away from the current
// window the backend resolves at query time. 'lastWeek' sends no baseline: the backend's
// default is the current window shifted back one week, from its own resolved bounds.
export function precedingDateRange(utcDateRange: { date_from?: string | null; date_to?: string | null }): {
    date_from: string
    date_to: string
} {
    const from = dateStringToDayJs(utcDateRange.date_from ?? null) ?? dayjs().subtract(1, 'hour')
    const to = dateStringToDayJs(utcDateRange.date_to ?? null) ?? dayjs()
    const windowMs = Math.max(to.diff(from), 0)
    return {
        date_from: from.subtract(windowMs, 'millisecond').toISOString(),
        date_to: from.toISOString(),
    }
}

const EMPTY_RESPONSE: _LogsPatternsResponseApi = {
    patterns: [],
    scanned_count: 0,
    total_count: 0,
    sampled: false,
    sample_coverage_pct: 100,
    sparkline_buckets: [],
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface logsPatternsLogicValues {
    baselineMode: PatternsBaselineMode // logsViewerConfigLogic
    compareEnabled: boolean // logsViewerConfigLogic
    viewMode: LogsViewerViewMode // logsViewerConfigLogic
    filters: LogsViewerFilters // logsViewerFiltersLogic
    personId: string | undefined // logsViewerFiltersLogic
    queryFilterGroup: UniversalFiltersGroup // logsViewerFiltersLogic
    sessionId: string | undefined // logsViewerFiltersLogic
    utcDateRange: {
        date_from: string | null | undefined
        date_to: string | null | undefined
        explicitDate: boolean | null | undefined
    } // logsViewerFiltersLogic
    currentTeamId: number | null // teamLogic
    diffError: string | null
    diffResponse: _LogsPatternsDiffResponseApi | null
    diffResponseLoading: boolean
    mineError: string | null
    patterns: _LogPatternApi[]
    patternsError: string | null
    patternsQueryBody: {
        dateRange: {
            date_from: string | null | undefined
            date_to: string | null | undefined
            explicitDate: boolean | null | undefined
        }
        filterGroup: _LogPropertyFilterApi[]
        personId: string | undefined
        searchTerm: string | undefined
        sessionId: string | undefined
    }
    patternsResponse: _LogsPatternsResponseApi
    patternsResponseLoading: boolean
    sparklineLabels: string[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface logsPatternsLogicActions {
    setBaselineMode: (mode: PatternsBaselineMode) => {
        mode: PatternsBaselineMode
    } // logsViewerConfigLogic
    setCompareEnabled: (enabled: boolean) => {
        enabled: boolean
    } // logsViewerConfigLogic
    setViewMode: (viewMode: LogsViewerViewMode) => {
        viewMode: LogsViewerViewMode
    } // logsViewerConfigLogic
    bumpFacetRefresh: () => {
        value: true
    } // logsViewerFiltersLogic
    setDateRange: (dateRange: DateRange) => {
        dateRange: DateRange
    } // logsViewerFiltersLogic
    setFilterGroup: (
        filterGroup: UniversalFiltersGroup,
        openFilterOnInsert?: boolean | undefined
    ) => {
        filterGroup: UniversalFiltersGroup
        openFilterOnInsert: boolean
    } // logsViewerFiltersLogic
    setFilters: (
        filters: Partial<LogsViewerFilters>,
        pushToHistory?: boolean | undefined
    ) => {
        filters: Partial<LogsViewerFilters>
        pushToHistory: boolean
    } // logsViewerFiltersLogic
    setPinnedFilters: (pinnedFilters: UniversalFiltersGroup | undefined) => {
        pinnedFilters: UniversalFiltersGroup | undefined
    } // logsViewerFiltersLogic
    setSearchTerm: (searchTerm: string | undefined) => {
        searchTerm: string | undefined
    } // logsViewerFiltersLogic
    zoomDateRange: (multiplier: number) => {
        multiplier: number
    } // logsViewerFiltersLogic
    loadDiff: (debounceMs?: number) => number
    loadDiffFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadDiffSuccess: (
        diffResponse: _LogsPatternsDiffResponseApi,
        payload?: number
    ) => {
        diffResponse: _LogsPatternsDiffResponseApi
        payload?: number
    }
    loadPatterns: (debounceMs?: number) => number
    loadPatternsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadPatternsSuccess: (
        patternsResponse: _LogsPatternsResponseApi,
        payload?: number
    ) => {
        patternsResponse: _LogsPatternsResponseApi
        payload?: number
    }
    viewMatchingLogs: (pattern: _LogPatternApi) => {
        pattern: _LogPatternApi
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface logsPatternsLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        patternsError: (compareEnabled: boolean, mineError: string | null, diffError: string | null) => string | null
        patternsQueryBody: (
            filters: LogsViewerFilters,
            utcDateRange: {
                date_from: string | null | undefined
                date_to: string | null | undefined
                explicitDate: boolean | null | undefined
            },
            queryFilterGroup: UniversalFiltersGroup,
            personId: string | undefined,
            sessionId: string | undefined
        ) => {
            dateRange: {
                date_from: string | null | undefined
                date_to: string | null | undefined
                explicitDate: boolean | null | undefined
            }
            filterGroup: _LogPropertyFilterApi[]
            personId: string | undefined
            searchTerm: string | undefined
            sessionId: string | undefined
        }
        patterns: (patternsResponse: _LogsPatternsResponseApi) => _LogPatternApi[]
        sparklineLabels: (patternsResponse: _LogsPatternsResponseApi) => string[]
    }
}

export type logsPatternsLogicType = MakeLogicType<
    logsPatternsLogicValues,
    logsPatternsLogicActions,
    LogsPatternsLogicProps,
    logsPatternsLogicMeta
>

// Keyed by the Viewer's `id`: the logic mounts only while the Patterns mode is active (the
// Viewer conditionally renders <LogsPatterns/>), so loading on mount + reloading on the
// shared filter actions never runs the heavier patterns query while the user is in Logs mode.
export const logsPatternsLogic = kea<logsPatternsLogicType>([
    props({ id: 'default' } as LogsPatternsLogicProps),
    key((props) => props.id),
    path((key) => ['products', 'logs', 'frontend', 'components', 'LogsPatterns', 'logsPatternsLogic', key]),

    connect((props: LogsPatternsLogicProps) => ({
        values: [
            teamLogic,
            ['currentTeamId'],
            logsViewerFiltersLogic({ id: props.id }),
            ['filters', 'utcDateRange', 'queryFilterGroup', 'personId', 'sessionId'],
            logsViewerConfigLogic({ id: props.id }),
            ['viewMode', 'compareEnabled', 'baselineMode'],
        ],
        actions: [
            logsViewerFiltersLogic({ id: props.id }),
            [
                'setDateRange',
                'zoomDateRange',
                'setSearchTerm',
                'setFilters',
                'setFilterGroup',
                'setPinnedFilters',
                'bumpFacetRefresh',
            ],
            logsViewerConfigLogic({ id: props.id }),
            ['setViewMode', 'setCompareEnabled', 'setBaselineMode'],
        ],
    })),

    actions({
        viewMatchingLogs: (pattern: _LogPatternApi) => ({ pattern }),
    }),

    loaders(({ values }) => ({
        patternsResponse: [
            EMPTY_RESPONSE,
            {
                loadPatterns: async (debounceMs: number = 0, breakpoint) => {
                    await breakpoint(debounceMs)
                    const response = await logsPatternsCreate(String(values.currentTeamId), {
                        query: values.patternsQueryBody,
                    })
                    // A superseded call must not land its (stale) response after the newer one.
                    breakpoint()
                    return response
                },
            },
        ],
        diffResponse: [
            null as _LogsPatternsDiffResponseApi | null,
            {
                loadDiff: async (debounceMs: number = 0, breakpoint) => {
                    await breakpoint(debounceMs)
                    const response = await logsPatternsDiffCreate(String(values.currentTeamId), {
                        query: values.patternsQueryBody,
                        baselineDateRange:
                            values.baselineMode === 'preceding' ? precedingDateRange(values.utcDateRange) : undefined,
                    })
                    breakpoint()
                    return response
                },
            },
        ],
    })),

    // A failed mine (e.g. the sampling query exceeding its execution budget) must surface as
    // an error, not render as "no patterns found" — that would misrepresent the data. The two
    // modes track their own error: the loaders run independently (toggling compare off fires a
    // plain mine without cancelling an in-flight diff), so a shared error would let a stale diff
    // failure clobber a successful plain-mine table's empty state.
    reducers({
        mineError: [
            null as string | null,
            {
                loadPatterns: () => null,
                loadPatternsSuccess: () => null,
                loadPatternsFailure: (_, { error }) => error ?? 'Pattern analysis failed',
            },
        ],
        diffError: [
            null as string | null,
            {
                loadDiff: () => null,
                loadDiffSuccess: () => null,
                loadDiffFailure: (_, { error }) => error ?? 'Pattern comparison failed',
            },
        ],
    }),

    selectors({
        // Surface only the active mode's error. The loaders run independently, so a stale diff
        // failure landing after the user toggled compare off must not overwrite the successful
        // plain-mine table's empty state (and vice versa).
        patternsError: [
            (s) => [s.compareEnabled, s.mineError, s.diffError],
            (compareEnabled: boolean, mineError: string | null, diffError: string | null): string | null =>
                compareEnabled ? diffError : mineError,
        ],
        // The shared query body for both the mine and the diff — the diff must scope its two
        // windows with exactly the filters a plain mine would use, or compare mode would
        // silently answer a different question than the table next to it.
        patternsQueryBody: [
            (s) => [s.filters, s.utcDateRange, s.queryFilterGroup, s.personId, s.sessionId],
            (
                filters: import('../LogsViewer/config/types').LogsViewerFilters,
                utcDateRange: {
                    date_from: string | null | undefined
                    date_to: string | null | undefined
                    explicitDate: boolean | null | undefined
                },
                queryFilterGroup: UniversalFiltersGroup,
                personId: string | undefined,
                sessionId: string | undefined
            ) => ({
                dateRange: utcDateRange,
                searchTerm: filters.searchTerm || undefined,
                // Scope mining to the same filters the Logs/sparkline queries use —
                // `queryFilterGroup` folds in any pinned filters from an embedded viewer
                // (person/trace logs), so a scoped viewer can't mine project-wide patterns.
                filterGroup: queryFilterGroup as unknown as _LogPropertyFilterApi[],
                // Person and session scoping travel as their own fields, not in the filter
                // group, so they have to be sent explicitly here too.
                personId,
                sessionId,
            }),
        ],
        patterns: [
            (s) => [s.patternsResponse],
            (response: _LogsPatternsResponseApi): _LogPatternApi[] => response.patterns,
        ],
        // Hover labels for the per-pattern sparklines, aligned with each pattern's `sparkline`
        // values. Buckets under a day apart show time-of-day; wider windows include the date.
        sparklineLabels: [
            (s) => [s.patternsResponse],
            (response: _LogsPatternsResponseApi): string[] => {
                const buckets = response.sparkline_buckets
                if (!buckets.length) {
                    return []
                }
                const first = dayjs(buckets[0].start)
                const last = dayjs(buckets[buckets.length - 1].end)
                const format = last.diff(first, 'hour') >= 24 ? 'MMM D HH:mm' : 'HH:mm'
                return buckets.map((b) => `${dayjs(b.start).format(format)} – ${dayjs(b.end).format(format)}`)
            },
        ],
    }),

    listeners(({ actions, values }) => {
        // Debounced so a multi-filter change or search typing collapses into one request —
        // kea's breakpoint cancels superseded loads before the fetch fires. Only mine while
        // Patterns is the active view: the pivot below flips to Logs mode before writing its
        // filter, so its own `setFilterGroup` (and any deferred unmount) can't queue a stray mine.
        const reload = (): void => {
            if (values.viewMode !== 'patterns') {
                return
            }
            if (values.compareEnabled) {
                actions.loadDiff(300)
            } else {
                actions.loadPatterns(300)
            }
        }
        return {
            setDateRange: reload,
            zoomDateRange: reload,
            setSearchTerm: reload,
            setFilters: reload,
            setFilterGroup: reload,
            setPinnedFilters: reload,

            // The refresh button fires `bumpFacetRefresh` through the shared filters logic. Re-mine
            // immediately (no debounce) — it is a deliberate click, not typing — but only while
            // Patterns is the active lens, and diff or plain to match compare mode.
            bumpFacetRefresh: () => {
                if (values.viewMode !== 'patterns') {
                    return
                }
                if (values.compareEnabled) {
                    actions.loadDiff()
                } else {
                    actions.loadPatterns()
                }
            },

            // Entering compare mode always diffs fresh; leaving it re-mines because the
            // filters may have changed while the plain response sat unused.
            setCompareEnabled: ({ enabled }) => {
                if (enabled) {
                    actions.loadDiff()
                } else {
                    actions.loadPatterns()
                }
            },
            setBaselineMode: () => {
                if (values.compareEnabled) {
                    actions.loadDiff()
                }
            },

            // Pivot to the Logs view scoped to this pattern. The predicate lands in the shared
            // filterGroup like any user-added filter — visible and removable in the filter bar,
            // never hidden state. Prefer the validated regex; fall back to plain-text matching
            // on the template's literal content when validation withheld it.
            viewMatchingLogs: ({ pattern }) => {
                const exactMatch = !!pattern.match_patterns?.length && pattern.pattern_version != null
                const predicate: LogPropertyFilter | null = exactMatch
                    ? {
                          key: 'pattern',
                          value: pattern.match_patterns!,
                          operator: PropertyOperator.Exact,
                          type: PropertyFilterType.Log,
                      }
                    : pattern.match_regex
                      ? {
                            key: 'message',
                            value: pattern.match_regex,
                            operator: PropertyOperator.Regex,
                            type: PropertyFilterType.Log,
                        }
                      : pattern.match_literal
                        ? {
                              key: 'message',
                              value: pattern.match_literal,
                              operator: PropertyOperator.IContains,
                              type: PropertyFilterType.Log,
                          }
                        : null
                if (!predicate) {
                    return
                }
                const predicates: LogPropertyFilter[] = exactMatch
                    ? [
                          predicate,
                          {
                              key: 'pattern_version',
                              value: pattern.pattern_version!,
                              operator: PropertyOperator.Exact,
                              type: PropertyFilterType.Log,
                          },
                      ]
                    : [predicate]
                const group = values.filters.filterGroup
                const inner = group.values[0] as UniversalFiltersGroup | undefined
                const newGroup: UniversalFiltersGroup =
                    inner && Array.isArray(inner.values)
                        ? {
                              ...group,
                              values: [
                                  { ...inner, values: [...inner.values, ...predicates] } as UniversalFiltersGroup,
                                  ...group.values.slice(1),
                              ],
                          }
                        : {
                              type: FilterLogicalOperator.And,
                              values: [
                                  { type: FilterLogicalOperator.And, values: predicates } as UniversalFiltersGroup,
                              ],
                          }
                // Leave Patterns mode first so the filter write below doesn't re-trigger a mine:
                // the `reload` guard sees Logs mode and bails on our own `setFilterGroup`.
                actions.setViewMode('logs')
                // Scope by every service and severity the sample saw: service_name is in the
                // table's sort key and severity is indexed, so these prune the scan the body regex
                // alone can't. Both are IN filters, so N values narrow just as validly as one, and
                // both land as visible chips the user can remove if the pattern exists beyond the
                // sample. Skipped only when the filter could silently exclude matching lines: a
                // cap-truncated services list, or a severity outside the canonical buckets. Written
                // into the same group as the pattern predicate so the drill-down is one filter change.
                let scopedGroup = newGroup
                if (!exactMatch && pattern.services.length > 0 && pattern.services.length < SERVICES_LIST_CAP) {
                    scopedGroup = setFacetIncluded(scopedGroup, SERVICE_NAME_FILTER, pattern.services)
                }
                const severities = Object.keys(pattern.severity_counts)
                if (!exactMatch && severities.length > 0 && severities.every((s) => CANONICAL_SEVERITIES.includes(s))) {
                    scopedGroup = setFacetIncluded(scopedGroup, SEVERITY_LEVEL_FILTER, severities)
                }
                actions.setFilterGroup(scopedGroup, false)
            },
        }
    }),

    afterMount(({ actions, values }) => {
        // Compare state lives on the config logic and survives Logs↔Patterns lens switches,
        // so this logic can mount with compare already on — loading the plain mine then would
        // answer the wrong question.
        if (values.compareEnabled) {
            actions.loadDiff()
        } else {
            actions.loadPatterns()
        }
    }),
])