From ef992244144afd5d1c7e2906a63ab6949ed57339 Mon Sep 17 00:00:00 2001 From: Kartik Date: Mon, 6 Jan 2025 11:56:54 +0530 Subject: [PATCH] fix: read from workspaces table when showing the dropdown --- Cargo.lock | 2 -- crates/frontend/src/components/side_nav.rs | 25 +++++++++++++++--- .../frontend/src/pages/config_version_list.rs | 2 +- crates/frontend/src/pages/custom_types.rs | 2 +- crates/frontend/src/pages/default_config.rs | 5 ++-- crates/frontend/src/pages/dimensions.rs | 2 +- crates/frontend/src/pages/experiment_list.rs | 2 +- .../src/pages/function/function_list.rs | 2 +- crates/frontend/src/pages/workspace.rs | 2 +- crates/frontend/src/types.rs | 1 - crates/frontend/src/utils.rs | 22 ---------------- crates/service_utils/src/service/types.rs | 5 +--- crates/superposition/Cargo.toml | 2 -- crates/superposition/src/app_state.rs | 26 +------------------ crates/superposition/src/main.rs | 23 +++------------- .../superposition_types/src/custom_query.rs | 4 ++- 16 files changed, 37 insertions(+), 90 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f5cf6321..6a645d26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4612,7 +4612,6 @@ dependencies = [ "actix-web", "anyhow", "aws-sdk-kms", - "cac_toml", "cfg-if", "chrono", "context_aware_config", @@ -4636,7 +4635,6 @@ dependencies = [ "service_utils", "superposition_macros", "superposition_types", - "toml 0.8.8", "url", ] diff --git a/crates/frontend/src/components/side_nav.rs b/crates/frontend/src/components/side_nav.rs index 1daf4199..52a9e37c 100644 --- a/crates/frontend/src/components/side_nav.rs +++ b/crates/frontend/src/components/side_nav.rs @@ -1,10 +1,12 @@ +use crate::api::fetch_workspaces; use crate::components::nav_item::NavItem; use crate::components::skeleton::{Skeleton, SkeletonVariant}; use crate::types::{AppRoute, OrganisationId, Tenant}; -use crate::utils::{get_tenants, use_url_base}; +use crate::utils::use_url_base; use leptos::*; use leptos_router::{use_location, use_navigate, A}; +use superposition_types::custom_query::PaginationParams; use web_sys::Event; fn create_routes(org: &str, tenant: &str) -> Vec { @@ -74,6 +76,15 @@ pub fn side_nav( org_rws.get_untracked().as_str(), tenant_rws.get_untracked().as_str(), )); + let workspace_resource = create_blocking_resource( + move || org_rws.get().0, + |org_id| async move { + let filters = PaginationParams::default(); + fetch_workspaces(&filters, &org_id) + .await + .unwrap_or_default() + }, + ); let resolved_path = create_rw_signal(resolved_path); let original_path = create_rw_signal(original_path); @@ -135,10 +146,16 @@ pub fn side_nav( > {move || { - let tenants = get_tenants(); - match tenants.is_empty() { + let workspaces = workspace_resource + .get() + .unwrap_or_default() + .data + .into_iter() + .map(|workspace| workspace.workspace_name) + .collect::>(); + match workspaces.is_empty() { false => { - tenants + workspaces .iter() .map(|tenant| { view! { diff --git a/crates/frontend/src/pages/config_version_list.rs b/crates/frontend/src/pages/config_version_list.rs index 29e93f1b..60648f2f 100644 --- a/crates/frontend/src/pages/config_version_list.rs +++ b/crates/frontend/src/pages/config_version_list.rs @@ -23,7 +23,7 @@ pub fn config_version_list() -> impl IntoView { let org_rws = use_context::>().unwrap(); // Signals for filters - let (filters, set_filters) = create_signal(PaginationParams::default_request()); + let (filters, set_filters) = create_signal(PaginationParams::default()); let table_columns = create_memo(move |_| snapshot_table_columns(tenant_rws.get().0, org_rws.get().0)); diff --git a/crates/frontend/src/pages/custom_types.rs b/crates/frontend/src/pages/custom_types.rs index e4d8e3cb..805bd790 100644 --- a/crates/frontend/src/pages/custom_types.rs +++ b/crates/frontend/src/pages/custom_types.rs @@ -31,7 +31,7 @@ pub fn types_page() -> impl IntoView { let types_resource = create_blocking_resource( move || (tenant_rws.get().0, org_rws.get().0), |(t, org_id)| async move { - fetch_types(&PaginationParams::default_request(), t, org_id) + fetch_types(&PaginationParams::default(), t, org_id) .await .map_or_else( |err| { diff --git a/crates/frontend/src/pages/default_config.rs b/crates/frontend/src/pages/default_config.rs index c0b52caa..97bfe424 100644 --- a/crates/frontend/src/pages/default_config.rs +++ b/crates/frontend/src/pages/default_config.rs @@ -34,7 +34,7 @@ pub fn default_config() -> impl IntoView { let tenant_rws = use_context::>().unwrap(); let org_rws = use_context::>().unwrap(); let enable_grouping = create_rw_signal(false); - let (filters, set_filters) = create_signal(PaginationParams::default_request()); + let (filters, set_filters) = create_signal(PaginationParams::default()); let default_config_resource = create_blocking_resource( move || (tenant_rws.get().0, filters.get(), org_rws.get().0), |(current_tenant, filters, org_id)| async move { @@ -51,8 +51,7 @@ pub fn default_config() -> impl IntoView { let set_filters_none = move || set_filters.set(PaginationParams::all_entries()); - let set_filters_default = - move || set_filters.set(PaginationParams::default_request()); + let set_filters_default = move || set_filters.set(PaginationParams::default()); create_effect(move |_| { let enable_grouping_val = diff --git a/crates/frontend/src/pages/dimensions.rs b/crates/frontend/src/pages/dimensions.rs index b196526f..3189dd1b 100644 --- a/crates/frontend/src/pages/dimensions.rs +++ b/crates/frontend/src/pages/dimensions.rs @@ -33,7 +33,7 @@ pub fn dimensions() -> impl IntoView { let org_rws = use_context::>().unwrap(); let (delete_modal_visible_rs, delete_modal_visible_ws) = create_signal(false); let (delete_id_rs, delete_id_ws) = create_signal::>(None); - let (filters, set_filters) = create_signal(PaginationParams::default_request()); + let (filters, set_filters) = create_signal(PaginationParams::default()); let dimensions_resource = create_blocking_resource( move || (tenant_rws.get().0, filters.get(), org_rws.get().0), |(current_tenant, filters, org_id)| async move { diff --git a/crates/frontend/src/pages/experiment_list.rs b/crates/frontend/src/pages/experiment_list.rs index 064b32da..59be746f 100644 --- a/crates/frontend/src/pages/experiment_list.rs +++ b/crates/frontend/src/pages/experiment_list.rs @@ -50,7 +50,7 @@ pub fn experiment_list() -> impl IntoView { }); let (pagination_filters_rs, pagination_filters_ws) = - create_signal(PaginationParams::default_request()); + create_signal(PaginationParams::default()); let (reset_exp_form, set_exp_form) = create_signal(0); diff --git a/crates/frontend/src/pages/function/function_list.rs b/crates/frontend/src/pages/function/function_list.rs index e616e3e8..a34a2490 100644 --- a/crates/frontend/src/pages/function/function_list.rs +++ b/crates/frontend/src/pages/function/function_list.rs @@ -25,7 +25,7 @@ struct CombinedResource { pub fn function_list() -> impl IntoView { let tenant_rws = use_context::>().unwrap(); let org_rws = use_context::>().unwrap(); - let (filters, set_filters) = create_signal(PaginationParams::default_request()); + let (filters, set_filters) = create_signal(PaginationParams::default()); let table_columns = create_memo(move |_| function_table_columns()); let combined_resource = create_blocking_resource( diff --git a/crates/frontend/src/pages/workspace.rs b/crates/frontend/src/pages/workspace.rs index e4bfc5f0..e28dc3de 100644 --- a/crates/frontend/src/pages/workspace.rs +++ b/crates/frontend/src/pages/workspace.rs @@ -22,7 +22,7 @@ use crate::utils::update_page_direction; pub fn workspace() -> impl IntoView { let org_id: RwSignal = use_context::>().unwrap(); - let (filters, set_filters) = create_signal(PaginationParams::default_request()); + let (filters, set_filters) = create_signal(PaginationParams::default()); let workspace_resource = create_blocking_resource( move || (filters.get(), org_id.get().0), |(filters, org_id)| async move { diff --git a/crates/frontend/src/types.rs b/crates/frontend/src/types.rs index ecdba957..a426c22e 100644 --- a/crates/frontend/src/types.rs +++ b/crates/frontend/src/types.rs @@ -58,7 +58,6 @@ impl FromStr for AppEnv { #[derive(Serialize, Deserialize, Clone, Debug)] pub struct Envs { pub host: String, - pub tenants: Vec, pub service_prefix: &'static str, } diff --git a/crates/frontend/src/utils.rs b/crates/frontend/src/utils.rs index 6d23610f..aed8cd5a 100644 --- a/crates/frontend/src/utils.rs +++ b/crates/frontend/src/utils.rs @@ -92,28 +92,6 @@ pub fn get_host() -> String { add_prefix(&host, &service_prefix) } -pub fn get_tenants() -> Vec { - let context = use_context::(); - context - .map(|ctx: Envs| ctx.tenants) - .or_else(|| match js_sys::eval("__APP_ENVS?.tenants") { - Ok(value) => value - .dyn_into::() - .expect("tenants is not an array") - .to_vec() - .into_iter() - .map(|tenant| { - tenant.dyn_into::().ok().map(String::from) - }) - .collect::>>(), - Err(e) => { - logging::log!("Unable to fetch tenants from __APP_ENVS: {:?}", e); - None - } - }) - .unwrap_or_default() -} - #[allow(dead_code)] pub fn use_env() -> Envs { let context = use_context::(); diff --git a/crates/service_utils/src/service/types.rs b/crates/service_utils/src/service/types.rs index abd70ed6..1afd6f46 100644 --- a/crates/service_utils/src/service/types.rs +++ b/crates/service_utils/src/service/types.rs @@ -1,6 +1,6 @@ use std::sync::Mutex; use std::{ - collections::{HashMap, HashSet}, + collections::HashSet, future::{ready, Ready}, str::FromStr, sync::Arc, @@ -13,7 +13,6 @@ use diesel::PgConnection; use jsonschema::JSONSchema; use serde_json::json; use snowflake::SnowflakeIdGenerator; -use superposition_types::TenantConfig; use crate::db::PgSchemaConnectionPool; @@ -42,7 +41,6 @@ pub enum AppHeader { pub struct AppState { pub cac_host: String, pub app_env: AppEnv, - pub tenants: HashSet, pub cac_version: String, pub db_pool: PgSchemaConnectionPool, pub meta_schema: JSONSchema, @@ -51,7 +49,6 @@ pub struct AppState { pub enable_tenant_and_scope: bool, pub tenant_middleware_exclusion_list: HashSet, pub service_prefix: String, - pub tenant_configs: HashMap, pub superposition_token: String, #[cfg(feature = "high-performance-mode")] pub redis: fred::clients::RedisPool, diff --git a/crates/superposition/Cargo.toml b/crates/superposition/Cargo.toml index f8200a47..b78536e2 100644 --- a/crates/superposition/Cargo.toml +++ b/crates/superposition/Cargo.toml @@ -10,7 +10,6 @@ actix-files = { version = "0.6" } actix-web = { workspace = true } anyhow = { workspace = true } aws-sdk-kms = { workspace = true } -cac_toml = { path = "../cac_toml" } cfg-if = { workspace = true } chrono = { workspace = true } context_aware_config = { path = "../context_aware_config" } @@ -34,7 +33,6 @@ serde_json = { workspace = true } service_utils = { path = "../service_utils" } superposition_macros = { path = "../superposition_macros" } superposition_types = { path = "../superposition_types", features = ["diesel_derives"]} -toml = { workspace = true } url = { workspace = true } [features] diff --git a/crates/superposition/src/app_state.rs b/crates/superposition/src/app_state.rs index abe807b3..513ce4b2 100644 --- a/crates/superposition/src/app_state.rs +++ b/crates/superposition/src/app_state.rs @@ -1,12 +1,11 @@ use std::{ - collections::{HashMap, HashSet}, + collections::HashSet, sync::{Arc, Mutex}, }; #[cfg(feature = "high-performance-mode")] use std::time::Duration; -use cac_toml::ContextAwareConfig; use context_aware_config::helpers::get_meta_schema; #[cfg(feature = "high-performance-mode")] @@ -21,16 +20,12 @@ use service_utils::{ service::types::{AppEnv, AppState, ExperimentationFlags}, }; use snowflake::SnowflakeIdGenerator; -use superposition_types::TenantConfig; - -const TENANT_CONFIG_FILE: &str = "crates/superposition/Superposition.cac.toml"; pub async fn get( app_env: AppEnv, kms_client: &Option, service_prefix: String, base: &String, - tenants: &HashSet, ) -> AppState { let cac_host = get_from_env_unsafe::("CAC_HOST").expect("CAC host is not set") + base; @@ -38,23 +33,6 @@ pub async fn get( let enable_tenant_and_scope = get_from_env_unsafe("ENABLE_TENANT_AND_SCOPE") .expect("ENABLE_TENANT_AND_SCOPE is not set"); - let cac = ContextAwareConfig::parse(TENANT_CONFIG_FILE) - .expect(&format!("File {TENANT_CONFIG_FILE} not found")); - - let tenant_configs = tenants - .clone() - .into_iter() - .filter_map(|tenant| { - serde_json::to_value(cac.get_resolved_config(&HashMap::from_iter(vec![( - String::from("tenant"), - toml::Value::String(tenant.clone()), - )]))) - .and_then(serde_json::from_value::) - .map(|config| (tenant, config)) - .ok() - }) - .collect::>(); - let snowflake_generator = Arc::new(Mutex::new(SnowflakeIdGenerator::new(1, 1))); cfg_if::cfg_if! { @@ -115,7 +93,6 @@ pub async fn get( meta_schema: get_meta_schema(), app_env, enable_tenant_and_scope, - tenants: tenants.clone(), tenant_middleware_exclusion_list: get_from_env_unsafe::( "TENANT_MIDDLEWARE_EXCLUSION_LIST", ) @@ -124,7 +101,6 @@ pub async fn get( .map(String::from) .collect::>(), service_prefix, - tenant_configs, superposition_token: get_superposition_token(&kms_client, &app_env).await, #[cfg(feature = "high-performance-mode")] redis: redis_pool, diff --git a/crates/superposition/src/main.rs b/crates/superposition/src/main.rs index a19b4b67..cae7066c 100644 --- a/crates/superposition/src/main.rs +++ b/crates/superposition/src/main.rs @@ -5,7 +5,7 @@ mod organisation; mod workspace; use idgenerator::{IdGeneratorOptions, IdInstance}; -use std::{collections::HashSet, io::Result, time::Duration}; +use std::{io::Result, time::Duration}; use actix_files::Files; use actix_web::{ @@ -76,21 +76,11 @@ async fn main() -> Result<()> { let cac_port: u16 = get_from_env_unsafe("PORT").unwrap_or(8080); - let tenants = get_from_env_unsafe::("TENANTS") - .expect("TENANTS is not set") - .split(',') - .map(String::from) - .collect::>(); - /* Frontend configurations */ - let ui_redirect_path = match tenants.iter().next() { - Some(tenant) => format!("{}/admin/{}/default-config", base, tenant), - None => String::from("/admin"), - }; + let ui_redirect_path = format!("{}/admin/organisations", base); let ui_envs = UIEnvs { service_prefix: service_prefix_str, - tenants: tenants.clone().into_iter().collect::>(), host: get_from_env_unsafe("API_HOSTNAME").expect("API_HOSTNAME is not set"), }; @@ -109,14 +99,7 @@ async fn main() -> Result<()> { }; let app_state = Data::new( - app_state::get( - app_env, - &kms_client, - service_prefix_str.to_owned(), - &base, - &tenants, - ) - .await, + app_state::get(app_env, &kms_client, service_prefix_str.to_owned(), &base).await, ); let auth = AuthHandler::init(&kms_client, &app_env, base.clone()).await; diff --git a/crates/superposition_types/src/custom_query.rs b/crates/superposition_types/src/custom_query.rs index 63175c4a..c73a0d34 100644 --- a/crates/superposition_types/src/custom_query.rs +++ b/crates/superposition_types/src/custom_query.rs @@ -173,8 +173,10 @@ impl PaginationParams { all: Some(true), } } +} - pub fn default_request() -> Self { +impl Default for PaginationParams { + fn default() -> Self { Self { count: Some(10), page: Some(1),