import React, { useState, useEffect } from 'react'; import { useNavigate, } from 'react-router'; import { Button, Table, Space, Form, Select, message, Card, Tag, DatePicker } from 'antd'; import dayjs from 'dayjs'; import 'dayjs/locale/zh-cn'; // 从share/types.ts导入所有类型,包括MapMode import type { ZichanInfo, DeviceAlert } from '../share/monitorTypes.ts'; import { AlertLevel, AlertStatus, MetricType, AlertLevelNameMap, AlertStatusNameMap, MetricTypeNameMap } from '../share/monitorTypes.ts'; import { getEnumOptions } from './utils.ts'; import { DeviceInstanceAPI, AlertAPI} from './api/index.ts'; // 告警记录页面 export const AlertRecordsPage = () => { const [loading, setLoading] = useState(false); const [alertData, setAlertData] = useState([]); const [pagination, setPagination] = useState({ current: 1, pageSize: 10, total: 0, }); const [deviceOptions, setDeviceOptions] = useState<{label: string, value: number}[]>([]); const [formRef] = Form.useForm(); useEffect(() => { fetchDeviceOptions(); fetchAlertData(); }, [pagination.current, pagination.pageSize]); const fetchDeviceOptions = async () => { try { const response = await DeviceInstanceAPI.getDeviceInstances(); if (response && response.data) { const options = response.data.map((device: ZichanInfo) => ({ label: device.asset_name || `设备${device.id}`, value: device.id })); setDeviceOptions(options); } } catch (error) { console.error('获取设备列表失败:', error); message.error('获取设备列表失败'); } }; const fetchAlertData = async () => { setLoading(true); try { const values = formRef.getFieldsValue(); const params = { page: pagination.current, pageSize: pagination.pageSize, device_id: values.device_id, metric_type: values.metric_type, alert_level: values.alert_level, status: values.status, start_time: values.time?.[0]?.format('YYYY-MM-DD HH:mm:ss'), end_time: values.time?.[1]?.format('YYYY-MM-DD HH:mm:ss'), }; const response = await AlertAPI.getAlertData(params); if (response) { setAlertData(response.data || []); setPagination({ ...pagination, total: response.total || 0, }); } } catch (error) { console.error('获取告警数据失败:', error); message.error('获取告警数据失败'); } finally { setLoading(false); } }; const handleSearch = (values: any) => { setPagination({ ...pagination, current: 1, }); fetchAlertData(); }; const handleTableChange = (newPagination: any) => { setPagination({ ...pagination, current: newPagination.current, pageSize: newPagination.pageSize, }); }; const handleAlertHandle = async (record: DeviceAlert, mode: 'view' | 'edit' = 'edit') => { try { navigate(`/admin/alert-handle/${record.id}?mode=${mode}`); } catch (error) { console.error('处理告警失败:', error); message.error('处理告警失败'); } }; const metricTypeOptions = getEnumOptions(MetricType, MetricTypeNameMap); const alertLevelOptions = getEnumOptions(AlertLevel, AlertLevelNameMap); const alertStatusOptions = getEnumOptions(AlertStatus, AlertStatusNameMap); const getAlertLevelTag = (level: AlertLevel) => { switch (level) { case AlertLevel.MINOR: return 次要; case AlertLevel.NORMAL: return 一般; case AlertLevel.IMPORTANT: return 重要; case AlertLevel.URGENT: return 紧急; default: return 未知; } }; const getAlertStatusTag = (status: AlertStatus) => { switch (status) { case AlertStatus.PENDING: return 待处理; case AlertStatus.HANDLING: return 处理中; case AlertStatus.RESOLVED: return 已解决; case AlertStatus.IGNORED: return 已忽略; default: return 未知; } }; const navigate = useNavigate(); const columns = [ { title: '告警ID', dataIndex: 'id', key: 'id', width: 80, }, { title: '设备名称', dataIndex: 'device_name', key: 'device_name', }, { title: '监控指标', dataIndex: 'metric_type', key: 'metric_type', render: (text: string) => { const option = metricTypeOptions.find(opt => opt.value === text); return option ? option.label : text; }, }, { title: '触发值', dataIndex: 'metric_value', key: 'metric_value', }, { title: '告警等级', dataIndex: 'alert_level', key: 'alert_level', render: (level: AlertLevel) => getAlertLevelTag(level), }, { title: '告警消息', dataIndex: 'alert_message', key: 'alert_message', ellipsis: true, }, { title: '状态', dataIndex: 'status', key: 'status', render: (status: AlertStatus) => getAlertStatusTag(status), }, { title: '告警时间', dataIndex: 'created_at', key: 'created_at', render: (text: Date) => dayjs(text).format('YYYY-MM-DD HH:mm:ss'), }, { title: '操作', key: 'action', render: (_: any, record: DeviceAlert) => ( {record.status === AlertStatus.PENDING && ( )} {record.status === AlertStatus.HANDLING && ( )} ), }, ]; return (
); };