Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mobile_verifier/migrations/52_rename_latest_timestamp.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE IF EXISTS wifi_heartbeats RENAME COLUMN latest_timestamp TO first_timestamp;
17 changes: 9 additions & 8 deletions mobile_verifier/src/heartbeats/last_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ use super::Heartbeat;
#[derive(Debug, sqlx::FromRow, Copy, Clone, PartialEq)]
pub struct LastLocation {
pub location_validation_timestamp: DateTime<Utc>,
pub latest_timestamp: DateTime<Utc>,
#[sqlx(rename = "first_timestamp")]
pub heartbeat_timestamp: DateTime<Utc>,
pub lat: f64,
pub lon: f64,
}

impl LastLocation {
pub fn new(
location_validation_timestamp: DateTime<Utc>,
latest_timestamp: DateTime<Utc>,
heartbeat_timestamp: DateTime<Utc>,
lat: f64,
lon: f64,
) -> Self {
Self {
location_validation_timestamp,
latest_timestamp,
heartbeat_timestamp,
lat,
lon,
}
Expand Down Expand Up @@ -113,13 +114,13 @@ impl LocationCache {
) -> anyhow::Result<Option<LastLocation>> {
let last_location: Option<LastLocation> = 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
"#,
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
Expand Down
4 changes: 2 additions & 2 deletions mobile_verifier/src/heartbeats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"#,
)
Expand Down
2 changes: 1 addition & 1 deletion mobile_verifier/src/rewarder/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub async fn no_wifi_heartbeats(
reward_period: &Range<DateTime<Utc>>,
) -> anyhow::Result<bool> {
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)
Expand Down
8 changes: 4 additions & 4 deletions mobile_verifier/tests/integrations/heartbeats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down