// 告警规则 Tab — 主机/中间件/SNMP/站点 四类分栏 // Logan.Li import { useState } from 'react'; import { Card, CardContent } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Switch } from '@/components/ui/switch'; import { Plus, Rocket, Server, Database, Network, Globe } from 'lucide-react'; import { PageEmpty } from '@/components/layout/PageEmpty'; import { PageLoading } from '@/components/layout/PageLoading'; import { getSeverityBadge } from './alert-center-helpers'; import { AgentRuleDialog } from '@/components/agent/AgentRuleDialog'; import { MonitorRuleDialog } from './MonitorRuleDialog'; import type { MetricCatalogCategory, UnifiedAlertRule } from '@/api'; import type { JsonData } from '@/types/common'; import { useTranslation } from 'react-i18next'; import i18n from '@/i18n'; type RuleCategory = 'host' | 'middleware' | 'snmp' | 'site'; type MonitorCategory = 'middleware' | 'snmp' | 'site'; const RULE_CATEGORIES: Array<{ id: RuleCategory; label: string; icon: typeof Server; desc: string; }> = [ { id: 'host', label: i18n.t('monitor.hostMonitor'), icon: Server, desc: i18n.t('monitor.alertrulestab.s836f7b') }, { id: 'middleware', label: i18n.t('dashboard.ops.middleware'), icon: Database, desc: i18n.t('monitor.alertrulestab.sf3e5db') }, { id: 'snmp', label: 'SNMP', icon: Network, desc: i18n.t('monitor.alertrulestab.s086e4c') }, { id: 'site', label: i18n.t('monitor.siteProbe'), icon: Globe, desc: i18n.t('monitor.alertrulestab.sf9f400') }, ]; interface Props { rules: UnifiedAlertRule[]; loading: boolean; catalog: MetricCatalogCategory[]; onRefresh: () => void; onToggleAgent: (id: number, enabled: boolean) => void; onDeleteAgent: (rule: UnifiedAlertRule) => void; onDeleteMonitor: (rule: UnifiedAlertRule) => void; onToggleMonitor: (id: number, enabled: boolean) => void; onQuickSetupHost: () => void; onQuickSetupMonitor: (cat: string) => void; quickSetupPending: boolean; } export function AlertRulesTab({ rules, loading, catalog, onRefresh, onToggleAgent, onDeleteAgent, onDeleteMonitor, onToggleMonitor, onQuickSetupHost, onQuickSetupMonitor, quickSetupPending, }: Props) { const { t } = useTranslation(); const [activeCat, setActiveCat] = useState('host'); const [agentDialogOpen, setAgentDialogOpen] = useState(false); const [monitorDialogOpen, setMonitorDialogOpen] = useState(false); const [monitorCategory, setMonitorCategory] = useState('middleware'); const [editingAgent, setEditingAgent] = useState(null); const [editingMonitor, setEditingMonitor] = useState(null); const filtered = rules.filter((r) => r.category === activeCat); const catMeta = RULE_CATEGORIES.find((c) => c.id === activeCat)!; const getMetricsFor = (cat: MonitorCategory) => catalog.find((c) => c.id === cat)?.metrics || []; const openAdd = () => { if (activeCat === 'host') { setEditingAgent(null); setAgentDialogOpen(true); return; } setMonitorCategory(activeCat as MonitorCategory); setEditingMonitor(null); setMonitorDialogOpen(true); }; const handleQuickSetup = () => { if (activeCat === 'host') { onQuickSetupHost(); } else { onQuickSetupMonitor(activeCat); } }; if (loading) return ; return (
{RULE_CATEGORIES.map((cat) => { const Icon = cat.icon; const count = rules.filter((r) => r.category === cat.id).length; return ( ); })}

{catMeta.desc}

{filtered.length === 0 ? ( ) : (
{filtered.map((rule) => (
{rule.name} {getSeverityBadge(rule.severity)} {rule.enabled ? t('common.enable') : t('common.deactivate')}

{rule.metric} {rule.operator} {rule.threshold} {rule.target ? ` · ${rule.target}` : ''}

{rule.description && (

{rule.description}

)}
rule.rule_type === 'agent' ? onToggleAgent(rule.id, v) : onToggleMonitor(rule.id, v)} />
))}
)} { setAgentDialogOpen(false); onRefresh(); }} />
); }