Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have location tracking in mobile-config track #907

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
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)
);
85 changes: 78 additions & 7 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>,
michaeldjeffrey marked this conversation as resolved.
Show resolved Hide resolved
asserted_location_change_at: Option<DateTime<Utc>>,
bbalser marked this conversation as resolved.
Show resolved Hide resolved
}

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_change_at: None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the thinking about making this None? The first time we see a radio, this field will be null and how do you envision clients working with it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the thinking about making this None?

My logic is next: At the beginning it is None because we don't have information about previous time when asserted_location was changed.

The first time we see a radio, this field will be null and how do you envision clients working with it?

I expect clients (or functions that use tracked mobile radio table) will handle None by their own logic.
Otherwise, if we hardcode some value here, we don't give an option for the clients.

}
}

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_change_at = Some(self.last_checked_at);
bbalser marked this conversation as resolved.
Show resolved Hide resolved
}

self.last_checked_at = Utc::now();
self
}
}
Expand Down Expand Up @@ -131,7 +139,7 @@ impl MobileRadioTracker {
}

async fn run(self, mut shutdown: triggered::Listener) -> anyhow::Result<()> {
tracing::info!("starting");
tracing::info!("starting with interval: {:?}", self.interval);
let mut interval = tokio::time::interval(self.interval);

loop {
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::bigint,
asserted_location_change_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_change_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.asserted_location,
asserted_location_change_at = EXCLUDED.asserted_location_change_at
"#,
)
.build()
Expand Down Expand Up @@ -321,6 +335,63 @@ mod tests {
assert_eq!(radio.hash(), result[0].hash);
}

#[tokio::test]
async fn last_asserted_location_will_not_updated_if_nothing_changes() {
// location None
let mut radio = mobile_radio(vec![1, 2, 3]);
radio.location = None;
let tracked_radio = TrackedMobileRadio::new(&radio);
let mut tracked_radios = HashMap::new();
tracked_radios.insert(tracked_radio.entity_key.clone(), tracked_radio);

let result = identify_changes(stream::iter(vec![radio.clone()]), tracked_radios).await;

assert!(result[0].asserted_location_change_at.is_none());
assert!(result[0].asserted_location.is_none());

// location is 1
let mut radio = mobile_radio(vec![1, 2, 3]);
radio.location = Some(1);
let tracked_radio = TrackedMobileRadio::new(&radio);
let mut tracked_radios = HashMap::new();
tracked_radios.insert(tracked_radio.entity_key.clone(), tracked_radio);

let result = identify_changes(stream::iter(vec![radio.clone()]), tracked_radios).await;
assert!(result[0].asserted_location_change_at.is_none());
assert_eq!(result[0].asserted_location, Some(1));
}

#[tokio::test]
async fn will_update_last_asserted_location_change_at_when_location_changes() {
let mut radio = mobile_radio(vec![1, 2, 3]);
radio.location = None;
let tracked_radio = TrackedMobileRadio::new(&radio);
radio.location = Some(1);

let mut tracked_radios = HashMap::new();
tracked_radios.insert(tracked_radio.entity_key.clone(), tracked_radio);

let result = identify_changes(stream::iter(vec![radio.clone()]), tracked_radios).await;

assert_eq!(
result[0].asserted_location_change_at.unwrap(),
result[0].last_checked_at
);
assert_eq!(result[0].asserted_location.unwrap(), 1);

let tracked_radio = TrackedMobileRadio::new(&radio);
radio.location = Some(2);
let mut tracked_radios = HashMap::new();
tracked_radios.insert(tracked_radio.entity_key.clone(), tracked_radio);
let result = identify_changes(stream::iter(vec![radio.clone()]), tracked_radios).await;

assert_eq!(
result[0].asserted_location_change_at.unwrap(),
result[0].last_checked_at
);
assert_eq!(result[0].asserted_location.unwrap(), 2);
}

fn mobile_radio(entity_key: EntityKey) -> MobileRadio {
MobileRadio {
entity_key,
Expand Down