up
This commit is contained in:
@@ -48,28 +48,39 @@ func AgentHeartbeat(c *fiber.Ctx) error {
|
||||
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
|
||||
var rawMap map[string]any
|
||||
if jsonErr := json.Unmarshal([]byte(req.Metricas), &rawMap); jsonErr == nil {
|
||||
if os, _ := rawMap["os"].(string); os != "" {
|
||||
updates["so"] = os
|
||||
}
|
||||
if m.RAM.TotalGB > 0 {
|
||||
updates["ram"] = fmt.Sprintf("%.0f GB", m.RAM.TotalGB)
|
||||
if ram, _ := rawMap["ram"].(map[string]any); ram != nil {
|
||||
if total, _ := ram["total_gb"].(float64); total > 0 {
|
||||
updates["ram"] = fmt.Sprintf("%.0f GB", total)
|
||||
}
|
||||
}
|
||||
if m.CPU.Nucleos > 0 {
|
||||
updates["nucleos"] = fmt.Sprintf("%d", m.CPU.Nucleos)
|
||||
if cpu, _ := rawMap["cpu"].(map[string]any); cpu != nil {
|
||||
if n, _ := cpu["nucleos"].(float64); n > 0 {
|
||||
updates["nucleos"] = fmt.Sprintf("%d", int(n))
|
||||
}
|
||||
}
|
||||
if m.Disco.TotalGB > 0 {
|
||||
updates["disco"] = fmt.Sprintf("%.0f GB", m.Disco.TotalGB)
|
||||
// Compatibilidad: nuevo formato (discos array) y viejo (disco objeto)
|
||||
discos, hasArray := rawMap["discos"].([]any)
|
||||
if hasArray {
|
||||
var maxTotal float64
|
||||
for _, item := range discos {
|
||||
if d, _ := item.(map[string]any); d != nil {
|
||||
if t, _ := d["total_gb"].(float64); t > maxTotal {
|
||||
maxTotal = t
|
||||
}
|
||||
}
|
||||
}
|
||||
if maxTotal > 0 {
|
||||
updates["disco"] = fmt.Sprintf("%.0f GB", maxTotal)
|
||||
}
|
||||
} else if disco, _ := rawMap["disco"].(map[string]any); disco != nil {
|
||||
if total, _ := disco["total_gb"].(float64); total > 0 {
|
||||
updates["disco"] = fmt.Sprintf("%.0f GB", total)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user