1
0
mirror of https://github.com/actix/examples synced 2025-02-09 04:15:37 +01:00

150 lines
4.3 KiB
Rust
Raw Normal View History

2018-05-26 17:05:12 -04:00
use actix::prelude::*;
use failure::Error;
use r2d2;
use r2d2_sqlite;
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>;
pub struct DbExecutor(pub Pool);
impl Actor for DbExecutor {
type Context = SyncContext<Self>;
}
#[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
}
//pub struct GetTopTenHottestYears;
impl Message for Queries {
type Result = Result<Vec<WeatherAgg>, Error>;
}
impl Handler<Queries> for DbExecutor {
type Result = Result<Vec<WeatherAgg>, Error>;
fn handle(&mut self, msg: Queries, _: &mut Self::Context) -> Self::Result {
let conn: Connection = self.0.get()?;
match msg {
Queries::GetTopTenHottestYears => get_hottest_years(conn),
Queries::GetTopTenColdestYears => get_coldest_years(conn),
2018-07-16 12:36:53 +06:00
Queries::GetTopTenHottestMonths => get_hottest_months(conn),
2018-05-26 17:05:12 -04:00
Queries::GetTopTenColdestMonths => get_coldest_months(conn),
}
}
}
fn get_hottest_years(conn: Connection) -> Result<Vec<WeatherAgg>, Error> {
let stmt = "
SELECT cast(strftime('%Y', date) as int) as theyear,
sum(tmax) as total
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,
sum(tmax) as total
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,
2018-05-26 17:05:12 -04:00
cast(strftime('%m', date) as int) as themonth,
sum(tmax) as total
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,
2018-05-26 17:05:12 -04:00
cast(strftime('%m', date) as int) as themonth,
sum(tmax) as total
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
}