From 2f7d9e4699e9ce5f4327bee6794161f0a0400dd4 Mon Sep 17 00:00:00 2001 From: Macpie Date: Wed, 16 Oct 2024 12:47:21 -0700 Subject: [PATCH] Populate cache with cbrs as well --- .../migrations/39_update_cbrs_hearbeats.sql | 4 +++ .../src/heartbeats/last_location.rs | 31 ++++++++++++++++++- mobile_verifier/src/heartbeats/mod.rs | 7 +++-- 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 mobile_verifier/migrations/39_update_cbrs_hearbeats.sql diff --git a/mobile_verifier/migrations/39_update_cbrs_hearbeats.sql b/mobile_verifier/migrations/39_update_cbrs_hearbeats.sql new file mode 100644 index 000000000..6a1056a3d --- /dev/null +++ b/mobile_verifier/migrations/39_update_cbrs_hearbeats.sql @@ -0,0 +1,4 @@ +ALTER TABLE cbrs_heartbeats +ADD COLUMN location_validation_timestamp TIMESTAMPTZ, +ADD COLUMN lat DOUBLE PRECISION NOT NULL DEFAULT 0.0, +ADD COLUMN lon DOUBLE PRECISION NOT NULL DEFAULT 0.0; \ No newline at end of file diff --git a/mobile_verifier/src/heartbeats/last_location.rs b/mobile_verifier/src/heartbeats/last_location.rs index 794926251..a422ca815 100644 --- a/mobile_verifier/src/heartbeats/last_location.rs +++ b/mobile_verifier/src/heartbeats/last_location.rs @@ -68,7 +68,7 @@ impl LocationCache { } else { match key { Key::WifiPubKey(pub_key_bin) => self.fetch_wifi_and_set(pub_key_bin).await?, - Key::CbrsId(_) => None, + Key::CbrsId(id) => self.fetch_cbrs_and_set(id).await?, } }, ) @@ -122,4 +122,33 @@ impl LocationCache { .await; Ok(last_location) } + + async fn fetch_cbrs_and_set(&self, cbsd_id: String) -> anyhow::Result> { + let last_location: Option = sqlx::query_as( + r#" + SELECT location_validation_timestamp, latest_timestamp, lat, lon + FROM cbrs_heartbeats + WHERE location_validation_timestamp IS NOT NULL + AND latest_timestamp >= $1 + AND hotspot_key = $2 + ORDER BY latest_timestamp DESC + LIMIT 1 + "#, + ) + .bind(Utc::now() - Duration::hours(12)) + .bind(cbsd_id.clone()) + .fetch_optional(&self.pool) + .await?; + self.locations + .insert( + Key::CbrsId(cbsd_id), + last_location, + last_location + .map(|x| x.duration_to_expiration()) + .unwrap_or_else(|| Duration::days(365)) + .to_std()?, + ) + .await; + Ok(last_location) + } } diff --git a/mobile_verifier/src/heartbeats/mod.rs b/mobile_verifier/src/heartbeats/mod.rs index afc107dbd..5f35609b9 100644 --- a/mobile_verifier/src/heartbeats/mod.rs +++ b/mobile_verifier/src/heartbeats/mod.rs @@ -680,8 +680,8 @@ impl ValidatedHeartbeat { let truncated_timestamp = self.truncated_timestamp()?; sqlx::query( r#" - INSERT INTO cbrs_heartbeats (cbsd_id, hotspot_key, cell_type, latest_timestamp, truncated_timestamp, coverage_object, location_trust_score_multiplier) - VALUES ($1, $2, $3, $4, $5, $6, $7) + INSERT INTO cbrs_heartbeats (cbsd_id, hotspot_key, cell_type, latest_timestamp, truncated_timestamp, coverage_object, location_trust_score_multiplier, location_validation_timestamp, lat, lon) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) ON CONFLICT (cbsd_id, truncated_timestamp) DO UPDATE SET latest_timestamp = EXCLUDED.latest_timestamp, coverage_object = EXCLUDED.coverage_object @@ -694,6 +694,9 @@ impl ValidatedHeartbeat { .bind(truncated_timestamp) .bind(self.heartbeat.coverage_object) .bind(self.location_trust_score_multiplier) + .bind(self.heartbeat.location_validation_timestamp) + .bind(self.heartbeat.lat) + .bind(self.heartbeat.lon) .execute(&mut *exec) .await?; Ok(())