From cfee2ff02fd4013a3637642a6a41085e8e75044a Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Mon, 8 Nov 2021 20:10:18 +0300 Subject: [PATCH 1/4] analyticTypes client begin --- client/src/components/ScatterPlot.vue | 74 +++++++++++++++++++++++++-- client/src/store/index.ts | 11 +++- client/src/store/types.ts | 4 ++ client/src/views/Home.vue | 21 ++++++-- 4 files changed, 100 insertions(+), 10 deletions(-) create mode 100644 client/src/store/types.ts diff --git a/client/src/components/ScatterPlot.vue b/client/src/components/ScatterPlot.vue index f121ed5..0eeab96 100644 --- a/client/src/components/ScatterPlot.vue +++ b/client/src/components/ScatterPlot.vue @@ -7,17 +7,85 @@ From 2ac6501ad86733208c9255342a32379394710230 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Mon, 8 Nov 2021 20:49:21 +0300 Subject: [PATCH 2/4] analytic unit begin --- server/src/services/analytic_service/analytic_unit/mod.rs | 3 +++ server/src/services/analytic_service/analytic_unit/types.rs | 3 +++ server/src/services/analytic_service/types.rs | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 server/src/services/analytic_service/analytic_unit/mod.rs create mode 100644 server/src/services/analytic_service/analytic_unit/types.rs diff --git a/server/src/services/analytic_service/analytic_unit/mod.rs b/server/src/services/analytic_service/analytic_unit/mod.rs new file mode 100644 index 0000000..2dc47f2 --- /dev/null +++ b/server/src/services/analytic_service/analytic_unit/mod.rs @@ -0,0 +1,3 @@ +trait AnalyticUnit where C: AnalyticUnitConfig { + +} \ No newline at end of file diff --git a/server/src/services/analytic_service/analytic_unit/types.rs b/server/src/services/analytic_service/analytic_unit/types.rs new file mode 100644 index 0000000..7aa5a98 --- /dev/null +++ b/server/src/services/analytic_service/analytic_unit/types.rs @@ -0,0 +1,3 @@ +pub enum AnalyticUnitConfig { + PatternDetector(PatternDetectorConfig) +} \ No newline at end of file diff --git a/server/src/services/analytic_service/types.rs b/server/src/services/analytic_service/types.rs index 0f0c8c5..d72f7d6 100644 --- a/server/src/services/analytic_service/types.rs +++ b/server/src/services/analytic_service/types.rs @@ -1,6 +1,6 @@ use crate::services::segments_service::Segment; -use super::pattern_detector::{self, LearningResults, PatternDetector}; +use super::pattern_detector::{self, LearningResults}; use anyhow::Result; use serde::Serialize; From d5d0c29dee5f8f502872734d04ed10a958236b75 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Mon, 8 Nov 2021 21:41:26 +0300 Subject: [PATCH 3/4] analytic unit config --- server/src/api/analytics.rs | 21 +++++++++++++++++++ .../analytic_service/analytic_client.rs | 9 ++++++++ .../analytic_service/analytic_service.rs | 13 +++++++++++- .../analytic_service/analytic_unit/mod.rs | 5 ++++- .../{ => analytic_unit}/pattern_detector.rs | 4 +++- .../analytic_service/analytic_unit/types.rs | 9 ++++++++ server/src/services/analytic_service/mod.rs | 2 +- server/src/services/analytic_service/types.rs | 4 +++- 8 files changed, 62 insertions(+), 5 deletions(-) rename server/src/services/analytic_service/{ => analytic_unit}/pattern_detector.rs (99%) diff --git a/server/src/api/analytics.rs b/server/src/api/analytics.rs index 8b156e0..0cbb229 100644 --- a/server/src/api/analytics.rs +++ b/server/src/api/analytics.rs @@ -9,6 +9,7 @@ pub mod filters { ) -> impl Filter + Clone { list(client.clone()) .or(status(client.clone())) + .or(get_config(client.clone())) .or(list_train(client.clone())) // .or(create(db.clone())) // // .or(update(db.clone())) @@ -36,6 +37,16 @@ pub mod filters { .and_then(handlers::status) } + /// GET /analytics/config + pub fn get_config( + client: Client, + ) -> impl Filter + Clone { + warp::path!("analytics" / "config") + .and(warp::get()) + .and(with_client(client)) + .and_then(handlers::config) + } + /// GET /analytics/model pub fn list_train( client: Client, @@ -82,6 +93,16 @@ mod handlers { } } + pub async fn config(client: Client) -> Result { + match client.get_config().await { + Ok(cf) => Ok(API::json(&cf)), + Err(e) => { + println!("{:?}", e); + Err(warp::reject::custom(BadQuery)) + } + } + } + pub async fn list_train(client: Client) -> Result { match client.get_train().await { Ok(lt) => Ok(API::json(<)), diff --git a/server/src/services/analytic_service/analytic_client.rs b/server/src/services/analytic_service/analytic_client.rs index 2276e53..c01c64a 100644 --- a/server/src/services/analytic_service/analytic_client.rs +++ b/server/src/services/analytic_service/analytic_client.rs @@ -3,6 +3,7 @@ use tokio::sync::oneshot; use crate::services::segments_service::Segment; +use super::analytic_unit::types::AnalyticUnitConfig; use super::types::DetectionTask; use super::types::LearningStatus; use super::types::LearningTrain; @@ -34,6 +35,14 @@ impl AnalyticClient { Ok(r) } + pub async fn get_config(&self) -> anyhow::Result { + let (tx, rx) = oneshot::channel(); + let req = AnalyticServiceMessage::Request(RequestType::GetConfig(tx)); + self.tx.send(req).await?; + let r = rx.await?; + Ok(r) + } + pub async fn get_train(&self) -> anyhow::Result { let (tx, rx) = oneshot::channel(); let req = AnalyticServiceMessage::Request(RequestType::GetLearningTrain(tx)); diff --git a/server/src/services/analytic_service/analytic_service.rs b/server/src/services/analytic_service/analytic_service.rs index a929f95..449b229 100644 --- a/server/src/services/analytic_service/analytic_service.rs +++ b/server/src/services/analytic_service/analytic_service.rs @@ -1,7 +1,8 @@ +use super::analytic_unit::types::{AnalyticUnitConfig, PatternDetectorConfig}; use super::types::{self, DetectionRunnerConfig, LearningTrain}; use super::{ analytic_client::AnalyticClient, - pattern_detector::{self, LearningResults, PatternDetector}, + analytic_unit::pattern_detector::{self, LearningResults, PatternDetector}, types::{AnalyticServiceMessage, DetectionTask, LearningStatus, RequestType, ResponseType}, }; @@ -51,6 +52,7 @@ pub struct AnalyticService { metric_service: MetricService, segments_service: SegmentsService, learning_results: Option, + analytic_unit_config: AnalyticUnitConfig, learning_status: LearningStatus, tx: mpsc::Sender, rx: mpsc::Receiver, @@ -78,8 +80,14 @@ impl AnalyticService { AnalyticService { metric_service, segments_service, + // TODO: get it from persistance learning_results: None, + analytic_unit_config: AnalyticUnitConfig::PatternDetector(PatternDetectorConfig { + correlation_score: 0.95, + model_score: 0.95 + }), + learning_status: LearningStatus::Initialization, tx, rx, @@ -180,6 +188,9 @@ impl AnalyticService { .unwrap(); } } + RequestType::GetConfig(tx) => { + tx.send(self.analytic_unit_config.clone()).unwrap(); + } }; } diff --git a/server/src/services/analytic_service/analytic_unit/mod.rs b/server/src/services/analytic_service/analytic_unit/mod.rs index 2dc47f2..a185aea 100644 --- a/server/src/services/analytic_service/analytic_unit/mod.rs +++ b/server/src/services/analytic_service/analytic_unit/mod.rs @@ -1,3 +1,6 @@ -trait AnalyticUnit where C: AnalyticUnitConfig { +pub mod pattern_detector; +pub mod types; + +trait AnalyticUnit { } \ No newline at end of file diff --git a/server/src/services/analytic_service/pattern_detector.rs b/server/src/services/analytic_service/analytic_unit/pattern_detector.rs similarity index 99% rename from server/src/services/analytic_service/pattern_detector.rs rename to server/src/services/analytic_service/analytic_unit/pattern_detector.rs index 62a5673..7663aa5 100644 --- a/server/src/services/analytic_service/pattern_detector.rs +++ b/server/src/services/analytic_service/analytic_unit/pattern_detector.rs @@ -11,7 +11,9 @@ use linfa_svm::{error::Result, Svm}; use ndarray::{Array, ArrayView, Axis}; -use super::types::LearningTrain; +use crate::services::analytic_service::types::LearningTrain; + + #[derive(Clone)] pub struct LearningResults { diff --git a/server/src/services/analytic_service/analytic_unit/types.rs b/server/src/services/analytic_service/analytic_unit/types.rs index 7aa5a98..d87f12f 100644 --- a/server/src/services/analytic_service/analytic_unit/types.rs +++ b/server/src/services/analytic_service/analytic_unit/types.rs @@ -1,3 +1,12 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct PatternDetectorConfig { + pub correlation_score: f32, + pub model_score: f32 +} + +#[derive(Debug, Serialize, Deserialize, Clone)] pub enum AnalyticUnitConfig { PatternDetector(PatternDetectorConfig) } \ No newline at end of file diff --git a/server/src/services/analytic_service/mod.rs b/server/src/services/analytic_service/mod.rs index a1100a7..fb15d78 100644 --- a/server/src/services/analytic_service/mod.rs +++ b/server/src/services/analytic_service/mod.rs @@ -1,5 +1,5 @@ mod analytic_service; -mod pattern_detector; +mod analytic_unit; pub mod types; pub mod analytic_client; diff --git a/server/src/services/analytic_service/types.rs b/server/src/services/analytic_service/types.rs index d72f7d6..7aa5669 100644 --- a/server/src/services/analytic_service/types.rs +++ b/server/src/services/analytic_service/types.rs @@ -1,6 +1,6 @@ use crate::services::segments_service::Segment; -use super::pattern_detector::{self, LearningResults}; +use super::analytic_unit::{pattern_detector::{self, LearningResults}, types::AnalyticUnitConfig}; use anyhow::Result; use serde::Serialize; @@ -15,6 +15,7 @@ pub enum LearningStatus { Ready, } +// TODO: move to analytic_unit config of pattern detector #[derive(Clone, Serialize, Debug)] pub struct LearningTrain { pub features: Vec, @@ -58,6 +59,7 @@ pub enum RequestType { RunLearning, RunDetection(DetectionTask), GetStatus(oneshot::Sender), + GetConfig(oneshot::Sender), GetLearningTrain(oneshot::Sender), } From 375848584d5a486e3f5666e0b5e0bfdd953e4008 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Mon, 8 Nov 2021 22:48:25 +0300 Subject: [PATCH 4/4] detector config on client begin --- client/src/services/analytics.service.ts | 6 ++++ client/src/store/index.ts | 40 ++++++++++++++++++------ client/src/store/types.ts | 4 --- client/src/types/analytic_units/index.ts | 14 ++++++--- client/src/views/Home.vue | 6 ++-- client/src/views/Model.vue | 1 - 6 files changed, 48 insertions(+), 23 deletions(-) delete mode 100644 client/src/store/types.ts diff --git a/client/src/services/analytics.service.ts b/client/src/services/analytics.service.ts index 51a8544..e8a9350 100644 --- a/client/src/services/analytics.service.ts +++ b/client/src/services/analytics.service.ts @@ -16,6 +16,12 @@ export async function getStatus(): Promise { return data.status; } +export async function getConfig(): Promise { + const uri = ANALYTICS_API_URL + `config`; + const res = await axios.get(uri); + return res['data'] as any; +} + export function getStatusGenerator(): AsyncIterableIterator { return getGenerator(100, getStatus); } diff --git a/client/src/store/index.ts b/client/src/store/index.ts index 98ca7b9..7e401cb 100644 --- a/client/src/store/index.ts +++ b/client/src/store/index.ts @@ -1,37 +1,43 @@ import { auth } from "./auth.module"; import { createStore } from 'vuex' -import { getStatusGenerator } from "@/services/analytics.service"; -import { AnalyticType } from './types' +import { getConfig, getStatusGenerator } from "@/services/analytics.service"; +import { DetectorConfig, DetectorType } from '@/types/analytic_units' // import { notify } from "@kyvg/vue3-notification"; const SET_ANALYTICS_STATUS = 'SET_ANALYTICS_STATUS'; -const SET_ANALYTICS_TYPE = 'SET_ANALYTICS_TYPE'; +const SET_DETECTOR_CONFIG = 'SET_DETECTOR_CONFIG'; const _SET_STATUS_GENERATOR = '_SET_STATUS_GENERATOR'; - type State = { analyticStatus: string, - analyticType?: AnalyticType, + detectorType?: DetectorType, + // TODO: maybe rename it + analyticUnitConfig?: DetectorConfig, _statusGenerator: AsyncIterableIterator } const store = createStore({ state: { analyticStatus: 'loading...', - analyticType: null, + detectorType: null, + analyticUnitConfig: null, _statusGenerator: null }, mutations: { [SET_ANALYTICS_STATUS](state, status: string) { state.analyticStatus = status; }, - [SET_ANALYTICS_TYPE](state, atype: AnalyticType) { - state.analyticType = atype; + [SET_DETECTOR_CONFIG](state, { detectorType, detectorConfig }) { + console.log(detectorType); + console.log(detectorConfig); + + state.detectorType = detectorType; + state.analyticUnitConfig = detectorConfig; }, [_SET_STATUS_GENERATOR](state, generator: AsyncIterableIterator) { - this._statusGenerator = generator; + state._statusGenerator = generator; } }, actions: { @@ -43,11 +49,25 @@ const store = createStore({ if(state._statusGenerator !== null) { return; } + const g = getStatusGenerator(); - for await (const data of g) { + commit(_SET_STATUS_GENERATOR, g); + for await (const data of state._statusGenerator) { + const st = data.toLocaleLowerCase(); + if(state.analyticStatus.toLocaleLowerCase() != 'ready' && st == 'ready') { + this.dispatch('fetchConfig'); + // TODO: update segments from here + } // this.status = data.toLowerCase(); commit(SET_ANALYTICS_STATUS, data); } + }, + async fetchConfig({commit}) { + const c = await getConfig(); + // TODO: move this logic to service getConfig() + const detectorType = c['PatternDetector'] !== undefined ? DetectorType.PATTERN : DetectorType.ANOMALY; + const detectorConfig = c['PatternDetector'] as DetectorConfig; + commit(SET_DETECTOR_CONFIG, { detectorType, detectorConfig }); } }, modules: { diff --git a/client/src/store/types.ts b/client/src/store/types.ts deleted file mode 100644 index d3a5390..0000000 --- a/client/src/store/types.ts +++ /dev/null @@ -1,4 +0,0 @@ -export enum AnalyticType { - PATTERN_DETECTOR = 'pattern detector', - ANOMALY_DETECTOR = 'anomaly detector', -} diff --git a/client/src/types/analytic_units/index.ts b/client/src/types/analytic_units/index.ts index acece0f..3fd2297 100644 --- a/client/src/types/analytic_units/index.ts +++ b/client/src/types/analytic_units/index.ts @@ -13,14 +13,18 @@ export enum DetectorType { PATTERN = 'pattern', THRESHOLD = 'threshold', ANOMALY = 'anomaly' -}; +} + +export type DetectorConfig = { + correlation_score: number, model_score: number +} export enum LabelingMode { LABELING = 'LABELING', UNLABELING = 'UNLABELING', DELETING = 'DELETING', NOT_IN_LABELING_MODE = 'NOT_IN_LABELING_MODE' -}; +} export type AnalyticSegmentPair = { analyticUnit: AnalyticUnit, segment: AnalyticSegment }; export type AnalyticSegmentsSearcher = (point: number, rangeDist: number) => AnalyticSegmentPair[]; @@ -56,8 +60,8 @@ const LABELING_MODES = []; export class AnalyticUnit { private _labelingMode: LabelingMode = LabelingMode.LABELING; - private _selected: boolean = false; - private _saving: boolean = false; + private _selected = false; + private _saving = false; private _segmentSet = new SegmentArray(); private _detectionSpans: DetectionSpan[]; private _inspect = false; @@ -143,7 +147,7 @@ export class AnalyticUnit { } removeSegmentsInRange(from: number, to: number): AnalyticSegment[] { - let deletedSegments = this._segmentSet.removeInRange(from, to); + const deletedSegments = this._segmentSet.removeInRange(from, to); return deletedSegments; } diff --git a/client/src/views/Home.vue b/client/src/views/Home.vue index 89c9120..9ea1e2e 100644 --- a/client/src/views/Home.vue +++ b/client/src/views/Home.vue @@ -21,7 +21,7 @@ import { defineComponent } from 'vue'; import Graph from '@/components/Graph.vue'; import AnalyticStatus from '@/components/AnlyticsStatus.vue'; -import { AnalyticType } from '@/store/types'; +import { DetectorType } from '@/types/analytic_units'; export default defineComponent({ @@ -37,12 +37,12 @@ export default defineComponent({ }, data: function () { return { - analyticTypes: [AnalyticType.PATTERN_DETECTOR, AnalyticType.ANOMALY_DETECTOR], + analyticTypes: [DetectorType.PATTERN, DetectorType.ANOMALY], } }, computed: { analyticType() { - return this.$store.state.analyticType; + return this.$store.state.detectorType; } } }); diff --git a/client/src/views/Model.vue b/client/src/views/Model.vue index 2ceefba..dd904d0 100644 --- a/client/src/views/Model.vue +++ b/client/src/views/Model.vue @@ -2,7 +2,6 @@
Vue logo -