feat(agent): persist 7-day metrics history per server heartbeat
- New table servidor_metricas_history (cpu%, ram%, swap%, disco%, tcp, load1) - Each heartbeat inserts a compact row (~60 bytes) - GET /app/servidor/:id/metricas-history?horas=24 returns the data - Daily cron at 3AM purges rows older than 7 days - ~1.2 MB per server per 7 days at 30s interval Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
4b98161070
commit
ae74d0ef90
@@ -89,9 +89,75 @@ func AgentHeartbeat(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"ok": false, "error": "no se pudo guardar"})
|
||||
}
|
||||
|
||||
// Insertar punto en el historial de métricas
|
||||
if req.Metricas != "" {
|
||||
var raw map[string]any
|
||||
if json.Unmarshal([]byte(req.Metricas), &raw) == nil {
|
||||
getFloat := func(obj map[string]any, key string) float64 {
|
||||
if sub, ok := obj[key].(map[string]any); ok {
|
||||
if v, ok := sub["porcentaje"].(float64); ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
discoPct := 0.0
|
||||
if discos, ok := raw["discos"].([]any); ok && len(discos) > 0 {
|
||||
if d, ok := discos[0].(map[string]any); ok {
|
||||
if v, ok := d["porcentaje"].(float64); ok {
|
||||
discoPct = v
|
||||
}
|
||||
}
|
||||
}
|
||||
tcpConns := 0
|
||||
if v, ok := raw["tcp_conns"].(float64); ok {
|
||||
tcpConns = int(v)
|
||||
}
|
||||
load1 := 0.0
|
||||
if la, ok := raw["load_avg"].(map[string]any); ok {
|
||||
if v, ok := la["load1"].(float64); ok {
|
||||
load1 = v
|
||||
}
|
||||
}
|
||||
swapPct := 0.0
|
||||
if sw, ok := raw["swap"].(map[string]any); ok {
|
||||
if v, ok := sw["porcentaje"].(float64); ok {
|
||||
swapPct = v
|
||||
}
|
||||
}
|
||||
models.InsertMetricasHistory(servidor.ID,
|
||||
getFloat(raw, "cpu"),
|
||||
getFloat(raw, "ram"),
|
||||
swapPct,
|
||||
discoPct,
|
||||
tcpConns,
|
||||
load1,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{"ok": true, "servidor_id": servidor.ID})
|
||||
}
|
||||
|
||||
// GetMetricasHistory devuelve el historial de métricas de un servidor.
|
||||
// GET /app/servidor/:id/metricas-history?horas=24
|
||||
func GetMetricasHistory(c *fiber.Ctx) error {
|
||||
id := c.Params("id")
|
||||
horas := c.QueryInt("horas", 24)
|
||||
if horas <= 0 || horas > 168 {
|
||||
horas = 24
|
||||
}
|
||||
var srv models.Servidor
|
||||
if err := app.Http.Database.DB.First(&srv, id).Error; err != nil {
|
||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "servidor no encontrado"})
|
||||
}
|
||||
items, err := models.GetMetricasHistory(srv.ID, horas)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
return c.JSON(items)
|
||||
}
|
||||
|
||||
// GenerateAgentToken genera o regenera el token de agente para un servidor.
|
||||
// Ruta protegida: POST /app/servidor/:id/agent-token
|
||||
func GenerateAgentToken(c *fiber.Ctx) error {
|
||||
|
||||
Reference in New Issue
Block a user