2019-03-06 11:51:05 -08:00
|
|
|
use actix_web::{blocking, Error as AWError};
|
2018-05-26 17:05:12 -04:00
|
|
|
use failure::Error;
|
2019-03-06 11:51:05 -08:00
|
|
|
use futures::Future;
|
2018-05-26 17:05:12 -04:00
|
|
|
use r2d2;
|
|
|
|
use r2d2_sqlite;
|
2019-03-06 11:51:05 -08:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2018-07-16 12:36:53 +06:00
|
|
|
use std::{thread::sleep, time::Duration};
|
2018-05-26 17:05:12 -04:00
|
|
|
|
|
|
|
pub type Pool = r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>;
|
|
|
|
pub type Connection = r2d2::PooledConnection<r2d2_sqlite::SqliteConnectionManager>;
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub enum WeatherAgg {
|
2018-07-16 12:36:53 +06:00
|
|
|
AnnualAgg { year: i32, total: f64 },
|
|
|
|
MonthAgg { year: i32, month: i32, total: f64 },
|
2018-05-26 17:05:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Queries {
|
|
|
|
GetTopTenHottestYears,
|
|
|
|
GetTopTenColdestYears,
|
|
|
|
GetTopTenHottestMonths,
|
2018-07-16 12:36:53 +06:00
|
|
|
GetTopTenColdestMonths,
|
2018-05-26 17:05:12 -04:00
|
|
|
}
|
|
|
|
|
2019-03-06 11:51:05 -08:00
|
|
|
pub fn execute(
|
|
|
|
pool: &Pool,
|
|
|
|
query: Queries,
|
|
|
|
) -> impl Future<Item = Vec<WeatherAgg>, Error = AWError> {
|
|
|
|
let pool = pool.clone();
|
|
|
|
blocking::run(move || match query {
|
|
|
|
Queries::GetTopTenHottestYears => get_hottest_years(pool.get()?),
|
|
|
|
Queries::GetTopTenColdestYears => get_coldest_years(pool.get()?),
|
|
|
|
Queries::GetTopTenHottestMonths => get_hottest_months(pool.get()?),
|
|
|
|
Queries::GetTopTenColdestMonths => get_coldest_months(pool.get()?),
|
|
|
|
})
|
|
|
|
.from_err()
|
2018-05-26 17:05:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_hottest_years(conn: Connection) -> Result<Vec<WeatherAgg>, Error> {
|
|
|
|
let stmt = "
|
|
|
|
SELECT cast(strftime('%Y', date) as int) as theyear,
|
2019-03-06 11:51:05 -08:00
|
|
|
sum(tmax) as total
|
2018-05-26 17:05:12 -04:00
|
|
|
FROM nyc_weather
|
|
|
|
WHERE tmax <> 'TMAX'
|
|
|
|
GROUP BY theyear
|
|
|
|
ORDER BY total DESC LIMIT 10;";
|
|
|
|
|
|
|
|
let mut prep_stmt = conn.prepare(stmt)?;
|
2018-07-16 12:36:53 +06:00
|
|
|
let annuals = prep_stmt
|
|
|
|
.query_map(&[], |row| WeatherAgg::AnnualAgg {
|
|
|
|
year: row.get(0),
|
|
|
|
total: row.get(1),
|
|
|
|
})
|
|
|
|
.and_then(|mapped_rows| {
|
|
|
|
Ok(mapped_rows
|
|
|
|
.map(|row| row.unwrap())
|
|
|
|
.collect::<Vec<WeatherAgg>>())
|
|
|
|
})?;
|
2018-05-26 17:05:12 -04:00
|
|
|
|
|
|
|
sleep(Duration::from_secs(2));
|
|
|
|
|
|
|
|
Ok(annuals)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_coldest_years(conn: Connection) -> Result<Vec<WeatherAgg>, Error> {
|
|
|
|
let stmt = "
|
|
|
|
SELECT cast(strftime('%Y', date) as int) as theyear,
|
2019-03-06 11:51:05 -08:00
|
|
|
sum(tmax) as total
|
2018-05-26 17:05:12 -04:00
|
|
|
FROM nyc_weather
|
|
|
|
WHERE tmax <> 'TMAX'
|
|
|
|
GROUP BY theyear
|
|
|
|
ORDER BY total ASC LIMIT 10;";
|
|
|
|
|
|
|
|
let mut prep_stmt = conn.prepare(stmt)?;
|
2018-07-16 12:36:53 +06:00
|
|
|
let annuals = prep_stmt
|
|
|
|
.query_map(&[], |row| WeatherAgg::AnnualAgg {
|
|
|
|
year: row.get(0),
|
|
|
|
total: row.get(1),
|
|
|
|
})
|
|
|
|
.and_then(|mapped_rows| {
|
|
|
|
Ok(mapped_rows
|
|
|
|
.map(|row| row.unwrap())
|
|
|
|
.collect::<Vec<WeatherAgg>>())
|
|
|
|
})?;
|
2018-05-26 17:05:12 -04:00
|
|
|
|
|
|
|
sleep(Duration::from_secs(2));
|
|
|
|
|
|
|
|
Ok(annuals)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_hottest_months(conn: Connection) -> Result<Vec<WeatherAgg>, Error> {
|
2018-07-16 12:36:53 +06:00
|
|
|
let stmt = "SELECT cast(strftime('%Y', date) as int) as theyear,
|
2019-03-06 11:51:05 -08:00
|
|
|
cast(strftime('%m', date) as int) as themonth,
|
|
|
|
sum(tmax) as total
|
2018-05-26 17:05:12 -04:00
|
|
|
FROM nyc_weather
|
|
|
|
WHERE tmax <> 'TMAX'
|
|
|
|
GROUP BY theyear, themonth
|
|
|
|
ORDER BY total DESC LIMIT 10;";
|
|
|
|
|
|
|
|
let mut prep_stmt = conn.prepare(stmt)?;
|
2018-07-16 12:36:53 +06:00
|
|
|
let annuals = prep_stmt
|
|
|
|
.query_map(&[], |row| WeatherAgg::MonthAgg {
|
|
|
|
year: row.get(0),
|
|
|
|
month: row.get(1),
|
|
|
|
total: row.get(2),
|
|
|
|
})
|
|
|
|
.and_then(|mapped_rows| {
|
|
|
|
Ok(mapped_rows
|
|
|
|
.map(|row| row.unwrap())
|
|
|
|
.collect::<Vec<WeatherAgg>>())
|
|
|
|
})?;
|
2018-05-26 17:05:12 -04:00
|
|
|
|
|
|
|
sleep(Duration::from_secs(2));
|
|
|
|
Ok(annuals)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_coldest_months(conn: Connection) -> Result<Vec<WeatherAgg>, Error> {
|
2018-07-16 12:36:53 +06:00
|
|
|
let stmt = "SELECT cast(strftime('%Y', date) as int) as theyear,
|
2019-03-06 11:51:05 -08:00
|
|
|
cast(strftime('%m', date) as int) as themonth,
|
|
|
|
sum(tmax) as total
|
2018-05-26 17:05:12 -04:00
|
|
|
FROM nyc_weather
|
|
|
|
WHERE tmax <> 'TMAX'
|
|
|
|
GROUP BY theyear, themonth
|
|
|
|
ORDER BY total ASC LIMIT 10;";
|
|
|
|
|
|
|
|
let mut prep_stmt = conn.prepare(stmt)?;
|
2018-07-16 12:36:53 +06:00
|
|
|
let annuals = prep_stmt
|
|
|
|
.query_map(&[], |row| WeatherAgg::MonthAgg {
|
|
|
|
year: row.get(0),
|
|
|
|
month: row.get(1),
|
|
|
|
total: row.get(2),
|
|
|
|
})
|
|
|
|
.and_then(|mapped_rows| {
|
|
|
|
Ok(mapped_rows
|
|
|
|
.map(|row| row.unwrap())
|
|
|
|
.collect::<Vec<WeatherAgg>>())
|
|
|
|
})?;
|
2018-05-26 17:05:12 -04:00
|
|
|
|
|
|
|
sleep(Duration::from_secs(2));
|
|
|
|
Ok(annuals)
|
2018-06-01 06:59:07 -04:00
|
|
|
}
|