Skip to content

Commit

Permalink
Convert empty string to None for cbsd_id in radio_threshold (#756)
Browse files Browse the repository at this point in the history
* Convert empty string to None for cbsd_id in radio_threshold

* Updates a test and added 2 more
  • Loading branch information
bbalser authored Mar 9, 2024
1 parent e9a6801 commit 60b5450
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
91 changes: 90 additions & 1 deletion file_store/src/mobile_radio_threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl TryFrom<RadioThresholdReportReqV1> for RadioThresholdReportReq {
type Error = Error;
fn try_from(v: RadioThresholdReportReqV1) -> Result<Self> {
Ok(Self {
cbsd_id: v.cbsd_id.parse().ok(),
cbsd_id: Some(v.cbsd_id).filter(|s| !s.is_empty()),
hotspot_pubkey: v.hotspot_pubkey.into(),
bytes_threshold: v.bytes_threshold,
subscriber_threshold: v.subscriber_threshold,
Expand Down Expand Up @@ -167,3 +167,92 @@ impl From<VerifiedRadioThresholdIngestReport> for VerifiedRadioThresholdIngestRe
}
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use chrono::NaiveDateTime;

use super::*;

#[test]
fn radio_threshold_from_proto_wifi() -> anyhow::Result<()> {
let pubkey = PublicKeyBinary::from_str("1trSusedShTqrkW7HUxv9QtrjadcnnQEmTJFTdBLDkERUV4bb3rXjzeL7QgGS6rFnsqkHwsUVTxodx2ZtKQ4KehaVzfji6jjTKH85JjmdQUAbqakURZJrdDjCnTHkaVae2mh5asCyQnDXvFpty4eKbaupQKgFuZmzVrowuAMjV1T31yZisa5i1eux2RTuMsfyHu6emk87X3BAcHwTd6vKok1SkBGQmPUo7ThJE7qSD5bixMuKXyowzCEeLkYkrhQr1yCsBwmBmnxT5ZydsTkJQdhKvtnyVxh1kSJi59MqAbD6N4DfGzSAqBSNQZSUXKrXoHDuYZ1wL7A2MLizXcEUGqWFdKfBaJ5ekKthRZjLGpWKP")?;

let carrier_pubkey =
PublicKeyBinary::from_str("14ihsKqVhXqfzET1dkLZGNQWrB9ZeGnqJtdMGajFjPmwKsKEEAC")?;

let proto = RadioThresholdIngestReportV1 {
received_timestamp: 1712624400000,
report: Some(RadioThresholdReportReqV1 {
cbsd_id: "".to_string(),
hotspot_pubkey: pubkey.as_ref().into(),
bytes_threshold: 1000,
subscriber_threshold: 3,
threshold_timestamp: 1712624400,
carrier_pub_key: carrier_pubkey.as_ref().into(),
signature: vec![],
}),
};

let report = RadioThresholdIngestReport::try_from(proto)?;
assert_eq!(parse_dt("2024-04-09 01:00:00"), report.received_timestamp);
assert_eq!(pubkey, report.report.hotspot_pubkey);
assert_eq!(None, report.report.cbsd_id);
assert_eq!(1000, report.report.bytes_threshold);
assert_eq!(3, report.report.subscriber_threshold);
assert_eq!(
parse_dt("2024-04-09 01:00:00"),
report.report.threshold_timestamp
);
assert_eq!(carrier_pubkey, report.report.carrier_pub_key);

Ok(())
}

#[test]
fn radio_threshold_from_proto_cbrs() -> anyhow::Result<()> {
let pubkey =
PublicKeyBinary::from_str("112HqsSX9Ft4ehxQCAcdb4cDSYX2ntsBZ7rtooioz3d3VXcF7MRr")?;

let carrier_pubkey =
PublicKeyBinary::from_str("14ihsKqVhXqfzET1dkLZGNQWrB9ZeGnqJtdMGajFjPmwKsKEEAC")?;

let proto = RadioThresholdIngestReportV1 {
received_timestamp: 1712624400000,
report: Some(RadioThresholdReportReqV1 {
cbsd_id: "P27-SCE4255W2112CW5003971".to_string(),
hotspot_pubkey: pubkey.as_ref().into(),
bytes_threshold: 1000,
subscriber_threshold: 3,
threshold_timestamp: 1712624400,
carrier_pub_key: carrier_pubkey.as_ref().into(),
signature: vec![],
}),
};

let report = RadioThresholdIngestReport::try_from(proto)?;
assert_eq!(parse_dt("2024-04-09 01:00:00"), report.received_timestamp);
assert_eq!(pubkey, report.report.hotspot_pubkey);
assert_eq!(
Some("P27-SCE4255W2112CW5003971".to_string()),
report.report.cbsd_id
);
assert_eq!(1000, report.report.bytes_threshold);
assert_eq!(3, report.report.subscriber_threshold);
assert_eq!(
parse_dt("2024-04-09 01:00:00"),
report.report.threshold_timestamp
);
assert_eq!(carrier_pubkey, report.report.carrier_pub_key);

Ok(())
}

fn parse_dt(dt: &str) -> DateTime<Utc> {
NaiveDateTime::parse_from_str(dt, "%Y-%m-%d %H:%M:%S")
.expect("unable_to_parse")
.and_utc()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE radio_threshold SET cbsd_id = NULL WHERE cbsd_id = '';
2 changes: 1 addition & 1 deletion mobile_verifier/src/radio_threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub async fn verified_radio_thresholds(
.fetch(pool);
let mut map = VerifiedRadioThresholds::default();
while let Some(row) = rows.try_next().await? {
map.insert(row.hotspot_pubkey, row.cbsd_id);
map.insert(row.hotspot_pubkey, row.cbsd_id.filter(|s| !s.is_empty()));
}
Ok(map)
}
4 changes: 2 additions & 2 deletions mobile_verifier/tests/hex_boosting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ async fn seed_radio_thresholds(
received_timestamp: Default::default(),
report: RadioThresholdReportReq {
hotspot_pubkey: HOTSPOT_1.parse().unwrap(),
cbsd_id: None,
cbsd_id: Some("".to_string()),
bytes_threshold: 1000000,
subscriber_threshold: 3,
threshold_timestamp: ts,
Expand All @@ -1472,7 +1472,7 @@ async fn seed_radio_thresholds(
received_timestamp: Default::default(),
report: RadioThresholdReportReq {
hotspot_pubkey: HOTSPOT_3.parse().unwrap(),
cbsd_id: None,
cbsd_id: Some("".to_string()),
bytes_threshold: 1000000,
subscriber_threshold: 3,
threshold_timestamp: ts,
Expand Down

0 comments on commit 60b5450

Please sign in to comment.