Browse Source

basic hsr

pull/25/head
Alexey Velikiy 3 years ago
parent
commit
ea18309ff5
  1. 28
      server/src/api/analytics.rs
  2. 33
      server/src/api/segments.rs
  3. 26
      server/src/services/analytic_service/analytic_service.rs
  4. 2
      server/src/services/analytic_service/analytic_unit/anomaly_analytic_unit.rs
  5. 6
      server/src/services/analytic_service/analytic_unit/pattern_analytic_unit.rs
  6. 12
      server/src/services/analytic_service/analytic_unit/types.rs
  7. 2
      server/src/services/analytic_service/types.rs

28
server/src/api/analytics.rs

@ -11,6 +11,7 @@ pub mod filters {
.or(status(client.clone()))
.or(get_config(client.clone()))
.or(put_config(client.clone()))
.or(get_hsr(client.clone()))
// .or(list_train(client.clone()))
// .or(create(db.clone()))
// // .or(update(db.clone()))
@ -69,6 +70,17 @@ pub mod filters {
// .and_then(handlers::list_train)
// }
/// GET /analytics/hsr
pub fn get_hsr(
client: Client,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path!("analytics" / "hsr")
.and(warp::get())
.and(warp::query::<ListOptions>())
.and(with_client(client))
.and_then(handlers::get_hsr)
}
fn with_client(
client: Client,
) -> impl Filter<Extract = (Client,), Error = std::convert::Infallible> + Clone {
@ -79,7 +91,6 @@ pub mod filters {
mod handlers {
use hastic::services::analytic_service::analytic_unit::types::PatchConfig;
use serde_json::Value;
use super::models::{Client, ListOptions, Status};
use crate::api::{BadQuery, API};
@ -132,6 +143,20 @@ mod handlers {
}
}
pub async fn get_hsr(
lo: ListOptions,
client: Client,
) -> Result<impl warp::Reply, warp::Rejection> {
// println!("{:?}", patch);
match client.get_hsr(lo.from, lo.to).await {
Ok(hsr) => Ok(API::json(&hsr)),
Err(e) => {
println!("{:?}", e);
Err(warp::reject::custom(BadQuery))
}
}
}
// pub async fn list_train(client: Client) -> Result<impl warp::Reply, warp::Rejection> {
// match client.get_train().await {
// Ok(lt) => Ok(API::json(&lt)),
@ -149,7 +174,6 @@ mod models {
pub type Client = analytic_service::analytic_client::AnalyticClient;
// The query parameters for list_todos.
#[derive(Debug, Deserialize)]
pub struct ListOptions {
pub from: u64,

33
server/src/api/segments.rs

@ -6,56 +6,56 @@ pub mod filters {
/// The 4 REST API filters combined.
pub fn filters(
db: Srv,
srv: Srv,
ac: AnalyticClient,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
list(db.clone())
.or(create(db.clone(), ac.clone()))
list(srv.clone())
.or(create(srv.clone(), ac.clone()))
// .or(update(db.clone()))
.or(delete(db.clone(), ac.clone()))
.or(delete(srv.clone(), ac.clone()))
}
/// GET /segments?from=3&to=5
pub fn list(
db: Srv,
srv: Srv,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path!("segments")
.and(warp::get())
.and(warp::query::<ListOptions>())
.and(with_srv(db))
.and(with_srv(srv))
.and_then(handlers::list)
}
/// POST /segments with JSON body
pub fn create(
db: Srv,
srv: Srv,
ac: AnalyticClient,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path!("segments")
.and(warp::post())
.and(warp::body::json())
.and(with_srv(db))
.and(with_srv(srv))
.and(warp::any().map(move || ac.clone()))
.and_then(handlers::create)
}
/// POST /segments with JSON body
pub fn delete(
db: Srv,
srv: Srv,
ac: AnalyticClient,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path!("segments")
.and(warp::delete())
.and(warp::query::<ListOptions>())
.and(with_srv(db))
.and(with_srv(srv))
.and(warp::any().map(move || ac.clone()))
.and_then(handlers::delete)
}
fn with_srv(
db: Srv,
srv: Srv,
) -> impl Filter<Extract = (Srv,), Error = std::convert::Infallible> + Clone {
warp::any().map(move || db.clone())
warp::any().map(move || srv.clone())
}
}
@ -78,10 +78,10 @@ mod handlers {
pub async fn create(
segment: segments_service::Segment,
src: Srv,
srv: Srv,
ac: AnalyticClient,
) -> Result<impl warp::Reply, warp::Rejection> {
match src.insert_segment(&segment) {
match srv.insert_segment(&segment) {
Ok(segment) => {
ac.run_learning().await.unwrap();
Ok(API::json(&segment))
@ -96,10 +96,10 @@ mod handlers {
pub async fn delete(
opts: ListOptions,
db: Srv,
srv: Srv,
ac: AnalyticClient,
) -> Result<impl warp::Reply, warp::Rejection> {
match db.delete_segments_in_range(opts.from, opts.to) {
match srv.delete_segments_in_range(opts.from, opts.to) {
Ok(count) => {
ac.run_learning().await.unwrap();
Ok(API::json(&api::Message {
@ -118,7 +118,6 @@ mod models {
pub type Srv = segments_service::SegmentsService;
// The query parameters for list_todos.
#[derive(Debug, Deserialize)]
pub struct ListOptions {
pub from: u64,

26
server/src/services/analytic_service/analytic_service.rs

@ -19,7 +19,6 @@ use crate::services::analytic_service::analytic_unit::types::{AnalyticUnit, Lear
use anyhow;
use serde_json::Value;
use tokio::sync::{mpsc, oneshot, RwLock};
use chrono::Utc;
@ -91,10 +90,14 @@ impl AnalyticService {
let au = self.analytic_unit.as_ref().unwrap().clone();
async move {
match learning_waiter {
LearningWaiter::Detection(task) => AnalyticService::get_detections(task.sender, au, ms, task.from, task.to).await,
LearningWaiter::HSR(task) => AnalyticService::get_hsr(task.sender, au, ms, task.from, task.to).await,
LearningWaiter::Detection(task) => {
AnalyticService::get_detections(task.sender, au, ms, task.from, task.to)
.await
}
LearningWaiter::HSR(task) => {
AnalyticService::get_hsr(task.sender, au, ms, task.from, task.to).await
}
}
}
});
}
@ -178,8 +181,11 @@ impl AnalyticService {
// tx.send(()).unwrap();
}
RequestType::GetHSR(task) => {
// self.analytic_unit.
// TODO: implement
if self.analytic_unit.is_some() {
self.run_learning_waiter(LearningWaiter::HSR(task));
} else {
self.learning_waiters.push(LearningWaiter::HSR(task));
}
}
};
}
@ -346,7 +352,12 @@ impl AnalyticService {
from: u64,
to: u64,
) {
let hsr = analytic_unit.read().await.get_hsr(ms, from, to).await.unwrap();
let hsr = analytic_unit
.read()
.await
.get_hsr(ms, from, to)
.await
.unwrap();
match tx.send(Ok(hsr)) {
Ok(_) => {}
@ -354,6 +365,5 @@ impl AnalyticService {
println!("failed to send results");
}
}
}
}

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

@ -52,7 +52,6 @@ impl AnalyticUnit for AnomalyAnalyticUnit {
let ct = ts[0];
// TODO: implement
// TODO: decide what to do it from is Some() in the end
@ -65,7 +64,6 @@ impl AnalyticUnit for AnomalyAnalyticUnit {
from: u64,
to: u64,
) -> anyhow::Result<Vec<(u64, f64)>> {
// TODO: implement
let mr = ms.query(from, to, DETECTION_STEP).await.unwrap();

6
server/src/services/analytic_service/analytic_unit/pattern_analytic_unit.rs

@ -353,7 +353,11 @@ impl AnalyticUnit for PatternAnalyticUnit {
let fs = get_features(&vs);
let lk = lr.model.lock();
let p = lk.predict(Array::from_vec(fs.to_vec()));
if p { 1 } else { -1 }
if p {
1
} else {
-1
}
};
let score = positive_corr * self.config.correlation_score

12
server/src/services/analytic_service/analytic_unit/types.rs

@ -31,7 +31,10 @@ pub struct AnomalyConfig {
impl Default for AnomalyConfig {
fn default() -> Self {
AnomalyConfig { alpha: 0.5, confidence: 10.0 }
AnomalyConfig {
alpha: 0.5,
confidence: 10.0,
}
}
}
@ -128,7 +131,12 @@ pub trait AnalyticUnit {
) -> anyhow::Result<Vec<(u64, u64)>>;
fn set_config(&mut self, c: AnalyticUnitConfig);
async fn get_hsr(&self, ms: MetricService, from: u64, to: u64) -> anyhow::Result<Vec<(u64, f64)>> ;
async fn get_hsr(
&self,
ms: MetricService,
from: u64,
to: u64,
) -> anyhow::Result<Vec<(u64, f64)>>;
}
#[derive(Deserialize, Serialize, Debug)]

2
server/src/services/analytic_service/types.rs

@ -72,7 +72,7 @@ pub struct HSRTask {
#[derive(Debug)]
pub enum LearningWaiter {
Detection(DetectionTask),
HSR(HSRTask)
HSR(HSRTask),
}
#[derive(Debug)]

Loading…
Cancel
Save