Skip to content

Commit 892574f

Browse files
committed
fix(heartbeats): Don't fetch heartbeats outside of the defined window range
1 parent b48297e commit 892574f

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

tools/heartbeats-processor/src/local_db.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,11 @@ pub async fn process_heartbeats(
245245
db: &FirestoreDb,
246246
pool: &SqlitePool,
247247
config: &Config,
248-
) -> Result<()> {
248+
) -> Result<usize> {
249249
let last_processed_time = get_last_processed_time(pool, Some(config)).await?;
250250
let now = Utc::now();
251+
// Don't fetch heartbeats beyond window range end
252+
let end_time = config.window_range_end.min(now);
251253

252254
let mut total_heartbeats = 0;
253255
let mut latest_time = last_processed_time;
@@ -266,7 +268,8 @@ pub async fn process_heartbeats(
266268
};
267269

268270
loop {
269-
let heartbeats = crate::remote_db::fetch_heartbeat_chunk(db, &mut chunk_state, now).await?;
271+
let heartbeats =
272+
crate::remote_db::fetch_heartbeat_chunk(db, &mut chunk_state, end_time).await?;
270273
if heartbeats.is_empty() {
271274
break;
272275
}
@@ -430,7 +433,7 @@ pub async fn process_heartbeats(
430433
update_last_processed_time(pool, latest_time).await?;
431434
}
432435

433-
Ok(())
436+
Ok(total_heartbeats)
434437
}
435438

436439
pub async fn create_tables_from_file(pool: &SqlitePool) -> Result<()> {

tools/heartbeats-processor/src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,12 @@ async fn run_process_loop(
112112

113113
loop {
114114
println!("Processing heartbeats...");
115-
local_db::process_heartbeats(db, pool, config).await?;
115+
let count = local_db::process_heartbeats(db, pool, config).await?;
116116

117-
println!("Posting scores...");
118-
post_scores_to_firestore(pool, db, config).await?;
117+
if count > 0 {
118+
println!("Posting scores...");
119+
post_scores_to_firestore(pool, db, config).await?;
120+
}
119121

120122
println!("Sleeping for {} seconds...", interval_seconds);
121123
tokio::time::sleep(interval).await;

tools/heartbeats-processor/src/remote_db.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,14 @@ pub async fn fetch_heartbeat_chunk(
171171
let chunk_end = (state.chunk_start + chunk_duration).min(end_time);
172172

173173
if state.chunk_start >= end_time {
174+
println!("Reached end of testing window: {}", end_time);
174175
return Ok(Vec::new());
175176
}
176177

177-
println!("Fetching heartbeat chunk... {}", state.chunk_start);
178+
println!(
179+
"Fetching heartbeat chunk... {} to {}",
180+
state.chunk_start, chunk_end
181+
);
178182

179183
let query = db
180184
.fluent()

0 commit comments

Comments
 (0)