: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, key, listeners, path, props, reducers } from 'kea'
import { loaders } from 'kea-loaders'
import { teamLogic } from 'scenes/teamLogic'
import { UniversalFiltersGroup } from '~/types'
import {
LogsViewerGroupBy,
LogsViewerViewMode,
logsViewerConfigLogic,
} from 'products/logs/frontend/components/LogsViewer/config/logsViewerConfigLogic'
import { logsViewerDataLogic } from 'products/logs/frontend/components/LogsViewer/data/logsViewerDataLogic'
import { logsViewerFiltersLogic } from 'products/logs/frontend/components/LogsViewer/Filters/logsViewerFiltersLogic'
import { logsImpactCreate } from 'products/logs/frontend/generated/api'
import type { _LogsCountBodyApi, _LogsImpactResponseApi } from 'products/logs/frontend/generated/api.schemas'
import type { LogsQuery } from '../../../../../frontend/src/queries/schema/schema-general'
export interface LogsImpactLogicProps {
id: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface logsImpactLogicValues {
personId: string | undefined // logsViewerFiltersLogic
queryFilterGroup: UniversalFiltersGroup // logsViewerFiltersLogic
searchTerm: LogsQuery[] // logsViewerFiltersLogic
sessionId: string | undefined // logsViewerFiltersLogic
utcDateRange: {
date_from: string | null | undefined
date_to: string | null | undefined
explicitDate: boolean | null | undefined
} // logsViewerFiltersLogic
currentTeamId: number | null // teamLogic
impact: _LogsImpactResponseApi | null
impactLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface logsImpactLogicActions {
setGroupBys: (groupBys: LogsViewerGroupBy[]) => {
groupBys: LogsViewerGroupBy[]
} // logsViewerConfigLogic
setViewMode: (viewMode: LogsViewerViewMode) => {
viewMode: LogsViewerViewMode
} // logsViewerConfigLogic
runQuery: (debounce?: number | undefined) => {
debounce: number | undefined
} // logsViewerDataLogic
loadImpact: (_: any) => any
loadImpactFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadImpactSuccess: (
impact: _LogsImpactResponseApi | null,
payload?: any
) => {
impact: _LogsImpactResponseApi | null
payload?: any
}
pivotToGroupBy: (dimension: LogsViewerGroupBy) => {
dimension: LogsViewerGroupBy
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface logsImpactLogicMeta {
key: string
}
export type logsImpactLogicType = MakeLogicType<
logsImpactLogicValues,
logsImpactLogicActions,
LogsImpactLogicProps,
logsImpactLogicMeta
>
export const logsImpactLogic = kea<logsImpactLogicType>([
props({} as LogsImpactLogicProps),
key(({ id }) => id),
path((key) => ['products', 'logs', 'frontend', 'components', 'LogsViewer', 'logsImpactLogic', key]),
connect(({ id }: LogsImpactLogicProps) => ({
values: [
logsViewerFiltersLogic({ id }),
['utcDateRange', 'queryFilterGroup', 'searchTerm', 'personId', 'sessionId'],
teamLogic,
['currentTeamId'],
],
actions: [
logsViewerDataLogic({ id }),
['runQuery'],
logsViewerConfigLogic({ id }),
['setViewMode', 'setGroupBys'],
],
})),
actions({
pivotToGroupBy: (dimension: LogsViewerGroupBy) => ({ dimension }),
}),
loaders(({ cache, values }) => ({
impact: [
null as _LogsImpactResponseApi | null,
{
loadImpact: async (_, breakpoint) => {
// Person- and session-scoped viewers pin a scope that the impact query
// does not carry, so its counts would cover the whole project. Skip the
// query; `impact` stays null and the strip renders nothing.
if (values.personId || values.sessionId) {
return null
}
await breakpoint(300)
// The serializer models `filterGroup` as a flat filter list, but the endpoint
// also accepts the nested group the viewer holds (the view normalizes both),
// so the request body needs a cast the generated type can't express.
const query = {
dateRange: values.utcDateRange,
searchTerm: values.searchTerm,
filterGroup: values.queryFilterGroup,
} as unknown as _LogsCountBodyApi
// Re-adding the key aborts the superseded in-flight request, and unmount
// aborts the current one. A request is one-shot work, not background work,
// so a hidden tab must not abort it.
const controller = new AbortController()
cache.disposables.add(() => () => controller.abort(), 'impactRequest', {
pauseOnPageHidden: false,
})
let response: _LogsImpactResponseApi | null = null
try {
response = await logsImpactCreate(
String(values.currentTeamId),
{ query },
{ signal: controller.signal }
)
} catch {
// The strip is passive decoration: on failure show nothing rather than
// stale counts or the global error toast.
}
breakpoint()
return response
},
},
],
})),
reducers({
impact: {
// Clear on refresh so counts from the previous query never render next to the
// new query's results.
loadImpact: () => null,
},
}),
listeners(({ actions }) => ({
// runQuery is the viewer's execute-this-query entrypoint, so the strip refreshes with
// the query rather than coupling to the sparkline loader's schedule. Bursts (typing)
// collapse in loadImpact's own breakpoint.
runQuery: () => actions.loadImpact(null),
// Set the dimension before the view mode: logsGroupByLogic mounts with Group view and
// loads on mount, so switching first would run a query against the old group-bys.
pivotToGroupBy: ({ dimension }) => {
actions.setGroupBys([dimension])
actions.setViewMode('group')
},
})),
afterMount(({ actions }) => {
// The data logic usually mounts (and fires its initial runQuery) before this logic
// exists, so the first load has to be explicit.
actions.loadImpact(null)
}),
])