Skip to content

Commit b48297e

Browse files
committed
fix(heartbeats): When computing last heartbeat time, consider disabled windows too
1 parent 5c8115e commit b48297e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

tools/heartbeats-processor/src/local_db.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,13 +516,21 @@ pub async fn update_scores(pool: &SqlitePool, config: &Config) -> Result<()> {
516516
GROUP BY public_key_id
517517
),
518518
HeartbeatCounts AS (
519+
-- Count heartbeats only within valid windows
519520
SELECT
520521
hp.public_key_id,
521-
COUNT(DISTINCT hp.window_id) as heartbeats,
522-
MAX(hp.heartbeat_time) as last_heartbeat
522+
COUNT(DISTINCT hp.window_id) as heartbeats
523523
FROM heartbeat_presence hp
524524
JOIN ValidWindows vw ON vw.id = hp.window_id
525525
GROUP BY hp.public_key_id
526+
),
527+
LastHeartbeats AS (
528+
-- Get last heartbeat time across all windows
529+
SELECT
530+
public_key_id,
531+
MAX(heartbeat_time) as last_heartbeat
532+
FROM heartbeat_presence
533+
GROUP BY public_key_id
526534
)
527535
INSERT INTO submitter_scores (
528536
public_key_id,
@@ -534,10 +542,11 @@ pub async fn update_scores(pool: &SqlitePool, config: &Config) -> Result<()> {
534542
pk.id,
535543
COALESCE(hc.heartbeats, 0) as score,
536544
COALESCE(bc.blocks, 0) as blocks_produced,
537-
COALESCE(hc.last_heartbeat, 0) as last_heartbeat
545+
COALESCE(lh.last_heartbeat, 0) as last_heartbeat
538546
FROM public_keys pk
539547
LEFT JOIN HeartbeatCounts hc ON hc.public_key_id = pk.id
540548
LEFT JOIN BlockCounts bc ON bc.public_key_id = pk.id
549+
LEFT JOIN LastHeartbeats lh ON lh.public_key_id = pk.id
541550
WHERE hc.heartbeats > 0 OR bc.blocks > 0
542551
ON CONFLICT(public_key_id) DO UPDATE SET
543552
score = excluded.score,

0 commit comments

Comments
 (0)