Skip to content

Commit ff00f37

Browse files
fix get alert
1 parent 2ab5cd2 commit ff00f37

2 files changed

Lines changed: 73 additions & 10 deletions

File tree

src/alerts/alert_enums.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,68 @@ impl Display for WhereConfigOperator {
231231
}
232232
}
233233

234+
#[cfg(test)]
235+
mod tests {
236+
use super::AlertQueryType;
237+
238+
#[test]
239+
fn alert_query_type_deserializes_supported_modes() {
240+
assert_eq!(
241+
serde_json::from_str::<AlertQueryType>("\"builder\"").unwrap(),
242+
AlertQueryType::Builder
243+
);
244+
assert_eq!(
245+
serde_json::from_str::<AlertQueryType>("\"code\"").unwrap(),
246+
AlertQueryType::Code
247+
);
248+
assert_eq!(
249+
serde_json::from_str::<AlertQueryType>("\"promql\"").unwrap(),
250+
AlertQueryType::Promql
251+
);
252+
}
253+
254+
#[test]
255+
fn alert_query_type_accepts_legacy_sql_as_code() {
256+
assert_eq!(
257+
serde_json::from_str::<AlertQueryType>("\"sql\"").unwrap(),
258+
AlertQueryType::Code
259+
);
260+
}
261+
262+
#[test]
263+
fn alert_query_type_rejects_unknown_mode() {
264+
assert!(serde_json::from_str::<AlertQueryType>("\"rawSql\"").is_err());
265+
}
266+
267+
#[test]
268+
fn alert_request_deserializes_promql_query_type() {
269+
let request: crate::alerts::alert_structs::AlertRequest = serde_json::from_value(
270+
serde_json::json!({
271+
"severity": "high",
272+
"title": "Test alert",
273+
"alertType": "threshold",
274+
"queryType": "promql",
275+
"query": "sum({\"k8s.pod.cpu.usage\"}) by (\"k8s.namespace.name\")",
276+
"thresholdConfig": {"operator": ">", "value": -1.0},
277+
"evalConfig": {
278+
"rollingWindow": {
279+
"evalStart": "10 minutes",
280+
"evalEnd": "now",
281+
"evalFrequency": 10
282+
}
283+
},
284+
"targets": [],
285+
"notificationConfig": {"interval": 1},
286+
"datasets": ["azure-prod-cluster-metrics"]
287+
}),
288+
)
289+
.unwrap();
290+
291+
assert_eq!(request.query_type, AlertQueryType::Promql);
292+
assert_eq!(request.datasets, vec!["azure-prod-cluster-metrics"]);
293+
}
294+
}
295+
234296
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
235297
#[serde(rename_all = "camelCase")]
236298
pub enum AggregateFunction {

src/handlers/http/alerts.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::{collections::HashMap, str::FromStr};
2020

2121
use crate::{
2222
alerts::{
23-
ALERTS, AlertError, AlertState, Severity,
23+
ALERTS, AlertError, AlertState, Severity, user_auth_for_alert_config,
2424
alert_enums::{AlertType, NotificationState},
2525
alert_structs::{AlertConfig, AlertRequest, AlertStateEntry, NotificationStateRequest},
2626
alert_traits::AlertTrait,
@@ -29,7 +29,7 @@ use crate::{
2929
},
3030
metastore::metastore_traits::MetastoreObject,
3131
parseable::PARSEABLE,
32-
utils::{actix::extract_session_key_from_req, get_tenant_id_from_request, user_auth_for_query},
32+
utils::{actix::extract_session_key_from_req, get_tenant_id_from_request},
3333
};
3434
use actix_web::{
3535
HttpRequest, Responder,
@@ -343,7 +343,7 @@ pub async fn get(req: HttpRequest, alert_id: Path<Ulid>) -> Result<impl Responde
343343

344344
let alert = alerts.get_alert_by_id(alert_id, &tenant_id).await?;
345345
// validate that the user has access to the tables mentioned in the query
346-
user_auth_for_query(&session_key, alert.get_query()).await?;
346+
user_auth_for_alert_config(&session_key, &alert.to_alert_config()).await?;
347347

348348
Ok(web::Json(alert.to_alert_config().to_response()))
349349
}
@@ -364,7 +364,7 @@ pub async fn delete(req: HttpRequest, alert_id: Path<Ulid>) -> Result<impl Respo
364364
let alert = alerts.get_alert_by_id(alert_id, &tenant_id).await?;
365365

366366
// validate that the user has access to the tables mentioned in the query
367-
user_auth_for_query(&session_key, alert.get_query()).await?;
367+
user_auth_for_alert_config(&session_key, &alert.to_alert_config()).await?;
368368

369369
PARSEABLE
370370
.metastore
@@ -436,7 +436,7 @@ pub async fn update_notification_state(
436436
// check if alert id exists in map
437437
let alert = alerts.get_alert_by_id(alert_id, &tenant_id).await?;
438438
// validate that the user has access to the tables mentioned in the query
439-
user_auth_for_query(&session_key, alert.get_query()).await?;
439+
user_auth_for_alert_config(&session_key, &alert.to_alert_config()).await?;
440440

441441
alerts
442442
.update_notification_state(alert_id, new_notification_state, &tenant_id)
@@ -466,7 +466,7 @@ pub async fn disable_alert(
466466
// check if alert id exists in map
467467
let alert = alerts.get_alert_by_id(alert_id, &tenant_id).await?;
468468
// validate that the user has access to the tables mentioned in the query
469-
user_auth_for_query(&session_key, alert.get_query()).await?;
469+
user_auth_for_alert_config(&session_key, &alert.to_alert_config()).await?;
470470

471471
alerts
472472
.update_state(alert_id, AlertState::Disabled, Some("".into()), &tenant_id)
@@ -504,7 +504,7 @@ pub async fn enable_alert(
504504
}
505505

506506
// validate that the user has access to the tables mentioned in the query
507-
user_auth_for_query(&session_key, alert.get_query()).await?;
507+
user_auth_for_alert_config(&session_key, &alert.to_alert_config()).await?;
508508

509509
alerts
510510
.update_state(
@@ -542,14 +542,14 @@ pub async fn modify_alert(
542542

543543
// Validate and prepare the new alert
544544
let alert = alerts.get_alert_by_id(alert_id, &tenant_id).await?;
545-
user_auth_for_query(&session_key, alert.get_query()).await?;
545+
user_auth_for_alert_config(&session_key, &alert.to_alert_config()).await?;
546546

547547
let mut new_config = alert_request.into(tenant_id.clone()).await?;
548548
if &new_config.alert_type != alert.get_alert_type() {
549549
return Err(AlertError::InvalidAlertModifyRequest);
550550
}
551551

552-
user_auth_for_query(&session_key, &new_config.query).await?;
552+
user_auth_for_alert_config(&session_key, &new_config).await?;
553553

554554
// Calculate notification config
555555
let eval_freq = new_config.get_eval_frequency();
@@ -568,6 +568,7 @@ pub async fn modify_alert(
568568
old_config.eval_config = new_config.eval_config;
569569
old_config.notification_config = new_config.notification_config;
570570
old_config.query = new_config.query;
571+
old_config.query_type = new_config.query_type;
571572
old_config.severity = new_config.severity;
572573
old_config.tags = new_config.tags;
573574
old_config.targets = new_config.targets;
@@ -623,7 +624,7 @@ pub async fn evaluate_alert(
623624

624625
let alert = alerts.get_alert_by_id(alert_id, &tenant_id).await?;
625626

626-
user_auth_for_query(&session_key, alert.get_query()).await?;
627+
user_auth_for_alert_config(&session_key, &alert.to_alert_config()).await?;
627628

628629
let config = alert.to_alert_config().to_response();
629630

0 commit comments

Comments
 (0)