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.

4.6 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'

import api from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { teamLogic } from 'scenes/teamLogic'

import type { AnyPropertyFilter } from '~/types'

/** A filter is valid if it has a key set (not just an empty placeholder row). */
export function isValidFilter(f: AnyPropertyFilter): boolean {
    return 'key' in f && f.key !== undefined && f.key !== ''
}

export interface ClusteringConfig {
    event_filters: AnyPropertyFilter[]
    created_at: string
    updated_at: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface clusteringConfigLogicValues {
    currentTeamIdStrict: number | string // teamLogic
    config: ClusteringConfig
    configLoading: boolean
    isSettingsPanelOpen: boolean
    localEventFilters: AnyPropertyFilter[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface clusteringConfigLogicActions {
    closeSettingsPanel: () => {
        value: true
    }
    loadConfig: () => any
    loadConfigFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadConfigSuccess: (
        config: ClusteringConfig,
        payload?: any
    ) => {
        config: ClusteringConfig
        payload?: any
    }
    openSettingsPanel: () => {
        value: true
    }
    saveEventFilters: () => any
    saveEventFiltersFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    saveEventFiltersSuccess: (
        config: ClusteringConfig,
        payload?: any
    ) => {
        config: ClusteringConfig
        payload?: any
    }
    setLocalEventFilters: (filters: AnyPropertyFilter[]) => {
        filters: AnyPropertyFilter[]
    }
}

export type clusteringConfigLogicType = MakeLogicType<clusteringConfigLogicValues, clusteringConfigLogicActions>

export const clusteringConfigLogic = kea<clusteringConfigLogicType>([
    path(['products', 'ai_observability', 'frontend', 'clusters', 'clusteringConfigLogic']),
    connect(() => ({ values: [teamLogic, ['currentTeamIdStrict']] })),

    actions({
        openSettingsPanel: true,
        closeSettingsPanel: true,
        setLocalEventFilters: (filters: AnyPropertyFilter[]) => ({ filters }),
    }),

    loaders(({ values }) => ({
        config: [
            { event_filters: [], created_at: '', updated_at: '' } as ClusteringConfig,
            {
                loadConfig: async () => {
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use llmAnalyticsClusteringConfigList() from 'products/ai_observability/frontend/generated/api' instead.
                    const response = await api.get(
                        `api/projects/${values.currentTeamIdStrict}/llm_analytics/clustering_config/`
                    )
                    return response as ClusteringConfig
                },
                saveEventFilters: async () => {
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. llmAnalyticsClusteringConfigSetEventFiltersCreate() from 'products/ai_observability/frontend/generated/api' serves this route, but its generated types do not describe this call yet, so fix the endpoint's OpenAPI schema first.
                    const response = await api.create(
                        `api/projects/${values.currentTeamIdStrict}/llm_analytics/clustering_config/set_event_filters/`,
                        {
                            event_filters: values.localEventFilters,
                        }
                    )
                    return response as ClusteringConfig
                },
            },
        ],
    })),

    reducers({
        isSettingsPanelOpen: [
            false,
            {
                openSettingsPanel: () => true,
                closeSettingsPanel: () => false,
                saveEventFiltersSuccess: () => false,
            },
        ],
        localEventFilters: [
            [] as AnyPropertyFilter[],
            {
                setLocalEventFilters: (_, { filters }) => filters,
                loadConfigSuccess: (_, { config }) => config.event_filters,
            },
        ],
    }),

    listeners(() => ({
        saveEventFiltersSuccess: () => {
            lemonToast.success('Clustering filters saved')
        },
    })),

    afterMount(({ actions }) => {
        actions.loadConfig()
    }),
])