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.8 KB
Raw Blame
import { ApiError } from 'lib/api-error'

import { InsightLogicProps } from '~/types'

export function getCapacityRetryAt(tileError: unknown, queryError: unknown): number | null {
    const tileRetryAt = tileError instanceof ApiError ? tileError.retryAfterTimestamp : null
    const queryRetryAt = queryError instanceof ApiError ? queryError.retryAfterTimestamp : null
    return Math.max(tileRetryAt ?? 0, queryRetryAt ?? 0) || null
}

export function getRetryCooldown(retryAt: number | null | undefined): {
    secondsLeft: number
    disabledReason: string | undefined
    remediation: string | null
} {
    if (!retryAt) {
        return { secondsLeft: 0, disabledReason: undefined, remediation: null }
    }
    const secondsLeft = Math.max(0, Math.ceil((retryAt - Date.now()) / 1000))
    if (secondsLeft === 0) {
        return { secondsLeft, disabledReason: undefined, remediation: 'You can try this query again now.' }
    }
    const unit = secondsLeft === 1 ? 'second' : 'seconds'
    const message = `PostHog is busy. You can retry in ${secondsLeft} ${unit}.`
    return { secondsLeft, disabledReason: message, remediation: message }
}

/**
 * Get a key function for InsightLogicProps.
 * The key will equals either 'scene', 'new' or an ID.
 *
 * @param defaultKey
 * @param sceneKey
 */
export const keyForInsightLogicProps =
    (defaultKey = 'new') =>
    (props: InsightLogicProps): string => {
        if (!('dashboardItemId' in props)) {
            throw new Error('Must init with dashboardItemId, even if undefined')
        }
        const baseKey = props.dashboardItemId
            ? `${props.dashboardItemId}${props.dashboardId ? `/on-dashboard-${props.dashboardId}` : ''}`
            : defaultKey
        return props.tabId ? `${baseKey}/tab-${props.tabId}` : baseKey
    }