mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-02 21:25:26 +09:00
SystemService.get_server_time fell through to Python's time.tzname on hosts without /etc/timezone or timedatectl (e.g. Windows dev), yielding a display name like "Eastern Daylight Time" — not a valid IANA/Intl key. The dashboard MetricsGraph then crashed with RangeError: Invalid time zone specified. - Backend: add a cross-platform tzlocal fallback + an IANA-validity guard so timezone_id is always an IANA key or null (display name stays in timezone_name). Declare tzlocal in requirements. - Frontend: MetricsGraph validates the zone and falls back to local time instead of throwing (defense in depth). - Test: test_server_time_timezone.py. ServerKit's timezone feature (get/list/set via /api/v1/system/timezone) is unchanged — this only fixes the value reported on non-Linux hosts. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
339 lines
14 KiB
React
339 lines
14 KiB
React
import { useState, useEffect, useMemo } 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 series colors — redesign "infra console" palette (see
|
|
// docs/REDESIGN_MAP.md). Kept as hex (not CSS var()) on purpose: var() does not
|
|
// reliably resolve inside SVG presentation attributes, so hardcoding keeps the
|
|
// series from rendering blank. Grid/axis/ticks ARE themed via CSS overrides in
|
|
// _metrics-graph.scss.
|
|
const CHART_COLORS = {
|
|
cpu: '#8b93ff', // accent-bright (periwinkle)
|
|
memory: '#3ddc97', // green (RAM)
|
|
disk: '#f5b945' // amber (Disk)
|
|
};
|
|
|
|
// Intl only accepts IANA zone names (e.g. "America/New_York"). A server on
|
|
// Windows can report a display name like "Eastern Daylight Time", which makes
|
|
// toLocale*String throw a RangeError and crash the chart. Validate once and
|
|
// fall back to the browser's local zone rather than blowing up the render.
|
|
function safeTimeZone(tz) {
|
|
if (!tz) return undefined;
|
|
try {
|
|
new Intl.DateTimeFormat([], { timeZone: tz });
|
|
return tz;
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
const MetricsGraph = ({ compact = false, timezone, serverId }) => {
|
|
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, serverId]);
|
|
|
|
async function loadHistory() {
|
|
try {
|
|
setLoading(true);
|
|
const response = serverId
|
|
? await api.getServerMetricsHistory(serverId, period)
|
|
: await api.getMetricsHistory(period);
|
|
setData(response);
|
|
setError(null);
|
|
} catch (err) {
|
|
setError(err.message);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
function formatTimestamp(isoString) {
|
|
const date = new Date(isoString);
|
|
const tz = safeTimeZone(timezone);
|
|
if (period === '1h' || period === '6h' || period === '24h') {
|
|
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', timeZone: tz });
|
|
} else if (period === '7d') {
|
|
return date.toLocaleDateString([], { weekday: 'short', hour: '2-digit', timeZone: tz });
|
|
} else {
|
|
return date.toLocaleDateString([], { month: 'short', day: 'numeric', timeZone: tz });
|
|
}
|
|
}
|
|
|
|
const chartData = data?.data?.map(point => ({
|
|
time: formatTimestamp(point.timestamp),
|
|
cpu: point.cpu?.percent ?? point.cpu_percent ?? 0,
|
|
memory: point.memory?.percent ?? point.memory_percent ?? 0,
|
|
disk: point.disk?.percent ?? point.disk_percent ?? 0
|
|
})) || [];
|
|
|
|
// Auto-zoom: compute Y-axis ceiling from visible metrics
|
|
const yDomain = useMemo(() => {
|
|
if (chartData.length === 0) return [0, 100];
|
|
|
|
const activeKeys = Object.entries(visibleMetrics)
|
|
.filter(([, visible]) => visible)
|
|
.map(([key]) => key);
|
|
|
|
if (activeKeys.length === 0) return [0, 100];
|
|
|
|
let maxVal = 0;
|
|
chartData.forEach(point => {
|
|
activeKeys.forEach(key => {
|
|
if (point[key] > maxVal) maxVal = point[key];
|
|
});
|
|
});
|
|
|
|
// Add 20% headroom, floor at 10%
|
|
const ceiling = Math.max(10, maxVal * 1.2);
|
|
// Snap to nearest nice tick value
|
|
const steps = [5, 10, 15, 20, 25, 30, 40, 50, 60, 75, 100];
|
|
const niceMax = steps.find(s => s >= ceiling) || 100;
|
|
|
|
return [0, niceMax];
|
|
}, [chartData, visibleMetrics]);
|
|
|
|
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 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 type="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">
|
|
<span>Real-time Performance</span>
|
|
</div>
|
|
<div className="metrics-filter-legend">
|
|
<button type="button"
|
|
className={`filter-btn cpu ${visibleMetrics.cpu ? 'active' : ''}`}
|
|
onClick={() => toggleMetric('cpu')}
|
|
>
|
|
CPU
|
|
</button>
|
|
<button type="button"
|
|
className={`filter-btn memory ${visibleMetrics.memory ? 'active' : ''}`}
|
|
onClick={() => toggleMetric('memory')}
|
|
>
|
|
RAM
|
|
</button>
|
|
<button type="button"
|
|
className={`filter-btn disk ${visibleMetrics.disk ? 'active' : ''}`}
|
|
onClick={() => toggleMetric('disk')}
|
|
>
|
|
Disk
|
|
</button>
|
|
</div>
|
|
<div className="period-selector">
|
|
{periods.map(p => (
|
|
<button type="button"
|
|
key={p}
|
|
className={`period-btn ${period === p ? 'active' : ''}`}
|
|
onClick={() => setPeriod(p)}
|
|
>
|
|
{p}
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
|
|
<div className="metrics-chart-container">
|
|
<ResponsiveContainer width="100%" height={420}>
|
|
<AreaChart data={chartData} margin={{ top: 10, right: 10, left: -10, bottom: 0 }}>
|
|
<defs>
|
|
{/* CPU Gradient - Purple/Indigo */}
|
|
<linearGradient id="cpuGradient" x1="0" y1="0" x2="0" y2="1">
|
|
<stop offset="5%" stopColor={CHART_COLORS.cpu} stopOpacity={0.5} />
|
|
<stop offset="95%" stopColor={CHART_COLORS.cpu} stopOpacity={0} />
|
|
</linearGradient>
|
|
{/* Memory Gradient - Green */}
|
|
<linearGradient id="memoryGradient" x1="0" y1="0" x2="0" y2="1">
|
|
<stop offset="5%" stopColor={CHART_COLORS.memory} stopOpacity={0.5} />
|
|
<stop offset="95%" stopColor={CHART_COLORS.memory} stopOpacity={0} />
|
|
</linearGradient>
|
|
{/* Disk Gradient - Orange */}
|
|
<linearGradient id="diskGradient" x1="0" y1="0" x2="0" y2="1">
|
|
<stop offset="5%" stopColor={CHART_COLORS.disk} stopOpacity={0.5} />
|
|
<stop offset="95%" stopColor={CHART_COLORS.disk} stopOpacity={0} />
|
|
</linearGradient>
|
|
</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={yDomain}
|
|
tick={{ fontSize: 11, fill: '#a1a1aa' }}
|
|
axisLine={{ stroke: '#27272a' }}
|
|
tickLine={false}
|
|
tickFormatter={(value) => `${value}%`}
|
|
/>
|
|
<Tooltip content={<CustomTooltip />} />
|
|
{visibleMetrics.cpu && (
|
|
<Area
|
|
type="stepAfter"
|
|
dataKey="cpu"
|
|
stroke={CHART_COLORS.cpu}
|
|
fill="url(#cpuGradient)"
|
|
strokeWidth={1.5}
|
|
dot={false}
|
|
name="CPU"
|
|
/>
|
|
)}
|
|
{visibleMetrics.memory && (
|
|
<Area
|
|
type="stepAfter"
|
|
dataKey="memory"
|
|
stroke={CHART_COLORS.memory}
|
|
fill="url(#memoryGradient)"
|
|
strokeWidth={1.5}
|
|
dot={false}
|
|
name="RAM"
|
|
/>
|
|
)}
|
|
{visibleMetrics.disk && (
|
|
<Area
|
|
type="stepAfter"
|
|
dataKey="disk"
|
|
stroke={CHART_COLORS.disk}
|
|
fill="url(#diskGradient)"
|
|
strokeWidth={1.5}
|
|
dot={false}
|
|
name="Disk"
|
|
/>
|
|
)}
|
|
</AreaChart>
|
|
</ResponsiveContainer>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default MetricsGraph;
|