diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index 65fb3db..a1f5b76 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -581,35 +581,34 @@ export async function getHSR(analyticUnit: AnalyticUnit.AnalyticUnit, from: numb if(analyticUnit.detectorType !== AnalyticUnit.DetectorType.ANOMALY) { return data; - } else { - let cache = await AnalyticUnitCache.findById(analyticUnit.id); - if( - cache === null || - cache.data.alpha !== (analyticUnit as AnalyticUnit.AnomalyAnalyticUnit).alpha || - cache.data.confidence !== (analyticUnit as AnalyticUnit.AnomalyAnalyticUnit).confidence - ) { - await runLearning(analyticUnit.id, from, to); - cache = await AnalyticUnitCache.findById(analyticUnit.id); - } - - cache = cache.data; - - const analyticUnitType = analyticUnit.type; - const detector = analyticUnit.detectorType; - const payload = { - data: data.values, - analyticUnitType, - detector, - cache - }; - - const processingTask = new AnalyticsTask(analyticUnit.id, AnalyticsTaskType.PROCESS, payload); - const result = await runTask(processingTask); - if(result.status !== AnalyticUnit.AnalyticUnitStatus.SUCCESS) { - throw new Error(`Data processing error: ${result.error}`); - } - return { values: result.payload.data, columns: data.columns } } + let cache = await AnalyticUnitCache.findById(analyticUnit.id); + if( + cache === null || + cache.data.alpha !== (analyticUnit as AnalyticUnit.AnomalyAnalyticUnit).alpha || + cache.data.confidence !== (analyticUnit as AnalyticUnit.AnomalyAnalyticUnit).confidence + ) { + await runLearning(analyticUnit.id, from, to); + cache = await AnalyticUnitCache.findById(analyticUnit.id); + } + + cache = cache.data; + + const analyticUnitType = analyticUnit.type; + const detector = analyticUnit.detectorType; + const payload = { + data: data.values, + analyticUnitType, + detector, + cache + }; + + const processingTask = new AnalyticsTask(analyticUnit.id, AnalyticsTaskType.PROCESS, payload); + const result = await runTask(processingTask); + if(result.status !== AnalyticUnit.AnalyticUnitStatus.SUCCESS) { + throw new Error(`Data processing error: ${result.error}`); + } + return { values: result.payload.data, columns: data.columns }; } catch (err) { const message = err.message || JSON.stringify(err); await AnalyticUnit.setStatus(analyticUnit.id, AnalyticUnit.AnalyticUnitStatus.FAILED, message); diff --git a/server/src/models/analytic_units/analytic_unit_model.ts b/server/src/models/analytic_units/analytic_unit_model.ts index 325dfc8..8b92936 100644 --- a/server/src/models/analytic_units/analytic_unit_model.ts +++ b/server/src/models/analytic_units/analytic_unit_model.ts @@ -4,6 +4,9 @@ import { Metric } from 'grafana-datasource-kit'; export abstract class AnalyticUnit { + + public learningAfterUpdateRequired = false; + constructor( public name: string, public grafanaUrl: string, diff --git a/server/src/models/analytic_units/anomaly_analytic_unit_model.ts b/server/src/models/analytic_units/anomaly_analytic_unit_model.ts index 0d66c2c..e6ad720 100644 --- a/server/src/models/analytic_units/anomaly_analytic_unit_model.ts +++ b/server/src/models/analytic_units/anomaly_analytic_unit_model.ts @@ -5,6 +5,9 @@ import { Metric } from 'grafana-datasource-kit'; export class AnomalyAnalyticUnit extends AnalyticUnit { + + public learningAfterUpdateRequired = true; + constructor( name: string, grafanaUrl: string, @@ -83,4 +86,4 @@ export class AnomalyAnalyticUnit extends AnalyticUnit { obj.visible ); } -} \ No newline at end of file +} diff --git a/server/src/models/analytic_units/threshold_analytic_unit_model.ts b/server/src/models/analytic_units/threshold_analytic_unit_model.ts index f88e986..f81b389 100644 --- a/server/src/models/analytic_units/threshold_analytic_unit_model.ts +++ b/server/src/models/analytic_units/threshold_analytic_unit_model.ts @@ -14,6 +14,9 @@ export enum Condition { }; export class ThresholdAnalyticUnit extends AnalyticUnit { + + public learningAfterUpdateRequired = true; + constructor( name: string, grafanaUrl: string, diff --git a/server/src/models/analytic_units/utils.ts b/server/src/models/analytic_units/utils.ts index 5194862..7b867ba 100644 --- a/server/src/models/analytic_units/utils.ts +++ b/server/src/models/analytic_units/utils.ts @@ -17,7 +17,7 @@ export function createAnalyticUnitFromObject(obj: any): AnalyticUnit { case DetectorType.PATTERN: return PatternAnalyticUnit.fromObject(obj); case DetectorType.ANOMALY: - return AnomalyAnalyticUnit.fromObject(obj) + return AnomalyAnalyticUnit.fromObject(obj); case DetectorType.THRESHOLD: return ThresholdAnalyticUnit.fromObject(obj); diff --git a/server/src/routes/analytic_units_router.ts b/server/src/routes/analytic_units_router.ts index a2c7062..60ca392 100644 --- a/server/src/routes/analytic_units_router.ts +++ b/server/src/routes/analytic_units_router.ts @@ -56,12 +56,18 @@ async function createUnit(ctx: Router.IRouterContext) { } async function updateUnit(ctx: Router.IRouterContext) { - const analyticUnit = ctx.request.body as AnalyticUnit.AnalyticUnit; - if(analyticUnit.id === undefined) { + const analyticUnitObj = ctx.request.body as AnalyticUnit.AnalyticUnit; + if(analyticUnitObj.id === undefined) { throw new Error('Cannot update undefined id'); } - await AnalyticUnit.update(analyticUnit.id, analyticUnit); + const updatedObj = await AnalyticUnit.update(analyticUnitObj.id, analyticUnitObj); + const analyticUnit = AnalyticUnit.createAnalyticUnitFromObject(updatedObj); + + if(analyticUnit.learningAfterUpdateRequired) { + await AnalyticsController.runLearning(analyticUnitObj.id); + } + ctx.response.body = { code: 200, message: 'Success'