diff --git a/server/db/db.go b/server/db/db.go index 8993227..1c1a813 100644 --- a/server/db/db.go +++ b/server/db/db.go @@ -134,12 +134,31 @@ func (d *DB) GetLastMetrics(agentID string) (*models.AgentMetrics, error) { var uptime, netRX, netTX *int64 var smartPassed, smartTemp, smartRealloc, smartHours, smartWear *int64 + // Chaque sous-requête prend la dernière valeur NON NULL de sa colonne. + // Nécessaire car les paquets rapides (2s) ne contiennent pas les métriques + // lentes (disque, smart) qui sont envoyées toutes les 60s. err := d.conn.QueryRow(` - SELECT cpu_percent, memory_used, memory_free, memory_total, - hdd_used, hdd_free, hdd_total, - uptime, network_rx, network_tx, temperature, - smart_passed, smart_temp, smart_realloc, smart_hours, smart_wear - FROM metrics WHERE agent_id = ? ORDER BY ts DESC LIMIT 1`, agentID). + SELECT + (SELECT cpu_percent FROM metrics WHERE agent_id=? AND cpu_percent IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT memory_used FROM metrics WHERE agent_id=? AND memory_used IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT memory_free FROM metrics WHERE agent_id=? AND memory_free IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT memory_total FROM metrics WHERE agent_id=? AND memory_total IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT hdd_used FROM metrics WHERE agent_id=? AND hdd_used IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT hdd_free FROM metrics WHERE agent_id=? AND hdd_free IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT hdd_total FROM metrics WHERE agent_id=? AND hdd_total IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT uptime FROM metrics WHERE agent_id=? AND uptime IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT network_rx FROM metrics WHERE agent_id=? AND network_rx IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT network_tx FROM metrics WHERE agent_id=? AND network_tx IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT temperature FROM metrics WHERE agent_id=? AND temperature IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT smart_passed FROM metrics WHERE agent_id=? AND smart_passed IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT smart_temp FROM metrics WHERE agent_id=? AND smart_passed IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT smart_realloc FROM metrics WHERE agent_id=? AND smart_passed IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT smart_hours FROM metrics WHERE agent_id=? AND smart_passed IS NOT NULL ORDER BY ts DESC LIMIT 1), + (SELECT smart_wear FROM metrics WHERE agent_id=? AND smart_passed IS NOT NULL ORDER BY ts DESC LIMIT 1)`, + agentID, agentID, agentID, agentID, + agentID, agentID, agentID, + agentID, agentID, agentID, agentID, + agentID, agentID, agentID, agentID, agentID). Scan(&cpu, &memUsed, &memFree, &memTotal, &hddUsed, &hddFree, &hddTotal, &uptime, &netRX, &netTX, &temperature,