Skip to content

Commit ea8e0ea

Browse files
enable mutation tools (#1836)
* enable mutation tools * deepsource fix
1 parent be51cd6 commit ea8e0ea

5 files changed

Lines changed: 443 additions & 209 deletions

File tree

‎src/handlers/http/alerts.rs‎

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -387,33 +387,39 @@ pub async fn delete(req: HttpRequest, alert_id: Path<Ulid>) -> Result<impl Respo
387387
let session_key = extract_session_key_from_req(&req)?;
388388
let alert_id = alert_id.into_inner();
389389
let tenant_id = get_tenant_id_from_request(&req);
390+
delete_internal(&session_key, alert_id, &tenant_id).await
391+
}
392+
393+
/// Shared alert deletion implementation for HTTP handlers and in-process callers.
394+
pub async fn delete_internal(
395+
session_key: &SessionKey,
396+
alert_id: Ulid,
397+
tenant_id: &Option<String>,
398+
) -> Result<String, AlertError> {
390399
let guard = ALERTS.write().await;
391400
let alerts = if let Some(alerts) = guard.as_ref() {
392401
alerts
393402
} else {
394403
return Err(AlertError::CustomError("No AlertManager set".into()));
395404
};
396405

397-
let alert = alerts.get_alert_by_id(alert_id, &tenant_id).await?;
406+
let alert = alerts.get_alert_by_id(alert_id, tenant_id).await?;
398407

399408
// validate that the user has access to the tables mentioned in the query
400-
user_auth_for_alert_config(&session_key, &alert.to_alert_config()).await?;
409+
user_auth_for_alert_config(session_key, &alert.to_alert_config()).await?;
401410

402-
PARSEABLE
403-
.metastore
404-
.delete_alert(&*alert, &tenant_id)
405-
.await?;
411+
PARSEABLE.metastore.delete_alert(&*alert, tenant_id).await?;
406412

407413
// delete the associated alert state
408414
let state_to_delete =
409415
AlertStateEntry::new(alert_id, AlertState::NotTriggered, tenant_id.clone()); // state doesn't matter for deletion
410416
PARSEABLE
411417
.metastore
412-
.delete_alert_state(&state_to_delete as &dyn MetastoreObject, &tenant_id)
418+
.delete_alert_state(&state_to_delete as &dyn MetastoreObject, tenant_id)
413419
.await?;
414420

415421
// delete from memory
416-
alerts.delete(alert_id, &tenant_id).await?;
422+
alerts.delete(alert_id, tenant_id).await?;
417423

418424
// delete the scheduled task
419425
alerts.delete_task(alert_id).await?;
@@ -585,6 +591,18 @@ pub async fn modify_alert(
585591
let session_key = extract_session_key_from_req(&req)?;
586592
let alert_id = alert_id.into_inner();
587593
let tenant_id = get_tenant_id_from_request(&req);
594+
Ok(web::Json(
595+
modify_alert_internal(&session_key, alert_id, alert_request, &tenant_id).await?,
596+
))
597+
}
598+
599+
/// Shared alert update implementation for HTTP handlers and in-process callers.
600+
pub async fn modify_alert_internal(
601+
session_key: &SessionKey,
602+
alert_id: Ulid,
603+
alert_request: AlertRequest,
604+
tenant_id: &Option<String>,
605+
) -> Result<AlertConfigResponse, AlertError> {
588606
// Get alerts manager reference without holding the global lock
589607
let alerts = {
590608
let guard = ALERTS.read().await;
@@ -596,15 +614,15 @@ pub async fn modify_alert(
596614
};
597615

598616
// Validate and prepare the new alert
599-
let alert = alerts.get_alert_by_id(alert_id, &tenant_id).await?;
600-
user_auth_for_alert_config(&session_key, &alert.to_alert_config()).await?;
617+
let alert = alerts.get_alert_by_id(alert_id, tenant_id).await?;
618+
user_auth_for_alert_config(session_key, &alert.to_alert_config()).await?;
601619

602620
let mut new_config = alert_request.into(tenant_id.clone()).await?;
603621
if &new_config.alert_type != alert.get_alert_type() {
604622
return Err(AlertError::InvalidAlertModifyRequest);
605623
}
606624

607-
user_auth_for_alert_config(&session_key, &new_config).await?;
625+
user_auth_for_alert_config(session_key, &new_config).await?;
608626

609627
// Calculate notification config
610628
let eval_freq = new_config.get_eval_frequency();
@@ -639,27 +657,26 @@ pub async fn modify_alert(
639657
}
640658
};
641659

642-
new_alert.validate(&session_key).await?;
660+
new_alert.validate(session_key).await?;
643661

644662
// Perform I/O operations
645663
PARSEABLE
646664
.metastore
647-
.put_alert(&new_alert.to_alert_config(), &tenant_id)
665+
.put_alert(&new_alert.to_alert_config(), tenant_id)
648666
.await?;
649667

650668
let is_disabled = new_alert.get_state().eq(&AlertState::Disabled);
651669
// Now perform the atomic operations
652670
alerts.delete_task(alert_id).await?;
653-
alerts.delete(alert_id, &tenant_id).await?;
671+
alerts.delete(alert_id, tenant_id).await?;
654672
alerts.update(&*new_alert).await;
655673

656674
// only restart the task if the state was not set to disabled
657675
if !is_disabled {
658676
alerts.start_task(new_alert.clone_box()).await?;
659677
}
660678

661-
let config = new_alert.to_alert_config().to_response();
662-
Ok(web::Json(config))
679+
Ok(new_alert.to_alert_config().to_response())
663680
}
664681

665682
// PUT /alerts/{alert_id}/evaluate_alert

‎src/handlers/http/targets.rs‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,23 @@ pub async fn get_internal(
136136
pub async fn update(
137137
req: HttpRequest,
138138
target_id: Path<Ulid>,
139-
Json(mut target): Json<Target>,
139+
Json(target): Json<Target>,
140140
) -> Result<impl Responder, AlertError> {
141141
let target_id = target_id.into_inner();
142142
let tenant_id = get_tenant_id_from_request(&req);
143+
Ok(web::Json(
144+
update_internal(target_id, target, &tenant_id).await?,
145+
))
146+
}
147+
148+
/// Shared target update implementation for HTTP handlers and in-process callers.
149+
pub async fn update_internal(
150+
target_id: Ulid,
151+
mut target: Target,
152+
tenant_id: &Option<String>,
153+
) -> Result<serde_json::Value, AlertError> {
143154
// if target_id does not exist, error
144-
let old_target = TARGETS.get_target_by_id(&target_id, &tenant_id).await?;
155+
let old_target = TARGETS.get_target_by_id(&target_id, tenant_id).await?;
145156

146157
// do not allow modifying name
147158
if old_target.name != target.name {
@@ -152,22 +163,29 @@ pub async fn update(
152163

153164
// esnure that the supplied target id is assigned to the target config
154165
target.id = target_id;
155-
target.tenant = tenant_id;
166+
target.tenant.clone_from(tenant_id);
156167
target.validate_outbound_policy().await?;
157168
// should check for duplicacy and liveness (??)
158169
// add to the map
159170
TARGETS.update(target.clone()).await?;
160171

161-
Ok(web::Json(target.mask()))
172+
Ok(serde_json::to_value(target.mask())?)
162173
}
163174

164175
// DELETE /targets/{target_id}
165176
pub async fn delete(req: HttpRequest, target_id: Path<Ulid>) -> Result<impl Responder, AlertError> {
166177
let target_id = target_id.into_inner();
167178
let tenant_id = get_tenant_id_from_request(&req);
168-
let target = TARGETS.delete(&target_id, &tenant_id).await?;
179+
Ok(web::Json(delete_internal(target_id, &tenant_id).await?))
180+
}
169181

170-
Ok(web::Json(target.mask()))
182+
/// Shared target deletion implementation for HTTP handlers and in-process callers.
183+
pub async fn delete_internal(
184+
target_id: Ulid,
185+
tenant_id: &Option<String>,
186+
) -> Result<serde_json::Value, AlertError> {
187+
let target = TARGETS.delete(&target_id, tenant_id).await?;
188+
Ok(serde_json::to_value(target.mask())?)
171189
}
172190

173191
#[cfg(test)]

0 commit comments

Comments
 (0)