From 1ba45868b59726b25a46b0a3c724842261ca2521 Mon Sep 17 00:00:00 2001 From: Danny Piper Date: Sun, 3 Mar 2024 08:14:50 +0000 Subject: [PATCH 1/3] feat: updated logging for server start --- backend/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/src/main.rs b/backend/src/main.rs index f983b50..847ce17 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -22,15 +22,15 @@ async fn main() { //initialise a hashmap to store currently connected clients. We may want some more logic here if we want currently connected clients to be stored somewhere let clients: Clients = Arc::new(Mutex::new(HashMap::new())); - println!("configuring websocket route"); //debug + println!("Configuring websocket route"); //debug let ws_route = warp::path("ws") .and(warp::ws()) .and(with_clients(clients.clone())) .and_then(handlers::ws_handler); let routes = ws_route.with(warp::cors().allow_any_origin()); - println!("starting server"); //debug - warp::serve(routes).run(([0, 0, 0, 0], 8000)).await; //PORT 8000 localhost + println!("Starting server on http://0.0.0.0:8000"); //debug + warp::serve(routes).run(([0, 0, 0, 0], 8000)).await; } fn with_clients(clients: Clients) -> impl Filter + Clone { From b0d1fcc63edd44efb8fc9427d3c7617e8791410b Mon Sep 17 00:00:00 2001 From: Danny Piper Date: Sun, 3 Mar 2024 08:22:31 +0000 Subject: [PATCH 2/3] feat: added metrics exporter --- backend/src/handlers.rs | 12 +++++++++--- backend/src/main.rs | 13 +++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/backend/src/handlers.rs b/backend/src/handlers.rs index c2a2eb3..c8a2e64 100644 --- a/backend/src/handlers.rs +++ b/backend/src/handlers.rs @@ -1,9 +1,15 @@ use crate::{ws, Clients, Result}; use warp::Reply; -pub async fn ws_handler(ws: warp::ws::Ws, clients: Clients) -> Result -{ +pub async fn ws_handler(ws: warp::ws::Ws, clients: Clients) -> Result { println!("ws_handler"); //debug Ok(ws.on_upgrade(move |socket| ws::client_connection(socket, clients))) -} \ No newline at end of file +} + +pub async fn metrics_handler(clients: Clients) -> Result { + let metricsString = "cosmic_kube_clients{type=\"connected\"} ".to_string() + + &clients.lock().await.len().to_string() + + "\n"; + Ok(metricsString) +} diff --git a/backend/src/main.rs b/backend/src/main.rs index 847ce17..5033bfa 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -18,22 +18,23 @@ type Result = std::result::Result; #[tokio::main] async fn main() { - //initialise a hashmap to store currently connected clients. We may want some more logic here if we want currently connected clients to be stored somewhere let clients: Clients = Arc::new(Mutex::new(HashMap::new())); println!("Configuring websocket route"); //debug let ws_route = warp::path("ws") - .and(warp::ws()) - .and(with_clients(clients.clone())) - .and_then(handlers::ws_handler); + .and(warp::ws()) + .and(with_clients(clients.clone())) + .and_then(handlers::ws_handler) + .or(warp::path("metrics") + .and(with_clients(clients.clone())) + .and_then(handlers::metrics_handler)); let routes = ws_route.with(warp::cors().allow_any_origin()); println!("Starting server on http://0.0.0.0:8000"); //debug - warp::serve(routes).run(([0, 0, 0, 0], 8000)).await; + warp::serve(routes).run(([0, 0, 0, 0], 8000)).await; } fn with_clients(clients: Clients) -> impl Filter + Clone { warp::any().map(move || clients.clone()) } - From 215c6f9a4a7e5e1699d1518343363bb32db5f051 Mon Sep 17 00:00:00 2001 From: Danny Piper Date: Sun, 3 Mar 2024 08:26:58 +0000 Subject: [PATCH 3/3] feat: added more debugging --- kube_cache/aiStuff/ai.go | 5 +++++ kube_cache/aiStuff/dalle.go | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/kube_cache/aiStuff/ai.go b/kube_cache/aiStuff/ai.go index 33fd6be..a3777ff 100644 --- a/kube_cache/aiStuff/ai.go +++ b/kube_cache/aiStuff/ai.go @@ -135,6 +135,11 @@ func (ai *KubeAi) generateKubeRecipe(kubeName1, kubeName2 string) (string, error return string(body), nil } + if len(aiResponse.Choices) == 0 { + log.Printf("The silly server sent %s, this is very bad", body) + return string(body), nil + } + actualLegitMessage := aiResponse.Choices[0].Message.Content var aiResp2 aiResp diff --git a/kube_cache/aiStuff/dalle.go b/kube_cache/aiStuff/dalle.go index 9756ce0..5c0a4c3 100644 --- a/kube_cache/aiStuff/dalle.go +++ b/kube_cache/aiStuff/dalle.go @@ -81,8 +81,8 @@ func (ai *KubeAi) generateDalleForKube(kubeName string) ([]byte, error) { } if len(dalleResp.Data) == 0 { - log.Println("AI is a bitch") - return nil, errors.New("AI is a bitch") + log.Printf("The silly server sent %s, this is very bad", body) + return nil, errors.New("No data in response") } log.Println("Downloading dalle response")