Skip to content

: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.

11.7 KB
Raw Blame
import { MakeLogicType, actions, afterMount, beforeUnmount, connect, kea, listeners, path, reducers } from 'kea'

import api from 'lib/api'
import { teamLogic } from 'scenes/teamLogic'

import {
    AnalyticsFeed,
    AnalyticsFeedQuery,
    AnalyticsSourcePage,
    AnalyticsSourceState,
    analyticsFeedFromSources,
    analyticsFeedTypes,
    analyticsSourceKey,
    fetchAnalyticsSourcePage,
    mergeAnalyticsSources,
} from './analyticsFeed'
import { AnalyticsType } from './analyticsUtils'

const MAX_FILTERED_SOURCES = 30

interface AnalyticsFeedSnapshot {
    sources: Record<string, AnalyticsSourceState>
}

let snapshot: AnalyticsFeedSnapshot | null = null

export function resetAnalyticsFeedSnapshot(): void {
    snapshot = null
}

function settledSources(sources: Record<string, AnalyticsSourceState>): Record<string, AnalyticsSourceState> {
    return Object.fromEntries(
        Object.entries(sources)
            .filter(([, source]) => source.initialized)
            .map(([key, source]) => [key, { ...source, refreshing: false, loading: false }])
    )
}

function isFiltered(query: AnalyticsFeedQuery): boolean {
    return !!query.search.trim() || query.createdBy.length > 0 || query.tags.length > 0
}

// Every search or filter combination is its own set of sources, so old ones are dropped to bound memory.
function withoutOldFiltered(sources: Record<string, AnalyticsSourceState>): Record<string, AnalyticsSourceState> {
    const filteredKeys = Object.keys(sources).filter((key) => isFiltered(sources[key].query))
    const dropped = new Set(filteredKeys.slice(0, Math.max(0, filteredKeys.length - MAX_FILTERED_SOURCES)))
    return dropped.size ? Object.fromEntries(Object.entries(sources).filter(([key]) => !dropped.has(key))) : sources
}

export function selectAnalyticsFeed(
    sources: Record<string, AnalyticsSourceState>,
    projectId: number | null,
    query: AnalyticsFeedQuery
): AnalyticsFeed {
    return analyticsFeedFromSources(
        analyticsFeedTypes(query).map((type) =>
            projectId ? sources[analyticsSourceKey(String(projectId), query, type)] : undefined
        )
    )
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface analyticsFeedLogicValues {
    currentTeamId: number | null // teamLogic
    sources: Record<string, AnalyticsSourceState>
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface analyticsFeedLogicActions {
    ensureFeed: (
        query: AnalyticsFeedQuery,
        count: number
    ) => {
        count: number
        query: AnalyticsFeedQuery
    }
    loadSourcePage: (key: string) => {
        key: string
    }
    refreshFeed: (query: AnalyticsFeedQuery) => {
        query: AnalyticsFeedQuery
    }
    refreshSource: (
        key: string,
        projectId: string,
        query: AnalyticsFeedQuery,
        type: AnalyticsType
    ) => {
        key: string
        projectId: string
        query: AnalyticsFeedQuery
        type: AnalyticsType
    }
    restoreSnapshot: (snapshot: AnalyticsFeedSnapshot) => {
        snapshot: AnalyticsFeedSnapshot
    }
    sourcePageLoaded: (
        key: string,
        generation: number,
        offset: number,
        page: AnalyticsSourcePage | null
    ) => {
        generation: number
        key: string
        offset: number
        page: AnalyticsSourcePage | null
    }
    sourceRefreshed: (
        key: string,
        generation: number,
        page: AnalyticsSourcePage | null
    ) => {
        generation: number
        key: string
        page: AnalyticsSourcePage | null
    }
}

export type analyticsFeedLogicType = MakeLogicType<analyticsFeedLogicValues, analyticsFeedLogicActions>

/**
 * The project's analytics, paged per type and merged newest first. Shared by the list page and the
 * sidebar, so each query is fetched once and a revisit shows rows at once.
 */
export const analyticsFeedLogic = kea<analyticsFeedLogicType>([
    path(['scenes', 'analytics', 'analyticsFeedLogic']),
    connect(() => ({ values: [teamLogic, ['currentTeamId']] })),
    actions({
        ensureFeed: (query: AnalyticsFeedQuery, count: number) => ({ query, count }),
        refreshFeed: (query: AnalyticsFeedQuery) => ({ query }),
        refreshSource: (key: string, projectId: string, query: AnalyticsFeedQuery, type: AnalyticsType) => ({
            key,
            projectId,
            query,
            type,
        }),
        sourceRefreshed: (key: string, generation: number, page: AnalyticsSourcePage | null) => ({
            key,
            generation,
            page,
        }),
        loadSourcePage: (key: string) => ({ key }),
        sourcePageLoaded: (key: string, generation: number, offset: number, page: AnalyticsSourcePage | null) => ({
            key,
            generation,
            offset,
            page,
        }),
        restoreSnapshot: (snapshot: AnalyticsFeedSnapshot) => ({ snapshot }),
    }),
    reducers({
        sources: [
            {} as Record<string, AnalyticsSourceState>,
            {
                restoreSnapshot: (state, { snapshot }) => ({ ...snapshot.sources, ...state }),
                refreshSource: (state, { key, projectId, query, type }) => {
                    const { [key]: current, ...rest } = state
                    return withoutOldFiltered({
                        ...rest,
                        [key]: current
                            ? { ...current, refreshing: true, generation: current.generation + 1 }
                            : {
                                  projectId,
                                  query,
                                  type,
                                  items: [],
                                  offset: 0,
                                  hasMore: true,
                                  initialized: false,
                                  refreshing: true,
                                  loading: false,
                                  failed: false,
                                  generation: 1,
                              },
                    })
                },
                sourceRefreshed: (state, { key, generation, page }) => {
                    const current = state[key]
                    if (!current || current.generation !== generation) {
                        return state
                    }
                    const next: AnalyticsSourceState = page
                        ? {
                              ...current,
                              items: page.items,
                              offset: page.fetched,
                              hasMore: page.hasMore,
                              initialized: true,
                              refreshing: false,
                              loading: false,
                              failed: false,
                          }
                        : current.initialized
                          ? { ...current, refreshing: false, failed: true }
                          : { ...current, hasMore: false, initialized: true, refreshing: false, failed: true }
                    return { ...state, [key]: next }
                },
                loadSourcePage: (state, { key }) =>
                    state[key] ? { ...state, [key]: { ...state[key], loading: true } } : state,
                sourcePageLoaded: (state, { key, generation, offset, page }) => {
                    const current = state[key]
                    if (!current || current.generation !== generation || current.offset !== offset) {
                        return state
                    }
                    if (!page) {
                        return { ...state, [key]: { ...current, loading: false, hasMore: false, failed: true } }
                    }
                    const known = new Set(current.items.map((item) => item.id))
                    return {
                        ...state,
                        [key]: {
                            ...current,
                            items: [...current.items, ...page.items.filter((item) => !known.has(item.id))],
                            offset: current.offset + page.fetched,
                            hasMore: page.hasMore,
                            loading: false,
                        },
                    }
                },
            },
        ],
    }),
    listeners(({ actions, values, cache }) => ({
        refreshFeed: async ({ query }) => {
            if (!values.currentTeamId) {
                return
            }
            // An object made before its product joined the file system has no row until the unfiled sweep
            // files it, and Today never opens the project tree that runs that sweep, so the first read runs it.
            // The sweep is slow in a large project, so the list does not wait for it and reloads only if it filed rows.
            if (!cache.unfiledSweep) {
                cache.unfiledSweep = api.fileSystem.unfiled().catch(() => null)
                void cache.unfiledSweep.then((result: { count: number } | null) => {
                    if (!result?.count || cache.disposables.isDisposed) {
                        return
                    }
                    // A new generation makes a read still in flight stale, so rows filed after it started are not lost.
                    for (const [key, source] of Object.entries(values.sources)) {
                        if (source.projectId === String(values.currentTeamId)) {
                            actions.refreshSource(key, source.projectId, source.query, source.type)
                        }
                    }
                })
            }
            const projectId = String(values.currentTeamId)
            for (const type of analyticsFeedTypes(query)) {
                const key = analyticsSourceKey(projectId, query, type)
                if (!values.sources[key]?.refreshing) {
                    actions.refreshSource(key, projectId, { ...query, search: query.search.trim() }, type)
                }
            }
        },
        ensureFeed: ({ query, count }) => {
            if (!values.currentTeamId) {
                return
            }
            const projectId = String(values.currentTeamId)
            const keys = analyticsFeedTypes(query).map((type) => analyticsSourceKey(projectId, query, type))
            if (keys.some((key) => !values.sources[key])) {
                actions.refreshFeed(query)
                return
            }
            const { items, blocking } = mergeAnalyticsSources(keys.map((key) => values.sources[key]))
            if (items.length >= count) {
                return
            }
            for (const source of blocking) {
                if (source.initialized && !source.loading && !source.refreshing) {
                    actions.loadSourcePage(analyticsSourceKey(source.projectId, source.query, source.type))
                }
            }
        },
        refreshSource: async ({ key, projectId, query, type }) => {
            const generation = values.sources[key].generation
            const page = await fetchAnalyticsSourcePage(projectId, type, query, 0).catch(() => null)
            actions.sourceRefreshed(key, generation, page)
        },
        loadSourcePage: async ({ key }) => {
            const source = values.sources[key]
            if (!source) {
                return
            }
            const { generation, offset } = source
            const page = await fetchAnalyticsSourcePage(source.projectId, source.type, source.query, offset).catch(
                () => null
            )
            actions.sourcePageLoaded(key, generation, offset, page)
        },
    })),
    afterMount(({ actions }) => {
        if (snapshot) {
            actions.restoreSnapshot(snapshot)
        }
    }),
    beforeUnmount(({ values }) => {
        snapshot = { sources: settledSources(values.sources) }
    }),
])