mirror of
https://github.com/infinilabs/coco-app.git
synced 2025-12-28 16:06:28 +01:00
refactor: refactoring health api (#187)
* refactor: refactoring health api * update release notes
This commit is contained in:
@@ -96,13 +96,14 @@ pub fn run() {
|
||||
server::servers::list_coco_servers,
|
||||
server::servers::logout_coco_server,
|
||||
server::servers::refresh_coco_server_info,
|
||||
server::servers::enable_server,
|
||||
server::servers::disable_server,
|
||||
server::auth::handle_sso_callback,
|
||||
server::profile::get_user_profiles,
|
||||
server::datasource::get_datasources_by_server,
|
||||
server::connector::get_connectors_by_server,
|
||||
search::query_coco_fusion,
|
||||
// server::get_coco_server_health_info,
|
||||
// server::get_coco_servers_health_info,
|
||||
server::health::get_server_health,
|
||||
// server::get_user_profiles,
|
||||
// server::get_coco_server_datasources,
|
||||
// server::get_coco_server_connectors,
|
||||
|
||||
@@ -1,79 +1,42 @@
|
||||
|
||||
use crate::COCO_TAURI_STORE;
|
||||
use core::panic;
|
||||
use futures::stream::FuturesUnordered;
|
||||
use futures::FutureExt;
|
||||
use futures::StreamExt;
|
||||
use futures::TryFutureExt;
|
||||
use ordered_float::OrderedFloat;
|
||||
use reqwest::Client;
|
||||
use serde::Serialize;
|
||||
use crate::server::http_client::HttpClient;
|
||||
use serde_json::Map as JsonMap;
|
||||
use serde_json::Value as Json;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::LazyLock;
|
||||
use tauri::AppHandle;
|
||||
use tauri::Runtime;
|
||||
use tauri_plugin_store::StoreExt;
|
||||
use crate::util::http::HTTP_CLIENT;
|
||||
|
||||
fn health_url(endpoint: &str) -> String {
|
||||
format!("{endpoint}/health")
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_coco_server_health_info(endpoint: String) -> bool {
|
||||
let response = match HTTP_CLIENT
|
||||
.get(health_url(&endpoint))
|
||||
.send()
|
||||
.map_err(|_request_err| ())
|
||||
.await
|
||||
{
|
||||
Ok(response) => response,
|
||||
Err(_) => return false,
|
||||
pub async fn get_server_health<R: Runtime>(
|
||||
_app_handle: AppHandle<R>,
|
||||
server_id: String,
|
||||
) -> bool {
|
||||
// Try to fetch the profile using the generic GET method
|
||||
let response = match HttpClient::get(&server_id, "/health").await {
|
||||
Ok(res) => res,
|
||||
Err(_) => return false, // If an error occurs, return false
|
||||
};
|
||||
let json: JsonMap<String, Json> = response.json().await.expect("invalid response");
|
||||
let status = json
|
||||
.get("status")
|
||||
.expect("response does not have a [status] field")
|
||||
.as_str()
|
||||
.expect("status field is not a string");
|
||||
|
||||
// Check if the status code is in the valid range
|
||||
let status_code = response.status().as_u16();
|
||||
if status_code < 200 || status_code >= 400 {
|
||||
return false; // If the status code is not in the success range, return false
|
||||
}
|
||||
|
||||
// Try to parse the JSON response
|
||||
let json: Result<JsonMap<String, Json>, _> = response.json().await;
|
||||
let json = match json {
|
||||
Ok(val) => val,
|
||||
Err(_) => return false, // If JSON parsing fails, return false
|
||||
};
|
||||
|
||||
// Try to get the "status" field and check if it's a string
|
||||
let status = match json.get("status") {
|
||||
Some(val) => match val.as_str() {
|
||||
Some(s) => s,
|
||||
None => return false, // If "status" is not a string, return false
|
||||
},
|
||||
None => return false, // If "status" is missing, return false
|
||||
};
|
||||
|
||||
// Return false if the status is "red", otherwise return true
|
||||
status != "red"
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_coco_servers_health_info<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
) -> Result<HashMap<String, bool>, ()> {
|
||||
// let coco_server_endpoints = _list_coco_server_endpoints(&app_handle).await?;
|
||||
//
|
||||
// let mut futures = FuturesUnordered::new();
|
||||
// for coco_server_endpoint in coco_server_endpoints {
|
||||
// let request_future = HTTP_CLIENT.get(health_url(&coco_server_endpoint)).send();
|
||||
// futures.push(request_future.map(|request_result| (coco_server_endpoint, request_result)));
|
||||
// }
|
||||
//
|
||||
// let mut health_info = HashMap::new();
|
||||
//
|
||||
// while let Some((endpoint, res_response)) = futures.next().await {
|
||||
// let healthy = match res_response {
|
||||
// Ok(response) => {
|
||||
// let json: JsonMap<String, Json> = response.json().await.expect("invalid response");
|
||||
// let status = json
|
||||
// .get("status")
|
||||
// .expect("response does not have a [status] field")
|
||||
// .as_str()
|
||||
// .expect("status field is not a string");
|
||||
// status != "red"
|
||||
// }
|
||||
// Err(_) => false,
|
||||
// };
|
||||
//
|
||||
// health_info.insert(endpoint, healthy);
|
||||
// }
|
||||
//
|
||||
// Ok(health_info)
|
||||
|
||||
Ok()
|
||||
}
|
||||
|
||||
@@ -1,17 +1,8 @@
|
||||
//! This file contains Rust APIs related to Coco Server management.
|
||||
|
||||
// use futures::FutureExt;
|
||||
// use futures::StreamExt;
|
||||
// use futures::TryFutureExt;
|
||||
// use reqwest::Client;
|
||||
// use serde::Serialize;
|
||||
// use std::sync::LazyLock;
|
||||
// use tauri::Runtime;
|
||||
// use tauri_plugin_store::StoreExt;
|
||||
|
||||
pub mod auth;
|
||||
pub mod servers;
|
||||
// pub mod health;
|
||||
pub mod health;
|
||||
pub mod connector;
|
||||
pub mod datasource;
|
||||
pub mod http_client;
|
||||
|
||||
@@ -546,6 +546,7 @@ fn test_trim_endpoint_last_forward_slash() {
|
||||
let mut server = Server {
|
||||
id: "test".to_string(),
|
||||
builtin: false,
|
||||
enabled: true,
|
||||
name: "".to_string(),
|
||||
endpoint: "https://example.com///".to_string(),
|
||||
provider: Provider {
|
||||
|
||||
Reference in New Issue
Block a user