From 5c093c81abcc350e48fecdf54f3b8a599dfdbd0a Mon Sep 17 00:00:00 2001 From: mbecker20 Date: Sun, 7 Apr 2024 21:23:51 -0700 Subject: [PATCH] enable / disable alerters backend --- bin/core/src/helpers/alert.rs | 20 +++++++++++++++----- bin/core/src/helpers/resource.rs | 10 ++++++---- client/core/rs/src/entities/alerter.rs | 7 +++++++ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/bin/core/src/helpers/alert.rs b/bin/core/src/helpers/alert.rs index 6ebfba982..157ab7067 100644 --- a/bin/core/src/helpers/alert.rs +++ b/bin/core/src/helpers/alert.rs @@ -6,7 +6,7 @@ use monitor_client::entities::{ deployment::DockerContainerState, server::stats::{SeverityLevel, SystemProcess}, }; -use mungos::find::find_collect; +use mungos::{find::find_collect, mongodb::bson::doc}; use reqwest::StatusCode; use slack::types::Block; @@ -17,8 +17,12 @@ pub async fn send_alerts(alerts: &[Alert]) { return; } - let alerters = - find_collect(&db_client().await.alerters, None, None).await; + let alerters = find_collect( + &db_client().await.alerters, + doc! { "config.params.enabled": true }, + None, + ) + .await; if let Err(e) = alerters { error!("ERROR sending alerts | failed to get alerters from db | {e:#?}"); @@ -40,12 +44,18 @@ async fn send_alert(alerters: &[Alerter], alert: &Alert) { let handles = alerters.iter().map(|alerter| async { match &alerter.config { - AlerterConfig::Slack(SlackAlerterConfig { url }) => { + AlerterConfig::Slack(SlackAlerterConfig { url, enabled }) => { + if !enabled { + return Ok(()); + } send_slack_alert(url, alert) .await .context("failed to send slack alert") } - AlerterConfig::Custom(CustomAlerterConfig { url }) => { + AlerterConfig::Custom(CustomAlerterConfig { url, enabled }) => { + if !enabled { + return Ok(()); + } send_custom_alert(url, alert).await.context(format!( "failed to send alert to custom alerter at {url}" )) diff --git a/bin/core/src/helpers/resource.rs b/bin/core/src/helpers/resource.rs index c0dca7117..1f382b37c 100644 --- a/bin/core/src/helpers/resource.rs +++ b/bin/core/src/helpers/resource.rs @@ -28,7 +28,8 @@ use monitor_client::{ ProcedureListItemInfo, ProcedureQuerySpecifics, }, repo::{ - Repo, RepoConfig, RepoInfo, RepoListItem, RepoListItemInfo, RepoQuerySpecifics + Repo, RepoConfig, RepoInfo, RepoListItem, RepoListItemInfo, + RepoQuerySpecifics, }, resource::{AddFilters, Resource, ResourceQuery}, server::{ @@ -519,9 +520,9 @@ impl StateResource for Alerter { async fn to_list_item( alerter: Alerter, ) -> anyhow::Result { - let alerter_type = match alerter.config { - AlerterConfig::Custom(_) => "custom", - AlerterConfig::Slack(_) => "slack", + let (alerter_type, enabled) = match alerter.config { + AlerterConfig::Custom(config) => ("custom", config.enabled), + AlerterConfig::Slack(config) => ("slack", config.enabled), }; Ok(AlerterListItem { name: alerter.name, @@ -534,6 +535,7 @@ impl StateResource for Alerter { info: AlerterListItemInfo { alerter_type: alerter_type.to_string(), is_default: alerter.info.is_default, + enabled, }, }) } diff --git a/client/core/rs/src/entities/alerter.rs b/client/core/rs/src/entities/alerter.rs index 5db4474c8..b0b6bdc1e 100644 --- a/client/core/rs/src/entities/alerter.rs +++ b/client/core/rs/src/entities/alerter.rs @@ -20,6 +20,7 @@ pub type AlerterListItem = ResourceListItem; #[typeshare] #[derive(Serialize, Deserialize, Debug, Clone)] pub struct AlerterListItemInfo { + pub enabled: bool, pub is_default: bool, pub alerter_type: String, } @@ -96,6 +97,7 @@ impl AlerterConfig { AlerterConfig::Custom(config) => { let config = CustomAlerterConfig { url: partial.url.unwrap_or(config.url), + enabled: partial.enabled.unwrap_or(config.enabled), }; AlerterConfig::Custom(config) } @@ -105,6 +107,7 @@ impl AlerterConfig { AlerterConfig::Slack(config) => { let config = SlackAlerterConfig { url: partial.url.unwrap_or(config.url), + enabled: partial.enabled.unwrap_or(config.enabled), }; AlerterConfig::Slack(config) } @@ -122,6 +125,8 @@ impl AlerterConfig { pub struct CustomAlerterConfig { #[partial_default(String::from("http://localhost:7000"))] pub url: String, + #[serde(default)] + pub enabled: bool, } #[typeshare] @@ -134,6 +139,8 @@ pub struct SlackAlerterConfig { "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX", ))] pub url: String, + #[serde(default)] + pub enabled: bool, } #[typeshare]