Browse Source

AnalyticUnitService begin

detection_runner_updade
Alexey Velikiy 2 years ago
parent
commit
bf0a684e5c
  1. 4
      server/src/services/analytic_service/analytic_service.rs
  2. 2
      server/src/services/analytic_service/analytic_unit/anomaly_analytic_unit.rs
  3. 16
      server/src/services/analytic_service/detection_runner.rs
  4. 32
      server/src/services/analytic_unit_service.rs
  5. 1
      server/src/services/segments_service.rs

4
server/src/services/analytic_service/analytic_service.rs

@ -114,7 +114,6 @@ impl AnalyticService {
}
}
// TODO: make
fn run_detection_runner(&mut self, from: u64) {
// TODO: handle case or make it impossible to run_detection_runner second time
@ -136,8 +135,7 @@ impl AnalyticService {
self.detection_runner = Some(dr);
self.detection_runner.as_mut().unwrap().run(from);
// TODO: create DetectionRunnerConfig from alerting
// TODO: rerun detection runner on analytic unit change
// TODO: rerun detection runner on analytic unit change (by setting analytic unit)
// if self.runner_handler.is_some() {
// self.runner_handler.as_mut().unwrap().abort();
// }

2
server/src/services/analytic_service/analytic_unit/anomaly_analytic_unit.rs

@ -46,7 +46,7 @@ impl SARIMA {
pub fn learn(&mut self, ts: &Vec<(u64, f64)>) -> anyhow::Result<()> {
// TODO: don't count NaNs in model
// TODO: add exponental smooting to model
// TODO: add exponental smooting to the model
// TODO: trend detection
if ts.len() < 2 {

16
server/src/services/analytic_service/detection_runner.rs

@ -1,7 +1,3 @@
use crate::services::analytic_service::analytic_unit::types::AnalyticUnit;
use std::sync::Arc;
use chrono::Utc;
use tokio::sync::{mpsc, RwLock};
@ -52,12 +48,11 @@ impl DetectionRunner {
ResponseType::DetectionRunnerStarted(from),
)))
.await
{
Ok(_) => {}
Err(_e) => println!("Fail to send detection runner started notification"),
}
{
Ok(_) => {}
Err(_e) => println!("Fail to send detection runner started notification"),
}
loop {
// TODO: run detection periodically
sleep(Duration::from_secs(cfg.interval)).await;
@ -71,5 +66,8 @@ impl DetectionRunner {
// self.analytic_unit = analytic_unit;
// // TODO: stop running_handler
// // TODO: rerun detection with new anomaly units
// if self.runner_handler.is_some() {
// self.runner_handler.as_mut().unwrap().abort();
// }
// }
}

32
server/src/services/analytic_unit_service.rs

@ -0,0 +1,32 @@
use crate::utils::get_random_str;
use rusqlite::{params, Connection, Row};
pub struct AnalyticUnitService {
// TODO: resolve by setting id for 3 types
// TODO: create database
// TODO: update detection
}
impl AnalyticUnitService {
pub fn new() -> anyhow::Result<AnalyticUnitService> {
// TODO: remove repetitoin with segment_service
std::fs::create_dir_all("./data").unwrap();
let conn = Connection::open("./data/analytic_units.db")?;
// TODO: add learning results field
conn.execute(
"CREATE TABLE IF NOT EXISTS analytic_unit (
id TEXT PRIMARY KEY,
last_detection INTEGER NOT NULL,
)",
[],
)?;
Ok(AnalyticUnitService {
connection: Arc::new(Mutex::new(conn)),
})
}
}

1
server/src/services/segments_service.rs

@ -68,6 +68,7 @@ impl SegmentsService {
// TODO: move it to data service
std::fs::create_dir_all("./data").unwrap();
// TODO: add unilytic_unit id as a new field
let conn = Connection::open("./data/segments.db")?;
conn.execute(
"CREATE TABLE IF NOT EXISTS segment (

Loading…
Cancel
Save