: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'
import api from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { teamLogic } from 'scenes/teamLogic'
import type { AnyPropertyFilter } from '~/types'
import { clusteringConfigLogic } from './clusteringConfigLogic'
import type { ClusteringConfig } from './clusteringConfigLogic'
export interface ClusteringRunParams {
analysis_level: 'trace' | 'generation' | 'evaluation'
lookback_days: number
max_samples: number
embedding_normalization: 'none' | 'l2'
dimensionality_reduction_method: 'none' | 'umap' | 'pca'
dimensionality_reduction_ndims: number
clustering_method: 'hdbscan' | 'kmeans'
// HDBSCAN params
min_cluster_size_fraction: number
hdbscan_min_samples: number
// K-means params
kmeans_min_k: number
kmeans_max_k: number
run_label: string
// Visualization params
visualization_method: 'umap' | 'pca' | 'tsne'
// Event filters - property filters to scope which traces are included
event_filters: AnyPropertyFilter[]
}
export interface ClusteringRunResponse {
workflow_id: string
status: string
parameters: ClusteringRunParams & { team_id: number }
}
export const DEFAULT_CLUSTERING_PARAMS: ClusteringRunParams = {
analysis_level: 'trace',
lookback_days: 7,
max_samples: 1000,
embedding_normalization: 'l2',
dimensionality_reduction_method: 'umap',
dimensionality_reduction_ndims: 100,
clustering_method: 'hdbscan',
min_cluster_size_fraction: 0.02,
hdbscan_min_samples: 5,
kmeans_min_k: 2,
kmeans_max_k: 10,
run_label: '',
visualization_method: 'umap',
event_filters: [],
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface clustersAdminLogicValues {
config: ClusteringConfig // clusteringConfigLogic
configLoading: boolean // clusteringConfigLogic
currentTeamIdStrict: number | string // teamLogic
clusteringRun: ClusteringRunResponse | null
clusteringRunLoading: boolean
isModalOpen: boolean
isRunning: boolean
params: ClusteringRunParams
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface clustersAdminLogicActions {
closeModal: () => {
value: true
}
openModal: () => {
value: true
}
resetParams: () => {
value: true
}
setParams: (params: Partial<ClusteringRunParams>) => {
params: Partial<ClusteringRunParams>
}
triggerClusteringRun: () => any
triggerClusteringRunFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
triggerClusteringRunSuccess: (
clusteringRun: ClusteringRunResponse,
payload?: any
) => {
clusteringRun: ClusteringRunResponse
payload?: any
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface clustersAdminLogicMeta {
__keaTypeGenInternalSelectorTypes: {
isRunning: (clusteringRunLoading: boolean) => boolean
}
}
export type clustersAdminLogicType = MakeLogicType<
clustersAdminLogicValues,
clustersAdminLogicActions,
Record<string, any>,
clustersAdminLogicMeta
>
export const clustersAdminLogic = kea<clustersAdminLogicType>([
path(['products', 'ai_observability', 'frontend', 'clusters', 'clustersAdminLogic']),
connect({
values: [clusteringConfigLogic, ['config', 'configLoading'], teamLogic, ['currentTeamIdStrict']],
}),
actions({
openModal: true,
closeModal: true,
setParams: (params: Partial<ClusteringRunParams>) => ({ params }),
resetParams: true,
}),
reducers({
isModalOpen: [
false,
{
openModal: () => true,
closeModal: () => false,
triggerClusteringRunSuccess: () => false,
},
],
params: [
DEFAULT_CLUSTERING_PARAMS as ClusteringRunParams,
{
setParams: (state, { params }) => ({ ...state, ...params }),
resetParams: () => DEFAULT_CLUSTERING_PARAMS,
},
],
}),
loaders(({ values }) => ({
clusteringRun: [
null as ClusteringRunResponse | null,
{
triggerClusteringRun: async () => {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use llmAnalyticsClusteringRunsCreate() from 'products/ai_observability/frontend/generated/api' instead.
const response = await api.create(
`api/projects/${values.currentTeamIdStrict}/llm_analytics/clustering_runs`,
values.params
)
return response as ClusteringRunResponse
},
},
],
})),
selectors({
isRunning: [(s) => [s.clusteringRunLoading], (loading: boolean): boolean => loading],
}),
listeners(({ actions, values }) => ({
openModal: () => {
// Only sync event_filters once config has actually loaded from the API
// (created_at is empty in the initial state before loadConfig completes)
if (values.config?.created_at && !values.configLoading) {
const savedFilters = values.config.event_filters ?? []
actions.setParams({ event_filters: savedFilters })
}
},
triggerClusteringRun: () => {
posthog.capture('llma clusters admin run triggered', {
level: values.params.analysis_level,
method: values.params.clustering_method,
normalization: values.params.embedding_normalization,
lookback_days: values.params.lookback_days,
})
},
triggerClusteringRunSuccess: ({ clusteringRun }) => {
lemonToast.success(`Clustering workflow started`, {
toastId: `clustering-run-${clusteringRun?.workflow_id}`,
button: {
label: 'Copy workflow ID',
action: () => {
void navigator.clipboard.writeText(clusteringRun?.workflow_id || '')
},
},
})
actions.resetParams()
},
})),
])