Skip to content

Commit

Permalink
Merge pull request #27 from pilksoc/logging
Browse files Browse the repository at this point in the history
metrics for connected clients
  • Loading branch information
djpiper28 authored Mar 3, 2024
2 parents b76dce4 + 215c6f9 commit 5f35801
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
12 changes: 9 additions & 3 deletions backend/src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use crate::{ws, Clients, Result};
use warp::Reply;

pub async fn ws_handler(ws: warp::ws::Ws, clients: Clients) -> Result<impl Reply>
{
pub async fn ws_handler(ws: warp::ws::Ws, clients: Clients) -> Result<impl Reply> {
println!("ws_handler"); //debug

Ok(ws.on_upgrade(move |socket| ws::client_connection(socket, clients)))
}
}

pub async fn metrics_handler(clients: Clients) -> Result<impl Reply> {
let metricsString = "cosmic_kube_clients{type=\"connected\"} ".to_string()
+ &clients.lock().await.len().to_string()
+ "\n";
Ok(metricsString)
}
17 changes: 9 additions & 8 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@ type Result<T> = std::result::Result<T, Rejection>;

#[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
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"); //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<Extract = (Clients,), Error = Infallible> + Clone {
warp::any().map(move || clients.clone())
}

5 changes: 5 additions & 0 deletions kube_cache/aiStuff/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions kube_cache/aiStuff/dalle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 5f35801

Please sign in to comment.