Browse Source

Relearning after unit updating (#647)

pull/1/head
Evgeny Smyshlyaev 6 years ago committed by rozetko
parent
commit
64d2ec31d9
  1. 55
      server/src/controllers/analytics_controller.ts
  2. 3
      server/src/models/analytic_units/analytic_unit_model.ts
  3. 5
      server/src/models/analytic_units/anomaly_analytic_unit_model.ts
  4. 3
      server/src/models/analytic_units/threshold_analytic_unit_model.ts
  5. 2
      server/src/models/analytic_units/utils.ts
  6. 12
      server/src/routes/analytic_units_router.ts

55
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);

3
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,

5
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
);
}
}
}

3
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,

2
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);

12
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'

Loading…
Cancel
Save