'use client' import { useState } from 'react' import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Loader2, Activity, Cpu, HardDrive, Network, Download, Upload, RefreshCw, ExternalLink, } from 'lucide-react' import Link from 'next/link' import { useServerMetrics } from '@/hooks/use-netcup' interface ServerMetricsPanelProps { netcupServerId: string serverName?: string } function formatBytes(bytes: number): string { if (bytes === 0) return '0 B' if (!Number.isFinite(bytes) || bytes < 0) return '0 B' const k = 1024 const sizes = ['B', 'KB', 'MB', 'GB', 'TB'] const i = Math.max(0, Math.min(Math.floor(Math.log(bytes) / Math.log(k)), sizes.length - 1)) return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i] } function formatBytesPerSec(bps: number): string { return formatBytes(bps) + '/s' } function MetricsBar({ value, max = 100, gradient }: { value: number max?: number gradient?: string }) { const percentage = Math.min((value / max) * 100, 100) const getThresholdColor = () => { if (percentage > 90) return 'bg-gradient-to-r from-red-500 to-red-600' if (percentage > 75) return 'bg-gradient-to-r from-amber-500 to-orange-500' return gradient || 'bg-gradient-to-r from-blue-500 to-blue-600' } return (
) } export function ServerMetricsPanel({ netcupServerId, serverName }: ServerMetricsPanelProps) { const [hours, setHours] = useState(24) const { data: metrics, isLoading, isFetching, error, refetch } = useServerMetrics(netcupServerId, hours, !!netcupServerId) if (!netcupServerId) { return null } const isInitialLoading = isLoading && !metrics if (error && !metrics) { return (
Server Metrics Performance monitoring

Failed to load metrics

) } return (
Server Metrics Performance monitoring {serverName && ( View full details )}
{/* Period selector */}
{[1, 6, 24, 168].map((h) => ( ))}
{isInitialLoading ? (
{[ { icon: Cpu, label: 'CPU', color: 'text-blue-500 bg-blue-100 dark:bg-blue-900/30' }, { icon: HardDrive, label: 'Disk I/O', color: 'text-emerald-500 bg-emerald-100 dark:bg-emerald-900/30' }, { icon: Network, label: 'Network', color: 'text-violet-500 bg-violet-100 dark:bg-violet-900/30' }, ].map(({ icon: Icon, label, color }) => (
{label}
))}
) : !metrics ? (

No metrics data available

Metrics will appear once the server is running

) : (
{/* CPU metrics */}
CPU
{metrics.cpu.average}%
Peak: {metrics.cpu.max}% {metrics.cpu.dataPoints.length} samples
{/* Disk I/O metrics */} {metrics.disk && (metrics.disk.readBps.length > 0 || metrics.disk.writeBps.length > 0) ? (
Disk I/O
Read

{metrics.disk.readBps.length > 0 ? formatBytesPerSec(metrics.disk.readBps[metrics.disk.readBps.length - 1]?.value || 0) : 'N/A'}

Write

{metrics.disk.writeBps.length > 0 ? formatBytesPerSec(metrics.disk.writeBps[metrics.disk.writeBps.length - 1]?.value || 0) : 'N/A'}

) : (
Disk I/O

No data

)} {/* Network metrics */} {metrics.network && (metrics.network.rxBps.length > 0 || metrics.network.txBps.length > 0) ? (
Network
RX

{metrics.network.rxBps.length > 0 ? formatBytesPerSec(metrics.network.rxBps[metrics.network.rxBps.length - 1]?.value || 0) : 'N/A'}

TX

{metrics.network.txBps.length > 0 ? formatBytesPerSec(metrics.network.txBps[metrics.network.txBps.length - 1]?.value || 0) : 'N/A'}

) : (
Network

No data

)}
)}
) }