package services

import (
	"os"
	"runtime"
	"time"
)

type SystemMetrics struct {
	Hostname      string  `json:"hostname"`
	GoRoutines    int     `json:"goroutines"`
	AllocMB       float64 `json:"alloc_mb"`
	TotalAllocMB  float64 `json:"total_alloc_mb"`
	SysMB         float64 `json:"sys_mb"`
	NumGC         uint32  `json:"num_gc"`
	Uptime        string  `json:"uptime"`
	GOOS          string  `json:"goos"`
	GOARCH        string  `json:"goarch"`
	NumCPU        int     `json:"num_cpu"`
}

var startTime = time.Now()

func GetSystemMetrics() SystemMetrics {
	var m runtime.MemStats
	runtime.ReadMemStats(&m)

	hostname, _ := os.Hostname()

	return SystemMetrics{
		Hostname:     hostname,
		GoRoutines:   runtime.NumGoroutine(),
		AllocMB:      float64(m.Alloc) / 1024 / 1024,
		TotalAllocMB: float64(m.TotalAlloc) / 1024 / 1024,
		SysMB:        float64(m.Sys) / 1024 / 1024,
		NumGC:        m.NumGC,
		Uptime:       time.Since(startTime).Round(time.Second).String(),
		GOOS:         runtime.GOOS,
		GOARCH:       runtime.GOARCH,
		NumCPU:       runtime.NumCPU(),
	}
}
