80 lines
2.2 KiB
Rust
80 lines
2.2 KiB
Rust
use std::time::Duration;
|
|
|
|
use sqlx::postgres::{PgPool, PgPoolOptions};
|
|
use url::Url;
|
|
|
|
#[derive(Clone)]
|
|
pub struct DB {
|
|
pool: PgPool,
|
|
}
|
|
|
|
impl DB {
|
|
pub async fn new(url: &Url, conn: u32) -> Self {
|
|
let pool = PgPoolOptions::new()
|
|
.max_connections(conn)
|
|
.acquire_timeout(Duration::new(1000, 0))
|
|
.idle_timeout(Duration::new(1000, 0))
|
|
.connect(&url.to_string())
|
|
.await
|
|
.unwrap();
|
|
|
|
let _ = sqlx::migrate!("./migrations/").run(&pool).await;
|
|
Self { pool }
|
|
}
|
|
|
|
pub async fn write_log(&self, log: &crate::Log, difficulty: u32) {
|
|
let time = log.time as i32;
|
|
sqlx::query!(
|
|
"INSERT INTO logs
|
|
(string, salt, time, difficulty)
|
|
VALUES
|
|
($1, $2, $3, $4)
|
|
ON CONFLICT(difficulty) DO NOTHING;",
|
|
log.string,
|
|
log.salt,
|
|
time,
|
|
difficulty as i32,
|
|
)
|
|
.execute(&self.pool)
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
pub async fn write_avg_individual_log(&self, log: &crate::Log, difficulty: u32) {
|
|
let time = log.time as i32;
|
|
sqlx::query!(
|
|
"INSERT INTO logs_avg_individual
|
|
(string, salt, time, difficulty) VALUES ($1, $2, $3, $4);",
|
|
log.string,
|
|
log.salt,
|
|
time,
|
|
difficulty as i32,
|
|
)
|
|
.execute(&self.pool)
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
pub async fn write_avg(&self, avg: &crate::avg::Avg) {
|
|
sqlx::query!(
|
|
"INSERT INTO logs_avg
|
|
(
|
|
minimum_time,
|
|
maximum_time,
|
|
variation_time,
|
|
difficulty,
|
|
mean_time
|
|
) VALUES ($1, $2, $3, $4, $5)
|
|
ON CONFLICT(difficulty) DO NOTHING;",
|
|
avg.minimum_time as i32,
|
|
avg.maximum_time as i32,
|
|
avg.variation_time as i32,
|
|
avg.difficulty as i32,
|
|
avg.mean_time as i32,
|
|
)
|
|
.execute(&self.pool)
|
|
.await
|
|
.unwrap();
|
|
}
|
|
}
|