pages_alert_records.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import React, { useState, useEffect } from 'react';
  2. import {
  3. useNavigate,
  4. } from 'react-router';
  5. import {
  6. Button, Table, Space,
  7. Form, Select, message,
  8. Card, Tag, DatePicker
  9. } from 'antd';
  10. import dayjs from 'dayjs';
  11. import 'dayjs/locale/zh-cn';
  12. // 从share/types.ts导入所有类型,包括MapMode
  13. import type {
  14. ZichanInfo, DeviceAlert
  15. } from '../share/monitorTypes.ts';
  16. import {
  17. AlertLevel, AlertStatus, MetricType,
  18. AlertLevelNameMap, AlertStatusNameMap,
  19. MetricTypeNameMap
  20. } from '../share/monitorTypes.ts';
  21. import { getEnumOptions } from './utils.ts';
  22. import { DeviceInstanceAPI, AlertAPI} from './api/index.ts';
  23. // 告警记录页面
  24. export const AlertRecordsPage = () => {
  25. const [loading, setLoading] = useState(false);
  26. const [alertData, setAlertData] = useState<DeviceAlert[]>([]);
  27. const [pagination, setPagination] = useState({
  28. current: 1,
  29. pageSize: 10,
  30. total: 0,
  31. });
  32. const [deviceOptions, setDeviceOptions] = useState<{label: string, value: number}[]>([]);
  33. const [formRef] = Form.useForm();
  34. useEffect(() => {
  35. fetchDeviceOptions();
  36. fetchAlertData();
  37. }, [pagination.current, pagination.pageSize]);
  38. const fetchDeviceOptions = async () => {
  39. try {
  40. const response = await DeviceInstanceAPI.getDeviceInstances();
  41. if (response && response.data) {
  42. const options = response.data.map((device: ZichanInfo) => ({
  43. label: device.asset_name || `设备${device.id}`,
  44. value: device.id
  45. }));
  46. setDeviceOptions(options);
  47. }
  48. } catch (error) {
  49. console.error('获取设备列表失败:', error);
  50. message.error('获取设备列表失败');
  51. }
  52. };
  53. const fetchAlertData = async () => {
  54. setLoading(true);
  55. try {
  56. const values = formRef.getFieldsValue();
  57. const params = {
  58. page: pagination.current,
  59. pageSize: pagination.pageSize,
  60. device_id: values.device_id,
  61. metric_type: values.metric_type,
  62. alert_level: values.alert_level,
  63. status: values.status,
  64. start_time: values.time?.[0]?.format('YYYY-MM-DD HH:mm:ss'),
  65. end_time: values.time?.[1]?.format('YYYY-MM-DD HH:mm:ss'),
  66. };
  67. const response = await AlertAPI.getAlertData(params);
  68. if (response) {
  69. setAlertData(response.data || []);
  70. setPagination({
  71. ...pagination,
  72. total: response.total || 0,
  73. });
  74. }
  75. } catch (error) {
  76. console.error('获取告警数据失败:', error);
  77. message.error('获取告警数据失败');
  78. } finally {
  79. setLoading(false);
  80. }
  81. };
  82. const handleSearch = (values: any) => {
  83. setPagination({
  84. ...pagination,
  85. current: 1,
  86. });
  87. fetchAlertData();
  88. };
  89. const handleTableChange = (newPagination: any) => {
  90. setPagination({
  91. ...pagination,
  92. current: newPagination.current,
  93. pageSize: newPagination.pageSize,
  94. });
  95. };
  96. const handleAlertHandle = async (record: DeviceAlert, mode: 'view' | 'edit' = 'edit') => {
  97. try {
  98. navigate(`/admin/alert-handle/${record.id}?mode=${mode}`);
  99. } catch (error) {
  100. console.error('处理告警失败:', error);
  101. message.error('处理告警失败');
  102. }
  103. };
  104. const metricTypeOptions = getEnumOptions(MetricType, MetricTypeNameMap);
  105. const alertLevelOptions = getEnumOptions(AlertLevel, AlertLevelNameMap);
  106. const alertStatusOptions = getEnumOptions(AlertStatus, AlertStatusNameMap);
  107. const getAlertLevelTag = (level: AlertLevel) => {
  108. switch (level) {
  109. case AlertLevel.MINOR:
  110. return <Tag color="blue">次要</Tag>;
  111. case AlertLevel.NORMAL:
  112. return <Tag color="green">一般</Tag>;
  113. case AlertLevel.IMPORTANT:
  114. return <Tag color="orange">重要</Tag>;
  115. case AlertLevel.URGENT:
  116. return <Tag color="red">紧急</Tag>;
  117. default:
  118. return <Tag>未知</Tag>;
  119. }
  120. };
  121. const getAlertStatusTag = (status: AlertStatus) => {
  122. switch (status) {
  123. case AlertStatus.PENDING:
  124. return <Tag color="red">待处理</Tag>;
  125. case AlertStatus.HANDLING:
  126. return <Tag color="orange">处理中</Tag>;
  127. case AlertStatus.RESOLVED:
  128. return <Tag color="green">已解决</Tag>;
  129. case AlertStatus.IGNORED:
  130. return <Tag color="default">已忽略</Tag>;
  131. default:
  132. return <Tag>未知</Tag>;
  133. }
  134. };
  135. const navigate = useNavigate();
  136. const columns = [
  137. {
  138. title: '告警ID',
  139. dataIndex: 'id',
  140. key: 'id',
  141. width: 80,
  142. },
  143. {
  144. title: '设备名称',
  145. dataIndex: 'device_name',
  146. key: 'device_name',
  147. },
  148. {
  149. title: '监控指标',
  150. dataIndex: 'metric_type',
  151. key: 'metric_type',
  152. render: (text: string) => {
  153. const option = metricTypeOptions.find(opt => opt.value === text);
  154. return option ? option.label : text;
  155. },
  156. },
  157. {
  158. title: '触发值',
  159. dataIndex: 'metric_value',
  160. key: 'metric_value',
  161. },
  162. {
  163. title: '告警等级',
  164. dataIndex: 'alert_level',
  165. key: 'alert_level',
  166. render: (level: AlertLevel) => getAlertLevelTag(level),
  167. },
  168. {
  169. title: '告警消息',
  170. dataIndex: 'alert_message',
  171. key: 'alert_message',
  172. ellipsis: true,
  173. },
  174. {
  175. title: '状态',
  176. dataIndex: 'status',
  177. key: 'status',
  178. render: (status: AlertStatus) => getAlertStatusTag(status),
  179. },
  180. {
  181. title: '告警时间',
  182. dataIndex: 'created_at',
  183. key: 'created_at',
  184. render: (text: Date) => dayjs(text).format('YYYY-MM-DD HH:mm:ss'),
  185. },
  186. {
  187. title: '操作',
  188. key: 'action',
  189. render: (_: any, record: DeviceAlert) => (
  190. <Space size="middle">
  191. {record.status === AlertStatus.PENDING && (
  192. <Button size="small" type="primary" onClick={() => handleAlertHandle(record, 'edit')}>
  193. 处理
  194. </Button>
  195. )}
  196. {record.status === AlertStatus.HANDLING && (
  197. <Button size="small" type="primary" onClick={() => handleAlertHandle(record, 'edit')}>
  198. 继续处理
  199. </Button>
  200. )}
  201. <Button size="small" onClick={() => handleAlertHandle(record, 'view')}>
  202. 查看
  203. </Button>
  204. </Space>
  205. ),
  206. },
  207. ];
  208. return (
  209. <div>
  210. <Card title="告警记录" style={{ marginBottom: 16 }}>
  211. <Form
  212. form={formRef}
  213. layout="inline"
  214. onFinish={handleSearch}
  215. style={{ marginBottom: 16 }}
  216. >
  217. <Form.Item name="device_id" label="设备">
  218. <Select
  219. placeholder="选择设备"
  220. style={{ width: 200 }}
  221. allowClear
  222. options={deviceOptions}
  223. />
  224. </Form.Item>
  225. <Form.Item name="metric_type" label="监控指标">
  226. <Select
  227. placeholder="选择监控指标"
  228. style={{ width: 150 }}
  229. allowClear
  230. options={metricTypeOptions}
  231. />
  232. </Form.Item>
  233. <Form.Item name="alert_level" label="告警等级">
  234. <Select
  235. placeholder="选择告警等级"
  236. style={{ width: 120 }}
  237. allowClear
  238. options={alertLevelOptions}
  239. />
  240. </Form.Item>
  241. <Form.Item name="status" label="状态">
  242. <Select
  243. placeholder="选择状态"
  244. style={{ width: 120 }}
  245. allowClear
  246. options={alertStatusOptions}
  247. />
  248. </Form.Item>
  249. <Form.Item name="time" label="时间范围">
  250. <DatePicker.RangePicker
  251. showTime
  252. style={{ width: 380 }}
  253. />
  254. </Form.Item>
  255. <Form.Item>
  256. <Button type="primary" htmlType="submit">
  257. 查询
  258. </Button>
  259. </Form.Item>
  260. </Form>
  261. <Table
  262. columns={columns}
  263. dataSource={alertData}
  264. rowKey="id"
  265. pagination={pagination}
  266. loading={loading}
  267. onChange={handleTableChange}
  268. />
  269. </Card>
  270. </div>
  271. );
  272. };