use std::sync::Arc;
use conundrum_lang::ecosystem::db::{db_traits::async_traits::try_from_async::FromAsync, tables::DatabaseTable};
use online::tokio::check;
use serde::{Deserialize, Serialize};
use strum::IntoEnumIterator;
use crate::vector::models::ecosystem_data::server_state::server_state::ServerState;
#[derive(Serialize, Deserialize, Clone, Debug, specta::Type)]
pub struct BackendStatus {
pub local_client_access: bool,
pub remote_client_access: bool,
pub all_tables_exist: bool,
pub any_tables_exist: bool,
pub is_online: bool,
}
impl FromAsync<Arc<ServerState>> for BackendStatus {
async fn from_async(input: Arc<ServerState>) -> Self
where Self: Sized {
let is_online = check(None).await.is_ok();
let db = input.db.inner_arc().lock_owned().await;
let mut any_tables_exist = false;
for k in DatabaseTable::iter() {
if db.open_table(k.to_string()).execute().await.is_err() {
return Self { local_client_access: input.local_client.is_some(),
remote_client_access: input.remote_client.is_some(),
all_tables_exist: false,
any_tables_exist,
is_online };
} else {
any_tables_exist = true;
}
}
Self { local_client_access: input.local_client.is_some(),
remote_client_access: input.remote_client.is_some(),
all_tables_exist: true,
any_tables_exist,
is_online }
}
}