Skip to content

Commit

Permalink
Merge pull request #14 from adnanjpg/db-pool
Browse files Browse the repository at this point in the history
feat: introduce sql pool
  • Loading branch information
isaidsari authored Jan 8, 2024
2 parents 1629b07 + cdaf3d6 commit 5f58e8e
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 107 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ log = "0.4.14"
local-ip-address = "0.5.6"
blake3 = "1.5.0"
maplit = "1.0.2"
lazy_static = "1.4.0"
async_once = "0.2.6"

[dev-dependencies]
ctor = "0.2.6"
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ use local_ip_address::local_ip;

use std::convert::TryInto;

// https://stackoverflow.com/a/39175997/12555423
#[macro_use]
extern crate lazy_static;

// TODO(adnanjpg): get port from env var
const DEFAULT_PORT: u16 = 8080;

Expand Down
5 changes: 2 additions & 3 deletions src/monitor/persistence.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use sqlx::SqliteConnection;

mod monitor_config;
use self::monitor_config::create_monitor_configs_table;
pub use self::monitor_config::{fetch_monitor_configs, insert_or_update_monitor_config};
Expand Down Expand Up @@ -28,9 +26,10 @@ mod status_mem;
use self::status_mem::{create_mem_status_frame_singles_table, create_mem_status_frames_table};
pub use self::status_mem::{get_mem_status_between_dates, insert_mem_status_frame};

use crate::persistence::SQLConnection;
pub use crate::persistence::{get_default_sql_connection, get_sql_connection, FetchId};

Check warning on line 30 in src/monitor/persistence.rs

View workflow job for this annotation

GitHub Actions / Build and test on Ubuntu

unused import: `get_sql_connection`

Check warning on line 30 in src/monitor/persistence.rs

View workflow job for this annotation

GitHub Actions / Build and test on Ubuntu

unused import: `get_sql_connection`

pub async fn init_db(conn: &mut SqliteConnection) -> Result<(), sqlx::Error> {
pub async fn init_db(conn: &SQLConnection) -> Result<(), sqlx::Error> {
create_monitor_configs_table(conn).await?;

create_hardware_cpu_infos_table(conn).await?;
Expand Down
18 changes: 8 additions & 10 deletions src/monitor/persistence/hardware_cpu_info.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use sqlx::SqliteConnection;

use crate::monitor::models::get_hardware_info::HardwareCpuInfo;
use crate::{monitor::models::get_hardware_info::HardwareCpuInfo, persistence::SQLConnection};

use super::{get_default_sql_connection, FetchId};

const HARDWARE_CPU_INFOS_TABLE_NAME: &str = "cpu_infos";

pub(super) async fn insert_hardware_cpu_info(info: &HardwareCpuInfo) -> Result<(), sqlx::Error> {
let mut conn = get_default_sql_connection().await?;
let conn = get_default_sql_connection().await?;

// check if a record with the same cpu_id already exists
let exists_record_check = format!(
Expand All @@ -16,7 +14,7 @@ pub(super) async fn insert_hardware_cpu_info(info: &HardwareCpuInfo) -> Result<(
);
let exists_check_res = sqlx::query_as::<_, FetchId>(&exists_record_check)
.bind(&info.cpu_id)
.fetch_optional(&mut conn)
.fetch_optional(&conn)
.await?;

// if exists, update it
Expand All @@ -32,7 +30,7 @@ pub(super) async fn insert_hardware_cpu_info(info: &HardwareCpuInfo) -> Result<(
sqlx::query(&statement)
.bind(&info.last_check)
.bind(&value.id)
.execute(&mut conn)
.execute(&conn)
.await?;
}
None => {
Expand All @@ -49,7 +47,7 @@ pub(super) async fn insert_hardware_cpu_info(info: &HardwareCpuInfo) -> Result<(
.bind(&info.vendor_id)
.bind(&info.brand)
.bind(&info.last_check)
.execute(&mut conn)
.execute(&conn)
.await?;
}
};
Expand All @@ -58,7 +56,7 @@ pub(super) async fn insert_hardware_cpu_info(info: &HardwareCpuInfo) -> Result<(
}

pub(super) async fn fetch_latest_hardware_cpus_info() -> Result<Vec<HardwareCpuInfo>, sqlx::Error> {
let mut conn = get_default_sql_connection().await?;
let conn = get_default_sql_connection().await?;

let statement = format!(
"
Expand All @@ -68,14 +66,14 @@ pub(super) async fn fetch_latest_hardware_cpus_info() -> Result<Vec<HardwareCpuI
HARDWARE_CPU_INFOS_TABLE_NAME
);
let info = sqlx::query_as::<_, HardwareCpuInfo>(&statement)
.fetch_all(&mut conn)
.fetch_all(&conn)
.await?;

Ok(info)
}

pub(super) async fn create_hardware_cpu_infos_table(
conn: &mut SqliteConnection,
conn: &SQLConnection,
) -> Result<(), sqlx::Error> {
let statement = format!(
"CREATE TABLE IF NOT EXISTS {} (
Expand Down
18 changes: 8 additions & 10 deletions src/monitor/persistence/hardware_disk_info.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use sqlx::SqliteConnection;

use crate::monitor::models::get_hardware_info::HardwareDiskInfo;
use crate::{monitor::models::get_hardware_info::HardwareDiskInfo, persistence::SQLConnection};

use super::{get_default_sql_connection, FetchId};

const HARDWARE_DISK_INFOS_TABLE_NAME: &str = "disk_infos";

pub(super) async fn insert_hardware_disk_info(info: &HardwareDiskInfo) -> Result<(), sqlx::Error> {
let mut conn = get_default_sql_connection().await?;
let conn = get_default_sql_connection().await?;

// check if a record with the same cpu_id already exists
let exists_record_check = format!(
Expand All @@ -17,7 +15,7 @@ pub(super) async fn insert_hardware_disk_info(info: &HardwareDiskInfo) -> Result

let exists_check_res = sqlx::query_as::<_, FetchId>(&exists_record_check)
.bind(&info.disk_id)
.fetch_optional(&mut conn)
.fetch_optional(&conn)
.await?;

// if exists, update it
Expand All @@ -33,7 +31,7 @@ pub(super) async fn insert_hardware_disk_info(info: &HardwareDiskInfo) -> Result
sqlx::query(&statement)
.bind(&info.last_check)
.bind(&value.id)
.execute(&mut conn)
.execute(&conn)
.await?;
}
None => {
Expand All @@ -50,7 +48,7 @@ pub(super) async fn insert_hardware_disk_info(info: &HardwareDiskInfo) -> Result
.bind(&info.mount_point)
.bind(&info.total_space)
.bind(&info.last_check)
.execute(&mut conn)
.execute(&conn)
.await?;
}
};
Expand All @@ -60,7 +58,7 @@ pub(super) async fn insert_hardware_disk_info(info: &HardwareDiskInfo) -> Result

pub(super) async fn fetch_latest_hardware_disks_info() -> Result<Vec<HardwareDiskInfo>, sqlx::Error>
{
let mut conn = get_default_sql_connection().await?;
let conn = get_default_sql_connection().await?;

// get all with distinct disk_id
let statement = format!(
Expand All @@ -72,14 +70,14 @@ pub(super) async fn fetch_latest_hardware_disks_info() -> Result<Vec<HardwareDis
);

let info = sqlx::query_as::<_, HardwareDiskInfo>(&statement)
.fetch_all(&mut conn)
.fetch_all(&conn)
.await?;

Ok(info)
}

pub(super) async fn create_hardware_disk_infos_table(
conn: &mut SqliteConnection,
conn: &SQLConnection,
) -> Result<(), sqlx::Error> {
let statement = format!(
"CREATE TABLE IF NOT EXISTS {} (
Expand Down
18 changes: 8 additions & 10 deletions src/monitor/persistence/hardware_mem_info.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use sqlx::SqliteConnection;

use crate::monitor::models::get_hardware_info::HardwareMemInfo;
use crate::{monitor::models::get_hardware_info::HardwareMemInfo, persistence::SQLConnection};

use super::{get_default_sql_connection, FetchId};

const HARDWARE_MEM_INFOS_TABLE_NAME: &str = "mem_infos";

pub(super) async fn insert_hardware_mem_info(info: &HardwareMemInfo) -> Result<(), sqlx::Error> {
let mut conn = get_default_sql_connection().await?;
let conn = get_default_sql_connection().await?;

// check if a record with the same cpu_id already exists
let exists_record_check = format!(
Expand All @@ -17,7 +15,7 @@ pub(super) async fn insert_hardware_mem_info(info: &HardwareMemInfo) -> Result<(

let exists_check_res = sqlx::query_as::<_, FetchId>(&exists_record_check)
.bind(&info.mem_id)
.fetch_optional(&mut conn)
.fetch_optional(&conn)
.await?;

// if exists, update it
Expand All @@ -33,7 +31,7 @@ pub(super) async fn insert_hardware_mem_info(info: &HardwareMemInfo) -> Result<(
sqlx::query(&statement)
.bind(&info.last_check)
.bind(&value.id)
.execute(&mut conn)
.execute(&conn)
.await?;
}
None => {
Expand All @@ -45,7 +43,7 @@ pub(super) async fn insert_hardware_mem_info(info: &HardwareMemInfo) -> Result<(
.bind(&info.mem_id)
.bind(&info.total_space)
.bind(&info.last_check)
.execute(&mut conn)
.execute(&conn)
.await?;
}
};
Expand All @@ -54,7 +52,7 @@ pub(super) async fn insert_hardware_mem_info(info: &HardwareMemInfo) -> Result<(
}

pub(super) async fn fetch_latest_hardware_mems_info() -> Result<Vec<HardwareMemInfo>, sqlx::Error> {
let mut conn = get_default_sql_connection().await?;
let conn = get_default_sql_connection().await?;

// get all with distinct mem_id
let statement = format!(
Expand All @@ -66,14 +64,14 @@ pub(super) async fn fetch_latest_hardware_mems_info() -> Result<Vec<HardwareMemI
);

let info = sqlx::query_as::<_, HardwareMemInfo>(&statement)
.fetch_all(&mut conn)
.fetch_all(&conn)
.await?;

Ok(info)
}

pub(super) async fn create_hardware_mem_infos_table(
conn: &mut SqliteConnection,
conn: &SQLConnection,
) -> Result<(), sqlx::Error> {
let statement = format!(
"CREATE TABLE IF NOT EXISTS {} (
Expand Down
20 changes: 8 additions & 12 deletions src/monitor/persistence/monitor_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use sqlx::SqliteConnection;

use crate::monitor::models::MonitorConfig;
use crate::{monitor::models::MonitorConfig, persistence::SQLConnection};

use super::{get_default_sql_connection, FetchId};

Expand All @@ -10,7 +8,7 @@ pub async fn insert_or_update_monitor_config(
config: &MonitorConfig,
device_id: &str,
) -> Result<(), sqlx::Error> {
let mut conn = get_default_sql_connection().await?;
let conn = get_default_sql_connection().await?;

// check if a record with the same device_id already exists
let exists_record_check = format!(
Expand All @@ -19,7 +17,7 @@ pub async fn insert_or_update_monitor_config(
);
let exists_check_res = sqlx::query_as::<_, FetchId>(&exists_record_check)
.bind(&device_id)
.fetch_optional(&mut conn)
.fetch_optional(&conn)
.await?;

match exists_check_res {
Expand All @@ -43,7 +41,7 @@ pub async fn insert_or_update_monitor_config(
.bind(&config.fcm_token)
.bind(&config.updated_at)
.bind(value.id)
.execute(&mut conn)
.execute(&conn)
.await?;
}
None => {
Expand All @@ -61,7 +59,7 @@ pub async fn insert_or_update_monitor_config(
.bind(&config.disk_threshold)
.bind(&config.fcm_token)
.bind(&config.updated_at)
.execute(&mut conn)
.execute(&conn)
.await?;
}
};
Expand All @@ -70,19 +68,17 @@ pub async fn insert_or_update_monitor_config(
}

pub async fn fetch_monitor_configs() -> Result<Vec<MonitorConfig>, sqlx::Error> {
let mut conn = get_default_sql_connection().await?;
let conn = get_default_sql_connection().await?;

let statement = format!("SELECT * FROM {}", MONITOR_CONFIGS_TABLE_NAME);
let configs = sqlx::query_as::<_, MonitorConfig>(&statement)
.fetch_all(&mut conn)
.fetch_all(&conn)
.await?;

Ok(configs)
}

pub(super) async fn create_monitor_configs_table(
conn: &mut SqliteConnection,
) -> Result<(), sqlx::Error> {
pub(super) async fn create_monitor_configs_table(conn: &SQLConnection) -> Result<(), sqlx::Error> {
let statement = format!(
"CREATE TABLE IF NOT EXISTS {} (
id INTEGER PRIMARY KEY NOT NULL,
Expand Down
Loading

0 comments on commit 5f58e8e

Please sign in to comment.