Skip to content

Commit

Permalink
fix: read from workspaces table when showing the dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Datron committed Jan 6, 2025
1 parent f68829c commit ef99224
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 90 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions crates/frontend/src/components/side_nav.rs
Original file line number Diff line number Diff line change
@@ -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<AppRoute> {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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::<Vec<String>>();
match workspaces.is_empty() {
false => {
tenants
workspaces
.iter()
.map(|tenant| {
view! {
Expand Down
2 changes: 1 addition & 1 deletion crates/frontend/src/pages/config_version_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn config_version_list() -> impl IntoView {
let org_rws = use_context::<RwSignal<OrganisationId>>().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));
Expand Down
2 changes: 1 addition & 1 deletion crates/frontend/src/pages/custom_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
5 changes: 2 additions & 3 deletions crates/frontend/src/pages/default_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn default_config() -> impl IntoView {
let tenant_rws = use_context::<RwSignal<Tenant>>().unwrap();
let org_rws = use_context::<RwSignal<OrganisationId>>().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 {
Expand All @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion crates/frontend/src/pages/dimensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn dimensions() -> impl IntoView {
let org_rws = use_context::<RwSignal<OrganisationId>>().unwrap();
let (delete_modal_visible_rs, delete_modal_visible_ws) = create_signal(false);
let (delete_id_rs, delete_id_ws) = create_signal::<Option<String>>(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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/frontend/src/pages/experiment_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion crates/frontend/src/pages/function/function_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct CombinedResource {
pub fn function_list() -> impl IntoView {
let tenant_rws = use_context::<RwSignal<Tenant>>().unwrap();
let org_rws = use_context::<RwSignal<OrganisationId>>().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(
Expand Down
2 changes: 1 addition & 1 deletion crates/frontend/src/pages/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::utils::update_page_direction;
pub fn workspace() -> impl IntoView {
let org_id: RwSignal<OrganisationId> =
use_context::<RwSignal<OrganisationId>>().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 {
Expand Down
1 change: 0 additions & 1 deletion crates/frontend/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ impl FromStr for AppEnv {
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Envs {
pub host: String,
pub tenants: Vec<String>,
pub service_prefix: &'static str,
}

Expand Down
22 changes: 0 additions & 22 deletions crates/frontend/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,28 +92,6 @@ pub fn get_host() -> String {
add_prefix(&host, &service_prefix)
}

pub fn get_tenants() -> Vec<String> {
let context = use_context::<Envs>();
context
.map(|ctx: Envs| ctx.tenants)
.or_else(|| match js_sys::eval("__APP_ENVS?.tenants") {
Ok(value) => value
.dyn_into::<js_sys::Array>()
.expect("tenants is not an array")
.to_vec()
.into_iter()
.map(|tenant| {
tenant.dyn_into::<js_sys::JsString>().ok().map(String::from)
})
.collect::<Option<Vec<String>>>(),
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::<Envs>();
Expand Down
5 changes: 1 addition & 4 deletions crates/service_utils/src/service/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Mutex;
use std::{
collections::{HashMap, HashSet},
collections::HashSet,
future::{ready, Ready},
str::FromStr,
sync::Arc,
Expand All @@ -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;

Expand Down Expand Up @@ -42,7 +41,6 @@ pub enum AppHeader {
pub struct AppState {
pub cac_host: String,
pub app_env: AppEnv,
pub tenants: HashSet<String>,
pub cac_version: String,
pub db_pool: PgSchemaConnectionPool,
pub meta_schema: JSONSchema,
Expand All @@ -51,7 +49,6 @@ pub struct AppState {
pub enable_tenant_and_scope: bool,
pub tenant_middleware_exclusion_list: HashSet<String>,
pub service_prefix: String,
pub tenant_configs: HashMap<String, TenantConfig>,
pub superposition_token: String,
#[cfg(feature = "high-performance-mode")]
pub redis: fred::clients::RedisPool,
Expand Down
2 changes: 0 additions & 2 deletions crates/superposition/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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]
Expand Down
26 changes: 1 addition & 25 deletions crates/superposition/src/app_state.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand All @@ -21,40 +20,19 @@ 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<aws_sdk_kms::Client>,
service_prefix: String,
base: &String,
tenants: &HashSet<String>,
) -> AppState {
let cac_host =
get_from_env_unsafe::<String>("CAC_HOST").expect("CAC host is not set") + base;
let max_pool_size = get_from_env_or_default("MAX_DB_CONNECTION_POOL_SIZE", 2);
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::<TenantConfig>)
.map(|config| (tenant, config))
.ok()
})
.collect::<HashMap<_, _>>();

let snowflake_generator = Arc::new(Mutex::new(SnowflakeIdGenerator::new(1, 1)));

cfg_if::cfg_if! {
Expand Down Expand Up @@ -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::<String>(
"TENANT_MIDDLEWARE_EXCLUSION_LIST",
)
Expand All @@ -124,7 +101,6 @@ pub async fn get(
.map(String::from)
.collect::<HashSet<_>>(),
service_prefix,
tenant_configs,
superposition_token: get_superposition_token(&kms_client, &app_env).await,
#[cfg(feature = "high-performance-mode")]
redis: redis_pool,
Expand Down
23 changes: 3 additions & 20 deletions crates/superposition/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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::<String>("TENANTS")
.expect("TENANTS is not set")
.split(',')
.map(String::from)
.collect::<HashSet<_>>();

/* 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::<Vec<_>>(),
host: get_from_env_unsafe("API_HOSTNAME").expect("API_HOSTNAME is not set"),
};

Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion crates/superposition_types/src/custom_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit ef99224

Please sign in to comment.