Skip to content

Commit

Permalink
Populate cache with cbrs as well
Browse files Browse the repository at this point in the history
  • Loading branch information
macpie committed Oct 16, 2024
1 parent 1224f18 commit 2f7d9e4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions mobile_verifier/migrations/39_update_cbrs_hearbeats.sql
Original file line number Diff line number Diff line change
@@ -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;
31 changes: 30 additions & 1 deletion mobile_verifier/src/heartbeats/last_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
}
},
)
Expand Down Expand Up @@ -122,4 +122,33 @@ impl LocationCache {
.await;
Ok(last_location)
}

async fn fetch_cbrs_and_set(&self, cbsd_id: String) -> anyhow::Result<Option<LastLocation>> {
let last_location: Option<LastLocation> = 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)
}
}
7 changes: 5 additions & 2 deletions mobile_verifier/src/heartbeats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(())
Expand Down

0 comments on commit 2f7d9e4

Please sign in to comment.