: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, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import api, { ApiError } from 'lib/api'
import { healthSummaryLogic } from 'lib/components/HelpMenu/healthSummaryLogic'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { sceneConfigurations } from 'scenes/scenes'
import { Scene } from 'scenes/sceneTypes'
import { teamLogic } from 'scenes/teamLogic'
import { Breadcrumb } from '~/types'
import { CATEGORY_ORDER, HEALTH_CATEGORY_CONFIG, categoryForKind } from './healthCategories'
import type { CategoryHealthSummary, HealthIssue, HealthIssueSeverity } from './types'
import { REFRESH_COOLDOWN_MS, REFRESH_POLL_COUNT, REFRESH_POLL_INTERVAL_MS, SEVERITY_ORDER } from './types'
export interface HealthIssuesResponse {
results: HealthIssue[]
count: number
next?: string | null
previous?: string | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface healthSceneLogicValues {
currentTeamIdStrict: number | string // teamLogic
breadcrumbs: Breadcrumb[]
categorySummaries: CategoryHealthSummary[]
healthIssues: HealthIssuesResponse | null
healthIssuesLoading: boolean
isManualRefresh: boolean
isRefreshInFlight: boolean
issues: HealthIssue[]
nextRefreshAvailableAt: number | null
showDismissed: boolean
totalCount: number
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface healthSceneLogicActions {
clearRefreshInFlight: () => {
value: true
}
dismissIssue: (id: string) => {
id: string
}
loadHealthIssues: () => any
loadHealthIssuesFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadHealthIssuesSuccess: (
healthIssues: HealthIssuesResponse | null,
payload?: any
) => {
healthIssues: HealthIssuesResponse | null
payload?: any
}
refreshHealthData: (isManual?: boolean) => {
isManual: boolean
}
resetManualRefresh: () => {
value: true
}
setNextRefreshAvailableAt: (timestamp: number | null) => {
timestamp: number | null
}
setShowDismissed: (show: boolean) => {
show: boolean
}
snoozeIssue: (
id: string,
duration: string
) => {
duration: string
id: string
}
undismissIssue: (id: string) => {
id: string
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface healthSceneLogicMeta {
__keaTypeGenInternalSelectorTypes: {
issues: (healthIssues: HealthIssuesResponse | null) => HealthIssue[]
totalCount: (healthIssues: HealthIssuesResponse | null) => number
categorySummaries: (issues: HealthIssue[]) => CategoryHealthSummary[]
}
}
export type healthSceneLogicType = MakeLogicType<
healthSceneLogicValues,
healthSceneLogicActions,
Record<string, any>,
healthSceneLogicMeta
>
export const healthSceneLogic = kea<healthSceneLogicType>([
path(['scenes', 'health', 'healthSceneLogic']),
connect({
values: [teamLogic, ['currentTeamIdStrict']],
}),
actions({
setShowDismissed: (show: boolean) => ({ show }),
snoozeIssue: (id: string, duration: string) => ({ id, duration }),
dismissIssue: (id: string) => ({ id }),
undismissIssue: (id: string) => ({ id }),
refreshHealthData: (isManual: boolean = true) => ({ isManual }),
resetManualRefresh: true,
setNextRefreshAvailableAt: (timestamp: number | null) => ({ timestamp }),
clearRefreshInFlight: true,
}),
reducers({
showDismissed: [
false,
{
setShowDismissed: (_, { show }) => show,
},
],
isManualRefresh: [
false,
{
refreshHealthData: (_, { isManual }) => isManual,
resetManualRefresh: () => false,
},
],
nextRefreshAvailableAt: [
null as number | null,
{ persist: true },
{
setNextRefreshAvailableAt: (_, { timestamp }) => timestamp,
},
],
isRefreshInFlight: [
false,
{
refreshHealthData: (state, { isManual }) => (isManual ? true : state),
clearRefreshInFlight: () => false,
},
],
}),
loaders(({ values }) => ({
healthIssues: [
null as HealthIssuesResponse | null,
{
loadHealthIssues: async (): Promise<HealthIssuesResponse | null> => {
const params: Record<string, string> = { status: 'active' }
if (!values.showDismissed) {
params.dismissed = 'false'
}
const queryString = new URLSearchParams(params).toString()
const url = `api/projects/${values.currentTeamIdStrict}/health_issues/?${queryString}`
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.
return await api.get(url)
},
},
],
})),
selectors({
issues: [
(s) => [s.healthIssues],
(healthIssues: HealthIssuesResponse | null): HealthIssue[] => healthIssues?.results ?? [],
],
totalCount: [
(s) => [s.healthIssues],
(healthIssues: HealthIssuesResponse | null): number => healthIssues?.count ?? 0,
],
categorySummaries: [
(s) => [s.issues],
(issues: HealthIssue[]): CategoryHealthSummary[] => {
const countByCategory: Record<string, number> = {}
const worstByCategory: Record<string, HealthIssueSeverity> = {}
for (const issue of issues) {
const category = categoryForKind(issue.kind)
countByCategory[category] = (countByCategory[category] ?? 0) + 1
const current = worstByCategory[category]
if (!current || SEVERITY_ORDER.indexOf(issue.severity) < SEVERITY_ORDER.indexOf(current)) {
worstByCategory[category] = issue.severity
}
}
const summaries: CategoryHealthSummary[] = []
for (const category of CATEGORY_ORDER) {
if (category === 'other') {
continue
}
const count = countByCategory[category] ?? 0
const config = HEALTH_CATEGORY_CONFIG[category]
if (!config.showInSummary && count === 0) {
continue
}
summaries.push({
category,
issueCount: count,
worstSeverity: worstByCategory[category] ?? null,
})
}
return summaries
},
],
breadcrumbs: [
() => [],
(): Breadcrumb[] => [
{
key: Scene.Health,
name: sceneConfigurations[Scene.Health].name,
iconType: sceneConfigurations[Scene.Health].iconType || 'default_icon_type',
},
],
],
}),
listeners(({ actions, values }) => ({
refreshHealthData: async ({ isManual }, breakpoint) => {
const url = `api/projects/${values.currentTeamIdStrict}/health_issues/refresh/`
try {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.
const response = await api.create<{
scheduled_kinds: string[]
kinds_failed: string[]
team_id: number
}>(url)
breakpoint()
actions.setNextRefreshAvailableAt(Date.now() + REFRESH_COOLDOWN_MS)
if ((response?.scheduled_kinds ?? []).length === 0) {
actions.clearRefreshInFlight()
actions.resetManualRefresh()
if (isManual) {
lemonToast.info('No health checks are registered for this project.')
}
return
}
if (isManual) {
lemonToast.success('Refreshing health checks...', { autoClose: 2000 })
}
for (let i = 0; i < REFRESH_POLL_COUNT; i++) {
await breakpoint(REFRESH_POLL_INTERVAL_MS)
actions.loadHealthIssues()
}
actions.clearRefreshInFlight()
} catch (error: unknown) {
actions.clearRefreshInFlight()
actions.resetManualRefresh()
if (error instanceof ApiError && error.status === 429) {
const retryAfterSeconds = Number(error.headers?.get('Retry-After'))
if (!isNaN(retryAfterSeconds) && retryAfterSeconds > 0) {
actions.setNextRefreshAvailableAt(Date.now() + retryAfterSeconds * 1000)
}
if (isManual) {
lemonToast.warning(`Refresh available again ${error.formattedRetryAfter ?? 'in a few minutes'}`)
}
} else if (isManual) {
lemonToast.error('Failed to refresh health checks')
}
}
},
setNextRefreshAvailableAt: async ({ timestamp }, breakpoint) => {
if (timestamp === null) {
return
}
const delay = timestamp - Date.now()
if (delay <= 0) {
actions.setNextRefreshAvailableAt(null)
return
}
await breakpoint(delay)
actions.setNextRefreshAvailableAt(null)
},
loadHealthIssuesSuccess: () => {
if (values.isManualRefresh) {
lemonToast.success('Health data refreshed', { autoClose: 1500 })
actions.resetManualRefresh()
}
},
setShowDismissed: () => {
actions.loadHealthIssues()
},
snoozeIssue: async ({ id, duration }) => {
try {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
await api.update(`api/projects/${values.currentTeamIdStrict}/health_issues/${id}/`, {
snoozed_until: duration,
})
actions.loadHealthIssues()
healthSummaryLogic.actions.loadHealthSummary()
} catch {
lemonToast.error("Couldn't snooze this issue. Try again.")
}
},
dismissIssue: async ({ id }) => {
try {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
await api.update(`api/projects/${values.currentTeamIdStrict}/health_issues/${id}/`, {
dismissed: true,
})
actions.loadHealthIssues()
healthSummaryLogic.actions.loadHealthSummary()
} catch {
lemonToast.error('Failed to dismiss issue')
}
},
undismissIssue: async ({ id }) => {
try {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
await api.update(`api/projects/${values.currentTeamIdStrict}/health_issues/${id}/`, {
dismissed: false,
})
actions.loadHealthIssues()
healthSummaryLogic.actions.loadHealthSummary()
} catch {
lemonToast.error('Failed to undismiss issue')
}
},
})),
afterMount(({ actions, values }) => {
// Only load the latest results here. Health checks are re-evaluated on their own daily
// schedule, and the refresh endpoint is throttled to one call per team every 5 minutes —
// auto-firing it on every mount just produced 429 storms across a team's users without
// giving them fresher data. Users can still trigger a re-run via the manual refresh button.
actions.loadHealthIssues()
// Restore the cooldown countdown for the manual refresh button if one is still active.
if (values.nextRefreshAvailableAt !== null) {
actions.setNextRefreshAvailableAt(values.nextRefreshAvailableAt)
}
}),
])