package prometheus

import (
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
	"github.com/user/nanometrics/server/models"
)

var (
	agentCPU = promauto.NewGaugeVec(prometheus.GaugeOpts{
		Name: "nanometrics_cpu_percent",
		Help: "Pourcentage CPU de l'agent",
	}, []string{"agent"})

	agentMemUsed = promauto.NewGaugeVec(prometheus.GaugeOpts{
		Name: "nanometrics_memory_used_bytes",
		Help: "RAM utilisée en octets",
	}, []string{"agent"})

	agentMemTotal = promauto.NewGaugeVec(prometheus.GaugeOpts{
		Name: "nanometrics_memory_total_bytes",
		Help: "RAM totale en octets",
	}, []string{"agent"})

	agentDiskUsed = promauto.NewGaugeVec(prometheus.GaugeOpts{
		Name: "nanometrics_disk_used_bytes",
		Help: "Disque utilisé en octets",
	}, []string{"agent"})

	agentDiskTotal = promauto.NewGaugeVec(prometheus.GaugeOpts{
		Name: "nanometrics_disk_total_bytes",
		Help: "Disque total en octets",
	}, []string{"agent"})

	agentUptime = promauto.NewGaugeVec(prometheus.GaugeOpts{
		Name: "nanometrics_uptime_seconds",
		Help: "Uptime de l'agent en secondes",
	}, []string{"agent"})

	agentStatus = promauto.NewGaugeVec(prometheus.GaugeOpts{
		Name: "nanometrics_agent_online",
		Help: "1 si l'agent est en ligne, 0 sinon",
	}, []string{"agent"})
)

func Update(m *models.AgentMetrics) {
	l := prometheus.Labels{"agent": m.Hostname}
	if m.CPUPercent != nil {
		agentCPU.With(l).Set(*m.CPUPercent)
	}
	if m.MemoryUsed != nil {
		agentMemUsed.With(l).Set(float64(*m.MemoryUsed))
	}
	if m.MemoryTotal != nil {
		agentMemTotal.With(l).Set(float64(*m.MemoryTotal))
	}
	if m.HDDUsed != nil {
		agentDiskUsed.With(l).Set(float64(*m.HDDUsed))
	}
	if m.HDDTotal != nil {
		agentDiskTotal.With(l).Set(float64(*m.HDDTotal))
	}
	if m.Uptime != nil {
		agentUptime.With(l).Set(float64(*m.Uptime))
	}
	online := 0.0
	if m.Status == "online" {
		online = 1.0
	}
	agentStatus.With(l).Set(online)
}
