diff --git a/mobile_verifier/migrations/52_rename_latest_timestamp.sql b/mobile_verifier/migrations/52_rename_latest_timestamp.sql new file mode 100644 index 000000000..98e315605 --- /dev/null +++ b/mobile_verifier/migrations/52_rename_latest_timestamp.sql @@ -0,0 +1 @@ +ALTER TABLE IF EXISTS wifi_heartbeats RENAME COLUMN latest_timestamp TO first_timestamp; diff --git a/mobile_verifier/src/heartbeats/last_location.rs b/mobile_verifier/src/heartbeats/last_location.rs index 1c478c370..e90529d33 100644 --- a/mobile_verifier/src/heartbeats/last_location.rs +++ b/mobile_verifier/src/heartbeats/last_location.rs @@ -10,7 +10,8 @@ use super::Heartbeat; #[derive(Debug, sqlx::FromRow, Copy, Clone, PartialEq)] pub struct LastLocation { pub location_validation_timestamp: DateTime, - pub latest_timestamp: DateTime, + #[sqlx(rename = "first_timestamp")] + pub heartbeat_timestamp: DateTime, pub lat: f64, pub lon: f64, } @@ -18,13 +19,13 @@ pub struct LastLocation { impl LastLocation { pub fn new( location_validation_timestamp: DateTime, - latest_timestamp: DateTime, + heartbeat_timestamp: DateTime, lat: f64, lon: f64, ) -> Self { Self { location_validation_timestamp, - latest_timestamp, + heartbeat_timestamp, lat, lon, } @@ -113,13 +114,13 @@ impl LocationCache { ) -> anyhow::Result> { let last_location: Option = sqlx::query_as( r#" - SELECT location_validation_timestamp, latest_timestamp, lat, lon + SELECT location_validation_timestamp, first_timestamp, lat, lon FROM wifi_heartbeats WHERE location_validation_timestamp IS NOT NULL - AND latest_timestamp >= $1 + AND first_timestamp >= $1 AND hotspot_key = $2 AND $3 - location_validation_timestamp <= INTERVAL '24 hours' - ORDER BY latest_timestamp DESC + ORDER BY first_timestamp DESC LIMIT 1 "#, ) @@ -193,7 +194,7 @@ mod tests { r#" INSERT INTO wifi_heartbeats ( - hotspot_key, location_validation_timestamp, latest_timestamp, + hotspot_key, location_validation_timestamp, first_timestamp, truncated_timestamp, coverage_object, -- hardcoded values @@ -225,7 +226,7 @@ mod tests { ) -> LastLocation { LastLocation { location_validation_timestamp: nanos_trunc(location_validation_timestamp), - latest_timestamp: nanos_trunc(latest_timestamp), + heartbeat_timestamp: nanos_trunc(latest_timestamp), lat: 0.0, lon: 0.0, } diff --git a/mobile_verifier/src/heartbeats/mod.rs b/mobile_verifier/src/heartbeats/mod.rs index 3a3e79c2c..2fcda5460 100644 --- a/mobile_verifier/src/heartbeats/mod.rs +++ b/mobile_verifier/src/heartbeats/mod.rs @@ -556,10 +556,10 @@ impl ValidatedHeartbeat { let truncated_timestamp = self.truncated_timestamp()?; sqlx::query( r#" - INSERT INTO wifi_heartbeats (hotspot_key, cell_type, latest_timestamp, truncated_timestamp, coverage_object, location_trust_score_multiplier, distance_to_asserted, location_validation_timestamp, lat, lon) + INSERT INTO wifi_heartbeats (hotspot_key, cell_type, first_timestamp, truncated_timestamp, coverage_object, location_trust_score_multiplier, distance_to_asserted, location_validation_timestamp, lat, lon) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) ON CONFLICT (hotspot_key, truncated_timestamp) DO UPDATE SET - latest_timestamp = EXCLUDED.latest_timestamp, + first_timestamp = EXCLUDED.first_timestamp, coverage_object = EXCLUDED.coverage_object "#, ) diff --git a/mobile_verifier/src/rewarder/db.rs b/mobile_verifier/src/rewarder/db.rs index f946ad5de..b7b772893 100644 --- a/mobile_verifier/src/rewarder/db.rs +++ b/mobile_verifier/src/rewarder/db.rs @@ -13,7 +13,7 @@ pub async fn no_wifi_heartbeats( reward_period: &Range>, ) -> anyhow::Result { let count = sqlx::query_scalar::<_, i64>( - "SELECT COUNT(*) FROM wifi_heartbeats WHERE latest_timestamp >= $1", + "SELECT COUNT(*) FROM wifi_heartbeats WHERE first_timestamp >= $1", ) .bind(reward_period.end) .fetch_one(pool) diff --git a/mobile_verifier/tests/integrations/heartbeats.rs b/mobile_verifier/tests/integrations/heartbeats.rs index b5f7fe1e2..645c046f4 100644 --- a/mobile_verifier/tests/integrations/heartbeats.rs +++ b/mobile_verifier/tests/integrations/heartbeats.rs @@ -57,7 +57,7 @@ async fn only_fetch_latest_hotspot(pool: PgPool) -> anyhow::Result<()> { "11sctWiP9r5wDJVuDe1Th4XSL2vaawaLLSQF8f8iokAoMAJHxqp".parse()?; sqlx::query( r#" -INSERT INTO wifi_heartbeats (hotspot_key, cell_type, latest_timestamp, truncated_timestamp, coverage_object, location_trust_score_multiplier, distance_to_asserted) +INSERT INTO wifi_heartbeats (hotspot_key, cell_type, first_timestamp, truncated_timestamp, coverage_object, location_trust_score_multiplier, distance_to_asserted) VALUES ($2, 'novagenericwifiindoor', '2023-08-25 00:00:00+00', '2023-08-25 00:00:00+00', $3, 1.0, 0), ($2, 'novagenericwifiindoor', '2023-08-25 01:00:00+00', '2023-08-25 01:00:00+00', $3, 1.0, 0), @@ -118,7 +118,7 @@ async fn ensure_minimum_count(pool: PgPool) -> anyhow::Result<()> { sqlx::query( r#" -INSERT INTO wifi_heartbeats (hotspot_key, cell_type, latest_timestamp, truncated_timestamp, coverage_object, location_trust_score_multiplier, distance_to_asserted) +INSERT INTO wifi_heartbeats (hotspot_key, cell_type, first_timestamp, truncated_timestamp, coverage_object, location_trust_score_multiplier, distance_to_asserted) VALUES ($1, 'novagenericwifiindoor', '2023-08-25 00:00:00+00', '2023-08-25 00:00:00+00', $2, 1.0, 0), ($1, 'novagenericwifiindoor', '2023-08-25 01:00:00+00', '2023-08-25 01:00:00+00', $2, 1.0, 0), @@ -157,7 +157,7 @@ async fn ensure_wifi_hotspots_are_rewarded(pool: PgPool) -> anyhow::Result<()> { "112NqN2WWMwtK29PMzRby62fDydBJfsCLkCAf392stdok48ovNT6".parse()?; sqlx::query( r#" -INSERT INTO wifi_heartbeats (hotspot_key, cell_type, latest_timestamp, truncated_timestamp, coverage_object, location_trust_score_multiplier, distance_to_asserted) +INSERT INTO wifi_heartbeats (hotspot_key, cell_type, first_timestamp, truncated_timestamp, coverage_object, location_trust_score_multiplier, distance_to_asserted) VALUES ($1, 'novagenericwifiindoor', '2023-08-25 00:00:00+00', '2023-08-25 00:00:00+00', $2, 1.0, 0), ($1, 'novagenericwifiindoor', '2023-08-25 01:00:00+00', '2023-08-25 01:00:00+00', $2, 1.0, 0), @@ -206,7 +206,7 @@ async fn ensure_wifi_hotspots_use_average_location_trust_score(pool: PgPool) -> "112NqN2WWMwtK29PMzRby62fDydBJfsCLkCAf392stdok48ovNT6".parse()?; sqlx::query( r#" -INSERT INTO wifi_heartbeats (hotspot_key, cell_type, latest_timestamp, truncated_timestamp, coverage_object, location_trust_score_multiplier, distance_to_asserted) +INSERT INTO wifi_heartbeats (hotspot_key, cell_type, first_timestamp, truncated_timestamp, coverage_object, location_trust_score_multiplier, distance_to_asserted) VALUES ($1, 'novagenericwifiindoor', '2023-08-25 00:00:00+00', '2023-08-25 00:00:00+00', $2, 1.0, 0), ($1, 'novagenericwifiindoor', '2023-08-25 01:00:00+00', '2023-08-25 01:00:00+00', $2, 1.0, 0),