Skip to content

Commit

Permalink
fix: mandatory dimensions with workspaces (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
Datron authored Jan 22, 2025
1 parent b36eb2e commit 37ba8b1
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 47 deletions.
16 changes: 3 additions & 13 deletions crates/context_aware_config/src/api/config/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use superposition_types::{
schema::{config_versions::dsl as config_versions, event_log::dsl as event_log},
},
result as superposition, Cac, Condition, Config, Context, DBConnection, Overrides,
PaginatedResponse, TenantConfig, User,
PaginatedResponse, User,
};
use uuid::Uuid;

Expand Down Expand Up @@ -412,7 +412,6 @@ fn construct_new_payload(
async fn reduce_config_key(
user: User,
conn: &mut DBConnection,
tenant_config: &TenantConfig,
mut og_contexts: Vec<Context>,
mut og_overrides: HashMap<String, Overrides>,
check_key: &str,
Expand Down Expand Up @@ -497,15 +496,8 @@ async fn reduce_config_key(
if is_approve {
let _ = context::delete(cid.clone(), user.clone(), conn, &tenant);
if let Ok(put_req) = construct_new_payload(request_payload) {
let _ = context::put(
put_req,
conn,
false,
&user,
&tenant,
&tenant_config,
false,
);
let _ =
context::put(put_req, conn, false, &user, &tenant, false);
}
}

Expand Down Expand Up @@ -549,7 +541,6 @@ async fn reduce_config(
req: HttpRequest,
user: User,
db_conn: DbConnection,
tenant_config: TenantConfig,
tenant: Tenant,
) -> superposition::Result<HttpResponse> {
let DbConnection(mut conn) = db_conn;
Expand All @@ -570,7 +561,6 @@ async fn reduce_config(
config = reduce_config_key(
user.clone(),
&mut conn,
&tenant_config,
contexts.clone(),
overrides.clone(),
key.as_str(),
Expand Down
12 changes: 1 addition & 11 deletions crates/context_aware_config/src/api/context/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ use superposition_types::{
models::cac::Context,
schema::contexts::{self, id},
},
result as superposition, Contextual, Overridden, PaginatedResponse, SortBy,
TenantConfig, User,
result as superposition, Contextual, Overridden, PaginatedResponse, SortBy, User,
};

pub fn endpoints() -> Scope {
Expand All @@ -67,7 +66,6 @@ async fn put_handler(
mut db_conn: DbConnection,
user: User,
tenant: Tenant,
tenant_config: TenantConfig,
) -> superposition::Result<HttpResponse> {
let tags = parse_config_tags(custom_headers.config_tags)?;

Expand All @@ -89,7 +87,6 @@ async fn put_handler(
true,
&user,
&tenant,
&tenant_config,
false,
)
.map_err(|err: superposition::AppError| {
Expand Down Expand Up @@ -133,7 +130,6 @@ async fn update_override_handler(
mut db_conn: DbConnection,
user: User,
tenant: Tenant,
tenant_config: TenantConfig,
) -> superposition::Result<HttpResponse> {
let tags = parse_config_tags(custom_headers.config_tags)?;
let (override_resp, version_id) = db_conn
Expand All @@ -152,7 +148,6 @@ async fn update_override_handler(
true,
&user,
&tenant,
&tenant_config,
true,
)
.map_err(|err: superposition::AppError| {
Expand Down Expand Up @@ -193,7 +188,6 @@ async fn move_handler(
mut db_conn: DbConnection,
user: User,
tenant: Tenant,
tenant_config: TenantConfig,
) -> superposition::Result<HttpResponse> {
let tags = parse_config_tags(custom_headers.config_tags)?;
let (move_response, version_id) = db_conn
Expand All @@ -205,7 +199,6 @@ async fn move_handler(
true,
&user,
&tenant,
&tenant_config,
)
.map_err(|err| {
log::info!("move api failed with error: {:?}", err);
Expand Down Expand Up @@ -418,7 +411,6 @@ async fn bulk_operations(
db_conn: DbConnection,
user: User,
tenant: Tenant,
tenant_config: TenantConfig,
) -> superposition::Result<HttpResponse> {
use contexts::dsl::contexts;
let DbConnection(mut conn) = db_conn;
Expand All @@ -438,7 +430,6 @@ async fn bulk_operations(
true,
&user,
&tenant,
&tenant_config,
false,
)
.map_err(|err| {
Expand Down Expand Up @@ -515,7 +506,6 @@ async fn bulk_operations(
true,
&user,
&tenant,
&tenant_config,
)
.map_err(|err| {
log::error!(
Expand Down
12 changes: 8 additions & 4 deletions crates/context_aware_config/src/api/context/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ use superposition_types::{
models::cac::Context,
schema::{contexts, default_configs::dsl, dimensions},
},
result as superposition, Cac, Condition, DBConnection, Overrides, TenantConfig, User,
result as superposition, Cac, Condition, DBConnection, Overrides, User,
};

use crate::api::functions::helpers::get_published_functions_by_names;
use crate::validation_functions::execute_fn;
use crate::{
api::functions::helpers::get_published_functions_by_names, helpers::get_workspace,
};
use crate::{
api::{
context::types::FunctionsInfo,
Expand Down Expand Up @@ -213,7 +215,6 @@ pub fn create_ctx_from_put_req(
req: Json<PutReq>,
conn: &mut DBConnection,
user: &User,
tenant_config: &TenantConfig,
tenant: &Tenant,
) -> superposition::Result<Context> {
let ctx_condition = req.context.to_owned().into_inner();
Expand All @@ -228,10 +229,13 @@ pub fn create_ctx_from_put_req(
.clone()
.ok_or_else(|| bad_argument!("Description should not be empty"))?
};

let workspace_settings = get_workspace(&tenant, conn)?;

let change_reason = req.change_reason.clone();
validate_condition_with_mandatory_dimensions(
&ctx_condition,
&tenant_config.mandatory_dimensions,
&workspace_settings.mandatory_dimensions.unwrap_or_default(),
)?;
validate_override_with_default_configs(conn, &r_override, tenant)?;
validate_condition_with_functions(conn, &ctx_condition, tenant)?;
Expand Down
12 changes: 6 additions & 6 deletions crates/context_aware_config/src/api/context/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use service_utils::service::types::Tenant;
use superposition_macros::{bad_argument, db_error, not_found, unexpected_error};
use superposition_types::{
database::{models::cac::Context, schema::contexts},
result, DBConnection, TenantConfig, User,
result, DBConnection, User,
};

use crate::{
Expand All @@ -25,7 +25,7 @@ use crate::{
},
dimension::{get_dimension_data, get_dimension_data_map},
},
helpers::calculate_context_weight,
helpers::{calculate_context_weight, get_workspace},
};

use super::{
Expand All @@ -39,11 +39,10 @@ pub fn put(
already_under_txn: bool,
user: &User,
tenant: &Tenant,
tenant_config: &TenantConfig,
replace: bool,
) -> result::Result<PutResp> {
use contexts::dsl::contexts;
let new_ctx = create_ctx_from_put_req(req, conn, user, tenant_config, &tenant)?;
let new_ctx = create_ctx_from_put_req(req, conn, user, &tenant)?;

if already_under_txn {
diesel::sql_query("SAVEPOINT put_ctx_savepoint").execute(conn)?;
Expand Down Expand Up @@ -80,7 +79,6 @@ pub fn r#move(
already_under_txn: bool,
user: &User,
tenant: &Tenant,
tenant_config: &TenantConfig,
) -> result::Result<PutResp> {
use contexts::dsl;
let req = req.into_inner();
Expand All @@ -102,9 +100,11 @@ pub fn r#move(
let weight = calculate_context_weight(&ctx_condition_value, &dimension_data_map)
.map_err(|_| unexpected_error!("Something Went Wrong"))?;

let workspace_settings = get_workspace(&tenant, conn)?;

validate_condition_with_mandatory_dimensions(
&req.context.into_inner(),
&tenant_config.mandatory_dimensions,
&workspace_settings.mandatory_dimensions.unwrap_or_default(),
)?;

if already_under_txn {
Expand Down
26 changes: 16 additions & 10 deletions crates/context_aware_config/src/api/dimension/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ use superposition_macros::{bad_argument, db_error, not_found, unexpected_error};
use superposition_types::{
custom_query::PaginationParams,
database::{
models::cac::Dimension,
models::{cac::Dimension, Workspace},
schema::dimensions::{self, dsl::*},
types::DimensionWithMandatory,
},
result as superposition, PaginatedResponse, TenantConfig, User,
result as superposition, PaginatedResponse, User,
};

use crate::{
api::dimension::{
types::{CreateReq, FunctionNameEnum},
utils::{get_dimension_usage_context_ids, validate_dimension_position},
},
helpers::validate_jsonschema,
helpers::{get_workspace, validate_jsonschema},
};

use super::types::{DeleteReq, DimensionName, UpdateReq};
Expand All @@ -46,7 +46,6 @@ async fn create(
user: User,
db_conn: DbConnection,
tenant: Tenant,
tenant_config: TenantConfig,
) -> superposition::Result<HttpResponse> {
let DbConnection(mut conn) = db_conn;

Expand Down Expand Up @@ -101,8 +100,11 @@ async fn create(

match insert_resp {
Ok(inserted_dimension) => {
let is_mandatory = tenant_config
let workspace_settings: Workspace =
get_workspace(&tenant, transaction_conn)?;
let is_mandatory = workspace_settings
.mandatory_dimensions
.unwrap_or_default()
.contains(&inserted_dimension.dimension);
Ok(HttpResponse::Created().json(DimensionWithMandatory::new(
inserted_dimension,
Expand Down Expand Up @@ -136,7 +138,6 @@ async fn update(
user: User,
db_conn: DbConnection,
tenant: Tenant,
tenant_config: TenantConfig,
) -> superposition::Result<HttpResponse> {
let name: String = path.clone().into();
use dimensions::dsl;
Expand Down Expand Up @@ -242,8 +243,10 @@ async fn update(
.map_err(|err| db_error!(err))
})?;

let is_mandatory = tenant_config
let workspace_settings = get_workspace(&tenant, &mut conn)?;
let is_mandatory = workspace_settings
.mandatory_dimensions
.unwrap_or_default()
.contains(&result.dimension);

Ok(HttpResponse::Ok().json(DimensionWithMandatory::new(result, is_mandatory)))
Expand All @@ -252,7 +255,6 @@ async fn update(
#[get("")]
async fn get(
db_conn: DbConnection,
tenant_config: TenantConfig,
filters: Query<PaginationParams>,
tenant: Tenant,
) -> superposition::Result<Json<PaginatedResponse<DimensionWithMandatory>>> {
Expand Down Expand Up @@ -285,11 +287,15 @@ async fn get(
}
};

let workspace_settings = get_workspace(&tenant, &mut conn)?;

let mandatory_dimensions =
workspace_settings.mandatory_dimensions.unwrap_or_default();

let dimensions_with_mandatory: Vec<DimensionWithMandatory> = result
.into_iter()
.map(|ele| {
let is_mandatory =
tenant_config.mandatory_dimensions.contains(&ele.dimension);
let is_mandatory = mandatory_dimensions.contains(&ele.dimension);
DimensionWithMandatory::new(ele, is_mandatory)
})
.collect();
Expand Down
13 changes: 12 additions & 1 deletion crates/context_aware_config/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ use service_utils::{
use superposition_macros::{bad_argument, db_error, unexpected_error, validation_error};
#[cfg(feature = "high-performance-mode")]
use superposition_types::database::schema::event_log::dsl as event_log;
use superposition_types::database::superposition_schema::superposition::workspaces;
use superposition_types::DBConnection;
use superposition_types::{
database::{
models::cac::ConfigVersion,
models::{cac::ConfigVersion, Workspace},
schema::{
config_versions,
contexts::dsl::{self as ctxt},
Expand Down Expand Up @@ -320,6 +321,16 @@ pub fn add_config_version(
Ok(version_id)
}

pub fn get_workspace(
workspace_schema_name: &String,
db_conn: &mut DBConnection,
) -> superposition::Result<Workspace> {
let workspace = workspaces::dsl::workspaces
.filter(workspaces::workspace_schema_name.eq(workspace_schema_name))
.get_result::<Workspace>(db_conn)?;
Ok(workspace)
}

#[cfg(feature = "high-performance-mode")]
pub async fn put_config_in_redis(
version_id: i64,
Expand Down
2 changes: 0 additions & 2 deletions crates/superposition_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ impl Display for RegexEnum {

#[derive(Clone, Deserialize)]
pub struct TenantConfig {
pub mandatory_dimensions: Vec<String>,
pub experiments_webhook_config: WebhookConfig,
}

Expand All @@ -173,7 +172,6 @@ impl FromRequest for TenantConfig {
impl Default for TenantConfig {
fn default() -> Self {
Self {
mandatory_dimensions: Default::default(),
experiments_webhook_config: WebhookConfig::Disbled,
}
}
Expand Down

0 comments on commit 37ba8b1

Please sign in to comment.