Update metrics-dialog.tsx

This commit is contained in:
MacRimi
2025-10-20 17:11:30 +02:00
parent 8e69a84e7a
commit 4dfcdcb0b2

View File

@@ -65,14 +65,50 @@ export function MetricsView({ vmid, vmName, vmType, metricType, onBack }: Metric
const result = await response.json() const result = await response.json()
console.log("[v0] Metrics data received:", result) console.log("[v0] Metrics data received:", result)
// Transform data for charts const transformedData = result.data.map((item: any) => {
const transformedData = result.data.map((item: any) => ({ const date = new Date(item.time * 1000)
time: new Date(item.time * 1000).toLocaleString("en-US", { let timeLabel = ""
// Format time based on timeframe
if (timeframe === "hour") {
// For 1 hour: show HH:mm
timeLabel = date.toLocaleString("en-US", {
hour: "2-digit",
minute: "2-digit",
hour12: false,
})
} else if (timeframe === "day") {
// For 24 hours: show HH:mm
timeLabel = date.toLocaleString("en-US", {
hour: "2-digit",
minute: "2-digit",
hour12: false,
})
} else if (timeframe === "week") {
// For 7 days: show Mon DD HH:mm
timeLabel = date.toLocaleString("en-US", {
month: "short", month: "short",
day: "numeric", day: "numeric",
hour: timeframe === "hour" ? "2-digit" : undefined, hour: "2-digit",
minute: timeframe === "hour" ? "2-digit" : undefined, minute: "2-digit",
}), hour12: false,
})
} else if (timeframe === "month") {
// For 30 days: show Mon DD
timeLabel = date.toLocaleString("en-US", {
month: "short",
day: "numeric",
})
} else {
// For 1 year: show Mon YYYY
timeLabel = date.toLocaleString("en-US", {
month: "short",
year: "numeric",
})
}
return {
time: timeLabel,
timestamp: item.time, timestamp: item.time,
cpu: item.cpu ? (item.cpu * 100).toFixed(2) : 0, cpu: item.cpu ? (item.cpu * 100).toFixed(2) : 0,
memory: item.mem ? ((item.mem / item.maxmem) * 100).toFixed(2) : 0, memory: item.mem ? ((item.mem / item.maxmem) * 100).toFixed(2) : 0,
@@ -82,7 +118,8 @@ export function MetricsView({ vmid, vmName, vmType, metricType, onBack }: Metric
netout: item.netout ? (item.netout / 1024 / 1024).toFixed(2) : 0, netout: item.netout ? (item.netout / 1024 / 1024).toFixed(2) : 0,
diskread: item.diskread ? (item.diskread / 1024 / 1024).toFixed(2) : 0, diskread: item.diskread ? (item.diskread / 1024 / 1024).toFixed(2) : 0,
diskwrite: item.diskwrite ? (item.diskwrite / 1024 / 1024).toFixed(2) : 0, diskwrite: item.diskwrite ? (item.diskwrite / 1024 / 1024).toFixed(2) : 0,
})) }
})
console.log("[v0] Transformed data:", transformedData.length, "points") console.log("[v0] Transformed data:", transformedData.length, "points")
setData(transformedData) setData(transformedData)
@@ -119,13 +156,24 @@ export function MetricsView({ vmid, vmName, vmType, metricType, onBack }: Metric
) )
} }
const tickInterval = Math.ceil(data.length / 10)
switch (metricType) { switch (metricType) {
case "cpu": case "cpu":
return ( return (
<ResponsiveContainer width="100%" height={400}> <ResponsiveContainer width="100%" height={400}>
<LineChart data={data}> <LineChart data={data}>
<CartesianGrid strokeDasharray="3 3" stroke="hsl(var(--border))" /> <CartesianGrid strokeDasharray="3 3" stroke="hsl(var(--border))" />
<XAxis dataKey="time" stroke="currentColor" tick={{ fill: "currentColor" }} className="text-foreground" /> <XAxis
dataKey="time"
stroke="currentColor"
tick={{ fill: "currentColor", fontSize: 12 }}
className="text-foreground"
angle={-45}
textAnchor="end"
height={80}
interval={tickInterval}
/>
<YAxis <YAxis
stroke="currentColor" stroke="currentColor"
tick={{ fill: "currentColor" }} tick={{ fill: "currentColor" }}
@@ -147,7 +195,16 @@ export function MetricsView({ vmid, vmName, vmType, metricType, onBack }: Metric
<ResponsiveContainer width="100%" height={400}> <ResponsiveContainer width="100%" height={400}>
<LineChart data={data}> <LineChart data={data}>
<CartesianGrid strokeDasharray="3 3" stroke="hsl(var(--border))" /> <CartesianGrid strokeDasharray="3 3" stroke="hsl(var(--border))" />
<XAxis dataKey="time" stroke="currentColor" tick={{ fill: "currentColor" }} className="text-foreground" /> <XAxis
dataKey="time"
stroke="currentColor"
tick={{ fill: "currentColor", fontSize: 12 }}
className="text-foreground"
angle={-45}
textAnchor="end"
height={80}
interval={tickInterval}
/>
<YAxis <YAxis
stroke="currentColor" stroke="currentColor"
tick={{ fill: "currentColor" }} tick={{ fill: "currentColor" }}
@@ -169,7 +226,16 @@ export function MetricsView({ vmid, vmName, vmType, metricType, onBack }: Metric
<ResponsiveContainer width="100%" height={400}> <ResponsiveContainer width="100%" height={400}>
<LineChart data={data}> <LineChart data={data}>
<CartesianGrid strokeDasharray="3 3" stroke="hsl(var(--border))" /> <CartesianGrid strokeDasharray="3 3" stroke="hsl(var(--border))" />
<XAxis dataKey="time" stroke="currentColor" tick={{ fill: "currentColor" }} className="text-foreground" /> <XAxis
dataKey="time"
stroke="currentColor"
tick={{ fill: "currentColor", fontSize: 12 }}
className="text-foreground"
angle={-45}
textAnchor="end"
height={80}
interval={tickInterval}
/>
<YAxis <YAxis
stroke="currentColor" stroke="currentColor"
tick={{ fill: "currentColor" }} tick={{ fill: "currentColor" }}
@@ -181,8 +247,8 @@ export function MetricsView({ vmid, vmName, vmType, metricType, onBack }: Metric
labelStyle={{ color: "hsl(var(--foreground))" }} labelStyle={{ color: "hsl(var(--foreground))" }}
/> />
<Legend /> <Legend />
<Line type="monotone" dataKey="netin" stroke="#3b82f6" name="In (MB)" strokeWidth={2} dot={false} /> <Line type="monotone" dataKey="netin" stroke="#10b981" name="Download (MB)" strokeWidth={2} dot={false} />
<Line type="monotone" dataKey="netout" stroke="#8b5cf6" name="Out (MB)" strokeWidth={2} dot={false} /> <Line type="monotone" dataKey="netout" stroke="#3b82f6" name="Upload (MB)" strokeWidth={2} dot={false} />
</LineChart> </LineChart>
</ResponsiveContainer> </ResponsiveContainer>
) )
@@ -192,7 +258,16 @@ export function MetricsView({ vmid, vmName, vmType, metricType, onBack }: Metric
<ResponsiveContainer width="100%" height={400}> <ResponsiveContainer width="100%" height={400}>
<LineChart data={data}> <LineChart data={data}>
<CartesianGrid strokeDasharray="3 3" stroke="hsl(var(--border))" /> <CartesianGrid strokeDasharray="3 3" stroke="hsl(var(--border))" />
<XAxis dataKey="time" stroke="currentColor" tick={{ fill: "currentColor" }} className="text-foreground" /> <XAxis
dataKey="time"
stroke="currentColor"
tick={{ fill: "currentColor", fontSize: 12 }}
className="text-foreground"
angle={-45}
textAnchor="end"
height={80}
interval={tickInterval}
/>
<YAxis <YAxis
stroke="currentColor" stroke="currentColor"
tick={{ fill: "currentColor" }} tick={{ fill: "currentColor" }}
@@ -208,7 +283,7 @@ export function MetricsView({ vmid, vmName, vmType, metricType, onBack }: Metric
<Line <Line
type="monotone" type="monotone"
dataKey="diskwrite" dataKey="diskwrite"
stroke="#f59e0b" stroke="#3b82f6"
name="Write (MB)" name="Write (MB)"
strokeWidth={2} strokeWidth={2}
dot={false} dot={false}