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

490 lines
17 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-21 19:05:38 +02:00
import { AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, Legend } from "recharts"
2025-11-13 18:32:44 +01:00
import { fetchApi } from "@/lib/api-config"
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"
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
]
2025-10-26 21:11:55 +01:00
const CustomCPUTooltip = ({ active, payload, label }: any) => {
if (active && payload && payload.length) {
return (
<div className="bg-gray-900/95 backdrop-blur-sm border border-gray-700 rounded-lg p-3 shadow-xl">
<p className="text-sm font-semibold text-white mb-2">{label}</p>
<div className="space-y-1.5">
{payload.map((entry: any, index: number) => (
<div key={index} className="flex items-center gap-2">
<div className="w-2.5 h-2.5 rounded-full flex-shrink-0" style={{ backgroundColor: entry.color }} />
<span className="text-xs text-gray-300 min-w-[60px]">{entry.name}:</span>
<span className="text-sm font-semibold text-white">{entry.value}</span>
</div>
))}
</div>
</div>
)
}
return null
}
const CustomMemoryTooltip = ({ active, payload, label }: any) => {
if (active && payload && payload.length) {
return (
<div className="bg-gray-900/95 backdrop-blur-sm border border-gray-700 rounded-lg p-3 shadow-xl">
<p className="text-sm font-semibold text-white mb-2">{label}</p>
<div className="space-y-1.5">
{payload.map((entry: any, index: number) => (
<div key={index} className="flex items-center gap-2">
<div className="w-2.5 h-2.5 rounded-full flex-shrink-0" style={{ backgroundColor: entry.color }} />
<span className="text-xs text-gray-300 min-w-[60px]">{entry.name}:</span>
<span className="text-sm font-semibold text-white">{entry.value} GB</span>
</div>
))}
</div>
</div>
)
}
return null
}
const CustomDiskTooltip = ({ active, payload, label }: any) => {
if (active && payload && payload.length) {
return (
<div className="bg-gray-900/95 backdrop-blur-sm border border-gray-700 rounded-lg p-3 shadow-xl">
<p className="text-sm font-semibold text-white mb-2">{label}</p>
<div className="space-y-1.5">
{payload.map((entry: any, index: number) => (
<div key={index} className="flex items-center gap-2">
<div className="w-2.5 h-2.5 rounded-full flex-shrink-0" style={{ backgroundColor: entry.color }} />
<span className="text-xs text-gray-300 min-w-[60px]">{entry.name}:</span>
<span className="text-sm font-semibold text-white">{entry.value} MB</span>
</div>
))}
</div>
</div>
)
}
return null
}
const CustomNetworkTooltip = ({ active, payload, label }: any) => {
if (active && payload && payload.length) {
return (
<div className="bg-gray-900/95 backdrop-blur-sm border border-gray-700 rounded-lg p-3 shadow-xl">
<p className="text-sm font-semibold text-white mb-2">{label}</p>
<div className="space-y-1.5">
{payload.map((entry: any, index: number) => (
<div key={index} className="flex items-center gap-2">
<div className="w-2.5 h-2.5 rounded-full flex-shrink-0" style={{ backgroundColor: entry.color }} />
<span className="text-xs text-gray-300 min-w-[60px]">{entry.name}:</span>
<span className="text-sm font-semibold text-white">{entry.value} MB</span>
</div>
))}
</div>
</div>
)
}
return null
}
2025-10-21 17:20:16 +02:00
export function MetricsView({ vmid, vmName, vmType, 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)
2025-10-23 12:47:28 +02:00
const [hiddenDiskLines, setHiddenDiskLines] = useState<string[]>([])
const [hiddenNetworkLines, setHiddenNetworkLines] = useState<string[]>([])
2025-10-19 16:51:52 +02:00
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)
try {
2025-11-13 18:32:44 +01:00
const result = await fetchApi<any>(`/api/vms/${vmid}/metrics?timeframe=${timeframe}`)
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 = ""
if (timeframe === "hour") {
timeLabel = date.toLocaleString("en-US", {
hour: "2-digit",
minute: "2-digit",
hour12: false,
})
} else if (timeframe === "day") {
timeLabel = date.toLocaleString("en-US", {
hour: "2-digit",
minute: "2-digit",
hour12: false,
})
} else if (timeframe === "week") {
timeLabel = date.toLocaleString("en-US", {
month: "short",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
hour12: false,
})
} else if (timeframe === "month") {
timeLabel = date.toLocaleString("en-US", {
month: "short",
day: "numeric",
})
} else {
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,
2025-10-21 18:43:56 +02:00
memoryGB: item.mem ? Number((item.mem / 1024 / 1024 / 1024).toFixed(2)) : 0,
maxMemoryGB: item.maxmem ? Number((item.maxmem / 1024 / 1024 / 1024).toFixed(2)) : 0,
2025-10-20 20:00:29 +02:00
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
setData(transformedData)
2025-10-19 17:29:23 +02:00
} catch (err: any) {
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-21 17:20:16 +02:00
const renderAllCharts = () => {
2025-10-19 16:51:52 +02:00
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
const tickInterval = Math.ceil(data.length / 8)
2025-10-20 17:11:30 +02:00
2025-10-21 17:20:16 +02:00
return (
<div className="space-y-8">
{/* CPU Chart */}
<div>
<h3 className="text-lg font-semibold mb-4">CPU Usage</h3>
<ResponsiveContainer width="100%" height={300}>
<AreaChart data={data} margin={{ bottom: 80 }}>
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"
2025-10-21 17:20:16 +02:00
height={60}
2025-10-20 17:11:30 +02:00
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-21 18:43:56 +02:00
domain={[0, "dataMax"]}
2025-10-20 16:43:07 +02:00
/>
2025-10-26 21:11:55 +01:00
<Tooltip content={<CustomCPUTooltip />} />
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>
2025-10-21 17:20:16 +02:00
</div>
2025-10-20 20:00:29 +02:00
2025-10-21 17:20:16 +02:00
{/* Memory Chart */}
<div>
<h3 className="text-lg font-semibold mb-4">Memory Usage</h3>
<ResponsiveContainer width="100%" height={300}>
<AreaChart data={data} margin={{ bottom: 80 }}>
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"
2025-10-21 17:20:16 +02:00
height={60}
2025-10-20 17:11:30 +02:00
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" }}
2025-10-21 18:43:56 +02:00
label={{ value: "GB", angle: -90, position: "insideLeft", fill: "currentColor" }}
domain={[0, "dataMax"]}
2025-10-20 16:43:07 +02:00
/>
2025-10-26 21:11:55 +01:00
<Tooltip content={<CustomMemoryTooltip />} />
2025-10-20 18:39:12 +02:00
<Area
type="monotone"
2025-10-21 18:43:56 +02:00
dataKey="memoryGB"
2025-10-20 18:39:12 +02:00
stroke="#10b981"
fill="#10b981"
fillOpacity={0.3}
strokeWidth={2}
2025-10-21 18:43:56 +02:00
name="Memory GB"
2025-10-20 18:39:12 +02:00
/>
</AreaChart>
2025-10-19 16:51:52 +02:00
</ResponsiveContainer>
2025-10-21 17:20:16 +02:00
</div>
2025-10-20 19:40:59 +02:00
2025-10-21 17:20:16 +02:00
{/* Disk I/O Chart */}
<div>
<h3 className="text-lg font-semibold mb-4">Disk I/O</h3>
<ResponsiveContainer width="100%" height={300}>
<AreaChart data={data} margin={{ bottom: 80 }}>
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"
2025-10-21 17:20:16 +02:00
height={60}
2025-10-20 17:11:30 +02:00
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-21 18:43:56 +02:00
domain={[0, "dataMax"]}
2025-10-19 16:51:52 +02:00
/>
2025-10-26 21:11:55 +01:00
<Tooltip content={<CustomDiskTooltip />} />
2025-10-23 12:55:31 +02:00
<Legend content={renderDiskLegend} verticalAlign="top" />
2025-10-20 18:39:12 +02:00
<Area
type="monotone"
2025-10-21 17:20:16 +02:00
dataKey="diskread"
2025-10-20 18:39:12 +02:00
stroke="#10b981"
fill="#10b981"
fillOpacity={0.3}
strokeWidth={2}
2025-10-21 19:05:38 +02:00
name="Read"
2025-10-23 12:47:28 +02:00
hide={hiddenDiskLines.includes("diskread")}
2025-10-20 18:39:12 +02:00
/>
<Area
type="monotone"
2025-10-21 17:20:16 +02:00
dataKey="diskwrite"
2025-10-20 18:39:12 +02:00
stroke="#3b82f6"
fill="#3b82f6"
fillOpacity={0.3}
strokeWidth={2}
2025-10-21 19:05:38 +02:00
name="Write"
2025-10-23 12:47:28 +02:00
hide={hiddenDiskLines.includes("diskwrite")}
2025-10-20 18:39:12 +02:00
/>
</AreaChart>
2025-10-19 16:51:52 +02:00
</ResponsiveContainer>
2025-10-21 17:20:16 +02:00
</div>
2025-10-20 19:40:59 +02:00
2025-10-21 17:20:16 +02:00
{/* Network I/O Chart */}
<div>
<h3 className="text-lg font-semibold mb-4">Network I/O</h3>
<ResponsiveContainer width="100%" height={300}>
<AreaChart data={data} margin={{ bottom: 80 }}>
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"
2025-10-21 17:20:16 +02:00
height={60}
2025-10-20 17:11:30 +02:00
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-21 18:43:56 +02:00
domain={[0, "dataMax"]}
2025-10-19 16:51:52 +02:00
/>
2025-10-26 21:11:55 +01:00
<Tooltip content={<CustomNetworkTooltip />} />
2025-10-23 12:55:31 +02:00
<Legend content={renderNetworkLegend} verticalAlign="top" />
2025-10-20 18:39:12 +02:00
<Area
type="monotone"
2025-10-21 17:20:16 +02:00
dataKey="netin"
2025-10-20 18:39:12 +02:00
stroke="#10b981"
fill="#10b981"
fillOpacity={0.3}
strokeWidth={2}
2025-10-21 19:05:38 +02:00
name="Download"
2025-10-23 12:47:28 +02:00
hide={hiddenNetworkLines.includes("netin")}
2025-10-20 18:39:12 +02:00
/>
<Area
2025-10-19 16:51:52 +02:00
type="monotone"
2025-10-21 17:20:16 +02:00
dataKey="netout"
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-21 19:05:38 +02:00
name="Upload"
2025-10-23 12:47:28 +02:00
hide={hiddenNetworkLines.includes("netout")}
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-21 17:20:16 +02:00
</div>
</div>
)
2025-10-19 16:51:52 +02:00
}
2025-10-23 12:47:28 +02:00
const handleDiskLegendClick = (dataKey: string) => {
setHiddenDiskLines((prev) => {
if (prev.includes(dataKey)) {
return prev.filter((key) => key !== dataKey)
} else {
return [...prev, dataKey]
}
})
}
const handleNetworkLegendClick = (dataKey: string) => {
setHiddenNetworkLines((prev) => {
if (prev.includes(dataKey)) {
return prev.filter((key) => key !== dataKey)
} else {
return [...prev, dataKey]
}
})
}
const renderDiskLegend = (props: any) => {
const { payload } = props
return (
<div className="flex justify-center gap-6 pb-2">
{payload.map((entry: any) => (
<button
key={entry.dataKey}
onClick={() => handleDiskLegendClick(entry.dataKey)}
className={`flex items-center gap-2 cursor-pointer transition-opacity hover:opacity-100 ${
hiddenDiskLines.includes(entry.dataKey) ? "opacity-40" : "opacity-100"
}`}
>
<span className="w-3 h-3 rounded-full" style={{ backgroundColor: entry.color }} />
<span className="text-sm">{entry.value}</span>
</button>
))}
</div>
)
}
const renderNetworkLegend = (props: any) => {
const { payload } = props
return (
<div className="flex justify-center gap-6 pb-2">
{payload.map((entry: any) => (
<button
key={entry.dataKey}
onClick={() => handleNetworkLegendClick(entry.dataKey)}
className={`flex items-center gap-2 cursor-pointer transition-opacity hover:opacity-100 ${
hiddenNetworkLines.includes(entry.dataKey) ? "opacity-40" : "opacity-100"
}`}
>
<span className="w-3 h-3 rounded-full" style={{ backgroundColor: entry.color }} />
<span className="text-sm">{entry.value}</span>
</button>
))}
</div>
)
}
2025-10-19 16:51:52 +02:00
return (
2025-10-21 18:04:35 +02:00
<div className="flex flex-col h-full max-h-[90vh]">
2025-10-19 17:29:23 +02:00
{/* 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>
2025-10-21 17:20:16 +02:00
<h2 className="text-xl font-semibold">Metrics - {vmName}</h2>
2025-10-19 17:29:23 +02:00
<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-21 17:20:16 +02:00
{/* Scrollable Content with all charts */}
2025-10-21 18:04:35 +02:00
<div className="flex-1 overflow-y-auto p-6 min-h-0">{renderAllCharts()}</div>
2025-10-19 17:29:23 +02:00
</div>
2025-10-19 16:51:52 +02:00
)
}