From 81ac206b5c672918989056d1400f5ae4b0d1f6e4 Mon Sep 17 00:00:00 2001 From: Howard Smith Date: Sat, 14 Dec 2024 22:02:50 +0000 Subject: [PATCH] feat: Infra changes to support `AlertConfig` domain model including names of monitors --- api/src/infrastructure/models/alert_config.rs | 45 ++++++++++++++----- api/tests/alert_config_repo_test.rs | 42 +++++++++++------ api/tests/common/seeds.rs | 5 +++ 3 files changed, 66 insertions(+), 26 deletions(-) diff --git a/api/src/infrastructure/models/alert_config.rs b/api/src/infrastructure/models/alert_config.rs index b2e3866c..7e3b537a 100644 --- a/api/src/infrastructure/models/alert_config.rs +++ b/api/src/infrastructure/models/alert_config.rs @@ -1,7 +1,9 @@ use diesel::prelude::*; use uuid::Uuid; -use crate::domain::models::alert_config::{AlertConfig, AlertType, SlackAlertConfig}; +use crate::domain::models::alert_config::{ + AlertConfig, AlertType, AppliedMonitor, SlackAlertConfig, +}; use crate::errors::Error; use crate::infrastructure::db_schema::{alert_config, monitor_alert_config, slack_alert_config}; @@ -31,6 +33,7 @@ pub struct AlertConfigData { pub struct MonitorAlertConfigData { pub alert_config_id: Uuid, pub monitor_id: Uuid, + pub monitor_name: String, } // Only used for writing data. @@ -89,9 +92,12 @@ impl AlertConfigData { } _ => return Err(Error::InvalidAlertConfig("Unknown alert type".to_owned())), }, - monitor_ids: monitor_alert_configs + monitors: monitor_alert_configs .iter() - .map(|mac| mac.monitor_id) + .map(|mac| AppliedMonitor { + monitor_id: mac.monitor_id, + name: mac.monitor_name.clone(), + }) .collect(), }) } @@ -127,11 +133,12 @@ impl NewAlertConfigData { on_error: alert_config.on_error, }, alert_config - .monitor_ids + .monitors .iter() - .map(|monitor_id| MonitorAlertConfigData { + .map(|monitor| MonitorAlertConfigData { alert_config_id: alert_config.alert_config_id, - monitor_id: *monitor_id, + monitor_id: monitor.monitor_id, + monitor_name: monitor.name.clone(), }) .collect(), specific_data, @@ -154,10 +161,12 @@ mod tests { MonitorAlertConfigData { alert_config_id: gen_uuid("41ebffb4-a188-48e9-8ec1-61380085cde3"), monitor_id: gen_uuid("02d9fd94-48dc-40e5-b2fa-fa6b66eaf2ca"), + monitor_name: "foo-monitor".to_string(), }, MonitorAlertConfigData { alert_config_id: gen_uuid("41ebffb4-a188-48e9-8ec1-61380085cde3"), monitor_id: gen_uuid("70810d10-1d86-4bde-b29d-b1f490528675"), + monitor_name: "bar-monitor".to_string(), }, ]; let alert_config_data = AlertConfigData { @@ -191,10 +200,16 @@ mod tests { }) ); assert_eq!( - alert_config.monitor_ids, + alert_config.monitors, vec![ - gen_uuid("02d9fd94-48dc-40e5-b2fa-fa6b66eaf2ca"), - gen_uuid("70810d10-1d86-4bde-b29d-b1f490528675") + AppliedMonitor { + monitor_id: gen_uuid("02d9fd94-48dc-40e5-b2fa-fa6b66eaf2ca"), + name: "foo-monitor".to_string() + }, + AppliedMonitor { + monitor_id: gen_uuid("70810d10-1d86-4bde-b29d-b1f490528675"), + name: "bar-monitor".to_string() + } ] ); } @@ -258,9 +273,15 @@ mod tests { channel: "test-channel".to_owned(), token: "test-token".to_owned(), }), - monitor_ids: vec![ - gen_uuid("02d9fd94-48dc-40e5-b2fa-fa6b66eaf2ca"), - gen_uuid("70810d10-1d86-4bde-b29d-b1f490528675"), + monitors: vec![ + AppliedMonitor { + monitor_id: gen_uuid("02d9fd94-48dc-40e5-b2fa-fa6b66eaf2ca"), + name: "foo-monitor".to_string(), + }, + AppliedMonitor { + monitor_id: gen_uuid("70810d10-1d86-4bde-b29d-b1f490528675"), + name: "bar-monitor".to_string(), + }, ], }; diff --git a/api/tests/alert_config_repo_test.rs b/api/tests/alert_config_repo_test.rs index 58fd9a05..762da380 100644 --- a/api/tests/alert_config_repo_test.rs +++ b/api/tests/alert_config_repo_test.rs @@ -5,7 +5,9 @@ use rstest::rstest; use test_utils::gen_uuid; -use cron_mon_api::domain::models::alert_config::{AlertConfig, AlertType, SlackAlertConfig}; +use cron_mon_api::domain::models::alert_config::{ + AlertConfig, AlertType, AppliedMonitor, SlackAlertConfig, +}; use cron_mon_api::errors::Error; use cron_mon_api::infrastructure::models::alert_config::NewAlertConfigData; use cron_mon_api::infrastructure::repositories::alert_config_repo::AlertConfigRepository; @@ -124,8 +126,11 @@ async fn test_get(#[future] infrastructure: Infrastructure) { let alert_config = should_be_some.unwrap(); assert_eq!(alert_config.name, "Test Slack alert (for lates)"); assert_eq!( - alert_config.monitor_ids, - vec![gen_uuid("c1bf0515-df39-448b-aa95-686360a33b36")] + alert_config.monitors, + vec![AppliedMonitor { + monitor_id: gen_uuid("c1bf0515-df39-448b-aa95-686360a33b36"), + name: "db-backup.py".to_string() + }] ) } @@ -144,9 +149,15 @@ async fn test_save_with_new(#[future] infrastructure: Infrastructure) { "#new-channel".to_string(), "new-test-token".to_string(), ); - new_alert_config.monitor_ids = vec![ - gen_uuid("c1bf0515-df39-448b-aa95-686360a33b36"), - gen_uuid("f0b291fe-bd41-4787-bc2d-1329903f7a6a"), + new_alert_config.monitors = vec![ + AppliedMonitor { + monitor_id: gen_uuid("c1bf0515-df39-448b-aa95-686360a33b36"), + name: "db-backup.py".to_string(), + }, + AppliedMonitor { + monitor_id: gen_uuid("f0b291fe-bd41-4787-bc2d-1329903f7a6a"), + name: "generate-orders.sh".to_string(), + }, ]; repo.save(&new_alert_config).await.unwrap(); @@ -166,10 +177,7 @@ async fn test_save_with_new(#[future] infrastructure: Infrastructure) { assert_eq!(new_alert_config.on_late, read_new_alert_config.on_late); assert_eq!(new_alert_config.on_error, read_new_alert_config.on_error); assert_eq!(new_alert_config.type_, read_new_alert_config.type_); - assert_eq!( - new_alert_config.monitor_ids, - read_new_alert_config.monitor_ids - ); + assert_eq!(new_alert_config.monitors, read_new_alert_config.monitors); } #[rstest] @@ -187,9 +195,15 @@ async fn test_save_with_existing(#[future] infrastructure: Infrastructure) { alert_config.active = false; alert_config.on_late = false; alert_config.on_error = false; - alert_config.monitor_ids = vec![ - gen_uuid("f0b291fe-bd41-4787-bc2d-1329903f7a6a"), - gen_uuid("cc6cf74e-b25d-4c8c-94a6-914e3f139c14"), + alert_config.monitors = vec![ + AppliedMonitor { + monitor_id: gen_uuid("f0b291fe-bd41-4787-bc2d-1329903f7a6a"), + name: "generate-orders.sh".to_string(), + }, + AppliedMonitor { + monitor_id: gen_uuid("cc6cf74e-b25d-4c8c-94a6-914e3f139c14"), + name: "data-snapshot.py".to_string(), + }, ]; repo.save(&alert_config).await.unwrap(); @@ -206,7 +220,7 @@ async fn test_save_with_existing(#[future] infrastructure: Infrastructure) { assert_eq!(alert_config.on_late, read_alert_config.on_late); assert_eq!(alert_config.on_error, read_alert_config.on_error); assert_eq!(alert_config.type_, read_alert_config.type_); - assert_eq!(alert_config.monitor_ids, read_alert_config.monitor_ids); + assert_eq!(alert_config.monitors, read_alert_config.monitors); } #[rstest] diff --git a/api/tests/common/seeds.rs b/api/tests/common/seeds.rs index 155c8a54..1913ab7a 100644 --- a/api/tests/common/seeds.rs +++ b/api/tests/common/seeds.rs @@ -193,22 +193,27 @@ pub fn alert_config_seeds() -> ( MonitorAlertConfigData { monitor_id: gen_uuid("c1bf0515-df39-448b-aa95-686360a33b36"), alert_config_id: gen_uuid("fadd7266-648b-4102-8f85-c768655f4297"), + monitor_name: "db-backup.py".to_string(), }, MonitorAlertConfigData { monitor_id: gen_uuid("f0b291fe-bd41-4787-bc2d-1329903f7a6a"), alert_config_id: gen_uuid("3ba21f52-32c9-41dc-924d-d18d4fc0e81c"), + monitor_name: "generate-orders.sh".to_string(), }, MonitorAlertConfigData { monitor_id: gen_uuid("c1bf0515-df39-448b-aa95-686360a33b36"), alert_config_id: gen_uuid("3ba21f52-32c9-41dc-924d-d18d4fc0e81c"), + monitor_name: "db-backup.py".to_string(), }, MonitorAlertConfigData { monitor_id: gen_uuid("cc6cf74e-b25d-4c8c-94a6-914e3f139c14"), alert_config_id: gen_uuid("3ba21f52-32c9-41dc-924d-d18d4fc0e81c"), + monitor_name: "data-snapshot.py".to_string(), }, MonitorAlertConfigData { monitor_id: gen_uuid("c1bf0515-df39-448b-aa95-686360a33b36"), alert_config_id: gen_uuid("8d307d12-4696-4801-bfb6-628f8f640864"), + monitor_name: "db-backup.py".to_string(), }, ], )