Files
ProxMenux/AppImage/components/metrics-dialog.tsx

450 lines
15 KiB
TypeScript
Raw Normal View History

2025-10-19 16:51:52 +02:00
"use client"
import { useState, useEffect } from "react"
import { Button } from "@/components/ui/button"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
import { ArrowLeft, Loader2 } from "lucide-react"
2025-10-20 18:39:12 +02:00
import { AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, Legend } from "recharts"
2025-10-19 16:51:52 +02:00
2025-10-19 17:29:23 +02:00
interface MetricsViewProps {
2025-10-19 16:51:52 +02:00
vmid: number
vmName: string
vmType: "qemu" | "lxc"
metricType: "cpu" | "memory" | "network" | "disk"
2025-10-19 17:29:23 +02:00
onBack: () => void
2025-10-19 16:51:52 +02:00
}
const TIMEFRAME_OPTIONS = [
2025-10-19 17:29:23 +02:00
{ value: "hour", label: "1 Hour" },
{ value: "day", label: "24 Hours" },
{ value: "week", label: "7 Days" },
{ value: "month", label: "30 Days" },
{ value: "year", label: "1 Year" },
2025-10-19 16:51:52 +02:00
]
const METRIC_TITLES = {
2025-10-19 17:29:23 +02:00
cpu: "CPU Usage",
memory: "Memory Usage",
network: "Network Traffic",
disk: "Disk I/O",
2025-10-19 16:51:52 +02:00
}
2025-10-19 17:29:23 +02:00
export function MetricsView({ vmid, vmName, vmType, metricType, onBack }: MetricsViewProps) {
2025-10-19 16:51:52 +02:00
const [timeframe, setTimeframe] = useState("week")
const [data, setData] = useState<any[]>([])
const [loading, setLoading] = useState(false)
const [error, setError] = useState<string | null>(null)
useEffect(() => {
2025-10-19 17:29:23 +02:00
fetchMetrics()
}, [vmid, timeframe])
2025-10-19 16:51:52 +02:00
const fetchMetrics = async () => {
setLoading(true)
setError(null)
2025-10-19 17:29:23 +02:00
console.log("[v0] Fetching metrics for VMID:", vmid, "Timeframe:", timeframe, "Type:", vmType)
2025-10-19 16:51:52 +02:00
try {
2025-10-19 17:53:39 +02:00
const baseUrl =
typeof window !== "undefined" ? `${window.location.protocol}//${window.location.hostname}:8008` : ""
const apiUrl = `${baseUrl}/api/vms/${vmid}/metrics?timeframe=${timeframe}`
console.log("[v0] Fetching from URL:", apiUrl)
const response = await fetch(apiUrl)
2025-10-19 16:51:52 +02:00
2025-10-19 17:29:23 +02:00
console.log("[v0] Response status:", response.status)
2025-10-19 16:51:52 +02:00
if (!response.ok) {
2025-10-19 17:29:23 +02:00
const errorData = await response.json()
console.error("[v0] Error response:", errorData)
throw new Error(errorData.error || "Failed to fetch metrics")
2025-10-19 16:51:52 +02:00
}
const result = await response.json()
2025-10-19 17:29:23 +02:00
console.log("[v0] Metrics data received:", result)
2025-10-19 16:51:52 +02:00
2025-10-20 17:11:30 +02:00
const transformedData = result.data.map((item: any) => {
const date = new Date(item.time * 1000)
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",
day: "numeric",
hour: "2-digit",
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,
2025-10-20 20:00:29 +02:00
cpu: item.cpu ? Number((item.cpu * 100).toFixed(2)) : 0,
memory: item.mem ? Number(((item.mem / item.maxmem) * 100).toFixed(2)) : 0,
memoryMB: item.mem ? Number((item.mem / 1024 / 1024).toFixed(0)) : 0,
maxMemoryMB: item.maxmem ? Number((item.maxmem / 1024 / 1024).toFixed(0)) : 0,
netin: item.netin ? Number((item.netin / 1024 / 1024).toFixed(2)) : 0,
netout: item.netout ? Number((item.netout / 1024 / 1024).toFixed(2)) : 0,
diskread: item.diskread ? Number((item.diskread / 1024 / 1024).toFixed(2)) : 0,
diskwrite: item.diskwrite ? Number((item.diskwrite / 1024 / 1024).toFixed(2)) : 0,
2025-10-20 17:11:30 +02:00
}
})
2025-10-19 16:51:52 +02:00
2025-10-19 17:29:23 +02:00
console.log("[v0] Transformed data:", transformedData.length, "points")
2025-10-19 16:51:52 +02:00
setData(transformedData)
2025-10-19 17:29:23 +02:00
} catch (err: any) {
2025-10-19 16:51:52 +02:00
console.error("[v0] Error fetching metrics:", err)
2025-10-19 17:29:23 +02:00
setError(err.message || "Error loading metrics")
2025-10-19 16:51:52 +02:00
} finally {
setLoading(false)
}
}
2025-10-20 18:39:12 +02:00
const formatXAxisTick = (tick: any) => {
return tick
}
2025-10-19 16:51:52 +02:00
const renderChart = () => {
if (loading) {
return (
2025-10-20 18:39:12 +02:00
<div className="flex items-center justify-center h-[400px]">
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
2025-10-19 16:51:52 +02:00
</div>
)
}
if (error) {
return (
2025-10-20 18:39:12 +02:00
<div className="flex items-center justify-center h-[400px]">
<p className="text-red-500">{error}</p>
2025-10-19 16:51:52 +02:00
</div>
)
}
if (data.length === 0) {
return (
2025-10-20 18:39:12 +02:00
<div className="flex items-center justify-center h-[400px]">
2025-10-19 17:29:23 +02:00
<p className="text-muted-foreground">No data available</p>
2025-10-19 16:51:52 +02:00
</div>
)
}
2025-10-20 18:39:12 +02:00
// Calculate tick interval based on data length
const tickInterval = Math.ceil(data.length / 8)
2025-10-20 17:11:30 +02:00
2025-10-19 16:51:52 +02:00
switch (metricType) {
case "cpu":
2025-10-20 20:00:29 +02:00
const maxCpuValue = Math.max(...data.map((d) => d.cpu || 0))
const cpuDomainMax = Math.ceil(maxCpuValue * 1.15) // 15% margin
2025-10-20 20:17:46 +02:00
console.log("[v0] CPU - Max value:", maxCpuValue, "Domain max:", cpuDomainMax, "Data points:", data.length)
console.log("[v0] CPU - Sample data:", data.slice(0, 3))
2025-10-20 20:00:29 +02:00
2025-10-19 16:51:52 +02:00
return (
<ResponsiveContainer width="100%" height={400}>
2025-10-20 19:40:59 +02:00
<AreaChart data={data} margin={{ bottom: 100 }}>
2025-10-20 18:39:12 +02:00
<CartesianGrid strokeDasharray="3 3" stroke="currentColor" className="text-border" />
2025-10-20 17:11:30 +02:00
<XAxis
dataKey="time"
stroke="currentColor"
className="text-foreground"
2025-10-20 18:39:12 +02:00
tick={{ fill: "currentColor" }}
2025-10-20 17:11:30 +02:00
angle={-45}
textAnchor="end"
height={80}
interval={tickInterval}
2025-10-20 18:39:12 +02:00
tickFormatter={formatXAxisTick}
2025-10-20 17:11:30 +02:00
/>
2025-10-20 16:43:07 +02:00
<YAxis
2025-10-20 16:52:16 +02:00
stroke="currentColor"
2025-10-20 18:39:12 +02:00
className="text-foreground"
2025-10-20 16:52:16 +02:00
tick={{ fill: "currentColor" }}
label={{ value: "%", angle: -90, position: "insideLeft", fill: "currentColor" }}
2025-10-20 20:00:29 +02:00
domain={[0, cpuDomainMax]}
2025-10-20 18:39:12 +02:00
allowDataOverflow={false}
2025-10-20 16:43:07 +02:00
/>
2025-10-19 16:51:52 +02:00
<Tooltip
2025-10-20 18:39:12 +02:00
contentStyle={{
backgroundColor: "hsl(var(--background))",
border: "1px solid hsl(var(--border))",
borderRadius: "6px",
}}
2025-10-19 16:51:52 +02:00
/>
2025-10-20 19:40:59 +02:00
<Legend wrapperStyle={{ paddingTop: "20px" }} />
2025-10-20 18:39:12 +02:00
<Area
type="monotone"
dataKey="cpu"
stroke="#3b82f6"
strokeWidth={2}
fill="#3b82f6"
fillOpacity={0.3}
name="CPU %"
/>
</AreaChart>
2025-10-19 16:51:52 +02:00
</ResponsiveContainer>
)
case "memory":
2025-10-20 20:00:29 +02:00
const maxMemoryValue = Math.max(...data.map((d) => d.memory || 0))
const memoryDomainMax = Math.ceil(maxMemoryValue * 1.15) // 15% margin
2025-10-20 20:17:46 +02:00
console.log(
"[v0] Memory - Max value:",
maxMemoryValue,
"Domain max:",
memoryDomainMax,
"Data points:",
data.length,
)
console.log("[v0] Memory - Sample data:", data.slice(0, 3))
2025-10-20 20:00:29 +02:00
2025-10-19 16:51:52 +02:00
return (
<ResponsiveContainer width="100%" height={400}>
2025-10-20 19:40:59 +02:00
<AreaChart data={data} margin={{ bottom: 100 }}>
2025-10-20 18:39:12 +02:00
<CartesianGrid strokeDasharray="3 3" stroke="currentColor" className="text-border" />
2025-10-20 17:11:30 +02:00
<XAxis
dataKey="time"
stroke="currentColor"
className="text-foreground"
2025-10-20 18:39:12 +02:00
tick={{ fill: "currentColor" }}
2025-10-20 17:11:30 +02:00
angle={-45}
textAnchor="end"
height={80}
interval={tickInterval}
2025-10-20 18:39:12 +02:00
tickFormatter={formatXAxisTick}
2025-10-20 17:11:30 +02:00
/>
2025-10-20 16:43:07 +02:00
<YAxis
2025-10-20 16:52:16 +02:00
stroke="currentColor"
2025-10-20 18:39:12 +02:00
className="text-foreground"
2025-10-20 16:52:16 +02:00
tick={{ fill: "currentColor" }}
label={{ value: "%", angle: -90, position: "insideLeft", fill: "currentColor" }}
2025-10-20 20:00:29 +02:00
domain={[0, memoryDomainMax]}
2025-10-20 18:39:12 +02:00
allowDataOverflow={false}
2025-10-20 16:43:07 +02:00
/>
2025-10-19 16:51:52 +02:00
<Tooltip
2025-10-20 18:39:12 +02:00
contentStyle={{
backgroundColor: "hsl(var(--background))",
border: "1px solid hsl(var(--border))",
borderRadius: "6px",
}}
2025-10-19 16:51:52 +02:00
/>
2025-10-20 19:40:59 +02:00
<Legend wrapperStyle={{ paddingTop: "20px" }} />
2025-10-20 18:39:12 +02:00
<Area
type="monotone"
dataKey="memory"
stroke="#10b981"
fill="#10b981"
fillOpacity={0.3}
strokeWidth={2}
name="Memory %"
/>
</AreaChart>
2025-10-19 16:51:52 +02:00
</ResponsiveContainer>
)
case "network":
2025-10-20 19:40:59 +02:00
const maxNetworkValue = Math.max(...data.map((d) => Math.max(d.netin || 0, d.netout || 0)))
2025-10-20 20:00:29 +02:00
const networkDomainMax = Math.ceil(maxNetworkValue * 1.15) // 15% margin
2025-10-20 20:17:46 +02:00
console.log(
"[v0] Network - Max value:",
maxNetworkValue,
"Domain max:",
networkDomainMax,
"Data points:",
data.length,
)
console.log("[v0] Network - Sample data:", data.slice(0, 3))
console.log(
"[v0] Network - All netin values:",
data.map((d) => d.netin),
)
console.log(
"[v0] Network - All netout values:",
data.map((d) => d.netout),
)
2025-10-20 19:40:59 +02:00
2025-10-19 16:51:52 +02:00
return (
<ResponsiveContainer width="100%" height={400}>
2025-10-20 19:40:59 +02:00
<AreaChart data={data} margin={{ bottom: 100 }}>
2025-10-20 18:39:12 +02:00
<CartesianGrid strokeDasharray="3 3" stroke="currentColor" className="text-border" />
2025-10-20 17:11:30 +02:00
<XAxis
dataKey="time"
stroke="currentColor"
className="text-foreground"
2025-10-20 18:39:12 +02:00
tick={{ fill: "currentColor" }}
2025-10-20 17:11:30 +02:00
angle={-45}
textAnchor="end"
height={80}
interval={tickInterval}
2025-10-20 18:39:12 +02:00
tickFormatter={formatXAxisTick}
2025-10-20 17:11:30 +02:00
/>
2025-10-19 16:51:52 +02:00
<YAxis
2025-10-20 16:52:16 +02:00
stroke="currentColor"
2025-10-20 18:39:12 +02:00
className="text-foreground"
2025-10-20 16:52:16 +02:00
tick={{ fill: "currentColor" }}
label={{ value: "MB", angle: -90, position: "insideLeft", fill: "currentColor" }}
2025-10-20 19:40:59 +02:00
domain={[0, networkDomainMax]}
2025-10-20 18:39:12 +02:00
allowDataOverflow={false}
2025-10-19 16:51:52 +02:00
/>
<Tooltip
2025-10-20 18:39:12 +02:00
contentStyle={{
backgroundColor: "hsl(var(--background))",
border: "1px solid hsl(var(--border))",
borderRadius: "6px",
}}
2025-10-19 16:51:52 +02:00
/>
2025-10-20 19:40:59 +02:00
<Legend wrapperStyle={{ paddingTop: "20px" }} />
2025-10-20 18:39:12 +02:00
<Area
type="monotone"
dataKey="netin"
stroke="#10b981"
fill="#10b981"
fillOpacity={0.3}
strokeWidth={2}
name="Download (MB)"
/>
<Area
type="monotone"
dataKey="netout"
stroke="#3b82f6"
fill="#3b82f6"
fillOpacity={0.3}
strokeWidth={2}
name="Upload (MB)"
/>
</AreaChart>
2025-10-19 16:51:52 +02:00
</ResponsiveContainer>
)
case "disk":
2025-10-20 19:40:59 +02:00
const maxDiskValue = Math.max(...data.map((d) => Math.max(d.diskread || 0, d.diskwrite || 0)))
2025-10-20 20:00:29 +02:00
const diskDomainMax = Math.ceil(maxDiskValue * 1.15) // 15% margin
2025-10-20 20:17:46 +02:00
console.log("[v0] Disk - Max value:", maxDiskValue, "Domain max:", diskDomainMax, "Data points:", data.length)
console.log("[v0] Disk - Sample data:", data.slice(0, 3))
2025-10-20 19:40:59 +02:00
2025-10-19 16:51:52 +02:00
return (
<ResponsiveContainer width="100%" height={400}>
2025-10-20 19:40:59 +02:00
<AreaChart data={data} margin={{ bottom: 100 }}>
2025-10-20 18:39:12 +02:00
<CartesianGrid strokeDasharray="3 3" stroke="currentColor" className="text-border" />
2025-10-20 17:11:30 +02:00
<XAxis
dataKey="time"
stroke="currentColor"
className="text-foreground"
2025-10-20 18:39:12 +02:00
tick={{ fill: "currentColor" }}
2025-10-20 17:11:30 +02:00
angle={-45}
textAnchor="end"
height={80}
interval={tickInterval}
2025-10-20 18:39:12 +02:00
tickFormatter={formatXAxisTick}
2025-10-20 17:11:30 +02:00
/>
2025-10-19 16:51:52 +02:00
<YAxis
2025-10-20 16:52:16 +02:00
stroke="currentColor"
2025-10-20 18:39:12 +02:00
className="text-foreground"
2025-10-20 16:52:16 +02:00
tick={{ fill: "currentColor" }}
label={{ value: "MB", angle: -90, position: "insideLeft", fill: "currentColor" }}
2025-10-20 19:40:59 +02:00
domain={[0, diskDomainMax]}
2025-10-20 18:39:12 +02:00
allowDataOverflow={false}
2025-10-19 16:51:52 +02:00
/>
<Tooltip
2025-10-20 18:39:12 +02:00
contentStyle={{
backgroundColor: "hsl(var(--background))",
border: "1px solid hsl(var(--border))",
borderRadius: "6px",
}}
2025-10-19 16:51:52 +02:00
/>
2025-10-20 19:40:59 +02:00
<Legend wrapperStyle={{ paddingTop: "20px" }} />
2025-10-20 18:39:12 +02:00
<Area
type="monotone"
dataKey="diskread"
stroke="#10b981"
fill="#10b981"
fillOpacity={0.3}
strokeWidth={2}
name="Read (MB)"
/>
<Area
2025-10-19 16:51:52 +02:00
type="monotone"
dataKey="diskwrite"
2025-10-20 17:11:30 +02:00
stroke="#3b82f6"
2025-10-20 18:39:12 +02:00
fill="#3b82f6"
fillOpacity={0.3}
2025-10-19 16:51:52 +02:00
strokeWidth={2}
2025-10-20 18:39:12 +02:00
name="Write (MB)"
2025-10-19 16:51:52 +02:00
/>
2025-10-20 18:39:12 +02:00
</AreaChart>
2025-10-19 16:51:52 +02:00
</ResponsiveContainer>
)
2025-10-20 18:39:12 +02:00
default:
return null
2025-10-19 16:51:52 +02:00
}
}
return (
2025-10-19 17:29:23 +02:00
<div className="flex flex-col h-full">
{/* Fixed Header */}
<div className="p-6 pb-4 border-b shrink-0">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<Button variant="ghost" size="icon" onClick={onBack}>
<ArrowLeft className="h-5 w-5" />
</Button>
<div>
<h2 className="text-xl font-semibold">
{METRIC_TITLES[metricType]} - {vmName}
</h2>
<p className="text-sm text-muted-foreground mt-1">
VMID: {vmid} Type: {vmType.toUpperCase()}
</p>
2025-10-19 16:51:52 +02:00
</div>
</div>
2025-10-19 17:29:23 +02:00
<Select value={timeframe} onValueChange={setTimeframe}>
<SelectTrigger className="w-32">
<SelectValue />
</SelectTrigger>
<SelectContent>
{TIMEFRAME_OPTIONS.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
</div>
2025-10-19 16:51:52 +02:00
2025-10-19 17:29:23 +02:00
{/* Scrollable Content */}
<div className="flex-1 overflow-y-auto p-6">{renderChart()}</div>
</div>
2025-10-19 16:51:52 +02:00
)
}