package main

import (
	"os/exec"
	"strconv"
	"strings"
)

// readGPU 读取GPU信息
func readGPU() []GPUMetrics {
	_, err := exec.LookPath("nvidia-smi")
	if err != nil {
		return []GPUMetrics{{Status: "not_available"}}
	}
	// Query GPU info including temperature
	cmd := exec.Command("nvidia-smi", "--query-gpu=name,memory.total,memory.used,utilization.gpu,temperature.gpu", "--format=csv,noheader,nounits")
	out, err := cmd.Output()
	if err != nil {
		return []GPUMetrics{{Status: "error", Name: "nvidia-smi", UtilizationPercent: 0}}
	}
	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
	gpus := make([]GPUMetrics, 0, len(lines))
	for _, line := range lines {
		parts := strings.Split(line, ",")
		for i := range parts {
			parts[i] = strings.TrimSpace(parts[i])
		}
		if len(parts) < 5 {
			continue
		}
		total, _ := strconv.ParseInt(parts[1], 10, 64)
		used, _ := strconv.ParseInt(parts[2], 10, 64)
		util, _ := strconv.ParseFloat(parts[3], 64)
		temp, _ := strconv.ParseFloat(parts[4], 64)
		gpus = append(gpus, GPUMetrics{
			Name:               parts[0],
			MemoryTotalMB:      total,
			MemoryUsedMB:       used,
			UtilizationPercent: round(util, 2),
			Temperature:        temp,
			Status:             "ok",
		})
	}
	if len(gpus) == 0 {
		return []GPUMetrics{{Status: "not_available"}}
	}
	return gpus
}
