Skip to content

Commit

Permalink
Simpler API
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Jan 22, 2024
1 parent 612e394 commit 0c76d84
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
10 changes: 5 additions & 5 deletions examples/dk_jets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error>> {
Expand Down Expand Up @@ -69,18 +69,18 @@ struct Cli {
async fn legs(
from: Date,
to: Date,
aircraft: &Aircraft,
icao_number: &str,
client: Option<&flights::fs_azure::ContainerClient>,
) -> Result<Vec<Leg>, Box<dyn Error>> {
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)
.flatten()
.collect::<Vec<_>>();
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()))
}

Expand Down Expand Up @@ -135,7 +135,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

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))
});
Expand Down
3 changes: 1 addition & 2 deletions examples/period.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
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)
Expand Down
10 changes: 5 additions & 5 deletions src/icao_to_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<HashMap<Date, Vec<Position>>, Box<dyn Error>> {
let dates = super::DateIter {
Expand All @@ -240,7 +240,7 @@ pub async fn aircraft_positions(
let tasks = dates.map(|date| async move {
Result::<_, Box<dyn Error>>::Ok((
date.clone(),
positions(&aircraft.icao_number, date, client)
positions(icao_number, date, client)
.await?
.collect::<Vec<_>>(),
))
Expand All @@ -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;
22 changes: 14 additions & 8 deletions src/trace_month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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<HashMap<Date, Vec<Position>>, Box<dyn Error>> {
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();

Expand All @@ -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<HashMap<Date, Vec<Position>>, Box<dyn Error>> {
let dates = super::DateIter {
Expand All @@ -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
Expand Down

0 comments on commit 0c76d84

Please sign in to comment.