diff options
author | FxQnLr <[email protected]> | 2023-11-10 12:23:31 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2023-11-10 12:23:31 +0100 |
commit | 9957da9b182ca209ff8caa8f670d3bd7eff154bb (patch) | |
tree | ef5bd866387661cdbfe885e3c36a7f0f386404e2 /src/requests | |
parent | 344af3ff7c9493b4e2c6eee134b9b341eaabf736 (diff) | |
download | webol-cli-9957da9b182ca209ff8caa8f670d3bd7eff154bb.tar webol-cli-9957da9b182ca209ff8caa8f670d3bd7eff154bb.tar.gz webol-cli-9957da9b182ca209ff8caa8f670d3bd7eff154bb.zip |
add basic eta
Diffstat (limited to 'src/requests')
-rw-r--r-- | src/requests/start.rs | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/src/requests/start.rs b/src/requests/start.rs index 882b154..ca4ca44 100644 --- a/src/requests/start.rs +++ b/src/requests/start.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | use futures_util::{StreamExt, SinkExt}; | 1 | use futures_util::{StreamExt, SinkExt}; |
2 | use indicatif::MultiProgress; | 2 | use indicatif::{MultiProgress, ProgressBar}; |
3 | use reqwest::StatusCode; | 3 | use reqwest::StatusCode; |
4 | use serde::Deserialize; | 4 | use serde::Deserialize; |
5 | use tokio_tungstenite::{connect_async, tungstenite::Message}; | 5 | use tokio_tungstenite::{connect_async, tungstenite::Message}; |
@@ -9,7 +9,7 @@ use crate::{error::CliError, default_headers, ErrorResponse, format_url, Protoco | |||
9 | pub async fn start(id: String, ping: bool) -> Result<(), CliError> { | 9 | pub async fn start(id: String, ping: bool) -> Result<(), CliError> { |
10 | 10 | ||
11 | let send_start = MultiProgress::new(); | 11 | let send_start = MultiProgress::new(); |
12 | let overview = add_pb(&send_start, OVERVIEW_STYLE, format!("start {}", id)); | 12 | let overview = add_pb(&send_start, OVERVIEW_STYLE, format!(") start {}", id)); |
13 | 13 | ||
14 | // TODO: calculate average start-time on server | 14 | // TODO: calculate average start-time on server |
15 | let url = format_url("start", Protocols::Http)?; | 15 | let url = format_url("start", Protocols::Http)?; |
@@ -38,7 +38,7 @@ pub async fn start(id: String, ping: bool) -> Result<(), CliError> { | |||
38 | } | 38 | } |
39 | 39 | ||
40 | if ping { | 40 | if ping { |
41 | let status = status_socket(body.uuid, &send_start).await?; | 41 | let status = status_socket(body.uuid, &send_start, &overview, id).await?; |
42 | if status { | 42 | if status { |
43 | finish_pb(overview, format!("successfully started {}", body.id), OVERVIEW_DONE); | 43 | finish_pb(overview, format!("successfully started {}", body.id), OVERVIEW_DONE); |
44 | } else { | 44 | } else { |
@@ -59,7 +59,7 @@ pub async fn start(id: String, ping: bool) -> Result<(), CliError> { | |||
59 | Ok(()) | 59 | Ok(()) |
60 | } | 60 | } |
61 | 61 | ||
62 | async fn status_socket(uuid: String, pb: &MultiProgress) -> Result<bool, CliError> { | 62 | async fn status_socket(uuid: String, pb: &MultiProgress, overview: &ProgressBar, id: String) -> Result<bool, CliError> { |
63 | // TODO: Remove unwraps | 63 | // TODO: Remove unwraps |
64 | let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); | 64 | let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); |
65 | let (mut ws_stream, _response) = connect_async(format_url("status", Protocols::Websocket)?) | 65 | let (mut ws_stream, _response) = connect_async(format_url("status", Protocols::Websocket)?) |
@@ -68,6 +68,11 @@ async fn status_socket(uuid: String, pb: &MultiProgress) -> Result<bool, CliErro | |||
68 | finish_pb(ws_pb, "connected to websocket".to_string(), DONE_STYLE); | 68 | finish_pb(ws_pb, "connected to websocket".to_string(), DONE_STYLE); |
69 | 69 | ||
70 | ws_stream.send(Message::Text(uuid.clone())).await.unwrap(); | 70 | ws_stream.send(Message::Text(uuid.clone())).await.unwrap(); |
71 | |||
72 | // Get ETA | ||
73 | let eta_msg = ws_stream.next().await.unwrap().unwrap(); | ||
74 | let eta = get_eta(eta_msg.into_text().unwrap(), uuid.clone())? + overview.elapsed().as_secs(); | ||
75 | overview.set_message(format!("/{}) start {}", eta, id)); | ||
71 | 76 | ||
72 | let msg_pb = add_pb(pb, DEFAULT_STYLE, "await message".to_string()); | 77 | let msg_pb = add_pb(pb, DEFAULT_STYLE, "await message".to_string()); |
73 | let msg = ws_stream.next().await.unwrap(); | 78 | let msg = ws_stream.next().await.unwrap(); |
@@ -101,6 +106,12 @@ async fn status_socket(uuid: String, pb: &MultiProgress) -> Result<bool, CliErro | |||
101 | } | 106 | } |
102 | } | 107 | } |
103 | 108 | ||
109 | fn get_eta(msg: String, uuid: String) -> Result<u64, CliError> { | ||
110 | let spl: Vec<&str> = msg.split('_').collect(); | ||
111 | if (spl[0] != "eta") || (spl[2] != uuid) { return Err(CliError::WsResponse); }; | ||
112 | Ok(u64::from_str_radix(spl[1], 10).map_err(CliError::Parse)?) | ||
113 | } | ||
114 | |||
104 | fn verify_response(res: String, org_uuid: String) -> Result<Verified, CliError> { | 115 | fn verify_response(res: String, org_uuid: String) -> Result<Verified, CliError> { |
105 | let spl: Vec<&str> = res.split('_').collect(); | 116 | let spl: Vec<&str> = res.split('_').collect(); |
106 | let res_type = spl[0]; | 117 | let res_type = spl[0]; |