From 0c76d84db9dc84e1e967af50ad3af31e0e9f3a7e Mon Sep 17 00:00:00 2001 From: Jorge Leitao Date: Mon, 22 Jan 2024 07:57:52 +0100 Subject: [PATCH] Simpler API --- examples/dk_jets.rs | 10 +++++----- examples/period.rs | 3 +-- src/icao_to_trace.rs | 10 +++++----- src/trace_month.rs | 22 ++++++++++++++-------- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/examples/dk_jets.rs b/examples/dk_jets.rs index 328a20c..e19644f 100644 --- a/examples/dk_jets.rs +++ b/examples/dk_jets.rs @@ -4,7 +4,7 @@ use clap::Parser; use num_format::{Locale, ToFormattedString}; use simple_logger::SimpleLogger; -use flights::{emissions, load_aircraft_types, load_aircrafts, Aircraft, Class, Fact, Leg}; +use flights::{emissions, load_aircraft_types, load_aircrafts, Class, Fact, Leg}; use time::Date; fn render(context: &Context) -> Result<(), Box> { @@ -69,10 +69,10 @@ struct Cli { async fn legs( from: Date, to: Date, - aircraft: &Aircraft, + icao_number: &str, client: Option<&flights::fs_azure::ContainerClient>, ) -> Result, Box> { - let positions = flights::cached_aircraft_positions(from, to, aircraft, client).await?; + let positions = flights::aircraft_positions(from, to, icao_number, client).await?; let mut positions = positions .into_iter() .map(|(_, p)| p) @@ -80,7 +80,7 @@ async fn legs( .collect::>(); positions.sort_unstable_by_key(|p| p.datetime()); - log::info!("Computing legs {}", aircraft.icao_number); + log::info!("Computing legs {}", icao_number); Ok(flights::legs(positions.into_iter())) } @@ -135,7 +135,7 @@ async fn main() -> Result<(), Box> { let client = client.as_ref(); let legs = private_jets.iter().map(|(_, aircraft)| async { - legs(from, to, aircraft, client) + legs(from, to, &aircraft.icao_number, client) .await .map(|legs| (aircraft.icao_number.clone(), legs)) }); diff --git a/examples/period.rs b/examples/period.rs index 8fb581f..03a8d00 100644 --- a/examples/period.rs +++ b/examples/period.rs @@ -123,8 +123,7 @@ async fn main() -> Result<(), Box> { let icao = &aircraft.icao_number; log::info!("ICAO number: {}", icao); - let positions = - flights::cached_aircraft_positions(from, to, &aircraft, client.as_ref()).await?; + let positions = flights::aircraft_positions(from, to, icao, client.as_ref()).await?; let mut positions = positions .into_iter() .map(|(_, p)| p) diff --git a/src/icao_to_trace.rs b/src/icao_to_trace.rs index 6fbe850..3303405 100644 --- a/src/icao_to_trace.rs +++ b/src/icao_to_trace.rs @@ -12,7 +12,7 @@ use time::Date; use time::PrimitiveDateTime; use super::Position; -use crate::{fs, fs_azure, Aircraft}; +use crate::{fs, fs_azure}; fn last_2(icao: &str) -> &str { let bytes = icao.as_bytes(); @@ -225,10 +225,10 @@ pub async fn positions( }) } -pub async fn aircraft_positions( +pub(crate) async fn cached_aircraft_positions( from: Date, to: Date, - aircraft: &Aircraft, + icao_number: &str, client: Option<&super::fs_azure::ContainerClient>, ) -> Result>, Box> { let dates = super::DateIter { @@ -240,7 +240,7 @@ pub async fn aircraft_positions( let tasks = dates.map(|date| async move { Result::<_, Box>::Ok(( date.clone(), - positions(&aircraft.icao_number, date, client) + positions(icao_number, date, client) .await? .collect::>(), )) @@ -253,4 +253,4 @@ pub async fn aircraft_positions( .await } -pub use crate::trace_month::cached_aircraft_positions; +pub use crate::trace_month::aircraft_positions; diff --git a/src/trace_month.rs b/src/trace_month.rs index b729a10..b05767d 100644 --- a/src/trace_month.rs +++ b/src/trace_month.rs @@ -8,7 +8,7 @@ use futures::{StreamExt, TryStreamExt}; use time::Date; use super::Position; -use crate::{fs, fs_azure, Aircraft}; +use crate::{fs, fs_azure}; fn cache_file_path(icao: &str, date: &time::Date) -> String { format!( @@ -37,19 +37,19 @@ fn get_month(current: &time::Date) -> (time::Date, time::Date) { async fn month_positions( month: &time::Date, - aircraft: &Aircraft, + icao_number: &str, client: Option<&super::fs_azure::ContainerClient>, ) -> Result>, Box> { - log::info!("month_positions({month},{})", aircraft.icao_number); + log::info!("month_positions({month},{icao_number})"); assert_eq!(month.day(), 1); - let blob_name = cache_file_path(&aircraft.icao_number, &month); + let blob_name = cache_file_path(&icao_number, &month); let (from, to) = get_month(&month); let action = fs::CacheAction::from_date(&to); // returns positions in the month, cached let fetch = async { - let positions = aircraft_positions(from, to, aircraft, client) + let positions = cached_aircraft_positions(from, to, icao_number, client) .await .unwrap(); @@ -67,10 +67,16 @@ async fn month_positions( Ok(serde_json::from_slice(&r)?) } -pub async fn cached_aircraft_positions( +/// Returns a map (date -> positions) for a given icao number. +/// # Implementation +/// This function is idempotent but not pure: +/// * the data is retrieved from `https://globe.adsbexchange.com` +/// * the call is cached on local disk or Azure Blob (depending on `client` configuration) +/// * the data is retrieved in batches of months and cached, to reduce IO +pub async fn aircraft_positions( from: Date, to: Date, - aircraft: &Aircraft, + icao_number: &str, client: Option<&super::fs_azure::ContainerClient>, ) -> Result>, Box> { let dates = super::DateIter { @@ -88,7 +94,7 @@ pub async fn cached_aircraft_positions( let tasks = months .into_iter() - .map(|month| async move { month_positions(&month, aircraft, client).await }); + .map(|month| async move { month_positions(&month, icao_number, client).await }); let positions = futures::stream::iter(tasks) // limit concurrent tasks