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.

16.9 KB
Raw Blame
import { useActions, useAsyncActions, useValues } from 'kea'

import { IconEye, IconPencil } from '@posthog/icons'
import {
    LemonBanner,
    LemonButton,
    LemonDialog,
    LemonInput,
    LemonSwitch,
    LemonTable,
    LemonTableColumns,
    LemonTag,
} from '@posthog/lemon-ui'

import { CopyToClipboardInline } from 'lib/components/CopyToClipboard'
import { TZLabel } from 'lib/components/TZLabel'
import { LemonField } from 'lib/lemon-ui/LemonField'
import { LemonProgress } from 'lib/lemon-ui/LemonProgress'
import { truncate } from 'lib/utils/strings'

import { featureFlagsStaffTeamConfigSetCreateBodyMaxFeatureFlagsOverrideMax } from '../generated/api.zod'
import {
    CACHE_LABELS,
    FLAG_EVALUATIONS_MODE_LABELS,
    featureFlagsStaffToolsLogic,
    parseFlagLimit,
    StaffCacheEntryStatus,
    StaffCacheKind,
    StaffReadableCacheKind,
    StaffTeamConfig,
    StaffTeamResult,
    StaffWarmRun,
} from './featureFlagsStaffToolsLogic'
import { StaffCacheEntryModal } from './StaffCacheEntryModal'
import { StaffFlagEvaluationsModeModal } from './StaffFlagEvaluationsModeModal'
import { StaffTeamSearchInput } from './StaffTeamSearchInput'

const NO_SELECTION_REASON = 'Select at least one team'

// Sourced from the generated zod schema so the input bound can't drift from the serializer's.
const MAX_FLAG_LIMIT = featureFlagsStaffTeamConfigSetCreateBodyMaxFeatureFlagsOverrideMax

const ALL_CACHES: StaffCacheKind[] = ['evaluation', 'definitions']

const READABLE_CACHE_KINDS: StaffReadableCacheKind[] = ['evaluation', 'definitions']

const WARM_RUN_STATE_TAGS: Record<StaffWarmRun['state'], { type: 'completion' | 'success' | 'muted'; label: string }> =
    {
        running: { type: 'completion', label: 'Running' },
        completed: { type: 'success', label: 'Completed' },
        cancelled: { type: 'muted', label: 'Cancelled' },
    }

const WARM_RUN_SCOPE_LABELS: Record<StaffWarmRun['scope'], string> = {
    all_teams: 'all teams',
    teams_with_flags: 'teams with flags',
}

function WarmRunPanel(): JSX.Element {
    const { warmRun, warmRunCancelResultLoading } = useValues(featureFlagsStaffToolsLogic)
    const { cancelWarmRun } = useAsyncActions(featureFlagsStaffToolsLogic)

    if (!warmRun) {
        return (
            <div className="border rounded p-4">
                <h3 className="mb-1">Warm-all run</h3>
                <p className="text-secondary mb-0">
                    No warm-all run has been recorded. Runs started with the Rust warmer (<code>warm-flags-cache</code>)
                    publish live status here.
                </p>
            </div>
        )
    }

    const stateTag = WARM_RUN_STATE_TAGS[warmRun.state]
    const percent = warmRun.total > 0 ? Math.min(100, Math.round((warmRun.processed / warmRun.total) * 100)) : 0
    const cancelDisabledReason = warmRun.cancel_requested ? 'Cancellation already requested' : undefined

    return (
        <div className="border rounded p-4 space-y-2">
            <div className="flex items-center gap-2">
                <h3 className="mb-0">Warm-all run</h3>
                <LemonTag type={stateTag.type}>{stateTag.label}</LemonTag>
                {warmRun.is_stale && <LemonTag type="warning">Stale</LemonTag>}
                <span className="flex items-baseline gap-1 text-secondary">
                    {WARM_RUN_SCOPE_LABELS[warmRun.scope] ?? warmRun.scope} · started
                    <TZLabel time={warmRun.started_at} />
                </span>
                {warmRun.state === 'running' && !warmRun.is_stale && (
                    <LemonButton
                        status="danger"
                        type="secondary"
                        size="small"
                        className="ml-auto"
                        loading={warmRunCancelResultLoading}
                        disabledReason={cancelDisabledReason}
                        onClick={() =>
                            LemonDialog.open({
                                title: 'Cancel the warm-all run?',
                                description:
                                    'The warmer stops dispatching new teams at its next heartbeat and finishes the teams already in flight. Already-warmed caches are kept.',
                                shouldAwaitSubmit: true,
                                primaryButton: {
                                    children: 'Cancel run',
                                    status: 'danger',
                                    onClick: async () => await cancelWarmRun(),
                                },
                                secondaryButton: {
                                    children: 'Keep running',
                                },
                            })
                        }
                    >
                        Cancel run
                    </LemonButton>
                )}
            </div>
            <LemonProgress percent={percent} />
            <div className="flex items-center gap-2 text-secondary">
                <span>
                    {warmRun.processed.toLocaleString()}/{warmRun.total.toLocaleString()} teams ({percent}%) ·{' '}
                    {warmRun.successful.toLocaleString()} ok, {warmRun.failed.toLocaleString()} failed
                </span>
                <span className="flex items-baseline gap-1 ml-auto">
                    Last heartbeat
                    <TZLabel time={warmRun.updated_at} />
                </span>
            </div>
            {warmRun.is_stale && (
                <LemonBanner type="warning">
                    This run stopped reporting progress. The warmer process likely died (deploy, OOM, or manual kill)
                    without writing a final state. Re-run the warmer to continue; its status shows the last dispatched
                    team id ({warmRun.last_team_id ?? 'unknown'}) as a resume cursor.
                </LemonBanner>
            )}
        </div>
    )
}

function CacheStatusCell({ status, onView }: { status?: StaffCacheEntryStatus; onView: () => void }): JSX.Element {
    const tag = !status ? (
        <LemonTag type="muted">Unknown</LemonTag>
    ) : status.source !== 'redis' ? (
        <LemonTag type="warning">Miss</LemonTag>
    ) : (
        <>
            <LemonTag type="success">Redis</LemonTag>
            <span className="text-secondary">{status.flag_count} flags</span>
        </>
    )
    return (
        <span className="flex items-center gap-2">
            {tag}
            <LemonButton size="small" icon={<IconEye />} onClick={onView} tooltip="View cache entry" noPadding />
        </span>
    )
}

function openFlagLimitDialog(
    team: StaffTeamResult,
    config: StaffTeamConfig,
    setMaxFeatureFlagsOverride: (teamId: number, maxFeatureFlagsOverride: number | null) => Promise<void>
): void {
    LemonDialog.openForm({
        title: `Flag limit for ${team.name}`,
        description: `This team has ${config.feature_flag_count.toLocaleString()} flags, with a limit of ${config.effective_max_feature_flags.toLocaleString()}. Leave the field empty to use the default limit. A limit at or below the current count blocks all new flags for this team, including the ones surveys and experiments create.`,
        initialValues: { maxFeatureFlags: config.max_feature_flags_override ?? '' },
        shouldAwaitSubmit: true,
        content: (
            <LemonField name="maxFeatureFlags" label="Maximum flags">
                <LemonInput type="number" min={1} max={MAX_FLAG_LIMIT} placeholder="Use the default" autoFocus />
            </LemonField>
        ),
        errors: {
            maxFeatureFlags: (value) => {
                const limit = parseFlagLimit(value)
                if (limit === null || (Number.isInteger(limit) && limit >= 1 && limit <= MAX_FLAG_LIMIT)) {
                    return undefined
                }
                return `Enter a whole number between 1 and ${MAX_FLAG_LIMIT.toLocaleString()}, or leave the field empty to use the default.`
            },
        },
        onSubmit: async ({ maxFeatureFlags }) => {
            // Null clears the override, which the backend reads as "fall back to the global default".
            // Awaited so the dialog keeps its submit button in a loading state until the write lands.
            await setMaxFeatureFlagsOverride(team.id, parseFlagLimit(maxFeatureFlags))
        },
    })
}

export function StaffCacheToolsTab(): JSX.Element {
    const {
        selectedTeams,
        selectedTeamIds,
        cacheStatusByTeamId,
        cacheStatusLoading,
        rebuildResultLoading,
        clearResultLoading,
        teamConfigByTeamId,
        pendingTeamConfigTeamIds,
    } = useValues(featureFlagsStaffToolsLogic)
    const {
        rebuildCache,
        clearCache,
        loadCacheStatus,
        viewCacheEntry,
        setMinimalFlagCalledEvents,
        openFlagEvaluationsModeModal,
    } = useActions(featureFlagsStaffToolsLogic)
    // Async so the dialog can await the write and keep its submit button loading meanwhile.
    const { setMaxFeatureFlagsOverride } = useAsyncActions(featureFlagsStaffToolsLogic)

    const hasSelection = selectedTeamIds.length > 0
    const mutating = rebuildResultLoading || clearResultLoading
    const disabledReason = !hasSelection ? NO_SELECTION_REASON : mutating ? 'Action in progress' : undefined

    const columns: LemonTableColumns<StaffTeamResult> = [
        {
            title: 'Team',
            key: 'team',
            render: (_, team) => (
                <span>
                    {team.name} <span className="text-secondary">(#{team.id})</span>
                </span>
            ),
        },
        {
            title: 'Organization',
            dataIndex: 'organization_name',
        },
        {
            title: 'Project token',
            dataIndex: 'api_token',
            render: (token) => (
                <CopyToClipboardInline
                    explicitValue={String(token)}
                    description="project token"
                    tooltipMessage="Copy project token"
                    iconSize="xsmall"
                    className="font-mono text-xs"
                >
                    {truncate(String(token), 16)}
                </CopyToClipboardInline>
            ),
        },
        {
            title: 'Minimal flag_called events',
            key: 'minimal_flag_called_events',
            render: (_, team) => {
                const config = teamConfigByTeamId[team.id]
                const pending = pendingTeamConfigTeamIds.includes(team.id)
                return (
                    <LemonSwitch
                        checked={config?.minimal_flag_called_events ?? false}
                        onChange={(checked) => setMinimalFlagCalledEvents(team.id, checked)}
                        loading={pending}
                        disabledReason={pending ? 'Update in progress' : !config ? 'Loading current value…' : undefined}
                        data-attr="ff-staff-team-config-minimal-flag-called-events"
                    />
                )
            },
        },
        {
            title: 'Flag limit',
            key: 'max_feature_flags_override',
            render: (_, team) => {
                const config = teamConfigByTeamId[team.id]
                const pending = pendingTeamConfigTeamIds.includes(team.id)
                if (!config) {
                    return <span className="text-secondary">Loading…</span>
                }
                const hasOverride = config.max_feature_flags_override !== null
                // Search returns environment teams too, and the endpoint refuses an override on
                // one. Keying on parent_team_id matches what the backend enforces on, so the
                // button is disabled for exactly the rows whose write would be rejected.
                const rootTeamId = team.parent_team_id
                return (
                    <span className="flex items-center gap-2">
                        <span>{config.effective_max_feature_flags.toLocaleString()} limit</span>
                        <LemonTag type={hasOverride ? 'completion' : 'muted'}>
                            {hasOverride ? 'Override' : 'Default'}
                        </LemonTag>
                        <span className="text-secondary">{config.feature_flag_count.toLocaleString()} in use</span>
                        <LemonButton
                            size="small"
                            icon={<IconPencil />}
                            noPadding
                            loading={pending}
                            disabledReason={
                                pending
                                    ? 'Update in progress'
                                    : rootTeamId !== null
                                      ? `The flag limit is set on the project's root team (#${rootTeamId})`
                                      : undefined
                            }
                            tooltip="Edit this team's flag limit"
                            onClick={() => openFlagLimitDialog(team, config, setMaxFeatureFlagsOverride)}
                            data-attr="ff-staff-team-config-max-feature-flags-override"
                        />
                    </span>
                )
            },
        },
        {
            title: 'Flag evaluations mode',
            key: 'flag_evaluations_mode',
            render: (_, team) => {
                const config = teamConfigByTeamId[team.id]
                if (!config) {
                    return <span className="text-secondary">Loading…</span>
                }
                return (
                    <LemonTag type={config.flag_evaluations_mode === 0 ? 'muted' : 'completion'}>
                        {FLAG_EVALUATIONS_MODE_LABELS[config.flag_evaluations_mode]}
                    </LemonTag>
                )
            },
        },
        ...READABLE_CACHE_KINDS.map((cacheKind) => ({
            title: CACHE_LABELS[cacheKind],
            key: cacheKind,
            render: (_: unknown, team: StaffTeamResult) => (
                <CacheStatusCell
                    status={cacheStatusByTeamId[team.id]?.[cacheKind]}
                    onView={() => viewCacheEntry({ teamId: team.id, cache: cacheKind })}
                />
            ),
        })),
    ]

    return (
        <div className="space-y-4">
            <WarmRunPanel />

            <StaffTeamSearchInput />

            <div className="flex flex-wrap items-center gap-2 mt-2">
                <LemonButton
                    type="primary"
                    size="small"
                    onClick={() => rebuildCache({ caches: ALL_CACHES })}
                    disabledReason={disabledReason}
                    loading={rebuildResultLoading}
                >
                    Rebuild flag caches
                </LemonButton>
                <LemonButton
                    status="danger"
                    type="secondary"
                    size="small"
                    onClick={() =>
                        LemonDialog.open({
                            title: 'Clear flag caches?',
                            description: `This immediately clears the evaluation and definitions caches in Redis for ${selectedTeamIds.length} selected team(s). Reads will fall through to the database until the caches are rebuilt.`,
                            primaryButton: {
                                children: 'Clear flag caches',
                                status: 'danger',
                                onClick: () => clearCache({ caches: ALL_CACHES }),
                            },
                            secondaryButton: {
                                children: 'Cancel',
                            },
                        })
                    }
                    disabledReason={disabledReason}
                    loading={clearResultLoading}
                >
                    Clear flag caches
                </LemonButton>
                <LemonButton
                    type="secondary"
                    size="small"
                    onClick={() => openFlagEvaluationsModeModal()}
                    disabledReason={!hasSelection ? NO_SELECTION_REASON : undefined}
                    data-attr="ff-staff-flag-evaluations-mode-open"
                >
                    Set flag evaluations mode
                </LemonButton>
                <LemonButton
                    type="tertiary"
                    size="small"
                    onClick={() => loadCacheStatus()}
                    disabledReason={!hasSelection ? NO_SELECTION_REASON : undefined}
                    className="ml-auto"
                >
                    Refresh status
                </LemonButton>
            </div>

            <LemonTable
                dataSource={selectedTeams}
                columns={columns}
                loading={cacheStatusLoading}
                rowKey={(team) => team.id}
                emptyState="Search for and select one or more teams to inspect and rebuild their flag caches."
            />

            <StaffCacheEntryModal />
            <StaffFlagEvaluationsModeModal />
        </div>
    )
}