告警数据

This commit is contained in:
mmc
2026-05-13 09:06:50 +08:00
parent 827822c22d
commit 348aa391ff
3 changed files with 585 additions and 457 deletions

View File

@@ -1,125 +1,165 @@
import React, { useEffect, useRef } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { Card, Row, Col, Space } from 'antd';
import { AreaChartOutlined } from '@ant-design/icons';
import {
BellOutlined,
ExclamationCircleOutlined,
ClockCircleOutlined,
CheckCircleOutlined
} from '@ant-design/icons';
import * as echarts from 'echarts';
const AntdCard = Card as any;
// 接口引入
import { getAlarmStatistics } from '../../api/alarmApi.js';
const AlarmStatistics = () => {
// 图表 ref
const barChartRef = useRef(null);
const pieChartRef = useRef(null);
const [statData, setStatData] = useState({
total: 0,
levelStats: {},
pending: 0,
processing: 0,
closed: 0,
});
// 获取统计(修复:接口 code = 0 才是成功)
const fetchStat = async () => {
try {
const res = await getAlarmStatistics();
if (res.code === 200) {
setStatData(res.data || {});
}
} catch (err) { }
};
// 柱状图
useEffect(() => {
if (!barChartRef.current) return;
const barChart = echarts.init(barChartRef.current);
barChart.setOption({
title: { text: '近7天告警趋势' },
xAxis: {
type: 'category',
data: ['1日', '2日', '3日', '4日', '5日', '6日', '7日']
},
const chart = echarts.init(barChartRef.current);
const option = {
title: { text: '近7天告警趋势', textStyle: { fontSize: 14 } },
xAxis: { type: 'category', data: ['1日', '2日', '3日', '4日', '5日', '6日', '7日'] },
yAxis: { type: 'value' },
series: [
{
data: [5, 8, 12, 9, 15, 7, 12],
type: 'bar',
itemStyle: { color: '#1677ff' }
}
]
});
const resizeFn = () => barChart.resize();
window.addEventListener('resize', resizeFn);
return () => {
barChart.dispose();
window.removeEventListener('resize', resizeFn);
series: [{
data: [5, 8, 12, 9, 15, 7, statData.total || 2],
type: 'bar',
itemStyle: { color: '#165DFF', borderRadius: 4 }
}]
};
}, []);
chart.setOption(option);
const resize = () => chart.resize();
window.addEventListener('resize', resize);
return () => {
chart.dispose();
window.removeEventListener('resize', resize);
};
}, [statData]);
// 饼图
useEffect(() => {
if (!pieChartRef.current) return;
const pieChart = echarts.init(pieChartRef.current);
pieChart.setOption({
const chart = echarts.init(pieChartRef.current);
const pieData = [
{ value: statData.levelStats?.[1] || 0, name: '严重告警' },
{ value: statData.levelStats?.[2] || 0, name: '重要告警' },
{ value: statData.levelStats?.[3] || 0, name: '一般告警' },
{ value: statData.levelStats?.[4] || 0, name: '提示告警' },
].filter(i => i.value > 0);
const option = {
title: { text: '告警级别分布' },
tooltip: { trigger: 'item' },
series: [
{
type: 'pie',
radius: '50%',
data: [
{ value: 15, name: '严重告警' },
{ value: 30, name: '警告告警' },
{ value: 25, name: '提示告警' }
]
}
]
});
const resizeFn = () => pieChart.resize();
window.addEventListener('resize', resizeFn);
return () => {
pieChart.dispose();
window.removeEventListener('resize', resizeFn);
legend: { bottom: 0 },
series: [{
type: 'pie',
radius: ['40%', '70%'],
data: pieData.length ? pieData : [{ value: 1, name: '暂无数据' }],
color: ['#F53F3F', '#FF7D00', '#F7BA1E', '#165DFF'],
}]
};
chart.setOption(option);
const resize = () => chart.resize();
window.addEventListener('resize', resize);
return () => {
chart.dispose();
window.removeEventListener('resize', resize);
};
}, [statData]);
useEffect(() => {
fetchStat();
}, []);
return (
<div style={{ padding: 24 }}>
{/* 顶部统计卡片 保留你原来的图标样式 */}
<AntdCard title="告警概览" style={{ marginBottom: 24 }}>
<Row gutter={16}>
<Col span={8}>
<Space>
<div style={{ background: '#e6f4ff', padding: 8, borderRadius: 8 }}>
<AreaChartOutlined style={{ color: '#1677ff', fontSize: 20 }} />
<div style={{ padding: 24, background: '#f5f7fa', minHeight: '100vh' }}>
<Card title="告警概览" bordered={false} style={{ marginBottom: 24, borderRadius: 8 }}>
<Row gutter={16} align="middle">
<Col xs={24} sm={12} md={6}>
<Space size={16}>
<div style={{ width: 48, height: 48, background: '#FFF1F0', borderRadius: '50%', display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
<BellOutlined style={{ fontSize: 24, color: '#F53F3F' }} />
</div>
<div>
<div style={{ color: '#666' }}>今日告警</div>
<div style={{ fontWeight: 600, fontSize: 18 }}>12</div>
<div style={{ fontSize: 14, color: '#4E5969' }}>告警总数</div>
<div style={{ fontSize: 24, fontWeight: 700 }}>{statData.total || 0}</div>
</div>
</Space>
</Col>
<Col span={8}>
<Space>
<div style={{ background: '#fff7e6', padding: 8, borderRadius: 8 }}>
<AreaChartOutlined style={{ color: '#fa8c16', fontSize: 20 }} />
<Col xs={24} sm={12} md={6}>
<Space size={16}>
<div style={{ width: 48, height: 48, background: '#FFF1F0', borderRadius: '50%', display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
<ExclamationCircleOutlined style={{ fontSize: 24, color: '#F53F3F' }} />
</div>
<div>
<div style={{ color: '#666' }}>本周告警</div>
<div style={{ fontWeight: 600, fontSize: 18 }}>45</div>
<div style={{ fontSize: 14, color: '#4E5969' }}>严重告警</div>
<div style={{ fontSize: 24, fontWeight: 700 }}>{statData.levelStats?.[1] || 0}</div>
</div>
</Space>
</Col>
<Col span={8}>
<Space>
<div style={{ background: '#f6ffed', padding: 8, borderRadius: 8 }}>
<AreaChartOutlined style={{ color: '#52c41a', fontSize: 20 }} />
<Col xs={24} sm={12} md={6}>
<Space size={16}>
<div style={{ width: 48, height: 48, background: '#FFF7E8', borderRadius: '50%', display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
<ClockCircleOutlined style={{ fontSize: 24, color: '#FF7D00' }} />
</div>
<div>
<div style={{ color: '#666' }}>本月告警</div>
<div style={{ fontWeight: 600, fontSize: 18 }}>128</div>
<div style={{ fontSize: 14, color: '#4E5969' }}>待处理</div>
<div style={{ fontSize: 24, fontWeight: 700 }}>{statData.pending || 0}</div>
</div>
</Space>
</Col>
<Col xs={24} sm={12} md={6}>
<Space size={16}>
<div style={{ width: 48, height: 48, background: '#E8FFEA', borderRadius: '50%', display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
<CheckCircleOutlined style={{ fontSize: 24, color: '#00B42A' }} />
</div>
<div>
<div style={{ fontSize: 14, color: '#4E5969' }}>已关闭</div>
<div style={{ fontSize: 24, fontWeight: 700 }}>{statData.closed || 0}</div>
</div>
</Space>
</Col>
</Row>
</AntdCard>
</Card>
{/* 两个图表 左右各一半 并排显示 不空洞 */}
<Row gutter={16}>
<Col span={12}>
<AntdCard title="告警趋势统计">
<div ref={barChartRef} style={{ width: '100%', height: 350 }} />
</AntdCard>
<Col xs={24} md={12}>
<Card bordered={false} style={{ borderRadius: 8 }}>
<div ref={barChartRef} style={{ width: '100%', height: 360 }} />
</Card>
</Col>
<Col span={12}>
<AntdCard title="告警分类统计">
<div ref={pieChartRef} style={{ width: '100%', height: 350 }} />
</AntdCard>
<Col xs={24} md={12}>
<Card bordered={false} style={{ borderRadius: 8 }}>
<div ref={pieChartRef} style={{ width: '100%', height: 360 }} />
</Card>
</Col>
</Row>
</div>