: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, afterMount, connect, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'
import { teamLogic } from 'scenes/teamLogic'
import { FilterLogicalOperator, PropertyFilterType, PropertyOperator, UniversalFiltersGroup } from '~/types'
import { metricsOverviewRetrieve } from '../generated/api'
import type { _MetricsOverviewResponseApi } from '../generated/api.schemas'
import { canViewMetrics } from '../metricsAccess'
import { metricsSceneLogic } from '../metricsSceneLogic'
import { EMPTY_SERVICE_PATTERN, SERVICE_NAME_KEY, metricsViewerLogic } from './metricsViewerLogic'
// Long enough to keep the landing page cheap, short enough that "receiving
// metrics" flips to a warning within a minute of ingestion stopping.
export const OVERVIEW_REFRESH_MS = 30_000
// The scrape agent defaults to 15s scrapes and the SDKs flush every 10s, so a
// quarter hour of silence means a sender stopped, not a slow interval.
export const STALE_AFTER_MS = 15 * 60 * 1000
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface metricsOverviewLogicValues {
currentTeamId: number | null // teamLogic
overview: _MetricsOverviewResponseApi | null
overviewLoadFailed: boolean
overviewLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface metricsOverviewLogicActions {
setActiveTab: (activeTab: import('../metricsSceneLogic').MetricsSceneActiveTab) => {
activeTab: import('../metricsSceneLogic').MetricsSceneActiveTab
} // metricsSceneLogic
setFilterGroup: (filterGroup: UniversalFiltersGroup) => {
filterGroup: UniversalFiltersGroup
} // metricsViewerLogic
loadOverview: () => any
loadOverviewFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadOverviewSuccess: (
overview: _MetricsOverviewResponseApi | null,
payload?: any
) => {
overview: _MetricsOverviewResponseApi | null
payload?: any
}
viewService: (serviceName: string) => {
serviceName: string
}
}
export type metricsOverviewLogicType = MakeLogicType<metricsOverviewLogicValues, metricsOverviewLogicActions>
const serviceFilterGroup = (serviceName: string): UniversalFiltersGroup => {
// A sender that omits the `service.name` resource attribute lands with an empty
// service_name, which the table shows as "unknown". The viewer drops empty filter
// values so a half-typed chip cannot fire a query, so an exact match on "" would
// leave the viewer unfiltered while still showing a service chip. Match that group
// as an anchored regex, which survives the same pipeline.
const matcher = serviceName
? { value: [serviceName], operator: PropertyOperator.Exact }
: { value: [EMPTY_SERVICE_PATTERN], operator: PropertyOperator.Regex }
return {
type: FilterLogicalOperator.And,
values: [
{
type: FilterLogicalOperator.And,
values: [{ type: PropertyFilterType.MetricAttribute, key: SERVICE_NAME_KEY, ...matcher }],
},
],
}
}
export const metricsOverviewLogic = kea<metricsOverviewLogicType>([
path(['products', 'metrics', 'frontend', 'components', 'metricsOverviewLogic']),
connect(() => ({
values: [teamLogic, ['currentTeamId']],
actions: [metricsViewerLogic, ['setFilterGroup'], metricsSceneLogic, ['setActiveTab']],
})),
actions({
viewService: (serviceName: string) => ({ serviceName }),
}),
loaders(({ values }) => ({
overview: {
__default: null as _MetricsOverviewResponseApi | null,
loadOverview: async (): Promise<_MetricsOverviewResponseApi | null> => {
if (!canViewMetrics()) {
return values.overview
}
return await metricsOverviewRetrieve(String(values.currentTeamId))
},
},
})),
reducers({
// Only a success clears the flag. The poll and the retry button both call
// loadOverview, and clearing on each call would swap the error banner for the
// skeleton every 30 seconds while the endpoint keeps failing.
overviewLoadFailed: [
false,
{
loadOverviewFailure: () => true,
loadOverviewSuccess: () => false,
},
],
}),
listeners(({ actions }) => ({
// A service row click lands in the viewer with that service already
// selected. The viewer reuses the filter to scope its metric picker.
viewService: ({ serviceName }) => {
actions.setFilterGroup(serviceFilterGroup(serviceName))
actions.setActiveTab('viewer')
},
})),
afterMount(({ actions, cache }) => {
actions.loadOverview()
cache.disposables.add(() => {
const pollTimer = window.setInterval(() => actions.loadOverview(), OVERVIEW_REFRESH_MS)
return () => clearInterval(pollTimer)
})
}),
])