Skip to content

Commit

Permalink
Added more countries (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecardleitao authored Jan 28, 2024
1 parent 794b0d0 commit d9c18db
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ cargo run --example country -- --from=2024-01-13 --to=2024-01-21 --country=denma
cargo run --example country -- --from=2024-01-13 --to=2024-01-21 --country=denmark
# Story about Portuguese private jets that flew between two dates
cargo run --example country -- --from=2024-01-13 --to=2024-01-21 --country=portugal

# Story about German private jets that flew between in 2023, where the azure-sas-token
# is on the file token.txt
cargo run --example country -- --from=2023-01-01 --to=2024-01-01 --country=germany --azure-sas-token=$(cat token.txt)
```

## Methodology
Expand Down
32 changes: 28 additions & 4 deletions examples/country.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{collections::HashMap, error::Error};

use clap::Parser;
use futures::{StreamExt, TryStreamExt};
use num_format::{Locale, ToFormattedString};
use simple_logger::SimpleLogger;

Expand Down Expand Up @@ -62,6 +63,8 @@ fn parse_date(arg: &str) -> Result<time::Date, time::error::Parse> {
enum Country {
Denmark,
Portugal,
Spain,
Germany,
}

#[derive(clap::ValueEnum, Debug, Clone, Copy)]
Expand Down Expand Up @@ -96,27 +99,35 @@ impl Country {
match self {
Self::Denmark => "Danish",
Self::Portugal => "Portuguese",
Self::Spain => "Spanish",
Self::Germany => "German",
}
}

fn plural(&self) -> &'static str {
match self {
Self::Denmark => "Danes",
Self::Portugal => "Portugueses",
Self::Spain => "Spanish",
Self::Germany => "Germans",
}
}

fn tail_number(&self) -> &'static str {
match self {
Country::Denmark => "OY-",
Country::Portugal => "CS-",
Self::Denmark => "OY-",
Self::Portugal => "CS-",
Self::Spain => "EC-",
Self::Germany => "D-",
}
}

fn name(&self) -> &'static str {
match self {
Country::Denmark => "Denmark",
Country::Portugal => "Portugal",
Country::Spain => "Spain",
Country::Germany => "Germany",
}
}

Expand All @@ -132,6 +143,16 @@ impl Country {
source: "A portuguese emitted 4.1 t CO2/person/year in 2022 according to [work bank data](https://ourworldindata.org/co2/country/denmark).".to_string(),
date: "2024-01-23".to_string(),
},
Country::Spain => Fact {
claim: 5.2,
source: "A spanish emitted 5.2 t CO2/person/year in 2022 according to [work bank data](https://ourworldindata.org/co2/country/spain).".to_string(),
date: "2024-01-23".to_string(),
},
Country::Germany => Fact {
claim: 8.0,
source: "A german emitted 8.0 t CO2/person/year in 2022 according to [work bank data](https://ourworldindata.org/co2/country/germany).".to_string(),
date: "2024-01-23".to_string(),
},
}
}
}
Expand Down Expand Up @@ -245,8 +266,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
.map(|legs| (aircraft.icao_number.clone(), legs))
});

let legs = futures::future::join_all(legs).await;
let legs = legs.into_iter().collect::<Result<HashMap<_, _>, _>>()?;
let legs = futures::stream::iter(legs)
// limit concurrent tasks
.buffered(1)
.try_collect::<HashMap<_, _>>()
.await?;

let number_of_private_jets = Fact {
claim: legs.iter().filter(|x| x.1.len() > 0).count().to_formatted_string(&Locale::en),
Expand Down

0 comments on commit d9c18db

Please sign in to comment.