package status

import (
	"github.com/shirou/gopsutil/v3/cpu"
	"github.com/shirou/gopsutil/v3/load"
	"time"
)

type CPUMetric struct {
	NumCores   int
	CPUPercent float64
	CPULoads   *load.AvgStat
}

type CPUInfo struct {
	CPUMetric

	CPUInfo []cpu.InfoStat
}

func GetCPUMetric() (*CPUMetric, error) {

	numCores, err := cpu.Counts(true)
	if err != nil {
		return nil, err
	}

	cpuPercent, err := cpu.Percent(time.Second, false)
	if err != nil {
		return nil, err
	}

	cpuLoads, err := load.Avg()
	if err != nil {
		return nil, err
	}

	return &CPUMetric{
		NumCores:   numCores,
		CPUPercent: cpuPercent[0],
		CPULoads:   cpuLoads,
	}, nil

}

func GetCPUInfo() (*CPUInfo, error) {

	numCores, err := cpu.Counts(true)
	if err != nil {
		return nil, err
	}

	cpuPercent, err := cpu.Percent(time.Second, false)
	if err != nil {
		return nil, err
	}

	cpuLoads, err := load.Avg()
	if err != nil {
		return nil, err
	}

	infoStats, err := cpu.Info()
	if err != nil {
		return nil, err
	}

	return &CPUInfo{
		CPUMetric: CPUMetric{
			NumCores:   numCores,
			CPUPercent: cpuPercent[0],
			CPULoads:   cpuLoads,
		},
		CPUInfo: infoStats,
	}, nil

}
