Skip to content

Commit

Permalink
Simplify sql query
Browse files Browse the repository at this point in the history
  • Loading branch information
kurotych committed Jan 3, 2025
1 parent a089f30 commit 96084a0
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions mobile_config/src/mobile_radio_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,32 +208,24 @@ pub async fn get_tracked_radios(
fn get_all_mobile_radios(metadata: &Pool<Postgres>) -> impl Stream<Item = MobileRadio> + '_ {
sqlx::query_as::<_, MobileRadio>(
r#"
WITH unique_entity_keys AS (
SELECT
kta.entity_key as entity_key,
kta.asset as asset,
MAX(mhi.refreshed_at) AS refreshed_at
FROM key_to_assets kta
INNER JOIN mobile_hotspot_infos mhi
ON kta.asset = mhi.asset
WHERE kta.entity_key IS NOT NULL
AND mhi.refreshed_at IS NOT NULL
GROUP BY kta.entity_key, kta.asset
)
SELECT
uek.entity_key,
mhi.refreshed_at,
mhi.location::bigint,
mhi.is_full_hotspot::int,
mhi.num_location_asserts,
mhi.is_active::int,
mhi.dc_onboarding_fee_paid::bigint,
mhi.device_type::text,
mhi.deployment_info::text
FROM unique_entity_keys uek
DISTINCT ON (kta.entity_key, mhi.asset)
kta.entity_key,
mhi.asset,
mhi.refreshed_at,
mhi.location::bigint,
mhi.is_full_hotspot::int,
mhi.num_location_asserts,
mhi.is_active::int,
mhi.dc_onboarding_fee_paid::bigint,
mhi.device_type::text,
mhi.deployment_info::text
FROM key_to_assets kta
INNER JOIN mobile_hotspot_infos mhi ON
uek.asset = mhi.asset and mhi.refreshed_at = uek.refreshed_at
kta.asset = mhi.asset
WHERE kta.entity_key IS NOT NULL
AND mhi.refreshed_at IS NOT NULL
ORDER BY kta.entity_key, mhi.asset, refreshed_at DESC
"#,
)
.fetch(metadata)
Expand Down

0 comments on commit 96084a0

Please sign in to comment.