Skip to content

Commit

Permalink
Update LastLocation to be 12 hours from last heartbeat with validate …
Browse files Browse the repository at this point in the history
…location
  • Loading branch information
bbalser committed Jun 3, 2024
1 parent 5f1e7c5 commit c015eb2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
18 changes: 12 additions & 6 deletions mobile_verifier/src/heartbeats/last_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,29 @@ use sqlx::PgPool;
#[derive(sqlx::FromRow, Copy, Clone)]
pub struct LastLocation {
pub location_validation_timestamp: DateTime<Utc>,
pub latest_timestamp: DateTime<Utc>,
pub lat: f64,
pub lon: f64,
}

impl LastLocation {
pub fn new(location_validation_timestamp: DateTime<Utc>, lat: f64, lon: f64) -> Self {
pub fn new(
location_validation_timestamp: DateTime<Utc>,
latest_timestamp: DateTime<Utc>,
lat: f64,
lon: f64,
) -> Self {
Self {
location_validation_timestamp,
latest_timestamp,
lat,
lon,
}
}

/// 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())
}
}

Expand Down Expand Up @@ -56,12 +62,12 @@ impl LocationCache {
) -> anyhow::Result<Option<LastLocation>> {
let last_location: Option<LastLocation> = 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
"#,
)
Expand Down
1 change: 1 addition & 0 deletions mobile_verifier/src/heartbeats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ impl ValidatedHeartbeat {
&heartbeat.hotspot_key,
LastLocation::new(
location_validation_timestamp,
heartbeat.timestamp,
heartbeat.lat,
heartbeat.lon,
),
Expand Down
14 changes: 7 additions & 7 deletions mobile_verifier/tests/integrations/heartbeats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Utc> = "2023-08-20 00:00:00.000000000 UTC".parse().unwrap();
let epoch_end: DateTime<Utc> = "2023-08-25 00:00:00.000000000 UTC".parse().unwrap();
let epoch_end: DateTime<Utc> = Utc::now() + Duration::days(1);

let first_heartbeat = Heartbeat {
hb_type: HbType::Wifi,
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit c015eb2

Please sign in to comment.