This commit is contained in:
Lizandro Guarnizo
2026-05-21 22:37:31 -05:00
parent 154a2fa245
commit 0cf7c6d279
7 changed files with 218 additions and 2 deletions
+34
View File
@@ -3,6 +3,8 @@ package controllers
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"time"
"github.com/gofiber/fiber/v2"
@@ -35,9 +37,41 @@ func AgentHeartbeat(c *fiber.Ctx) error {
now := time.Now()
updates := map[string]interface{}{
"agent_last_seen": &now,
"ultimo_ping": now.Format(time.RFC3339),
}
if req.Metricas != "" {
updates["metricas_json"] = req.Metricas
// Actualizar campos planos desde las métricas del agente (la verdad viene del agente)
type agentRAM struct {
TotalGB float64 `json:"total_gb"`
}
type agentCPU struct {
Nucleos int `json:"nucleos"`
}
type agentDisco struct {
TotalGB float64 `json:"total_gb"`
}
type agentPayload struct {
OS string `json:"os"`
RAM agentRAM `json:"ram"`
CPU agentCPU `json:"cpu"`
Disco agentDisco `json:"disco"`
}
var m agentPayload
if jsonErr := json.Unmarshal([]byte(req.Metricas), &m); jsonErr == nil {
if m.OS != "" {
updates["so"] = m.OS
}
if m.RAM.TotalGB > 0 {
updates["ram"] = fmt.Sprintf("%.0f GB", m.RAM.TotalGB)
}
if m.CPU.Nucleos > 0 {
updates["nucleos"] = fmt.Sprintf("%d", m.CPU.Nucleos)
}
if m.Disco.TotalGB > 0 {
updates["disco"] = fmt.Sprintf("%.0f GB", m.Disco.TotalGB)
}
}
}
if err := db.Model(&servidor).Updates(updates).Error; err != nil {