From ff385a30a9bd32405682dfcd8aeccfa301f2194f Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Sat, 20 Nov 2021 04:02:32 +0300 Subject: [PATCH] resolve_alerting in config --- server/Cargo.toml | 3 -- server/src/config.rs | 41 +++++++++++++++---- .../analytic_unit/anomaly_analytic_unit.rs | 3 ++ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/server/Cargo.toml b/server/Cargo.toml index 26c037f..b9af5d9 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -28,6 +28,3 @@ ndarray = "0.15.3" bincode = "1.3.3" async-trait = "0.1.51" rustfft = "6.0.1" - -# TODO: remove this from prod -# plotlib = "0.5.1" diff --git a/server/src/config.rs b/server/src/config.rs index 466d790..6d52633 100644 --- a/server/src/config.rs +++ b/server/src/config.rs @@ -23,7 +23,7 @@ pub struct Config { pub alerting: Option, } -fn resolve_datasource(config: &mut config::Config) -> anyhow::Result { +fn resolve_datasource(config: &config::Config) -> anyhow::Result { if config.get::("prometheus.url").is_ok() { return Ok(DatasourceConfig::Prometheus(PrometheusConfig { url: config.get("prometheus.url")?, @@ -43,6 +43,36 @@ fn resolve_datasource(config: &mut config::Config) -> anyhow::Result anyhow::Result> { + if config.get::("alerting.type").is_err() { + return Ok(None); + } + + if config.get::("alerting.endpoint").is_err() { + return Err(anyhow::format_err!("missing endpoint param in alerting")); + } + if config.get::("alerting.interval").is_err() { + return Err(anyhow::format_err!("missing interval param in alerting")); + } + if config.get::("alerting.interval").is_err() { + return Err(anyhow::format_err!("alerting interval should be a positive integer number")); + } + let analytic_type = config.get::("alerting.type").unwrap(); + if analytic_type != "webhook" { + return Err(anyhow::format_err!("unknown alerting typy {}", analytic_type)); + } + + let endpoint = config.get::("alerting.endpoint").unwrap(); + let interval = config.get::("alerting.interval").unwrap(); + return Ok(Some(AlertingConfig { + alerting_type: AlertingType::Webhook(WebhookAlertingConfig{ + endpoint + }), + interval + })) + +} + // TODO: use actual config and env variables impl Config { pub fn new() -> anyhow::Result { @@ -54,6 +84,7 @@ impl Config { if std::path::Path::new("config.toml").exists() { config.merge(config::File::with_name("config")).unwrap(); } + config .merge(config::Environment::with_prefix("HASTIC")) .unwrap(); @@ -62,15 +93,11 @@ 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()); - // } Ok(Config { port: config.get::("port").unwrap(), - datasource_config: resolve_datasource(&mut config)?, - alerting: None, + datasource_config: resolve_datasource(&config)?, + alerting: resolve_alerting(&config)? }) } } diff --git a/server/src/services/analytic_service/analytic_unit/anomaly_analytic_unit.rs b/server/src/services/analytic_service/analytic_unit/anomaly_analytic_unit.rs index 49017ee..c6e737b 100644 --- a/server/src/services/analytic_service/analytic_unit/anomaly_analytic_unit.rs +++ b/server/src/services/analytic_service/analytic_unit/anomaly_analytic_unit.rs @@ -79,6 +79,9 @@ impl AnalyticUnit for AnomalyAnalyticUnit { } } async fn learn(&mut self, _ms: MetricService, _ss: SegmentsService) -> LearningResult { + // TODO: build SARIMA model based on seasonality + // TODO: don't count NaNs in model + // TODO: update model to work online return LearningResult::Finished; } async fn detect(