From a197f4721d3b6e79c73f16c8db69ae9f3154acec Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Wed, 8 Nov 2023 12:44:17 +0100 Subject: add eta with average startup time --- src/routes/device.rs | 4 ++-- src/routes/start.rs | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'src/routes') diff --git a/src/routes/device.rs b/src/routes/device.rs index 1eeff0b..678d117 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs @@ -16,7 +16,7 @@ pub async fn get_device(State(state): State>, headers: Head let device = sqlx::query_as!( Device, r#" - SELECT id, mac, broadcast_addr, ip + SELECT id, mac, broadcast_addr, ip, times FROM devices WHERE id = $1; "#, @@ -79,7 +79,7 @@ pub async fn post_device(State(state): State>, headers: Hea r#" UPDATE devices SET mac = $1, broadcast_addr = $2, ip = $3 WHERE id = $4 - RETURNING id, mac, broadcast_addr, ip; + RETURNING id, mac, broadcast_addr, ip, times; "#, payload.mac, payload.broadcast_addr, diff --git a/src/routes/start.rs b/src/routes/start.rs index 271f924..401ae97 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -22,7 +22,7 @@ pub async fn start(State(state): State>, headers: HeaderMap let device = sqlx::query_as!( Device, r#" - SELECT id, mac, broadcast_addr, ip + SELECT id, mac, broadcast_addr, ip, times FROM devices WHERE id = $1; "#, @@ -40,19 +40,20 @@ pub async fn start(State(state): State>, headers: HeaderMap &device.broadcast_addr.parse().map_err(WebolError::IpParse)?, create_buffer(&device.mac)? )?; - + let dev_id = device.id.clone(); let uuid = if payload.ping.is_some_and(|ping| ping) { let uuid_gen = Uuid::new_v4().to_string(); let uuid_genc = uuid_gen.clone(); + // TODO: Check if service already runs tokio::spawn(async move { debug!("init ping service"); state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false }); - crate::services::ping::spawn(state.ping_send.clone(), device.ip, uuid_gen.clone(), &state.ping_map).await + crate::services::ping::spawn(state.ping_send.clone(), device, uuid_gen.clone(), &state.ping_map, &state.db).await }); Some(uuid_genc) } else { None }; - Ok(Json(json!(StartResponse { id: device.id, boot: true, uuid }))) + Ok(Json(json!(StartResponse { id: dev_id, boot: true, uuid }))) } else { Err(WebolError::Generic) } -- cgit v1.2.3 From 075b0bdc47713e303f9954556fa4b4bb472b441a Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Thu, 16 Nov 2023 14:17:03 +0100 Subject: check if already runs and bug fix --- src/routes/start.rs | 15 +++++++++++++-- src/services/ping.rs | 14 ++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) (limited to 'src/routes') diff --git a/src/routes/start.rs b/src/routes/start.rs index 401ae97..1555db3 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -42,9 +42,20 @@ pub async fn start(State(state): State>, headers: HeaderMap )?; let dev_id = device.id.clone(); let uuid = if payload.ping.is_some_and(|ping| ping) { - let uuid_gen = Uuid::new_v4().to_string(); + let mut uuid: Option = None; + for (key, value) in state.ping_map.clone() { + if value.ip == device.ip { + debug!("service already exists"); + uuid = Some(key); + break; + } + }; + let uuid_gen = match uuid { + Some(u) => u, + None => Uuid::new_v4().to_string(), + }; let uuid_genc = uuid_gen.clone(); - // TODO: Check if service already runs + tokio::spawn(async move { debug!("init ping service"); state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false }); diff --git a/src/services/ping.rs b/src/services/ping.rs index 2bff61f..c3bdced 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs @@ -104,15 +104,13 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc) { async fn get_eta(db: &PgPool) -> i64 { let query = sqlx::query!( r#"SELECT times FROM devices;"# - ).fetch_optional(db).await.unwrap(); + ).fetch_one(db).await.unwrap(); - match query { - None => { -1 }, - Some(rec) => { - let times = rec.times.unwrap(); - times.iter().sum::() / times.len() as i64 - } - } + let times = match query.times { + None => { vec![0] }, + Some(t) => t, + }; + times.iter().sum::() / times.len() as i64 } -- cgit v1.2.3