Skip to content

Commit

Permalink
fix: experiment handlers to send org_id
Browse files Browse the repository at this point in the history
  • Loading branch information
Datron committed Jan 13, 2025
1 parent 4bb04a1 commit a247081
Show file tree
Hide file tree
Showing 92 changed files with 1,083 additions and 628 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ test_logs
.gradle
build
.java~
postman/superposition_env.json
21 changes: 21 additions & 0 deletions crates/context_aware_config/src/api/config/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,30 @@ fn construct_new_payload(
},
)?;

let description = match res.get("description") {
Some(Value::String(s)) => Some(s.clone()),
Some(_) => {
log::error!("construct new payload: Description is not a valid string");
return Err(bad_argument!("Description must be a string"));
}
None => None,
};

// Handle change_reason
let change_reason = res
.get("change_reason")
.and_then(|val| val.as_str())
.map(|s| s.to_string())
.ok_or_else(|| {
log::error!("construct new payload: Change reason not present or invalid");
bad_argument!("Change reason is required and must be a string")
})?;

return Ok(web::Json(PutReq {
context: context,
r#override: override_,
description,
change_reason,
}));
}

Expand Down
45 changes: 38 additions & 7 deletions crates/context_aware_config/src/api/context/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ fn create_ctx_from_put_req(
last_modified_at: Utc::now().naive_utc(),
last_modified_by: user.get_email(),
weight,
description,
change_reason,
})
}

Expand Down Expand Up @@ -326,6 +328,8 @@ fn get_put_resp(ctx: Context) -> PutResp {
context_id: ctx.id,
override_id: ctx.override_id,
weight: ctx.weight,
description: ctx.description,
change_reason: ctx.change_reason,
}
}

Expand Down Expand Up @@ -582,6 +586,8 @@ fn r#move(
last_modified_at: Utc::now().naive_utc(),
last_modified_by: user.get_email(),
weight,
description,
change_reason,
};

let handle_unique_violation =
Expand Down Expand Up @@ -841,6 +847,9 @@ async fn delete_context_handler(
tenant: Tenant,
mut db_conn: DbConnection,
) -> superposition::Result<HttpResponse> {
use superposition_types::database::schema::contexts::dsl::{
contexts as contexts_table, id as context_id,
};
let ctx_id = path.into_inner();
let tags = parse_config_tags(custom_headers.config_tags)?;
let version_id =
Expand Down Expand Up @@ -888,6 +897,9 @@ async fn bulk_operations(
) -> superposition::Result<HttpResponse> {
use contexts::dsl::contexts;
let DbConnection(mut conn) = db_conn;
let mut all_descriptions = Vec::new();
let mut all_change_reasons = Vec::new();

let tags = parse_config_tags(custom_headers.config_tags)?;
let (response, version_id) =
conn.transaction::<_, superposition::AppError, _>(|transaction_conn| {
Expand All @@ -896,7 +908,7 @@ async fn bulk_operations(
match action {
ContextAction::Put(put_req) => {
let put_resp = put(
Json(put_req),
Json(put_req.clone()),
transaction_conn,
true,
&user,
Expand Down Expand Up @@ -941,7 +953,15 @@ async fn bulk_operations(
.filter(id.eq(&ctx_id))
.schema_name(&tenant)
.execute(transaction_conn);
let email: String = user.get_email();

let description = context.description;

let email: String = user.clone().get_email();
let change_reason =
format!("Context deleted by {}", email.clone());
all_descriptions.push(description.clone());
all_change_reasons.push(change_reason.clone());

match deleted_row {
// Any kind of error would rollback the tranction but explicitly returning rollback tranction allows you to rollback from any point in transaction.
Ok(0) => {
Expand Down Expand Up @@ -979,6 +999,8 @@ async fn bulk_operations(
);
err
})?;
all_descriptions.push(move_context_resp.description.clone());
all_change_reasons.push(move_context_resp.change_reason.clone());
response.push(ContextBulkResponse::Move(move_context_resp));
}
}
Expand Down Expand Up @@ -1018,7 +1040,9 @@ async fn weight_recompute(
tenant: Tenant,
user: User,
) -> superposition::Result<HttpResponse> {
use superposition_types::database::schema::contexts::dsl::*;
use superposition_types::database::schema::contexts::dsl::{
contexts, last_modified_at, last_modified_by, weight,
};
let DbConnection(mut conn) = db_conn;

let result: Vec<Context> =
Expand All @@ -1035,7 +1059,7 @@ async fn weight_recompute(
let mut response: Vec<WeightRecomputeResponse> = vec![];
let tags = parse_config_tags(custom_headers.config_tags)?;

let contexts_new_weight: Vec<(BigDecimal, String)> = result
let contexts_new_weight: Vec<(BigDecimal, String, String, String)> = result
.clone()
.into_iter()
.map(|context| {
Expand All @@ -1051,21 +1075,28 @@ async fn weight_recompute(
condition: context.value.clone(),
old_weight: context.weight.clone(),
new_weight: val.clone(),
description: context.description.clone(),
change_reason: context.change_reason.clone(),
});
Ok((val, context.id.clone()))
Ok((
val,
context.id.clone(),
context.description.clone(),
context.change_reason.clone(),
))
}
Err(e) => {
log::error!("failed to calculate context weight: {}", e);
Err(unexpected_error!("Something went wrong"))
}
}
})
.collect::<superposition::Result<Vec<(BigDecimal, String)>>>()?;
.collect::<superposition::Result<Vec<(BigDecimal, String, String, String)>>>()?;

let last_modified_time = Utc::now().naive_utc();
let config_version_id =
conn.transaction::<_, superposition::AppError, _>(|transaction_conn| {
for (context_weight, context_id) in contexts_new_weight {
for (context_weight, context_id, _description, _change_reason) in contexts_new_weight.clone() {
diesel::update(contexts.filter(id.eq(context_id)))
.set((
weight.eq(context_weight),
Expand Down
14 changes: 13 additions & 1 deletion crates/context_aware_config/src/api/context/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ use superposition_types::{
pub struct PutReq {
pub context: Cac<Condition>,
pub r#override: Cac<Overrides>,
pub description: Option<String>,
pub change_reason: String,
}

#[cfg_attr(test, derive(Debug, PartialEq))] // Derive traits only when running tests
#[derive(Deserialize, Clone)]
pub struct MoveReq {
pub context: Cac<Condition>,
pub description: Option<String>,
pub change_reason: String,
}

#[derive(Deserialize, Clone)]
Expand All @@ -28,6 +32,8 @@ pub struct PutResp {
pub context_id: String,
pub override_id: String,
pub weight: BigDecimal,
pub description: String,
pub change_reason: String,
}

impl From<Context> for PutResp {
Expand Down Expand Up @@ -94,6 +100,8 @@ pub struct WeightRecomputeResponse {
pub condition: Condition,
pub old_weight: BigDecimal,
pub new_weight: BigDecimal,
pub description: String,
pub change_reason: String,
}

#[cfg(test)]
Expand All @@ -113,7 +121,9 @@ mod tests {
},
"override": {
"foo": "baz"
}
},
"description": "",
"change_reason": ""
});

let action_str = json!({
Expand All @@ -135,6 +145,8 @@ mod tests {
let expected_action = ContextAction::Put(PutReq {
context: context,
r#override: override_,
description: Some("".to_string()),
change_reason: "".to_string(),
});

let action_deserialized =
Expand Down
12 changes: 11 additions & 1 deletion crates/context_aware_config/src/api/default_config/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ async fn create_default_config(
let req = request.into_inner();
let key = req.key;
let tags = parse_config_tags(custom_headers.config_tags)?;

let description = req.description;
let change_reason = req.change_reason;
if req.schema.is_empty() {
return Err(bad_argument!("Schema cannot be empty."));
}
Expand All @@ -76,6 +77,8 @@ async fn create_default_config(
created_at: Utc::now(),
last_modified_at: Utc::now().naive_utc(),
last_modified_by: user.get_email(),
description: description.clone(),
change_reason: change_reason.clone(),
};

let schema_compile_result = JSONSchema::options()
Expand Down Expand Up @@ -177,6 +180,11 @@ async fn update_default_config(
}
})?;

let description = req
.description
.unwrap_or_else(|| existing.description.clone());
let change_reason = req.change_reason;

let value = req.value.unwrap_or_else(|| existing.value.clone());
let schema = req
.schema
Expand All @@ -196,6 +204,8 @@ async fn update_default_config(
created_at: existing.created_at,
last_modified_at: Utc::now().naive_utc(),
last_modified_by: user.get_email(),
description: description.clone(),
change_reason: change_reason.clone(),
};

let jschema = JSONSchema::options()
Expand Down
4 changes: 4 additions & 0 deletions crates/context_aware_config/src/api/default_config/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub struct CreateReq {
pub value: Value,
pub schema: Map<String, Value>,
pub function_name: Option<String>,
pub description: String,
pub change_reason: String,
}

#[derive(Debug, Deserialize)]
Expand All @@ -17,6 +19,8 @@ pub struct UpdateReq {
pub value: Option<Value>,
pub schema: Option<Map<String, Value>>,
pub function_name: Option<FunctionNameEnum>,
pub description: Option<String>,
pub change_reason: String,
}

#[derive(Debug, Clone)]
Expand Down
13 changes: 11 additions & 2 deletions crates/context_aware_config/src/api/dimension/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ async fn create(
function_name: create_req.function_name.clone(),
last_modified_at: Utc::now().naive_utc(),
last_modified_by: user.get_email(),
description: create_req.description,
change_reason: create_req.change_reason,
};

conn.transaction::<_, superposition::AppError, _>(|transaction_conn| {
Expand Down Expand Up @@ -161,6 +163,11 @@ async fn update(
dimension_row.schema = schema_value;
}

dimension_row.change_reason = update_req.change_reason;
dimension_row.description = update_req
.description
.unwrap_or_else(|| dimension_row.description);

dimension_row.function_name = match update_req.function_name {
Some(FunctionNameEnum::Name(func_name)) => Some(func_name),
Some(FunctionNameEnum::Remove) => None,
Expand Down Expand Up @@ -221,11 +228,13 @@ async fn update(
diesel::update(dimensions)
.filter(dsl::dimension.eq(&dimension_row.dimension))
.set((
dsl::last_modified_at.eq(Utc::now().naive_utc()),
dsl::last_modified_by.eq(user.get_email()),
dimensions::last_modified_at.eq(Utc::now().naive_utc()),
dimensions::last_modified_by.eq(user.get_email()),
dimensions::function_name.eq(dimension_row.function_name),
dimensions::schema.eq(dimension_row.schema),
dimensions::position.eq(new_position),
dimensions::description.eq(dimension_row.description),
dimensions::change_reason.eq(dimension_row.change_reason),
))
.returning(Dimension::as_returning())
.schema_name(&tenant)
Expand Down
4 changes: 4 additions & 0 deletions crates/context_aware_config/src/api/dimension/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub struct CreateReq {
pub position: Position,
pub schema: Value,
pub function_name: Option<String>,
pub description: String,
pub change_reason: String,
}

#[derive(Debug, Deserialize, AsRef, Deref, DerefMut, Into, Clone)]
Expand Down Expand Up @@ -42,6 +44,8 @@ pub struct UpdateReq {
pub position: Option<Position>,
pub schema: Option<Value>,
pub function_name: Option<FunctionNameEnum>,
pub description: Option<String>,
pub change_reason: String,
}

#[derive(Debug, Clone)]
Expand Down
6 changes: 4 additions & 2 deletions crates/context_aware_config/src/api/functions/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ async fn create(
published_at: None,
published_by: None,
published_runtime_version: None,
function_description: req.description,
description: req.description,
last_modified_at: Utc::now().naive_utc(),
last_modified_by: user.get_email(),
change_reason: req.change_reason,
};

let insert: Result<Function, diesel::result::Error> = diesel::insert_into(functions)
Expand Down Expand Up @@ -141,7 +142,7 @@ async fn update(
draft_runtime_version: req
.runtime_version
.unwrap_or(result.draft_runtime_version),
function_description: req.description.unwrap_or(result.function_description),
description: req.description.unwrap_or(result.description),
draft_edited_by: user.get_email(),
draft_edited_at: Utc::now().naive_utc(),
published_code: result.published_code,
Expand All @@ -150,6 +151,7 @@ async fn update(
published_runtime_version: result.published_runtime_version,
last_modified_at: Utc::now().naive_utc(),
last_modified_by: user.get_email(),
change_reason: req.change_reason,
};

let mut updated_function = diesel::update(functions)
Expand Down
2 changes: 2 additions & 0 deletions crates/context_aware_config/src/api/functions/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct UpdateFunctionRequest {
pub function: Option<String>,
pub runtime_version: Option<String>,
pub description: Option<String>,
pub change_reason: String,
}

#[derive(Debug, Deserialize)]
Expand All @@ -16,6 +17,7 @@ pub struct CreateFunctionRequest {
pub function: String,
pub runtime_version: String,
pub description: String,
pub change_reason: String,
}

#[derive(Debug, Deserialize, AsRef, Deref, DerefMut, Into)]
Expand Down
Loading

0 comments on commit a247081

Please sign in to comment.