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.

1.7 KB
Raw Blame
import { DASHBOARD_MIN_REFRESH_INTERVAL_MINUTES } from 'scenes/dashboard/dashboardUtils'

import type { DashboardTile } from '~/types'

import { dashboardsRunWidgetsRetrieve } from './generated/api'
import type { DashboardWidgetRunResultApi } from './generated/api.schemas'

export const WIDGET_CLIENT_TTL_MS = DASHBOARD_MIN_REFRESH_INTERVAL_MINUTES * 60 * 1000

export function isWidgetStale(
    status: { loading?: boolean; error?: string | null; fetchedAt?: number } | undefined,
    currentTime = Date.now()
): boolean {
    return (
        !status?.loading &&
        (!!status?.error || (!!status?.fetchedAt && currentTime - status.fetchedAt >= WIDGET_CLIENT_TTL_MS))
    )
}

export function findNewlyAddedWidgetTiles(
    previousTileIds: ReadonlySet<number>,
    tiles: DashboardTile[] | undefined | null
): DashboardTile[] {
    if (!tiles?.length) {
        return []
    }

    return tiles.filter((tile) => !!tile.widget && !tile.deleted && !previousTileIds.has(tile.id))
}

export function chunkTileIds(tileIds: number[], chunkSize = 4): number[][] {
    const chunks: number[][] = []
    for (let i = 0; i < tileIds.length; i += chunkSize) {
        chunks.push(tileIds.slice(i, i + chunkSize))
    }
    return chunks
}

export type FetchRunWidgetsOptions = {
    signal?: AbortSignal
}

export async function fetchRunWidgets(
    projectId: string,
    dashboardId: number,
    tileIds: number[],
    options?: FetchRunWidgetsOptions
): Promise<DashboardWidgetRunResultApi[]> {
    if (tileIds.length === 0) {
        return []
    }

    const response = await dashboardsRunWidgetsRetrieve(
        projectId,
        dashboardId,
        { tile_ids: tileIds.join(',') },
        { signal: options?.signal }
    )

    return response.results
}