diff --git a/mobile_verifier/src/heartbeats/last_location.rs b/mobile_verifier/src/heartbeats/last_location.rs index 17cab6460..cb6e45c76 100644 --- a/mobile_verifier/src/heartbeats/last_location.rs +++ b/mobile_verifier/src/heartbeats/last_location.rs @@ -8,14 +8,21 @@ use sqlx::PgPool; #[derive(sqlx::FromRow, Copy, Clone)] pub struct LastLocation { pub location_validation_timestamp: DateTime, + pub latest_timestamp: DateTime, pub lat: f64, pub lon: f64, } impl LastLocation { - pub fn new(location_validation_timestamp: DateTime, lat: f64, lon: f64) -> Self { + pub fn new( + location_validation_timestamp: DateTime, + latest_timestamp: DateTime, + lat: f64, + lon: f64, + ) -> Self { Self { location_validation_timestamp, + latest_timestamp, lat, lon, } @@ -23,8 +30,7 @@ impl LastLocation { /// Calculates the duration from now in which last_valid_timestamp is 12 hours old pub fn duration_to_expiration(&self) -> Duration { - ((self.location_validation_timestamp + Duration::hours(12)) - Utc::now()) - .max(Duration::zero()) + ((self.latest_timestamp + Duration::hours(12)) - Utc::now()).max(Duration::zero()) } } @@ -56,12 +62,12 @@ impl LocationCache { ) -> anyhow::Result> { let last_location: Option = sqlx::query_as( r#" - SELECT location_validation_timestamp, lat, lon + SELECT location_validation_timestamp, latest_timestamp, lat, lon FROM wifi_heartbeats WHERE location_validation_timestamp IS NOT NULL - AND location_validation_timestamp >= $1 + AND latest_timestamp >= $1 AND hotspot_key = $2 - ORDER BY location_validation_timestamp DESC + ORDER BY latest_timestamp DESC LIMIT 1 "#, ) diff --git a/mobile_verifier/src/heartbeats/mod.rs b/mobile_verifier/src/heartbeats/mod.rs index af4d1fd2d..6352b6788 100644 --- a/mobile_verifier/src/heartbeats/mod.rs +++ b/mobile_verifier/src/heartbeats/mod.rs @@ -544,6 +544,7 @@ impl ValidatedHeartbeat { &heartbeat.hotspot_key, LastLocation::new( location_validation_timestamp, + heartbeat.timestamp, heartbeat.lat, heartbeat.lon, ), diff --git a/mobile_verifier/tests/integrations/heartbeats.rs b/mobile_verifier/tests/integrations/heartbeats.rs index 9eb48600c..167717380 100644 --- a/mobile_verifier/tests/integrations/heartbeats.rs +++ b/mobile_verifier/tests/integrations/heartbeats.rs @@ -461,7 +461,7 @@ async fn use_previous_location_if_timestamp_is_none(pool: PgPool) -> anyhow::Res let lat_lng: LatLng = cell.into(); let epoch_start: DateTime = "2023-08-20 00:00:00.000000000 UTC".parse().unwrap(); - let epoch_end: DateTime = "2023-08-25 00:00:00.000000000 UTC".parse().unwrap(); + let epoch_end: DateTime = Utc::now() + Duration::days(1); let first_heartbeat = Heartbeat { hb_type: HbType::Wifi, @@ -472,7 +472,7 @@ async fn use_previous_location_if_timestamp_is_none(pool: PgPool) -> anyhow::Res lon: lat_lng.lng(), coverage_object: Some(coverage_obj), location_validation_timestamp: Some(Utc::now()), - timestamp: "2023-08-23 00:00:00.000000000 UTC".parse().unwrap(), + timestamp: Utc::now(), }; let first_heartbeat = ValidatedHeartbeat::validate( @@ -500,7 +500,7 @@ async fn use_previous_location_if_timestamp_is_none(pool: PgPool) -> anyhow::Res lon: 0.0, coverage_object: Some(coverage_obj), location_validation_timestamp: None, - timestamp: "2023-08-23 00:00:00.000000000 UTC".parse().unwrap(), + timestamp: Utc::now(), }; let second_heartbeat = ValidatedHeartbeat::validate( @@ -537,7 +537,7 @@ async fn use_previous_location_if_timestamp_is_none(pool: PgPool) -> anyhow::Res lon: 0.0, coverage_object: Some(coverage_obj), location_validation_timestamp: None, - timestamp: "2023-08-23 00:00:00.000000000 UTC".parse().unwrap(), + timestamp: Utc::now(), }; let third_heartbeat = ValidatedHeartbeat::validate( @@ -566,7 +566,7 @@ async fn use_previous_location_if_timestamp_is_none(pool: PgPool) -> anyhow::Res lon: -104.697568066261, coverage_object: Some(coverage_obj_2), location_validation_timestamp: Some(Utc::now()), - timestamp: "2023-08-23 00:00:00.000000000 UTC".parse().unwrap(), + timestamp: Utc::now(), }; let hotspot_2_hb_1 = ValidatedHeartbeat::validate( @@ -613,7 +613,7 @@ async fn use_previous_location_if_timestamp_is_none(pool: PgPool) -> anyhow::Res lon: 0.0, coverage_object: Some(coverage_obj), location_validation_timestamp: None, - timestamp: "2023-08-23 00:00:00.000000000 UTC".parse().unwrap(), + timestamp: Utc::now(), }; let fourth_heartbeat = ValidatedHeartbeat::validate( @@ -651,7 +651,7 @@ async fn use_previous_location_if_timestamp_is_none(pool: PgPool) -> anyhow::Res location_validation_timestamp: Some( Utc::now() - (Duration::hours(12) + Duration::seconds(1)), ), - timestamp: "2023-08-23 00:00:00.000000000 UTC".parse().unwrap(), + timestamp: Utc::now() - (Duration::hours(12) + Duration::seconds(1)), }; let fifth_heartbeat = ValidatedHeartbeat::validate(