mirror of
https://github.com/moghtech/komodo.git
synced 2026-10-29 08:04:54 +00:00
enable / disable alerters backend
This commit is contained in:
@@ -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}"
|
||||
))
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user