mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-04 22:21:21 +09:00
Introduces clickable filter buttons to toggle CPU, memory, and disk metrics in MetricsGraph, replacing the static legend. Refines app name validation in both backend and frontend for template installation, allowing 2+ character names and improving error handling. WorkflowBuilder now handles missing workflow data gracefully and supports a fullscreen layout. Updates related styles for new filter legend and fullscreen workflow view.
314 lines
13 KiB
React
314 lines
13 KiB
React
import React, { useState, useEffect } from 'react';
|
|
import {
|
|
XAxis, YAxis, CartesianGrid,
|
|
Tooltip, ResponsiveContainer, Area, AreaChart
|
|
} from 'recharts';
|
|
import { Cpu, MemoryStick, HardDrive, TrendingUp } from 'lucide-react';
|
|
import api from '../services/api';
|
|
|
|
// Chart colors - using actual hex values for SVG compatibility
|
|
const CHART_COLORS = {
|
|
cpu: '#10b981', // Green
|
|
memory: '#6366f1', // Purple/Indigo
|
|
disk: '#f59e0b' // Amber/Orange
|
|
};
|
|
|
|
const MetricsGraph = ({ compact = false }) => {
|
|
const [data, setData] = useState(null);
|
|
const [period, setPeriod] = useState('1h');
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState(null);
|
|
const [visibleMetrics, setVisibleMetrics] = useState({
|
|
cpu: true,
|
|
memory: true,
|
|
disk: true
|
|
});
|
|
|
|
const periods = ['1h', '6h', '24h', '7d', '30d'];
|
|
|
|
const toggleMetric = (metric) => {
|
|
setVisibleMetrics(prev => ({
|
|
...prev,
|
|
[metric]: !prev[metric]
|
|
}));
|
|
};
|
|
|
|
useEffect(() => {
|
|
loadHistory();
|
|
}, [period]);
|
|
|
|
async function loadHistory() {
|
|
try {
|
|
setLoading(true);
|
|
const response = await api.getMetricsHistory(period);
|
|
setData(response);
|
|
setError(null);
|
|
} catch (err) {
|
|
setError(err.message);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
function formatTimestamp(isoString) {
|
|
const date = new Date(isoString);
|
|
if (period === '1h' || period === '6h') {
|
|
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
|
} else if (period === '24h') {
|
|
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
|
} else if (period === '7d') {
|
|
return date.toLocaleDateString([], { weekday: 'short', hour: '2-digit' });
|
|
} else {
|
|
return date.toLocaleDateString([], { month: 'short', day: 'numeric' });
|
|
}
|
|
}
|
|
|
|
if (loading && !data) {
|
|
return (
|
|
<div className="metrics-graph-card loading">
|
|
<div className="loading-spinner" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (error) {
|
|
return (
|
|
<div className="metrics-graph-card error">
|
|
<span>Failed to load metrics history</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!data || data.points === 0) {
|
|
return (
|
|
<div className="metrics-graph-card empty">
|
|
<TrendingUp size={24} />
|
|
<span>No historical data yet</span>
|
|
<span className="muted">Metrics are collected every minute</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const chartData = data.data.map(point => ({
|
|
time: formatTimestamp(point.timestamp),
|
|
cpu: point.cpu.percent,
|
|
memory: point.memory.percent,
|
|
disk: point.disk.percent
|
|
}));
|
|
|
|
const CustomTooltip = ({ active, payload, label }) => {
|
|
if (active && payload && payload.length) {
|
|
return (
|
|
<div className="metrics-tooltip">
|
|
<div className="tooltip-time">{label}</div>
|
|
{payload.map((entry, index) => (
|
|
<div key={index} className="tooltip-row">
|
|
<span className="tooltip-dot" style={{ backgroundColor: entry.stroke || entry.color }} />
|
|
<span className="tooltip-label">{entry.name}:</span>
|
|
<span className="tooltip-value" style={{ color: entry.stroke || entry.color }}>{entry.value?.toFixed(1)}%</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
return null;
|
|
};
|
|
|
|
if (compact) {
|
|
return (
|
|
<div className="metrics-graph-compact">
|
|
<div className="metrics-graph-header">
|
|
<div className="graph-title">
|
|
<TrendingUp size={16} />
|
|
<span>System Metrics</span>
|
|
</div>
|
|
<div className="period-selector">
|
|
{periods.map(p => (
|
|
<button
|
|
key={p}
|
|
className={`period-btn ${period === p ? 'active' : ''}`}
|
|
onClick={() => setPeriod(p)}
|
|
>
|
|
{p}
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
<div className="metrics-chart-container compact">
|
|
<ResponsiveContainer width="100%" height={120}>
|
|
<AreaChart data={chartData} margin={{ top: 5, right: 5, left: -20, bottom: 0 }}>
|
|
<defs>
|
|
<linearGradient id="cpuGradientCompact" x1="0" y1="0" x2="0" y2="1">
|
|
<stop offset="5%" stopColor={CHART_COLORS.cpu} stopOpacity={0.4} />
|
|
<stop offset="95%" stopColor={CHART_COLORS.cpu} stopOpacity={0} />
|
|
</linearGradient>
|
|
</defs>
|
|
<XAxis dataKey="time" tick={false} axisLine={false} />
|
|
<YAxis domain={[0, 100]} tick={{ fontSize: 10 }} axisLine={false} tickLine={false} />
|
|
<Tooltip content={<CustomTooltip />} />
|
|
<Area
|
|
type="monotone"
|
|
dataKey="cpu"
|
|
stroke={CHART_COLORS.cpu}
|
|
fill="url(#cpuGradientCompact)"
|
|
strokeWidth={2}
|
|
name="CPU"
|
|
/>
|
|
</AreaChart>
|
|
</ResponsiveContainer>
|
|
</div>
|
|
<div className="metrics-summary-compact">
|
|
<div className="summary-item">
|
|
<Cpu size={14} />
|
|
<span>{data.summary.cpu_avg}%</span>
|
|
</div>
|
|
<div className="summary-item">
|
|
<MemoryStick size={14} />
|
|
<span>{data.summary.memory_avg}%</span>
|
|
</div>
|
|
<div className="summary-item">
|
|
<HardDrive size={14} />
|
|
<span>{data.summary.disk_avg}%</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="metrics-graph-card">
|
|
<div className="metrics-graph-header">
|
|
<div className="graph-title">
|
|
<TrendingUp size={18} />
|
|
<span>System Metrics History</span>
|
|
</div>
|
|
<div className="period-selector">
|
|
{periods.map(p => (
|
|
<button
|
|
key={p}
|
|
className={`period-btn ${period === p ? 'active' : ''}`}
|
|
onClick={() => setPeriod(p)}
|
|
>
|
|
{p}
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Clickable filter legend */}
|
|
<div className="metrics-filter-legend">
|
|
<button
|
|
className={`filter-btn cpu ${visibleMetrics.cpu ? 'active' : ''}`}
|
|
onClick={() => toggleMetric('cpu')}
|
|
>
|
|
<Cpu size={14} />
|
|
<span>CPU</span>
|
|
</button>
|
|
<button
|
|
className={`filter-btn memory ${visibleMetrics.memory ? 'active' : ''}`}
|
|
onClick={() => toggleMetric('memory')}
|
|
>
|
|
<MemoryStick size={14} />
|
|
<span>Memory</span>
|
|
</button>
|
|
<button
|
|
className={`filter-btn disk ${visibleMetrics.disk ? 'active' : ''}`}
|
|
onClick={() => toggleMetric('disk')}
|
|
>
|
|
<HardDrive size={14} />
|
|
<span>Disk</span>
|
|
</button>
|
|
</div>
|
|
|
|
<div className="metrics-chart-container">
|
|
<ResponsiveContainer width="100%" height={280}>
|
|
<AreaChart data={chartData} margin={{ top: 10, right: 10, left: -10, bottom: 0 }}>
|
|
<defs>
|
|
{/* CPU Gradient - Green */}
|
|
<linearGradient id="cpuGradient" x1="0" y1="0" x2="0" y2="1">
|
|
<stop offset="5%" stopColor={CHART_COLORS.cpu} stopOpacity={0.3} />
|
|
<stop offset="95%" stopColor={CHART_COLORS.cpu} stopOpacity={0} />
|
|
</linearGradient>
|
|
{/* Memory Gradient - Purple */}
|
|
<linearGradient id="memoryGradient" x1="0" y1="0" x2="0" y2="1">
|
|
<stop offset="5%" stopColor={CHART_COLORS.memory} stopOpacity={0.3} />
|
|
<stop offset="95%" stopColor={CHART_COLORS.memory} stopOpacity={0} />
|
|
</linearGradient>
|
|
{/* Disk Gradient - Amber */}
|
|
<linearGradient id="diskGradient" x1="0" y1="0" x2="0" y2="1">
|
|
<stop offset="5%" stopColor={CHART_COLORS.disk} stopOpacity={0.3} />
|
|
<stop offset="95%" stopColor={CHART_COLORS.disk} stopOpacity={0} />
|
|
</linearGradient>
|
|
{/* Glow filters for lines */}
|
|
<filter id="glow" x="-20%" y="-20%" width="140%" height="140%">
|
|
<feGaussianBlur stdDeviation="2" result="coloredBlur" />
|
|
<feMerge>
|
|
<feMergeNode in="coloredBlur" />
|
|
<feMergeNode in="SourceGraphic" />
|
|
</feMerge>
|
|
</filter>
|
|
</defs>
|
|
<CartesianGrid strokeDasharray="3 3" stroke="#27272a" opacity={0.5} />
|
|
<XAxis
|
|
dataKey="time"
|
|
tick={{ fontSize: 11, fill: '#a1a1aa' }}
|
|
axisLine={{ stroke: '#27272a' }}
|
|
tickLine={false}
|
|
interval="preserveStartEnd"
|
|
/>
|
|
<YAxis
|
|
domain={[0, 100]}
|
|
tick={{ fontSize: 11, fill: '#a1a1aa' }}
|
|
axisLine={{ stroke: '#27272a' }}
|
|
tickLine={false}
|
|
tickFormatter={(value) => `${value}%`}
|
|
/>
|
|
<Tooltip content={<CustomTooltip />} />
|
|
{visibleMetrics.cpu && (
|
|
<Area
|
|
type="monotone"
|
|
dataKey="cpu"
|
|
stroke={CHART_COLORS.cpu}
|
|
fill="url(#cpuGradient)"
|
|
strokeWidth={2.5}
|
|
dot={false}
|
|
activeDot={{ r: 6, strokeWidth: 2, stroke: CHART_COLORS.cpu, fill: '#18181b' }}
|
|
name="CPU"
|
|
filter="url(#glow)"
|
|
/>
|
|
)}
|
|
{visibleMetrics.memory && (
|
|
<Area
|
|
type="monotone"
|
|
dataKey="memory"
|
|
stroke={CHART_COLORS.memory}
|
|
fill="url(#memoryGradient)"
|
|
strokeWidth={2.5}
|
|
dot={false}
|
|
activeDot={{ r: 6, strokeWidth: 2, stroke: CHART_COLORS.memory, fill: '#18181b' }}
|
|
name="Memory"
|
|
filter="url(#glow)"
|
|
/>
|
|
)}
|
|
{visibleMetrics.disk && (
|
|
<Area
|
|
type="monotone"
|
|
dataKey="disk"
|
|
stroke={CHART_COLORS.disk}
|
|
fill="url(#diskGradient)"
|
|
strokeWidth={2.5}
|
|
dot={false}
|
|
activeDot={{ r: 6, strokeWidth: 2, stroke: CHART_COLORS.disk, fill: '#18181b' }}
|
|
name="Disk"
|
|
filter="url(#glow)"
|
|
/>
|
|
)}
|
|
</AreaChart>
|
|
</ResponsiveContainer>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default MetricsGraph;
|