Skip to content

Commit

Permalink
Update location cache to handle cbrs and wifi
Browse files Browse the repository at this point in the history
  • Loading branch information
macpie committed Oct 16, 2024
1 parent 8c5c856 commit 1224f18
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 43 deletions.
83 changes: 42 additions & 41 deletions mobile_verifier/src/heartbeats/last_location.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use std::sync::Arc;

use chrono::{DateTime, Duration, Utc};
use helium_crypto::PublicKeyBinary;
use retainer::Cache;
use sqlx::PgPool;
use std::sync::Arc;

#[derive(Debug, Clone, Eq, Ord, PartialEq, PartialOrd)]
pub enum Key {
CbrsId(String),
WifiPubKey(PublicKeyBinary),
}

#[derive(sqlx::FromRow, Copy, Clone)]
pub struct LastLocation {
Expand Down Expand Up @@ -38,7 +43,7 @@ impl LastLocation {
#[derive(Clone)]
pub struct LocationCache {
pool: PgPool,
locations: Arc<Cache<PublicKeyBinary, Option<LastLocation>>>,
locations: Arc<Cache<Key, Option<LastLocation>>>,
}

impl LocationCache {
Expand All @@ -56,9 +61,39 @@ impl LocationCache {
}
}

async fn fetch_from_db_and_set(
pub async fn fetch_last_location(&self, key: Key) -> anyhow::Result<Option<LastLocation>> {
Ok(
if let Some(last_location) = self.locations.get(&key).await {
*last_location
} else {
match key {
Key::WifiPubKey(pub_key_bin) => self.fetch_wifi_and_set(pub_key_bin).await?,
Key::CbrsId(_) => None,
}
},
)
}

pub async fn set_last_location(
&self,
key: Key,
last_location: LastLocation,
) -> anyhow::Result<()> {
let duration_to_expiration = last_location.duration_to_expiration();
self.locations
.insert(key, Some(last_location), duration_to_expiration.to_std()?)
.await;
Ok(())
}

/// Only used for testing.
pub async fn delete_last_location(&self, key: Key) {
self.locations.remove(&key).await;
}

async fn fetch_wifi_and_set(
&self,
hotspot: &PublicKeyBinary,
pub_key_bin: PublicKeyBinary,
) -> anyhow::Result<Option<LastLocation>> {
let last_location: Option<LastLocation> = sqlx::query_as(
r#"
Expand All @@ -72,12 +107,12 @@ impl LocationCache {
"#,
)
.bind(Utc::now() - Duration::hours(12))
.bind(hotspot)
.bind(pub_key_bin.clone())
.fetch_optional(&self.pool)
.await?;
self.locations
.insert(
hotspot.clone(),
Key::WifiPubKey(pub_key_bin),
last_location,
last_location
.map(|x| x.duration_to_expiration())
Expand All @@ -87,38 +122,4 @@ impl LocationCache {
.await;
Ok(last_location)
}

pub async fn fetch_last_location(
&self,
hotspot: &PublicKeyBinary,
) -> anyhow::Result<Option<LastLocation>> {
Ok(
if let Some(last_location) = self.locations.get(hotspot).await {
*last_location
} else {
self.fetch_from_db_and_set(hotspot).await?
},
)
}

pub async fn set_last_location(
&self,
hotspot: &PublicKeyBinary,
last_location: LastLocation,
) -> anyhow::Result<()> {
let duration_to_expiration = last_location.duration_to_expiration();
self.locations
.insert(
hotspot.clone(),
Some(last_location),
duration_to_expiration.to_std()?,
)
.await;
Ok(())
}

/// Only used for testing.
pub async fn delete_last_location(&self, hotspot: &PublicKeyBinary) {
self.locations.remove(hotspot).await;
}
}
5 changes: 3 additions & 2 deletions mobile_verifier/src/heartbeats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use futures::stream::{Stream, StreamExt};
use h3o::{CellIndex, LatLng};
use helium_crypto::PublicKeyBinary;
use helium_proto::services::poc_mobile::{self as proto, LocationSource};
use last_location::Key;
use retainer::Cache;
use rust_decimal::{prelude::ToPrimitive, Decimal};
use rust_decimal_macros::dec;
Expand Down Expand Up @@ -521,7 +522,7 @@ impl ValidatedHeartbeat {
let is_valid = match heartbeat.location_validation_timestamp {
None => {
if let Some(last_location) = last_location_cache
.fetch_last_location(&heartbeat.hotspot_key)
.fetch_last_location(Key::WifiPubKey(heartbeat.hotspot_key.clone()))
.await?
{
heartbeat.lat = last_location.lat;
Expand All @@ -538,7 +539,7 @@ impl ValidatedHeartbeat {
Some(location_validation_timestamp) => {
last_location_cache
.set_last_location(
&heartbeat.hotspot_key,
Key::WifiPubKey(heartbeat.hotspot_key.clone()),
LastLocation::new(
location_validation_timestamp,
heartbeat.timestamp,
Expand Down

0 comments on commit 1224f18

Please sign in to comment.