fix(db): GetLastMetrics retourne la dernière valeur non-nulle par colonne
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 <noreply@anthropic.com>
This commit is contained in:
+24
-5
@@ -134,12 +134,31 @@ func (d *DB) GetLastMetrics(agentID string) (*models.AgentMetrics, error) {
|
|||||||
var uptime, netRX, netTX *int64
|
var uptime, netRX, netTX *int64
|
||||||
var smartPassed, smartTemp, smartRealloc, smartHours, smartWear *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(`
|
err := d.conn.QueryRow(`
|
||||||
SELECT cpu_percent, memory_used, memory_free, memory_total,
|
SELECT
|
||||||
hdd_used, hdd_free, hdd_total,
|
(SELECT cpu_percent FROM metrics WHERE agent_id=? AND cpu_percent IS NOT NULL ORDER BY ts DESC LIMIT 1),
|
||||||
uptime, network_rx, network_tx, temperature,
|
(SELECT memory_used FROM metrics WHERE agent_id=? AND memory_used IS NOT NULL ORDER BY ts DESC LIMIT 1),
|
||||||
smart_passed, smart_temp, smart_realloc, smart_hours, smart_wear
|
(SELECT memory_free FROM metrics WHERE agent_id=? AND memory_free IS NOT NULL ORDER BY ts DESC LIMIT 1),
|
||||||
FROM metrics WHERE agent_id = ? ORDER BY ts DESC LIMIT 1`, agentID).
|
(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,
|
Scan(&cpu, &memUsed, &memFree, &memTotal,
|
||||||
&hddUsed, &hddFree, &hddTotal,
|
&hddUsed, &hddFree, &hddTotal,
|
||||||
&uptime, &netRX, &netTX, &temperature,
|
&uptime, &netRX, &netTX, &temperature,
|
||||||
|
|||||||
Reference in New Issue
Block a user