blob: be9eb99fc73270868ae1972438c2d5c5c90b447d [file] [log] [blame]
TRM-coding85e5c322025-06-18 19:49:21 +08001import React, { useState, useEffect } from 'react';
2import {
3 ResponsiveContainer, LineChart, Line,
4 XAxis, YAxis, Tooltip, CartesianGrid, Legend
5} from 'recharts';
6import { fetchSysCost } from '../api/posts_trm';
7
8function PerformanceLogs({ userId }) {
9 const [data, setData] = useState([]);
10
11 useEffect(() => {
12 fetchSysCost(userId)
TRM-coding882dc442025-06-18 20:13:21 +080013 .then(list => {
14 const msList = list.map(item => ({
15 ...item,
16 elapsed_time: item.elapsed_time * 1000,
17 cpu_user: item.cpu_user * 1000,
18 cpu_system: item.cpu_system * 1000,
19 memory_rss: item.memory_rss / (1024 * 1024*8), // convert bytes to MB
20 record_time_ts: new Date(item.record_time).getTime() // add numeric timestamp
21 }));
22 console.log('Converted data:', msList[0]); // debug first item
23 setData(msList);
24 })
TRM-coding85e5c322025-06-18 19:49:21 +080025 .catch(err => console.error('fetchSysCost error:', err));
26 }, [userId]);
27
28 return (
29 <section className="dashboard-performance">
TRM-coding882dc442025-06-18 20:13:21 +080030 {/* 响应时间图表 */}
31 <div style={{ marginBottom: '30px' }}>
32 <h3>响应时间</h3>
33 <ResponsiveContainer width="100%" height={300}>
34 <LineChart data={data} margin={{ top: 20, right: 30, left: 20, bottom: 5 }}>
35 <CartesianGrid strokeDasharray="3 3" />
36 <XAxis
37 dataKey="record_time_ts"
38 type="number"
39 domain={['dataMin', 'dataMax']}
40 tickFormatter={ts => new Date(ts).toLocaleTimeString()}
41 />
42 <YAxis domain={[0, 'auto']} label={{ value: '时间 (ms)', angle: -90, position: 'insideLeft' }} />
43 <Tooltip formatter={(val) => typeof val === 'number' ? `${val.toFixed(2)} ms` : val} />
44 <Legend />
45 <Line type="monotone" dataKey="elapsed_time" stroke="#8884d8" name="响应时间 (ms)" />
46 </LineChart>
47 </ResponsiveContainer>
48 </div>
49
50 {/* CPU时间图表 */}
51 <div style={{ marginBottom: '30px' }}>
52 <h3>CPU 使用时间</h3>
53 <ResponsiveContainer width="100%" height={300}>
54 <LineChart data={data} margin={{ top: 20, right: 30, left: 20, bottom: 5 }}>
55 <CartesianGrid strokeDasharray="3 3" />
56 <XAxis
57 dataKey="record_time_ts"
58 type="number"
59 domain={['dataMin', 'dataMax']}
60 tickFormatter={ts => new Date(ts).toLocaleTimeString()}
61 />
62 <YAxis domain={[0, 'auto']} label={{ value: '时间 (ms)', angle: -90, position: 'insideLeft' }} />
63 <Tooltip formatter={(val) => typeof val === 'number' ? `${val.toFixed(2)} ms` : val} />
64 <Legend />
65 <Line type="monotone" dataKey="cpu_user" stroke="#82ca9d" name="CPU 用户时间 (ms)" />
66 <Line type="monotone" dataKey="cpu_system" stroke="#ffc658" name="CPU 系统时间 (ms)" />
67 </LineChart>
68 </ResponsiveContainer>
69 </div>
70
71 {/* 内存图表 */}
72 <div style={{ marginBottom: '30px' }}>
73 <h3>内存使用</h3>
74 <ResponsiveContainer width="100%" height={300}>
75 <LineChart data={data} margin={{ top: 20, right: 30, left: 20, bottom: 5 }}>
76 <CartesianGrid strokeDasharray="3 3" />
77 <XAxis
78 dataKey="record_time_ts"
79 type="number"
80 domain={['dataMin', 'dataMax']}
81 tickFormatter={ts => new Date(ts).toLocaleTimeString()}
82 />
83 <YAxis domain={[0, 'auto']} label={{ value: '内存 (MB)', angle: -90, position: 'insideLeft' }} />
84 <Tooltip formatter={(val) => typeof val === 'number' ? `${val.toFixed(2)} MB` : val} />
85 <Legend />
86 <Line type="monotone" dataKey="memory_rss" stroke="#ff7300" name="内存 RSS" />
87 </LineChart>
88 </ResponsiveContainer>
89 </div>
TRM-coding85e5c322025-06-18 19:49:21 +080090 </section>
91 );
92}
93
94export default PerformanceLogs;