From 3428a637ce420baef9aa9f9803e71bd587867005 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Wed, 10 Apr 2024 00:16:55 +0200 Subject: Closes #24. Changed postgres to json directory storage --- src/routes/status.rs | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) (limited to 'src/routes/status.rs') diff --git a/src/routes/status.rs b/src/routes/status.rs index 0e25f7d..b38202b 100644 --- a/src/routes/status.rs +++ b/src/routes/status.rs @@ -3,7 +3,6 @@ use crate::AppState; use axum::extract::ws::{Message, WebSocket}; use axum::extract::{State, WebSocketUpgrade}; use axum::response::Response; -use sqlx::PgPool; use std::sync::Arc; use tracing::{debug, trace}; @@ -18,13 +17,13 @@ pub async fn websocket(mut socket: WebSocket, state: Arc) { trace!("Search for uuid: {}", uuid); - let eta = get_eta(&state.db).await; - let _ = socket - .send(Message::Text(format!("eta_{eta}_{uuid}"))) - .await; let device_exists = state.ping_map.contains_key(&uuid); if device_exists { + let eta = state.ping_map.get(&uuid).unwrap().eta; + let _ = socket + .send(Message::Text(format!("eta_{eta}_{uuid}"))) + .await; let _ = socket .send(receive_ping_broadcast(state.clone(), uuid).await) .await; @@ -62,18 +61,3 @@ async fn receive_ping_broadcast(state: Arc, uuid: String) -> Message { } } } - -async fn get_eta(db: &PgPool) -> i64 { - let query = sqlx::query!(r#"SELECT times FROM devices;"#) - .fetch_one(db) - .await - .unwrap(); - - let times = if let Some(times) = query.times { - times - } else { - vec![0] - }; - - times.iter().sum::() / i64::try_from(times.len()).unwrap() -} -- cgit v1.2.3