: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, events, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import api from 'lib/api'
import type { CohortType } from '../../types'
export interface CohortCalculationHistorySceneLogicProps {
cohortId: number
}
export interface CohortCalculationHistoryRecord {
id: number
team: number
cohort: number
filters: any
count: number | null
started_at: string
finished_at: string | null
queries: Array<{
query: string
query_id: string
query_ms: number
memory_mb: number
read_rows: number
written_rows: number
}>
error: string | null
total_query_ms: number
total_memory_mb: number
total_read_rows: number
total_written_rows: number
main_query: any
main_query_id: string | null
}
export interface CohortCalculationHistoryResponse {
results: CohortCalculationHistoryRecord[]
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 cohortCalculationHistorySceneLogicValues {
calculationHistory: CohortCalculationHistoryRecord[]
calculationHistoryResponse: CohortCalculationHistoryResponse
calculationHistoryResponseLoading: boolean
cohort: any
cohortLoading: boolean
cohortMissing: boolean
limit: number
page: number
totalRecords: number
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cohortCalculationHistorySceneLogicActions {
loadCalculationHistory: (_: any) => any
loadCalculationHistoryFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadCalculationHistorySuccess: (
calculationHistoryResponse: CohortCalculationHistoryResponse,
payload?: any
) => {
calculationHistoryResponse: CohortCalculationHistoryResponse
payload?: any
}
loadCohort: (_: any) => any
loadCohortFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadCohortSuccess: (
cohort: CohortType | null,
payload?: any
) => {
cohort: CohortType | null
payload?: any
}
setCohortMissing: () => {
value: true
}
setLimit: (limit: number) => {
limit: number
}
setPage: (page: number) => {
page: number
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cohortCalculationHistorySceneLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
calculationHistory: (
calculationHistoryResponse: CohortCalculationHistoryResponse
) => CohortCalculationHistoryRecord[]
totalRecords: (calculationHistoryResponse: CohortCalculationHistoryResponse) => number
}
}
export type cohortCalculationHistorySceneLogicType = MakeLogicType<
cohortCalculationHistorySceneLogicValues,
cohortCalculationHistorySceneLogicActions,
CohortCalculationHistorySceneLogicProps,
cohortCalculationHistorySceneLogicMeta
>
export const cohortCalculationHistorySceneLogic = kea<cohortCalculationHistorySceneLogicType>([
props({} as CohortCalculationHistorySceneLogicProps),
key(({ cohortId }) => String(cohortId)),
path((key) => ['scenes', 'cohorts', 'cohortCalculationHistorySceneLogic', key]),
actions({
setPage: (page: number) => ({ page }),
setLimit: (limit: number) => ({ limit }),
setCohortMissing: true,
}),
loaders(({ props, actions, values }) => ({
calculationHistoryResponse: [
{ results: [], count: 0, next: null, previous: null } as CohortCalculationHistoryResponse,
{
loadCalculationHistory: async (_, breakpoint) => {
if (!props.cohortId || props.cohortId <= 0) {
return { results: [], count: 0, next: null, previous: null }
}
breakpoint()
try {
const offset = (values.page - 1) * values.limit
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. cohortsCalculationHistoryRetrieve() from 'products/cohorts/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.get(
`api/cohort/${props.cohortId}/calculation_history/?limit=${values.limit}&offset=${offset}`
)
return response
} catch (error: any) {
if (error.status === 404) {
actions.setCohortMissing()
}
throw error
}
},
},
],
cohort: [
null as any,
{
loadCohort: async (_, breakpoint) => {
if (!props.cohortId || props.cohortId <= 0) {
return null
}
breakpoint()
try {
return await api.cohorts.get(props.cohortId)
} catch {
actions.setCohortMissing()
return null
}
},
},
],
})),
reducers({
page: [
1,
{
setPage: (_, { page }) => page,
},
],
limit: [
100,
{
setLimit: (_, { limit }) => limit,
},
],
cohortMissing: [
false,
{
setCohortMissing: () => true,
},
],
}),
listeners(({ actions }) => ({
setPage: () => {
actions.loadCalculationHistory({})
},
setLimit: () => {
actions.loadCalculationHistory({})
},
})),
selectors({
calculationHistory: [
(s) => [s.calculationHistoryResponse],
(response: CohortCalculationHistoryResponse): CohortCalculationHistoryRecord[] => response.results,
],
totalRecords: [
(s) => [s.calculationHistoryResponse],
(response: CohortCalculationHistoryResponse): number => response.count,
],
}),
events(({ actions, props }) => ({
afterMount: () => {
if (props.cohortId && props.cohortId > 0) {
actions.loadCalculationHistory({})
actions.loadCohort({})
}
},
})),
])