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.

18.1 KB
Raw Blame
import { BindLogic, useActions, useValues } from 'kea'
import { Form } from 'kea-forms'

import { IconChevronDown, IconTestTube } from '@posthog/icons'
import { LemonBanner, LemonButton, LemonCard, LemonModal, LemonTabs } from '@posthog/lemon-ui'

import { TZLabel } from 'lib/components/TZLabel'
import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { More } from 'lib/lemon-ui/LemonButton/More'
import { LemonMenu, LemonMenuOverlay } from 'lib/lemon-ui/LemonMenu/LemonMenu'
import { SceneExport } from 'scenes/sceneTypes'

import { SceneContent } from '~/layout/scenes/components/SceneContent'
import { SceneTitleSection } from '~/layout/scenes/components/SceneTitleSection'
import { LogSeverityLevel } from '~/queries/schema/schema-general'
import { FilterLogicalOperator, UniversalFiltersGroup } from '~/types'

import { LogsAlertEventHistoryContent } from 'products/logs/frontend/components/LogsAlerting/LogsAlertEventHistory'
import { LogsAlertForm } from 'products/logs/frontend/components/LogsAlerting/LogsAlertForm'
import { logsAlertFormLogic } from 'products/logs/frontend/components/LogsAlerting/logsAlertFormLogic'
import { logsAlertNotificationLogic } from 'products/logs/frontend/components/LogsAlerting/logsAlertNotificationLogic'
import { LogsAlertNotifications } from 'products/logs/frontend/components/LogsAlerting/LogsAlertNotifications'
import { LogsAlertRegroupConfirmModal } from 'products/logs/frontend/components/LogsAlerting/LogsAlertRegroupConfirmModal'
import { LogsAlertSimulation } from 'products/logs/frontend/components/LogsAlerting/LogsAlertSimulation'
import { LogsAlertStateIndicator } from 'products/logs/frontend/components/LogsAlerting/LogsAlertStateIndicator'
import { LogsAlertStateTimeline } from 'products/logs/frontend/components/LogsAlerting/LogsAlertStateTimeline'
import { hasAnyFilter } from 'products/logs/frontend/components/LogsAlerting/logsAlertUtils'
import { LogsViewer } from 'products/logs/frontend/components/LogsViewer'
import {
    LogsAlertConfigurationApi,
    LogsAlertConfigurationStateEnumApi,
} from 'products/logs/frontend/generated/api.schemas'

import {
    LogsAlertDetailSceneLogicProps,
    LogsAlertDetailTab,
    SNOOZE_DURATIONS,
    logsAlertDetailSceneLogic,
} from './logsAlertDetailSceneLogic'
import { openDeleteLogsAlertDialog } from './openDeleteLogsAlertDialog'
import { QuillLogsAlertDetailActions } from './QuillLogsAlertDetailActions'

export const scene: SceneExport<LogsAlertDetailSceneLogicProps> = {
    component: LogsAlertDetailScene,
    logic: logsAlertDetailSceneLogic,
    paramsToProps: ({ params: { id } }) => ({ id }),
}

export function LogsAlertDetailScene(): JSX.Element {
    const { alert, alertId, alertLoading, activeTab } = useValues(logsAlertDetailSceneLogic)
    const { setActiveTab } = useActions(logsAlertDetailSceneLogic)

    const formLogicProps = { alert: alert ?? ({ id: alertId } as LogsAlertConfigurationApi) }
    const notifLogicProps = { alertId }
    const { isSimulationPanelOpen } = useValues(logsAlertFormLogic(formLogicProps))
    const { closeSimulationPanel } = useActions(logsAlertFormLogic(formLogicProps))

    if (!alert && !alertLoading) {
        return (
            <SceneContent>
                <div className="p-8 text-muted text-center">Alert not found.</div>
            </SceneContent>
        )
    }

    return (
        <BindLogic logic={logsAlertFormLogic} props={formLogicProps}>
            <BindLogic logic={logsAlertNotificationLogic} props={notifLogicProps}>
                <SceneContent>
                    <AlertHeader />
                    <div className="flex flex-col gap-4 p-4">
                        {alert && <AlertStatusBand />}
                        {alert && (
                            <LogsAlertStateTimeline timeline={alert.state_timeline} className="h-8 w-full" showAxis />
                        )}
                        <LemonTabs
                            activeKey={activeTab}
                            onChange={(key) => setActiveTab(key as LogsAlertDetailTab)}
                            tabs={[
                                {
                                    key: 'configuration',
                                    label: 'Configuration',
                                    content: <ConfigurationTab />,
                                },
                                {
                                    key: 'notifications',
                                    label: 'Notifications',
                                    content: <NotificationsTab />,
                                },
                                {
                                    key: 'history',
                                    label: 'History',
                                    content: alert ? <LogsAlertEventHistoryContent alert={alert} /> : <></>,
                                },
                                {
                                    key: 'logs',
                                    label: 'Observed logs',
                                    content: alert ? <ObservedLogsTab alert={alert} /> : <></>,
                                },
                            ]}
                        />
                    </div>
                    <LemonModal
                        isOpen={isSimulationPanelOpen}
                        onClose={closeSimulationPanel}
                        title="Alert simulation"
                        description="Run the alert against historical data to preview when it would have fired. Includes threshold evaluation, N-of-M noise reduction, and cooldown."
                        width={960}
                    >
                        <LogsAlertSimulation />
                    </LemonModal>
                    <LogsAlertRegroupConfirmModal />
                </SceneContent>
            </BindLogic>
        </BindLogic>
    )
}

function primaryDisabledReason({
    isDraft,
    alertForm,
    alertFormChanged,
    nameError,
    maxInstancesError,
    hasPendingNotifications,
}: {
    isDraft: boolean
    alertForm: { name: string; severityLevels: string[]; serviceNames: string[]; filterGroup: UniversalFiltersGroup }
    alertFormChanged: boolean
    nameError: string | undefined
    maxInstancesError: string | undefined
    hasPendingNotifications: boolean
}): string | undefined {
    if (nameError) {
        return nameError
    }
    if (maxInstancesError) {
        return maxInstancesError
    }
    if (isDraft) {
        return !hasAnyFilter(alertForm.severityLevels, alertForm.serviceNames, alertForm.filterGroup)
            ? 'Add at least one filter to enable'
            : undefined
    }
    return !alertFormChanged && !hasPendingNotifications ? 'No changes to save' : undefined
}

function AlertHeader(): JSX.Element {
    const { alert, alertLoading, isDraft } = useValues(logsAlertDetailSceneLogic)
    const { toggleEnabled, snoozeAlert, unsnoozeAlert, resetAlert, deleteAlert, enableAlert } =
        useActions(logsAlertDetailSceneLogic)

    const { isAlertFormSubmitting, alertFormChanged, alertForm, alertFormErrors, alertFormValidationErrors } =
        useValues(logsAlertFormLogic)
    const { submitAlertForm, setAlertFormValue } = useActions(logsAlertFormLogic)
    const { pendingNotifications } = useValues(logsAlertNotificationLogic)
    const todayRailNav = useFeatureFlag('TODAY_RAIL_NAV')

    const isEnabled = alert?.enabled ?? true
    const isBroken = alert?.state === LogsAlertConfigurationStateEnumApi.Broken
    const isSnoozed = alert?.state === LogsAlertConfigurationStateEnumApi.Snoozed

    return (
        <SceneTitleSection
            name={alert ? alertForm.name : undefined}
            resourceType={{ type: 'logs' }}
            isLoading={alertLoading && !alert}
            canEdit={!!alert}
            forceEdit={isDraft}
            onNameChange={(name) => setAlertFormValue('name', name)}
            renameDebounceMs={0}
            actions={
                alert && todayRailNav ? (
                    <QuillLogsAlertDetailActions
                        alert={alert}
                        primaryDisabledReason={primaryDisabledReason({
                            isDraft,
                            alertForm,
                            alertFormChanged,
                            nameError: typeof alertFormErrors?.name === 'string' ? alertFormErrors.name : undefined,
                            maxInstancesError: alertFormValidationErrors.maxInstances as string | undefined,
                            hasPendingNotifications: pendingNotifications.length > 0,
                        })}
                    />
                ) : alert ? (
                    <div className="flex items-center gap-2">
                        {(!isEnabled || alert.state !== LogsAlertConfigurationStateEnumApi.NotFiring) && (
                            <LogsAlertStateIndicator
                                state={alert.state}
                                enabled={isEnabled}
                                firstEnabledAt={alert.first_enabled_at}
                                lastErrorMessage={alert.last_error_message}
                                snoozeUntil={alert.snooze_until}
                            />
                        )}
                        {!isDraft && (
                            <LemonButton
                                size="small"
                                type="secondary"
                                status={isEnabled ? 'danger' : undefined}
                                onClick={toggleEnabled}
                                disabledReason={isBroken ? 'Reset this alert to re-enable checks' : undefined}
                                data-attr="logs-alert-toggle-enabled"
                            >
                                {isEnabled ? 'Disable' : 'Enable'}
                            </LemonButton>
                        )}
                        {isEnabled &&
                            (isSnoozed ? (
                                <LemonButton size="small" type="secondary" onClick={unsnoozeAlert}>
                                    Unsnooze
                                </LemonButton>
                            ) : (
                                <LemonMenu
                                    placement="bottom-end"
                                    items={SNOOZE_DURATIONS.map((d) => ({
                                        label: d.label,
                                        onClick: () => snoozeAlert(d.minutes),
                                    }))}
                                >
                                    <LemonButton size="small" type="secondary" sideIcon={<IconChevronDown />}>
                                        Snooze
                                    </LemonButton>
                                </LemonMenu>
                            ))}
                        <LemonButton
                            size="small"
                            type="primary"
                            onClick={isDraft ? enableAlert : submitAlertForm}
                            loading={isAlertFormSubmitting}
                            disabledReason={primaryDisabledReason({
                                isDraft,
                                alertForm,
                                alertFormChanged,
                                nameError: typeof alertFormErrors?.name === 'string' ? alertFormErrors.name : undefined,
                                maxInstancesError: alertFormValidationErrors.maxInstances as string | undefined,
                                hasPendingNotifications: pendingNotifications.length > 0,
                            })}
                            data-attr={isDraft ? 'logs-alert-enable-primary' : 'logs-alert-save-primary'}
                        >
                            {isDraft ? 'Enable alert' : 'Save'}
                        </LemonButton>
                        <More
                            overlay={
                                <LemonMenuOverlay
                                    items={[
                                        ...(isBroken ? [{ label: 'Reset alert', onClick: resetAlert }] : []),
                                        {
                                            'data-attr': 'logs-alert-detail-delete',
                                            label: 'Delete',
                                            status: 'danger' as const,
                                            onClick: () =>
                                                openDeleteLogsAlertDialog({ alertName: alert.name, deleteAlert }),
                                        },
                                    ]}
                                />
                            }
                        />
                    </div>
                ) : undefined
            }
        />
    )
}

function AlertStatusBand(): JSX.Element | null {
    const { alert } = useValues(logsAlertDetailSceneLogic)
    const { resetAlert, unsnoozeAlert, enableAlert, setActiveTab } = useActions(logsAlertDetailSceneLogic)
    const { destinationGroups, existingHogFunctionsLoading } = useValues(logsAlertNotificationLogic)

    if (!alert) {
        return null
    }

    const banners: JSX.Element[] = []

    if (!alert.enabled && alert.state !== LogsAlertConfigurationStateEnumApi.Broken) {
        banners.push(
            <LemonBanner
                key="disabled"
                type="warning"
                action={{ children: 'Enable', onClick: enableAlert, 'data-attr': 'logs-alert-banner-enable' }}
            >
                This alert is disabled — no checks are running.
            </LemonBanner>
        )
    }

    if (alert.state === LogsAlertConfigurationStateEnumApi.Broken) {
        banners.push(
            <LemonBanner key="broken" type="error" action={{ children: 'Reset alert', onClick: resetAlert }}>
                <div className="font-semibold">
                    Auto-disabled after {alert.consecutive_failures ?? 'repeated'} consecutive check failures.
                </div>
                {alert.last_error_message && (
                    <div className="text-xs font-mono mt-1 whitespace-pre-wrap break-words">
                        {alert.last_error_message}
                    </div>
                )}
            </LemonBanner>
        )
    }

    if (alert.state === LogsAlertConfigurationStateEnumApi.Snoozed && alert.snooze_until) {
        banners.push(
            <LemonBanner key="snoozed" type="info" action={{ children: 'Unsnooze', onClick: unsnoozeAlert }}>
                Snoozed until <TZLabel time={alert.snooze_until} timestampStyle="absolute" />.
            </LemonBanner>
        )
    }

    if (alert.state === LogsAlertConfigurationStateEnumApi.Firing) {
        banners.push(
            <LemonBanner key="firing" type="error">
                <span className="font-semibold">Alert is currently firing.</span>
                {alert.last_checked_at && (
                    <span className="text-xs ml-2">
                        Last checked <TZLabel time={alert.last_checked_at} />.
                    </span>
                )}
            </LemonBanner>
        )
    }

    if (alert.enabled && !existingHogFunctionsLoading && destinationGroups.length === 0) {
        banners.push(
            <LemonBanner
                key="no-destinations"
                type="warning"
                action={{
                    children: 'Add notification',
                    onClick: () => setActiveTab('notifications'),
                    'data-attr': 'logs-alert-banner-add-notification',
                }}
            >
                No notifications configured — this alert will fire silently.
            </LemonBanner>
        )
    }

    if (banners.length === 0) {
        return null
    }

    return <div className="flex flex-col gap-2">{banners}</div>
}

function ConfigurationTab(): JSX.Element {
    const { alert } = useValues(logsAlertDetailSceneLogic)
    const { isSimulationPanelOpen } = useValues(logsAlertFormLogic)
    const { openSimulationPanel } = useActions(logsAlertFormLogic)
    const formLogicProps = { alert: alert ?? null }

    if (!alert) {
        return <div className="p-4 text-muted">Loading…</div>
    }

    return (
        <LemonCard className="max-w-3xl p-6" hoverEffect={false}>
            <div className="flex flex-col gap-6">
                <div>
                    <LemonButton
                        type="secondary"
                        icon={<IconTestTube />}
                        onClick={openSimulationPanel}
                        active={isSimulationPanelOpen}
                        tooltip="Run this alert against historical data to see when it would have fired"
                    >
                        Simulate
                    </LemonButton>
                </div>
                <Form
                    logic={logsAlertFormLogic}
                    props={formLogicProps}
                    formKey="alertForm"
                    enableFormOnSubmit
                    className="flex flex-col gap-6"
                >
                    <LogsAlertForm />
                </Form>
            </div>
        </LemonCard>
    )
}

function NotificationsTab(): JSX.Element {
    const { alert } = useValues(logsAlertDetailSceneLogic)

    if (!alert) {
        return <div className="p-4 text-muted">Loading…</div>
    }

    return (
        <LemonCard className="max-w-3xl p-6" hoverEffect={false}>
            <div className="max-w-2xl">
                <LogsAlertNotifications alertId={alert.id} />
            </div>
        </LemonCard>
    )
}

function ObservedLogsTab({ alert }: { alert: LogsAlertConfigurationApi }): JSX.Element {
    const filters = (alert.filters ?? {}) as Record<string, unknown>
    const initialFilters = {
        severityLevels: ((filters.severityLevels as string[] | undefined) ?? []) as LogSeverityLevel[],
        serviceNames: (filters.serviceNames as string[] | undefined) ?? [],
        filterGroup: (filters.filterGroup as UniversalFiltersGroup | undefined) ?? {
            type: FilterLogicalOperator.And,
            values: [],
        },
    }

    return (
        <div className="h-[calc(100vh-20rem)] min-h-[400px]">
            <LogsViewer id={`alert-${alert.id}-logs`} initialFilters={initialFilters} showSavedViewsButton={false} />
        </div>
    )
}