Skip to content

Commit

Permalink
feat: Infra changes to support AlertConfig domain model including n…
Browse files Browse the repository at this point in the history
…ames of monitors
  • Loading branch information
howamith committed Dec 14, 2024
1 parent e2fca59 commit 81ac206
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 26 deletions.
45 changes: 33 additions & 12 deletions api/src/infrastructure/models/alert_config.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(),
})
}
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
}
]
);
}
Expand Down Expand Up @@ -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(),
},
],
};

Expand Down
42 changes: 28 additions & 14 deletions api/tests/alert_config_repo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
}]
)
}

Expand All @@ -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();
Expand All @@ -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]
Expand All @@ -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();
Expand All @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions api/tests/common/seeds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
],
)
Expand Down

0 comments on commit 81ac206

Please sign in to comment.