enable / disable alerters backend

This commit is contained in:
mbecker20
2024-04-07 21:23:51 -07:00
parent e73146533a
commit 5c093c81ab
3 changed files with 28 additions and 9 deletions
+15 -5
View File
@@ -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}"
))
+6 -4
View File
@@ -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<AlerterListItem> {
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,
},
})
}
+7
View File
@@ -20,6 +20,7 @@ pub type AlerterListItem = ResourceListItem<AlerterListItemInfo>;
#[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]