'use client'; import { useState, useEffect } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Progress } from '@/components/ui/progress'; import { Badge } from '@/components/ui/badge'; import { Monitor, Thermometer, Zap, Fan, Cpu, HardDrive, Activity } from 'lucide-react'; import { invoke } from '@tauri-apps/api/core'; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'; interface GpuStats { id: string; name: string; vendor: string | null; vram_used_mb: number | null; vram_total_mb: number | null; vram_percent: number | null; compute_usage_percent: number | null; graphics_usage_percent: number | null; overall_usage_percent: number | null; temperature_celsius: number | null; temperature_max_celsius: number | null; power_watts: number | null; power_max_watts: number | null; fan_speed_rpm: number | null; fan_speed_percent: number | null; processes_count: number | null; driver_version: string | null; api: string | null; } interface GpuDashboardProps { gpuId?: string; pollInterval?: number; // em milissegundos, padrão 2000ms } export function GpuDashboard({ gpuId, pollInterval = 2000 }: GpuDashboardProps) { const [stats, setStats] = useState(null); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const loadStats = async () => { try { const data = await invoke('get_gpu_stats', { gpuId: gpuId || null }); setStats(data); setError(null); setIsLoading(false); } catch (err) { console.error('Failed to load GPU stats:', err); setError(err instanceof Error ? err.message : 'Erro ao carregar estatísticas da GPU'); setIsLoading(false); } }; loadStats(); const interval = setInterval(loadStats, pollInterval); return () => clearInterval(interval); }, [gpuId, pollInterval]); if (isLoading) { return ( Carregando estatísticas da GPU... ); } if (error || !stats) { return ( {error || 'GPU não detectada ou sem suporte para monitoramento detalhado'} ); } const formatBytes = (mb: number | null): string => { if (!mb) return '-'; if (mb < 1024) return `${mb} MB`; return `${(mb / 1024).toFixed(2)} GB`; }; const getStatusColor = (value: number | null, thresholds: { good: number; warning: number }): string => { if (!value) return 'bg-muted'; if (value < thresholds.good) return 'bg-green-500'; if (value < thresholds.warning) return 'bg-yellow-500'; return 'bg-red-500'; }; const getTemperatureColor = (temp: number | null): string => { if (!temp) return 'text-muted-foreground'; if (temp < 60) return 'text-green-500'; if (temp < 80) return 'text-yellow-500'; return 'text-red-500'; }; return (
{/* Card Principal da GPU */}
GPU
{stats.vendor && ( {stats.vendor} )} {stats.api && ( {stats.api} )}

{stats.name}

{stats.driver_version && (

Driver: {stats.driver_version}

)}
{/* VRAM */} {stats.vram_total_mb && (
Memória VRAM {formatBytes(stats.vram_used_mb)} / {formatBytes(stats.vram_total_mb)} {stats.vram_percent && ` (${stats.vram_percent.toFixed(1)}%)`}
{stats.vram_percent !== null && ( )}
)} {/* Uso de Processamento */} {stats.overall_usage_percent !== null && (
Uso de Processamento {stats.overall_usage_percent.toFixed(1)}%
{stats.compute_usage_percent !== null && stats.graphics_usage_percent !== null && (
Compute: {stats.compute_usage_percent.toFixed(1)}% Graphics: {stats.graphics_usage_percent.toFixed(1)}%
)}
)} {/* Grid de Métricas */}
{/* Temperatura */} {stats.temperature_celsius !== null && (
Temperatura
{stats.temperature_celsius.toFixed(0)} °C {stats.temperature_max_celsius && ( / {stats.temperature_max_celsius.toFixed(0)}°C )}
)} {/* Energia */} {stats.power_watts !== null && (
Energia
{stats.power_watts.toFixed(1)} W {stats.power_max_watts && ( / {stats.power_max_watts.toFixed(1)}W )}
)} {/* Ventilador */} {(stats.fan_speed_percent !== null || stats.fan_speed_rpm !== null) && (
Ventilador
{stats.fan_speed_percent !== null && ( <> {stats.fan_speed_percent.toFixed(0)} % )} {stats.fan_speed_rpm !== null && ( ({stats.fan_speed_rpm} RPM) )}
)} {/* Processos */} {stats.processes_count !== null && (
Processos
{stats.processes_count} {stats.processes_count === 1 ? 'processo' : 'processos'}
)}
); }