From 21f4b6eaaa0a1568763b2ecbf27ef9690ffcfe80 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Fri, 24 Dec 2021 13:33:21 +0300 Subject: [PATCH] need_learning on patch case --- .../analytic_service/analytic_service.rs | 52 ++++++++++--------- .../analytic_service/analytic_unit/types.rs | 41 +++++++++++++++ 2 files changed, 69 insertions(+), 24 deletions(-) diff --git a/server/src/services/analytic_service/analytic_service.rs b/server/src/services/analytic_service/analytic_service.rs index 1c482ba..ea3df6f 100644 --- a/server/src/services/analytic_service/analytic_service.rs +++ b/server/src/services/analytic_service/analytic_service.rs @@ -276,29 +276,46 @@ impl AnalyticService { fn patch_config(&mut self, patch: PatchConfig, tx: oneshot::Sender<()>) { let my_id = self.analytic_unit_service.get_config_id(&self.analytic_unit_config); + let patch_id = patch.get_type_id(); let same_type = my_id == patch_id; + + // TODO: need_learning and same_type logic overlaps, there is a way to optimise this + let need_learning = self.analytic_unit_config.patch_needs_learning(&patch); + if same_type { // TODO: check when learning should be started let new_conf = patch.get_new_config(); self.analytic_unit_config = new_conf.clone(); self.analytic_unit_service.update_config_by_id(&my_id, &new_conf).unwrap(); + if self.analytic_unit.is_some() { - tokio::spawn({ - let au = self.analytic_unit.clone(); - let cfg = self.analytic_unit_config.clone(); - async move { - au.unwrap().write().await.set_config(cfg); - match tx.send(()) { - Ok(_) => {} - Err(_e) => { - println!("Can`t send patch config notification"); - } + if need_learning { + self.consume_request(RequestType::RunLearning); + match tx.send(()) { + Ok(_) => {} + Err(_e) => { + println!("Can`t send patch config notification"); } } - }); + return; + } else { + tokio::spawn({ + let au = self.analytic_unit.clone(); + let cfg = self.analytic_unit_config.clone(); + async move { + au.unwrap().write().await.set_config(cfg); + match tx.send(()) { + Ok(_) => {} + Err(_e) => { + println!("Can`t send patch config notification"); + } + } + } + }); + } } else { // TODO: check if we need this else match tx.send(()) { @@ -309,7 +326,6 @@ impl AnalyticService { } } } else { - // TODO: extracdt from db let new_conf = self.analytic_unit_service.get_config_by_id(&patch_id).unwrap(); self.analytic_unit_config = new_conf.clone(); self.consume_request(RequestType::RunLearning); @@ -320,18 +336,6 @@ impl AnalyticService { } } } - - - // TODO: update analytic_unit config if some - // TODO: save updated - // TODO: run learning when different - // TODO: run learning when it's necessary - - - - - - } pub async fn serve(&mut self) { diff --git a/server/src/services/analytic_service/analytic_unit/types.rs b/server/src/services/analytic_service/analytic_unit/types.rs index 8b84a97..f42d40a 100644 --- a/server/src/services/analytic_service/analytic_unit/types.rs +++ b/server/src/services/analytic_service/analytic_unit/types.rs @@ -73,6 +73,47 @@ impl AnalyticUnitConfig { _ => panic!("bad id for getting get_default_by_id") } } + + pub fn patch_needs_learning(&self, patch: &PatchConfig) -> bool { + // TODO: maybe use type id's to optimise code + match patch { + PatchConfig::Pattern(tcfg) => match self.clone() { + AnalyticUnitConfig::Pattern(_) => { + return false; + } + _ => { + return true + } + }, + + PatchConfig::Anomaly(tcfg) => match self.clone() { + AnalyticUnitConfig::Anomaly(scfg) => { + if tcfg.is_some() { + let t = tcfg.as_ref().unwrap(); + let mut need_learning = t.seasonality != scfg.seasonality; + need_learning |= t.seasonality_iterations != scfg.seasonality_iterations; + return need_learning; + } else { + return false; + } + } + _ => { + return true; + } + }, + + PatchConfig::Threshold(tcfg) => match self.clone() { + AnalyticUnitConfig::Threshold(_) => { + return false; + } + _ => { + return true; + } + }, + } + } + + // TODO: maybe this method depricated // return true if need needs relearning pub fn patch(&self, patch: PatchConfig) -> (AnalyticUnitConfig, bool) { match patch {