From 3933301cfff843306a1e7a4319cdb4ef6347349e Mon Sep 17 00:00:00 2001 From: Gilles Soulier Date: Fri, 22 May 2026 22:29:06 +0200 Subject: [PATCH] =?UTF-8?q?fix(db):=20GetLastMetrics=20retourne=20la=20der?= =?UTF-8?q?ni=C3=A8re=20valeur=20non-nulle=20par=20colonne?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit La requête précédente prenait la dernière ligne (paquet rapide, 2s) qui a hdd_*/smart_* à NULL. Chaque sous-requête cible maintenant la dernière valeur non-nulle indépendamment, ce qui restitue les données disque/smart au rechargement même si le dernier paquet ne les contenait pas. Co-Authored-By: Claude Sonnet 4.6 --- server/db/db.go | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) 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,