diff options
author | FxQnLr <[email protected]> | 2024-04-10 00:16:55 +0200 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-04-10 00:16:55 +0200 |
commit | 3428a637ce420baef9aa9f9803e71bd587867005 (patch) | |
tree | a1ad8234ae9bf3709794324a41e38c2f7fa58d0d /src/routes/status.rs | |
parent | 907e5cb5bc48899b444f7fedd85af7b5974d9a2e (diff) | |
download | webol-3428a637ce420baef9aa9f9803e71bd587867005.tar webol-3428a637ce420baef9aa9f9803e71bd587867005.tar.gz webol-3428a637ce420baef9aa9f9803e71bd587867005.zip |
Closes #24. Changed postgres to json directory storage
Diffstat (limited to 'src/routes/status.rs')
-rw-r--r-- | src/routes/status.rs | 24 |
1 files changed, 4 insertions, 20 deletions
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; | |||
3 | use axum::extract::ws::{Message, WebSocket}; | 3 | use axum::extract::ws::{Message, WebSocket}; |
4 | use axum::extract::{State, WebSocketUpgrade}; | 4 | use axum::extract::{State, WebSocketUpgrade}; |
5 | use axum::response::Response; | 5 | use axum::response::Response; |
6 | use sqlx::PgPool; | ||
7 | use std::sync::Arc; | 6 | use std::sync::Arc; |
8 | use tracing::{debug, trace}; | 7 | use tracing::{debug, trace}; |
9 | 8 | ||
@@ -18,13 +17,13 @@ pub async fn websocket(mut socket: WebSocket, state: Arc<AppState>) { | |||
18 | 17 | ||
19 | trace!("Search for uuid: {}", uuid); | 18 | trace!("Search for uuid: {}", uuid); |
20 | 19 | ||
21 | let eta = get_eta(&state.db).await; | ||
22 | let _ = socket | ||
23 | .send(Message::Text(format!("eta_{eta}_{uuid}"))) | ||
24 | .await; | ||
25 | 20 | ||
26 | let device_exists = state.ping_map.contains_key(&uuid); | 21 | let device_exists = state.ping_map.contains_key(&uuid); |
27 | if device_exists { | 22 | if device_exists { |
23 | let eta = state.ping_map.get(&uuid).unwrap().eta; | ||
24 | let _ = socket | ||
25 | .send(Message::Text(format!("eta_{eta}_{uuid}"))) | ||
26 | .await; | ||
28 | let _ = socket | 27 | let _ = socket |
29 | .send(receive_ping_broadcast(state.clone(), uuid).await) | 28 | .send(receive_ping_broadcast(state.clone(), uuid).await) |
30 | .await; | 29 | .await; |
@@ -62,18 +61,3 @@ async fn receive_ping_broadcast(state: Arc<AppState>, uuid: String) -> Message { | |||
62 | } | 61 | } |
63 | } | 62 | } |
64 | } | 63 | } |
65 | |||
66 | async fn get_eta(db: &PgPool) -> i64 { | ||
67 | let query = sqlx::query!(r#"SELECT times FROM devices;"#) | ||
68 | .fetch_one(db) | ||
69 | .await | ||
70 | .unwrap(); | ||
71 | |||
72 | let times = if let Some(times) = query.times { | ||
73 | times | ||
74 | } else { | ||
75 | vec![0] | ||
76 | }; | ||
77 | |||
78 | times.iter().sum::<i64>() / i64::try_from(times.len()).unwrap() | ||
79 | } | ||