diff --git a/server/config.example.toml b/server/config.example.toml index d442724..9a0df60 100644 --- a/server/config.example.toml +++ b/server/config.example.toml @@ -16,6 +16,8 @@ query = "rate(go_memstats_alloc_bytes_total[5m])" # |> yield(name: "mean") # """ -[webhook] -endpoint = "http://localhost:8086" +[alerting] +type = "webhook" +interval = 10 # in seconds +endpoint = "http://localhost:9092" diff --git a/server/src/config.rs b/server/src/config.rs index da97c5b..ed5c936 100644 --- a/server/src/config.rs +++ b/server/src/config.rs @@ -1,20 +1,39 @@ use subbeat::types::{DatasourceConfig, InfluxConfig, PrometheusConfig}; + +#[derive(Clone)] +pub struct WebhookAlertingConfig { + endpoint: String +} + +#[derive(Clone)] +pub enum AlertingType { + Webhook(WebhookAlertingConfig) +} + +#[derive(Clone)] +pub struct AlertingConfig { + alerting_type: AlertingType, + interval: u64 // interval in seconds +} + + +#[derive(Clone)] pub struct Config { pub port: u16, pub datasource_config: DatasourceConfig, - pub endpoint: Option, + pub alerting: Option, } -impl Clone for Config { - fn clone(&self) -> Self { - return Config { - port: self.port, - datasource_config: self.datasource_config.clone(), - endpoint: self.endpoint.clone(), - }; - } -} +// impl Clone for Config { +// fn clone(&self) -> Self { +// return Config { +// port: self.port, +// datasource_config: self.datasource_config.clone(), +// alerting: self.alerting.clone() +// }; +// } +// } fn resolve_datasource(config: &mut config::Config) -> anyhow::Result { if config.get::("prometheus.url").is_ok() { @@ -39,6 +58,10 @@ fn resolve_datasource(config: &mut config::Config) -> anyhow::Result anyhow::Result { + + // TODO: parse alerting config + // TODO: throw error on bad config + let mut config = config::Config::default(); if std::path::Path::new("config.toml").exists() { @@ -52,15 +75,15 @@ impl Config { config.set("port", "8000").unwrap(); } - let mut endpoint = None; - if config.get::("webhook.endpoint").is_ok() { - endpoint = Some(config.get("webhook.endpoint").unwrap()); - } + // let mut endpoint = None; + // if config.get::("webhook.endpoint").is_ok() { + // endpoint = Some(config.get("webhook.endpoint").unwrap()); + // } Ok(Config { port: config.get::("port").unwrap(), datasource_config: resolve_datasource(&mut config)?, - endpoint, + alerting: None, }) } } diff --git a/server/src/main.rs b/server/src/main.rs index b710c88..6864f11 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -15,7 +15,7 @@ async fn main() -> anyhow::Result<()> { let mut analytic_service = analytic_service::AnalyticService::new( metric_service.clone(), segments_service.clone(), - config.endpoint, + config.alerting ); let api = api::API::new( diff --git a/server/src/services/analytic_service/analytic_service.rs b/server/src/services/analytic_service/analytic_service.rs index acd2607..3f05369 100644 --- a/server/src/services/analytic_service/analytic_service.rs +++ b/server/src/services/analytic_service/analytic_service.rs @@ -8,6 +8,7 @@ use super::{ types::{AnalyticServiceMessage, DetectionTask, LearningStatus, RequestType, ResponseType}, }; +use crate::config::AlertingConfig; use crate::services::analytic_service::analytic_unit::resolve; use crate::services::{ metric_service::MetricService, @@ -48,7 +49,7 @@ impl AnalyticService { pub fn new( metric_service: MetricService, segments_service: segments_service::SegmentsService, - endpoint: Option, + alerting: Option, ) -> AnalyticService { let (tx, rx) = mpsc::channel::(32);