Add relay view to faster load controller_uid
This commit is contained in:
parent
277b159200
commit
d4ff664f74
16 changed files with 42 additions and 31 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1
migrations/20240611000000_add_relays_view.down.sql
Normal file
1
migrations/20240611000000_add_relays_view.down.sql
Normal file
|
@ -0,0 +1 @@
|
||||||
|
DROP VIEW v_relays;
|
8
migrations/20240611000000_add_relays_view.up.sql
Normal file
8
migrations/20240611000000_add_relays_view.up.sql
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
CREATE VIEW v_relays
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
relays.*,
|
||||||
|
controllers.uid AS controller_uid
|
||||||
|
FROM
|
||||||
|
relays
|
||||||
|
INNER JOIN controllers ON controllers.id = relays.controller_id;
|
|
@ -158,7 +158,7 @@ impl DbController {
|
||||||
) -> Result<Vec<DbRelay>, DatabaseError> {
|
) -> Result<Vec<DbRelay>, DatabaseError> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
DbRelay,
|
DbRelay,
|
||||||
"SELECT * FROM relays WHERE controller_id = ?",
|
"SELECT * FROM v_relays WHERE v_relays.controller_id = ?",
|
||||||
self.id
|
self.id
|
||||||
)
|
)
|
||||||
.fetch_all(conn.deref_mut())
|
.fetch_all(conn.deref_mut())
|
||||||
|
|
|
@ -50,8 +50,8 @@ impl DbJunctionRelaySchedule {
|
||||||
) -> Result<Vec<DbRelay>, DatabaseError> {
|
) -> Result<Vec<DbRelay>, DatabaseError> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
DbRelay,
|
DbRelay,
|
||||||
r#"SELECT relays.* FROM relays INNER JOIN junction_relay_schedule
|
r#"SELECT v_relays.* FROM v_relays INNER JOIN junction_relay_schedule
|
||||||
ON junction_relay_schedule.relay_id = relays.id
|
ON junction_relay_schedule.relay_id = v_relays.id
|
||||||
WHERE junction_relay_schedule.schedule_id = ?
|
WHERE junction_relay_schedule.schedule_id = ?
|
||||||
ORDER BY junction_relay_schedule.weekday"#,
|
ORDER BY junction_relay_schedule.weekday"#,
|
||||||
schedule.id
|
schedule.id
|
||||||
|
|
|
@ -6,6 +6,7 @@ use sqlx::Sqlite;
|
||||||
|
|
||||||
use crate::db::{DbController, DbJunctionTag, DbTag};
|
use crate::db::{DbController, DbJunctionTag, DbTag};
|
||||||
use crate::errors::DatabaseError;
|
use crate::errors::DatabaseError;
|
||||||
|
use crate::types::EmgauwaUid;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct DbRelay {
|
pub struct DbRelay {
|
||||||
|
@ -13,13 +14,15 @@ pub struct DbRelay {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub number: i64,
|
pub number: i64,
|
||||||
|
#[serde(rename = "controller_id")]
|
||||||
|
pub controller_uid: EmgauwaUid,
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
pub controller_id: i64,
|
pub controller_id: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DbRelay {
|
impl DbRelay {
|
||||||
pub async fn get_all(conn: &mut PoolConnection<Sqlite>) -> Result<Vec<DbRelay>, DatabaseError> {
|
pub async fn get_all(conn: &mut PoolConnection<Sqlite>) -> Result<Vec<DbRelay>, DatabaseError> {
|
||||||
sqlx::query_as!(DbRelay, "SELECT * FROM relays")
|
sqlx::query_as!(DbRelay, "SELECT * FROM v_relays")
|
||||||
.fetch_all(conn.deref_mut())
|
.fetch_all(conn.deref_mut())
|
||||||
.await
|
.await
|
||||||
.map_err(DatabaseError::from)
|
.map_err(DatabaseError::from)
|
||||||
|
@ -29,7 +32,7 @@ impl DbRelay {
|
||||||
conn: &mut PoolConnection<Sqlite>,
|
conn: &mut PoolConnection<Sqlite>,
|
||||||
id: i64,
|
id: i64,
|
||||||
) -> Result<Option<DbRelay>, DatabaseError> {
|
) -> Result<Option<DbRelay>, DatabaseError> {
|
||||||
sqlx::query_as!(DbRelay, "SELECT * FROM relays WHERE id = ?", id)
|
sqlx::query_as!(DbRelay, "SELECT * FROM v_relays WHERE v_relays.id = ?", id)
|
||||||
.fetch_optional(conn.deref_mut())
|
.fetch_optional(conn.deref_mut())
|
||||||
.await
|
.await
|
||||||
.map_err(DatabaseError::from)
|
.map_err(DatabaseError::from)
|
||||||
|
@ -42,7 +45,7 @@ impl DbRelay {
|
||||||
) -> Result<Option<DbRelay>, DatabaseError> {
|
) -> Result<Option<DbRelay>, DatabaseError> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
DbRelay,
|
DbRelay,
|
||||||
"SELECT * FROM relays WHERE controller_id = ? AND number = ?",
|
"SELECT * FROM v_relays WHERE v_relays.controller_id = ? AND v_relays.number = ?",
|
||||||
controller.id,
|
controller.id,
|
||||||
number
|
number
|
||||||
)
|
)
|
||||||
|
@ -70,7 +73,7 @@ impl DbRelay {
|
||||||
conn: &mut PoolConnection<Sqlite>,
|
conn: &mut PoolConnection<Sqlite>,
|
||||||
tag: &DbTag,
|
tag: &DbTag,
|
||||||
) -> Result<Vec<DbRelay>, DatabaseError> {
|
) -> Result<Vec<DbRelay>, DatabaseError> {
|
||||||
sqlx::query_as!(DbRelay, "SELECT relay.* FROM relays AS relay INNER JOIN junction_tag ON junction_tag.relay_id = relay.id WHERE junction_tag.tag_id = ?", tag.id)
|
sqlx::query_as!(DbRelay, "SELECT v_relays.* FROM v_relays INNER JOIN junction_tag ON junction_tag.relay_id = v_relays.id WHERE junction_tag.tag_id = ?", tag.id)
|
||||||
.fetch_all(conn.deref_mut())
|
.fetch_all(conn.deref_mut())
|
||||||
.await
|
.await
|
||||||
.map_err(DatabaseError::from)
|
.map_err(DatabaseError::from)
|
||||||
|
@ -82,16 +85,25 @@ impl DbRelay {
|
||||||
new_number: i64,
|
new_number: i64,
|
||||||
new_controller: &DbController,
|
new_controller: &DbController,
|
||||||
) -> Result<DbRelay, DatabaseError> {
|
) -> Result<DbRelay, DatabaseError> {
|
||||||
sqlx::query_as!(
|
let result = sqlx::query!(
|
||||||
DbRelay,
|
"INSERT INTO relays (name, number, controller_id) VALUES (?, ?, ?)",
|
||||||
"INSERT INTO relays (name, number, controller_id) VALUES (?, ?, ?) RETURNING *",
|
|
||||||
new_name,
|
new_name,
|
||||||
new_number,
|
new_number,
|
||||||
new_controller.id,
|
new_controller.id,
|
||||||
)
|
)
|
||||||
.fetch_optional(conn.deref_mut())
|
.execute(conn.deref_mut())
|
||||||
.await?
|
.await?;
|
||||||
.ok_or(DatabaseError::InsertGetError)
|
|
||||||
|
let last_insert_id = result.last_insert_rowid();
|
||||||
|
|
||||||
|
sqlx::query_as!(
|
||||||
|
DbRelay,
|
||||||
|
"SELECT * FROM v_relays WHERE id = ?",
|
||||||
|
last_insert_id
|
||||||
|
)
|
||||||
|
.fetch_one(conn.deref_mut())
|
||||||
|
.await
|
||||||
|
.map_err(DatabaseError::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(&self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> {
|
pub async fn delete(&self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> {
|
||||||
|
|
|
@ -9,7 +9,7 @@ use sqlx::Sqlite;
|
||||||
|
|
||||||
use crate::db::DbController;
|
use crate::db::DbController;
|
||||||
use crate::errors::{DatabaseError, EmgauwaError};
|
use crate::errors::{DatabaseError, EmgauwaError};
|
||||||
use crate::models::{convert_db_list_cache, FromDbModel, Relay};
|
use crate::models::{convert_db_list, FromDbModel, Relay};
|
||||||
use crate::types::{EmgauwaNow, RelayState, RelayStates};
|
use crate::types::{EmgauwaNow, RelayState, RelayStates};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, MessageResponse)]
|
#[derive(Serialize, Deserialize, Debug, Clone, MessageResponse)]
|
||||||
|
@ -28,7 +28,7 @@ impl FromDbModel for Controller {
|
||||||
db_model: Self::DbModel,
|
db_model: Self::DbModel,
|
||||||
) -> Result<Self, DatabaseError> {
|
) -> Result<Self, DatabaseError> {
|
||||||
let relays_db = block_on(db_model.get_relays(conn))?;
|
let relays_db = block_on(db_model.get_relays(conn))?;
|
||||||
let cache = convert_db_list_cache(conn, relays_db, db_model.clone())?;
|
let cache = convert_db_list(conn, relays_db)?;
|
||||||
Self::from_db_model_cache(conn, db_model, cache)
|
Self::from_db_model_cache(conn, db_model, cache)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,18 +6,16 @@ use serde_derive::{Deserialize, Serialize};
|
||||||
use sqlx::pool::PoolConnection;
|
use sqlx::pool::PoolConnection;
|
||||||
use sqlx::Sqlite;
|
use sqlx::Sqlite;
|
||||||
|
|
||||||
use crate::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
|
use crate::db::{DbJunctionRelaySchedule, DbRelay, DbSchedule};
|
||||||
use crate::errors::DatabaseError;
|
use crate::errors::DatabaseError;
|
||||||
use crate::models::FromDbModel;
|
use crate::models::FromDbModel;
|
||||||
use crate::types::{EmgauwaUid, RelayState};
|
use crate::types::RelayState;
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct Relay {
|
pub struct Relay {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub r: DbRelay,
|
pub r: DbRelay,
|
||||||
pub controller: DbController,
|
|
||||||
pub controller_id: EmgauwaUid,
|
|
||||||
pub schedules: Vec<DbSchedule>,
|
pub schedules: Vec<DbSchedule>,
|
||||||
pub active_schedule: Option<DbSchedule>,
|
pub active_schedule: Option<DbSchedule>,
|
||||||
pub override_schedule: Option<DbSchedule>,
|
pub override_schedule: Option<DbSchedule>,
|
||||||
|
@ -27,32 +25,27 @@ pub struct Relay {
|
||||||
// for internal use only.
|
// for internal use only.
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
pub pulsing: Option<Instant>,
|
pub pulsing: Option<Instant>,
|
||||||
#[serde(
|
#[serde(skip, default = "utils::default_weekday")]
|
||||||
skip,
|
|
||||||
default = "utils::default_weekday",
|
|
||||||
)]
|
|
||||||
pub override_schedule_weekday: Weekday,
|
pub override_schedule_weekday: Weekday,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromDbModel for Relay {
|
impl FromDbModel for Relay {
|
||||||
type DbModel = DbRelay;
|
type DbModel = DbRelay;
|
||||||
type DbModelCache = DbController;
|
type DbModelCache = ();
|
||||||
|
|
||||||
fn from_db_model(
|
fn from_db_model(
|
||||||
conn: &mut PoolConnection<Sqlite>,
|
conn: &mut PoolConnection<Sqlite>,
|
||||||
db_model: Self::DbModel,
|
db_model: Self::DbModel,
|
||||||
) -> Result<Self, DatabaseError> {
|
) -> Result<Self, DatabaseError> {
|
||||||
let cache = block_on(db_model.get_controller(conn))?;
|
Self::from_db_model_cache(conn, db_model, ())
|
||||||
Self::from_db_model_cache(conn, db_model, cache)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_db_model_cache(
|
fn from_db_model_cache(
|
||||||
conn: &mut PoolConnection<Sqlite>,
|
conn: &mut PoolConnection<Sqlite>,
|
||||||
db_model: Self::DbModel,
|
db_model: Self::DbModel,
|
||||||
cache: Self::DbModelCache,
|
_cache: Self::DbModelCache,
|
||||||
) -> Result<Self, DatabaseError> {
|
) -> Result<Self, DatabaseError> {
|
||||||
let tags = block_on(db_model.get_tags(conn))?;
|
let tags = block_on(db_model.get_tags(conn))?;
|
||||||
let controller_id = cache.uid.clone();
|
|
||||||
|
|
||||||
let schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &db_model))?;
|
let schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &db_model))?;
|
||||||
|
|
||||||
|
@ -60,8 +53,6 @@ impl FromDbModel for Relay {
|
||||||
|
|
||||||
Ok(Relay {
|
Ok(Relay {
|
||||||
r: db_model,
|
r: db_model,
|
||||||
controller: cache,
|
|
||||||
controller_id,
|
|
||||||
schedules,
|
schedules,
|
||||||
active_schedule: None,
|
active_schedule: None,
|
||||||
override_schedule: None,
|
override_schedule: None,
|
||||||
|
@ -136,5 +127,4 @@ impl Relay {
|
||||||
self.apply_state(&stated_relay.into());
|
self.apply_state(&stated_relay.into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue