import { Activity, AlertTriangle, Check, CheckCheck, RotateCw } from "lucide-react"; import { useCallback, useEffect, useState } from "react"; import { platformApiClient } from "../api/client"; import type { AlertResponse, ProductionCapacitySummaryResponse } from "../api/types"; import { cx } from "../utils/classes"; import { ConfirmDialog } from "./OperationControls"; import { ErrorState, LoadingState, ResultBadge } from "./StateViews"; type AlertAction = "acknowledge" | "resolve" | "retry"; interface ProductionOperationsPanelProps { compact?: boolean; title?: string; } export function ProductionOperationsPanel({ compact = false, title = "容量与告警" }: ProductionOperationsPanelProps) { const [capacity, setCapacity] = useState(null); const [alerts, setAlerts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); const [intent, setIntent] = useState<{ alert: AlertResponse; action: AlertAction } | null>(null); const [busyKey, setBusyKey] = useState(""); const [result, setResult] = useState<{ status: "succeeded" | "failed"; label: string } | null>(null); const refresh = useCallback(async () => { setLoading(true); setError(""); try { const [capacityResponse, alertResponse] = await Promise.all([platformApiClient.getProductionCapacity(), platformApiClient.listAlerts()]); setCapacity(capacityResponse); setAlerts(alertResponse.items); } catch (caught) { setError(caught instanceof Error ? caught.message : "生产状态加载失败"); } finally { setLoading(false); } }, []); useEffect(() => { void refresh(); }, [refresh]); async function submitIntent() { if (!intent || busyKey) return; const key = `${intent.alert.id}:${intent.action}`; setBusyKey(key); setResult(null); try { if (intent.action === "acknowledge") { await platformApiClient.acknowledgeAlert(intent.alert.id, "operator acknowledged from production console"); } else if (intent.action === "resolve") { await platformApiClient.resolveAlert(intent.alert.id, "operator resolved after production review"); } else { await platformApiClient.retryAlert(intent.alert.id, `web:alert.retry:${intent.alert.id}:${Date.now()}`); } setResult({ status: "succeeded", label: `${alertActionLabel(intent.action)}已由 Platform 持久化` }); setIntent(null); await refresh(); } catch (caught) { setResult({ status: "failed", label: caught instanceof Error ? caught.message : `${alertActionLabel(intent.action)}失败` }); setIntent(null); } finally { setBusyKey(""); } } const visibleAlerts = compact ? alerts.filter((alert) => alert.state !== "resolved").slice(0, 3) : alerts.slice(0, 12); const visibleEndpoints = compact ? capacity?.endpoints.slice(0, 3) ?? [] : capacity?.endpoints ?? []; return (

{title}

{result && } {loading && } {!loading && error && void refresh()} compact />} {!loading && !error && capacity && ( <>
运行槽位
{capacity.totalRunningJobs}/{capacity.totalMaxJobs}
排队
{capacity.totalQueuedJobs}
未关闭告警
{capacity.activeAlerts}
{visibleEndpoints.map((endpoint) => (
{endpoint.displayName}{endpoint.pressureCodes?.join(", ") || "capacity.available"} {endpoint.pressureCodes?.length ? "压力" : endpoint.status} {endpoint.runningJobs}/{endpoint.maxJobs} · 队列 {endpoint.queuedJobs}
))}
{visibleAlerts.length === 0 &&

当前没有持久告警。

} {visibleAlerts.map((alert) => { const pending = busyKey.startsWith(`${alert.id}:`); return (
{alert.title} {alert.state}

{alert.message}

{alert.sourceKind} · {alert.sourceId} 发生 {alert.occurrenceCount} 次 {alert.lastJobId && 任务 {alert.lastJobId}}
{alert.state !== "resolved" && (
{alert.state === "active" && } {alert.retryable && }
)}
); })}
)} { if (!busyKey) setIntent(null); }} onConfirm={() => void submitIntent()} />
); } function alertActionLabel(action: AlertAction) { if (action === "acknowledge") return "确认"; if (action === "resolve") return "解决"; return "重试来源"; }