: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,
kea,
key,
path,
props,
propsChanged,
reducers,
selectors,
type LogicWrapper,
} from 'kea'
import { loaders } from 'kea-loaders'
import { objectsEqual } from 'lib/utils/objects'
import * as api from '../generated/api'
import type { OfflineHistoryPageApi, OfflineHistoryPointApi, ScoreDefinitionApi } from '../generated/api.schemas'
import {
OFFLINE_ALL_UPLOAD_STATES,
withOfflineTrendReadLimit,
type OfflineOverviewTrendFilters,
} from './offlineOverviewState'
import { aggregateOfflineScoreHistory, type OfflineScoreHistorySummary } from './offlineScoreTrends'
export interface OfflineOverviewTrendLogicProps {
teamId: number
scorerId: string
overviewId?: string
dateFrom?: string
dateTo: string
refreshKey: number
filters?: OfflineOverviewTrendFilters
}
export type OfflineOverviewTrendQuery = Pick<
OfflineOverviewTrendLogicProps,
'dateFrom' | 'dateTo' | 'refreshKey' | 'filters'
>
export interface OfflineOverviewTrendResult {
definition: ScoreDefinitionApi
page: OfflineHistoryPageApi
query: OfflineOverviewTrendQuery
}
export type OfflineOverviewTrendState = Pick<offlineOverviewTrendLogicValues, 'trend' | 'trendLoading' | 'trendError'>
const EMPTY_TREND_STATES: Record<string, OfflineOverviewTrendState> = {}
const MAX_HISTORY_PAGES = 5
export function getOfflineOverviewTrendKey({
teamId,
scorerId,
overviewId,
}: Pick<OfflineOverviewTrendLogicProps, 'teamId' | 'scorerId' | 'overviewId'>): string {
return `${overviewId ?? teamId}:${scorerId}`
}
export function selectOfflineOverviewTrendStates(state: {
products?: { ai_observability?: { offlineOverviewTrendLogic?: Record<string, OfflineOverviewTrendState> } }
}): Record<string, OfflineOverviewTrendState> {
return state.products?.ai_observability?.offlineOverviewTrendLogic ?? EMPTY_TREND_STATES
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface offlineOverviewTrendLogicValues {
periodSummary: OfflineScoreHistorySummary | null
points: OfflineHistoryPointApi[]
selectedVersionId: string | null
trend: OfflineOverviewTrendResult | null
trendError: boolean
trendLoading: boolean
versionSummaries: OfflineScoreHistorySummary[]
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface offlineOverviewTrendLogicActions {
loadOfflineOverviewTrend: (_?: any) => any
loadOfflineOverviewTrendFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadOfflineOverviewTrendSuccess: (
trend: OfflineOverviewTrendResult,
payload?: any
) => {
trend: OfflineOverviewTrendResult
payload?: any
}
selectVersion: (versionId: string) => {
versionId: string
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface offlineOverviewTrendLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
points: (trend: OfflineOverviewTrendResult | null) => OfflineHistoryPointApi[]
versionSummaries: (points: OfflineHistoryPointApi[]) => OfflineScoreHistorySummary[]
periodSummary: (
versionSummaries: OfflineScoreHistorySummary[],
selectedVersionId: string | null
) => OfflineScoreHistorySummary | null
}
}
export type offlineOverviewTrendLogicType = MakeLogicType<
offlineOverviewTrendLogicValues,
offlineOverviewTrendLogicActions,
OfflineOverviewTrendLogicProps,
offlineOverviewTrendLogicMeta
>
export const offlineOverviewTrendLogic: LogicWrapper<offlineOverviewTrendLogicType> =
kea<offlineOverviewTrendLogicType>([
props({} as OfflineOverviewTrendLogicProps),
key(getOfflineOverviewTrendKey),
path((key) => ['products', 'ai_observability', 'offlineOverviewTrendLogic', key]),
actions({
selectVersion: (versionId: string) => ({ versionId }),
}),
loaders(({ props }) => ({
trend: [
null as OfflineOverviewTrendResult | null,
{
loadOfflineOverviewTrend: async (_ = null, breakpoint): Promise<OfflineOverviewTrendResult> => {
const query: OfflineOverviewTrendQuery = {
dateFrom: props.dateFrom,
dateTo: props.dateTo,
refreshKey: props.refreshKey,
filters: props.filters,
}
await breakpoint(150)
return withOfflineTrendReadLimit(async () => {
try {
breakpoint()
const definition = await api.llmAnalyticsScoreDefinitionsRetrieve(
String(props.teamId),
props.scorerId
)
breakpoint()
let page = await api.aiObservabilityOfflineScorersHistoryList(
String(props.teamId),
props.scorerId,
{
...props.filters,
date_from: props.dateFrom,
date_to: props.dateTo,
statuses: props.filters?.statuses || OFFLINE_ALL_UPLOAD_STATES,
limit: 100,
}
)
breakpoint()
const results = [...page.results]
for (
let pagesLoaded = 1;
page.next_cursor && pagesLoaded < MAX_HISTORY_PAGES;
pagesLoaded++
) {
page = await api.aiObservabilityOfflineScorersHistoryList(
String(props.teamId),
props.scorerId,
{
...props.filters,
date_from: props.dateFrom,
date_to: props.dateTo,
statuses: props.filters?.statuses || OFFLINE_ALL_UPLOAD_STATES,
limit: 100,
cursor: page.next_cursor,
}
)
breakpoint()
results.push(...page.results)
}
return { definition, page: { ...page, results }, query }
} catch (error) {
breakpoint()
throw error
}
})
},
},
],
})),
reducers({
selectedVersionId: [null as string | null, { selectVersion: (_, { versionId }) => versionId }],
trendError: [false, { loadOfflineOverviewTrend: () => false, loadOfflineOverviewTrendFailure: () => true }],
}),
selectors({
points: [
(s) => [s.trend],
(trend: OfflineOverviewTrendResult | null): OfflineHistoryPointApi[] => trend?.page.results || [],
],
versionSummaries: [
(s) => [s.points],
(points: OfflineHistoryPointApi[]): OfflineScoreHistorySummary[] => {
const byVersion = new Map<string, OfflineHistoryPointApi[]>()
for (const point of points) {
const versionPoints = byVersion.get(point.summary.scorer.id) || []
versionPoints.push(point)
byVersion.set(point.summary.scorer.id, versionPoints)
}
return [...byVersion.values()]
.map(aggregateOfflineScoreHistory)
.filter((summary): summary is OfflineScoreHistorySummary => summary !== null)
.sort((a, b) => b.scorer.version - a.scorer.version)
},
],
periodSummary: [
(s) => [s.versionSummaries, s.selectedVersionId],
(
summaries: OfflineScoreHistorySummary[],
selectedVersionId: string | null
): OfflineScoreHistorySummary | null =>
summaries.find((summary) => summary.scorer.id === selectedVersionId) || summaries[0] || null,
],
}),
afterMount(({ actions }) => actions.loadOfflineOverviewTrend()),
propsChanged(({ actions, props }, previous) => {
if (
props.dateFrom !== previous.dateFrom ||
props.dateTo !== previous.dateTo ||
props.refreshKey !== previous.refreshKey ||
!objectsEqual(props.filters || {}, previous.filters || {})
) {
actions.loadOfflineOverviewTrend()
}
}),
])