2022-07-09 21:08:11 +01:00
|
|
|
use std::{thread::sleep, time::Duration};
|
|
|
|
|
2022-02-06 08:19:35 +00:00
|
|
|
use actix_web::{error, web, Error};
|
2022-01-29 16:25:46 +00:00
|
|
|
use rusqlite::Statement;
|
2020-01-12 14:04:02 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
2018-05-26 17:05:12 -04:00
|
|
|
|
|
|
|
pub type Pool = r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>;
|
|
|
|
pub type Connection = r2d2::PooledConnection<r2d2_sqlite::SqliteConnectionManager>;
|
2020-01-30 07:52:10 +01:00
|
|
|
type WeatherAggResult = Result<Vec<WeatherAgg>, rusqlite::Error>;
|
2018-05-26 17:05:12 -04:00
|
|
|
|
|
|
|
#[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
|
|
|
}
|
|
|
|
|
2021-10-07 03:04:59 +01:00
|
|
|
#[allow(clippy::enum_variant_names)]
|
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
|
|
|
}
|
|
|
|
|
2022-02-06 08:19:35 +00:00
|
|
|
pub async fn execute(pool: &Pool, query: Queries) -> Result<Vec<WeatherAgg>, Error> {
|
2019-03-06 11:51:05 -08:00
|
|
|
let pool = pool.clone();
|
2022-02-06 08:19:35 +00:00
|
|
|
|
|
|
|
let conn = web::block(move || pool.get())
|
|
|
|
.await?
|
|
|
|
.map_err(error::ErrorInternalServerError)?;
|
|
|
|
|
2020-01-30 07:52:10 +01:00
|
|
|
web::block(move || {
|
|
|
|
// simulate an expensive query, see comments at top of main.rs
|
2022-02-18 03:13:49 +00:00
|
|
|
sleep(Duration::from_secs(1));
|
2020-01-30 07:52:10 +01:00
|
|
|
|
2022-02-06 08:19:35 +00:00
|
|
|
match query {
|
|
|
|
Queries::GetTopTenHottestYears => get_hottest_years(conn),
|
|
|
|
Queries::GetTopTenColdestYears => get_coldest_years(conn),
|
|
|
|
Queries::GetTopTenHottestMonths => get_hottest_months(conn),
|
|
|
|
Queries::GetTopTenColdestMonths => get_coldest_months(conn),
|
|
|
|
}
|
2019-03-06 11:51:05 -08:00
|
|
|
})
|
2022-02-06 08:19:35 +00:00
|
|
|
.await?
|
|
|
|
.map_err(error::ErrorInternalServerError)
|
2018-05-26 17:05:12 -04:00
|
|
|
}
|
|
|
|
|
2020-01-30 07:52:10 +01:00
|
|
|
fn get_hottest_years(conn: Connection) -> WeatherAggResult {
|
|
|
|
let stmt = conn.prepare(
|
|
|
|
"
|
2018-05-26 17:05:12 -04:00
|
|
|
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
|
2020-01-30 07:52:10 +01:00
|
|
|
ORDER BY total DESC LIMIT 10",
|
|
|
|
)?;
|
2018-05-26 17:05:12 -04:00
|
|
|
|
2020-01-30 07:52:10 +01:00
|
|
|
get_rows_as_annual_agg(stmt)
|
2018-05-26 17:05:12 -04:00
|
|
|
}
|
|
|
|
|
2020-01-30 07:52:10 +01:00
|
|
|
fn get_coldest_years(conn: Connection) -> WeatherAggResult {
|
|
|
|
let stmt = conn.prepare(
|
|
|
|
"
|
2018-05-26 17:05:12 -04:00
|
|
|
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
|
2020-01-30 07:52:10 +01:00
|
|
|
ORDER BY total ASC LIMIT 10",
|
|
|
|
)?;
|
2018-05-26 17:05:12 -04:00
|
|
|
|
2020-01-30 07:52:10 +01:00
|
|
|
get_rows_as_annual_agg(stmt)
|
|
|
|
}
|
2018-05-26 17:05:12 -04:00
|
|
|
|
2020-01-30 07:52:10 +01:00
|
|
|
fn get_rows_as_annual_agg(mut statement: Statement) -> WeatherAggResult {
|
|
|
|
statement
|
2022-01-29 16:25:46 +00:00
|
|
|
.query_map([], |row| {
|
2020-01-30 07:52:10 +01:00
|
|
|
Ok(WeatherAgg::AnnualAgg {
|
|
|
|
year: row.get(0)?,
|
|
|
|
total: row.get(1)?,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.and_then(Iterator::collect)
|
2018-05-26 17:05:12 -04:00
|
|
|
}
|
|
|
|
|
2020-01-30 07:52:10 +01:00
|
|
|
fn get_hottest_months(conn: Connection) -> WeatherAggResult {
|
|
|
|
let stmt = conn.prepare(
|
|
|
|
"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
|
2020-01-30 07:52:10 +01:00
|
|
|
ORDER BY total DESC LIMIT 10",
|
|
|
|
)?;
|
2018-05-26 17:05:12 -04:00
|
|
|
|
2020-01-30 07:52:10 +01:00
|
|
|
get_rows_as_month_agg(stmt)
|
2018-05-26 17:05:12 -04:00
|
|
|
}
|
|
|
|
|
2020-01-30 07:52:10 +01:00
|
|
|
fn get_coldest_months(conn: Connection) -> WeatherAggResult {
|
|
|
|
let stmt = conn.prepare(
|
|
|
|
"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
|
2020-01-30 07:52:10 +01:00
|
|
|
ORDER BY total ASC LIMIT 10",
|
|
|
|
)?;
|
2018-05-26 17:05:12 -04:00
|
|
|
|
2020-01-30 07:52:10 +01:00
|
|
|
get_rows_as_month_agg(stmt)
|
|
|
|
}
|
2018-05-26 17:05:12 -04:00
|
|
|
|
2020-01-30 07:52:10 +01:00
|
|
|
fn get_rows_as_month_agg(mut statement: Statement) -> WeatherAggResult {
|
|
|
|
statement
|
2022-01-29 16:25:46 +00:00
|
|
|
.query_map([], |row| {
|
2020-01-30 07:52:10 +01:00
|
|
|
Ok(WeatherAgg::MonthAgg {
|
|
|
|
year: row.get(0)?,
|
|
|
|
month: row.get(1)?,
|
|
|
|
total: row.get(2)?,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.and_then(Iterator::collect)
|
2018-06-01 06:59:07 -04:00
|
|
|
}
|