From 57f8126c88ff97cb19e2c050c3f5ff52c2088c3d Mon Sep 17 00:00:00 2001 From: Andrew McKenzie Date: Thu, 10 Aug 2023 16:30:40 +0100 Subject: [PATCH] make speedtests time validity more explicit --- mobile_verifier/src/rewarder.rs | 2 +- mobile_verifier/src/speedtests.rs | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/mobile_verifier/src/rewarder.rs b/mobile_verifier/src/rewarder.rs index 836ec3fac..42678d293 100644 --- a/mobile_verifier/src/rewarder.rs +++ b/mobile_verifier/src/rewarder.rs @@ -215,7 +215,7 @@ impl Rewarder { // clear out the various db tables heartbeats::clear_heartbeats(&mut transaction, &reward_period.start).await?; - speedtests::clear_speedtests(&mut transaction, &reward_period.start).await?; + speedtests::clear_speedtests(&mut transaction, &reward_period.end).await?; data_session::clear_hotspot_data_sessions(&mut transaction, &reward_period.end).await?; // subscriber_location::clear_location_shares(&mut transaction, &reward_period.end).await?; diff --git a/mobile_verifier/src/speedtests.rs b/mobile_verifier/src/speedtests.rs index dca6d646d..c6b6f28b3 100644 --- a/mobile_verifier/src/speedtests.rs +++ b/mobile_verifier/src/speedtests.rs @@ -1,5 +1,5 @@ -use crate::speedtests_average::SpeedtestAverage; -use chrono::{DateTime, Utc}; +use crate::speedtests_average::{SpeedtestAverage, SPEEDTEST_LAPSE}; +use chrono::{DateTime, Duration, Utc}; use file_store::{ file_info_poller::FileInfoStream, file_sink::FileSinkClient, @@ -154,14 +154,17 @@ pub async fn aggregate_epoch_speedtests<'a>( exec: &sqlx::Pool, ) -> Result { let mut speedtests = EpochSpeedTests::new(); + // use latest speedtest which are no older than hours defined by SPEEDTEST_LAPSE + let start = epoch_end - Duration::hours(SPEEDTEST_LAPSE); // pull the last N most recent speedtests from prior to the epoch end for each pubkey let mut rows = sqlx::query_as::<_, Speedtest>( "select * from ( SELECT distinct(pubkey), upload_speed, download_speed, latency, timestamp, serial, row_number() - over (partition by pubkey order by timestamp desc) as count FROM speedtests where timestamp < $1 + over (partition by pubkey order by timestamp desc) as count FROM speedtests where timestamp >= $1 and timestamp < $2 ) as tmp - where count <= $2" + where count <= $3" ) + .bind(start) .bind(epoch_end) .bind(SPEEDTEST_AVG_MAX_DATA_POINTS as i64) .fetch(exec); @@ -175,12 +178,14 @@ pub async fn aggregate_epoch_speedtests<'a>( Ok(speedtests) } + pub async fn clear_speedtests( tx: &mut sqlx::Transaction<'_, sqlx::Postgres>, - timestamp: &DateTime, + epoch_end: &DateTime, ) -> Result<(), sqlx::Error> { + let oldest_ts = *epoch_end - Duration::hours(SPEEDTEST_LAPSE); sqlx::query("DELETE FROM speedtests WHERE timestamp < $1") - .bind(timestamp) + .bind(oldest_ts) .execute(&mut *tx) .await?; Ok(())