Skip to content

Instantly share code, notes, and snippets.

@jayhuang75
Last active October 25, 2020 03:18
Show Gist options
  • Select an option

  • Save jayhuang75/661d087a5420e72853fc4d09ec7dc412 to your computer and use it in GitHub Desktop.

Select an option

Save jayhuang75/661d087a5420e72853fc4d09ec7dc412 to your computer and use it in GitHub Desktop.
SMS alert Covid-19
pub async fn run(file: &str) -> Result<Vec<alert::CovidStats>, Box<dyn Error>> {
let mut reader = csv::ReaderBuilder::new()
.has_headers(true)
.from_path(file)?;
let today_covid_report: Vec<alert::CovidStats> = reader
.records()
.into_iter()
.par_bridge()
.map(|line| {
let result = line.as_ref().expect("a CSV record");
alert::CovidStats {
pruid: result[0].to_string(),
prname: result[1].to_string(),
date: result[3].to_string(),
numtoday: result[12].to_string().parse::<u64>().unwrap_or(0),
numdeathstoday: result[16].to_string().parse::<u64>().unwrap_or(0),
}
})
.filter(|line| {
let chrono_date = NaiveDate::parse_from_str(&line.date, "%Y-%m-%d").unwrap();
let now: Date<Local> = Local::now().date();
now.naive_local()
.signed_duration_since(chrono_date)
.num_days()
== 1
})
.filter(|msg| msg.pruid == env::var("PRUID").unwrap().as_str().to_string())
.collect();
Ok(today_covid_report)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment