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.

2.7 KB
Raw Blame
import { MakeLogicType, connect, kea, key, path, props, selectors } from 'kea'

import { PropertyFilterType } from '~/types'

import { logsViewerDataLogic } from './components/LogsViewer/data/logsViewerDataLogic'
import type { ParsedLogMessage } from './types'

export interface AttributeBreakdownLogicProps {
    attribute: string
    type: PropertyFilterType
    id: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface attributeBreakdownLogicValues {
    logs: ParsedLogMessage[] // logsViewerDataLogic
    attributeValues: string[]
    breakdowns: {
        [key: string]: number
    }
    logCount: number
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface attributeBreakdownLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        logCount: (logs: ParsedLogMessage[]) => number
        attributeValues: (logs: ParsedLogMessage[]) => string[]
        breakdowns: (attributeValues: string[]) => {
            [key: string]: number
        }
    }
}

export type attributeBreakdownLogicType = MakeLogicType<
    attributeBreakdownLogicValues,
    {},
    AttributeBreakdownLogicProps,
    attributeBreakdownLogicMeta
>

export const attributeBreakdownLogic = kea<attributeBreakdownLogicType>([
    props({} as AttributeBreakdownLogicProps),
    key((props) => `${props.id}-${props.type}-${props.attribute}`),
    path((key) => ['products', 'logs', 'frontend', 'logsAttributeBreakdownsLogic', key]),

    connect((props: AttributeBreakdownLogicProps) => ({
        values: [logsViewerDataLogic({ id: props.id }), ['parsedLogs as logs']],
    })),

    selectors(({ props }) => ({
        logCount: [(s) => [s.logs], (logs: import('./types').ParsedLogMessage[]): number => logs.length],
        attributeValues: [
            (s) => [s.logs],
            (logs: import('./types').ParsedLogMessage[]): string[] => {
                let attributesKey: 'attributes' | 'resource_attributes' = 'attributes'
                if (props.type === PropertyFilterType.LogResourceAttribute) {
                    attributesKey = 'resource_attributes'
                }
                return logs
                    .filter((l) => props.attribute in l[attributesKey])
                    .map((l) => l[attributesKey][props.attribute])
            },
        ],
        breakdowns: [
            (s) => [s.attributeValues],
            (attributeValues: string[]) =>
                attributeValues.reduce(
                    (acc, value) => {
                        acc[value] = (acc[value] || 0) + 1
                        return acc
                    },
                    {} as {
                        [key: string]: number
                    }
                ),
        ],
    })),
])