Skip to content

Commit

Permalink
Add location tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
kurotych committed Dec 4, 2024
1 parent 44b26c5 commit 758bdaf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 2 additions & 0 deletions mobile_config/migrations/7_mobile_radio_tracker.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ CREATE TABLE IF NOT EXISTS mobile_radio_tracker (
hash TEXT NOT NULL,
last_changed_at TIMESTAMPTZ NOT NULL,
last_checked_at TIMESTAMPTZ NOT NULL,
asserted_location NUMERIC,
asserted_location_change_at TIMESTAMPTZ,
PRIMARY KEY (entity_key)
);
26 changes: 20 additions & 6 deletions mobile_config/src/mobile_radio_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ struct TrackedMobileRadio {
hash: String,
last_changed_at: DateTime<Utc>,
last_checked_at: DateTime<Utc>,
asserted_location: Option<i64>,
asserted_location_at: Option<DateTime<Utc>>,
}

impl TrackedMobileRadio {
Expand All @@ -86,17 +88,23 @@ impl TrackedMobileRadio {
hash: radio.hash(),
last_changed_at: radio.refreshed_at,
last_checked_at: Utc::now(),
asserted_location: radio.location,
asserted_location_at: None,
}
}

fn update_from_radio(mut self, radio: &MobileRadio) -> Self {
let new_hash = radio.hash();
self.last_checked_at = Utc::now();
if self.hash != new_hash {
self.hash = new_hash;
self.last_changed_at = radio.refreshed_at;
}
if self.asserted_location != radio.location {
self.asserted_location = radio.location;
self.asserted_location_at = Some(self.last_checked_at);
}

self.last_checked_at = Utc::now();
self
}
}
Expand Down Expand Up @@ -192,7 +200,9 @@ async fn get_tracked_radios(
entity_key,
hash,
last_changed_at,
last_checked_at
last_checked_at,
asserted_location,
asserted_location_at
FROM mobile_radio_tracker
"#,
)
Expand Down Expand Up @@ -241,24 +251,28 @@ async fn update_tracked_radios(
) -> anyhow::Result<()> {
let mut txn = pool.begin().await?;

const BATCH_SIZE: usize = (u16::MAX / 4) as usize;
const BATCH_SIZE: usize = (u16::MAX / 6) as usize;

for chunk in tracked_radios.chunks(BATCH_SIZE) {
QueryBuilder::new(
"INSERT INTO mobile_radio_tracker(entity_key, hash, last_changed_at, last_checked_at)",
"INSERT INTO mobile_radio_tracker(entity_key, hash, last_changed_at, last_checked_at, asserted_location, asserted_location_change_at)",
)
.push_values(chunk, |mut b, tracked_radio| {
b.push_bind(&tracked_radio.entity_key)
.push_bind(&tracked_radio.hash)
.push_bind(tracked_radio.last_changed_at)
.push_bind(tracked_radio.last_checked_at);
.push_bind(tracked_radio.last_checked_at)
.push_bind(tracked_radio.asserted_location)
.push_bind(tracked_radio.asserted_location_at);
})
.push(
r#"
ON CONFLICT (entity_key) DO UPDATE SET
hash = EXCLUDED.hash,
last_changed_at = EXCLUDED.last_changed_at,
last_checked_at = EXCLUDED.last_checked_at
last_checked_at = EXCLUDED.last_checked_at,
asserted_location = EXCLUDED.last_asserted_location,
asserted_location_at = EXCLUDED.asserted_location_change_at,
"#,
)
.build()
Expand Down

0 comments on commit 758bdaf

Please sign in to comment.