package main

import (
	"bufio"
	"os"
	"runtime"
	"strconv"
	"strings"
	"time"
)

// CollectMetrics 收集所有系统监控指标
func CollectMetrics() (*Metrics, error) {
	hostname, _ := os.Hostname()

	cpuModel := firstMatchInFile("/proc/cpuinfo", "model name")
	cpuUsage, err := readCPUUsage()
	if err != nil {
		return nil, err
	}
	cpuTemp := readCPUTemperature()
	perCoreUsage := readPerCoreUsage()

	mem, err := readMemory()
	if err != nil {
		return nil, err
	}

	storage, err := readAllStorage()
	if err != nil {
		return nil, err
	}

	gpu := readGPU()
	network := readNetworkInterfaces()
	systemStats := readSystemStats()
	systemStats.DockerStats = readDockerStats()

	osInfo := readOSInfo()
	uptime := readUptime()

	loads := readLoadAverages()

	return &Metrics{
		Hostname:  hostname,
		Timestamp: time.Now().UTC(),
		CPU: CPUMetrics{
			Model:        cpuModel,
			Cores:        runtime.NumCPU(),
			UsagePercent: round(cpuUsage, 2),
			LoadAverages: loads,
			Temperature:  cpuTemp,
			PerCoreUsage: perCoreUsage,
		},
		Memory:        mem,
		Storage:       storage,
		GPU:           gpu,
		Network:       network,
		System:        systemStats,
		OS:            osInfo,
		UptimeSeconds: uptime,
	}, nil
}

func readOSInfo() OSInfo {
	distro := readOSRelease()
	kernel := strings.TrimSpace(readFirstLine("/proc/version"))
	arch := runtime.GOARCH
	return OSInfo{
		Kernel:       kernel,
		Distro:       distro,
		Architecture: arch,
	}
}

func readOSRelease() string {
	f, err := os.Open("/etc/os-release")
	if err != nil {
		return runtime.GOOS
	}
	defer f.Close()
	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, "PRETTY_NAME=") {
			return strings.Trim(line[len("PRETTY_NAME="):], "\"")
		}
	}
	return runtime.GOOS
}

func readUptime() float64 {
	line := readFirstLine("/proc/uptime")
	fields := strings.Fields(line)
	if len(fields) == 0 {
		return 0
	}
	v, err := strconv.ParseFloat(fields[0], 64)
	if err != nil {
		return 0
	}
	return v
}
