Browse Source

Relearning after unit updating (#647)

pull/1/head
Evgeny Smyshlyaev 6 years ago committed by rozetko
parent
commit
64d2ec31d9
  1. 5
      server/src/controllers/analytics_controller.ts
  2. 3
      server/src/models/analytic_units/analytic_unit_model.ts
  3. 3
      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

5
server/src/controllers/analytics_controller.ts

@ -581,7 +581,7 @@ export async function getHSR(analyticUnit: AnalyticUnit.AnalyticUnit, from: numb
if(analyticUnit.detectorType !== AnalyticUnit.DetectorType.ANOMALY) { if(analyticUnit.detectorType !== AnalyticUnit.DetectorType.ANOMALY) {
return data; return data;
} else { }
let cache = await AnalyticUnitCache.findById(analyticUnit.id); let cache = await AnalyticUnitCache.findById(analyticUnit.id);
if( if(
cache === null || cache === null ||
@ -608,8 +608,7 @@ export async function getHSR(analyticUnit: AnalyticUnit.AnalyticUnit, from: numb
if(result.status !== AnalyticUnit.AnalyticUnitStatus.SUCCESS) { if(result.status !== AnalyticUnit.AnalyticUnitStatus.SUCCESS) {
throw new Error(`Data processing error: ${result.error}`); throw new Error(`Data processing error: ${result.error}`);
} }
return { values: result.payload.data, columns: data.columns } return { values: result.payload.data, columns: data.columns };
}
} catch (err) { } catch (err) {
const message = err.message || JSON.stringify(err); const message = err.message || JSON.stringify(err);
await AnalyticUnit.setStatus(analyticUnit.id, AnalyticUnit.AnalyticUnitStatus.FAILED, message); 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 { export abstract class AnalyticUnit {
public learningAfterUpdateRequired = false;
constructor( constructor(
public name: string, public name: string,
public grafanaUrl: string, public grafanaUrl: string,

3
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 { export class AnomalyAnalyticUnit extends AnalyticUnit {
public learningAfterUpdateRequired = true;
constructor( constructor(
name: string, name: string,
grafanaUrl: string, grafanaUrl: string,

3
server/src/models/analytic_units/threshold_analytic_unit_model.ts

@ -14,6 +14,9 @@ export enum Condition {
}; };
export class ThresholdAnalyticUnit extends AnalyticUnit { export class ThresholdAnalyticUnit extends AnalyticUnit {
public learningAfterUpdateRequired = true;
constructor( constructor(
name: string, name: string,
grafanaUrl: string, grafanaUrl: string,

2
server/src/models/analytic_units/utils.ts

@ -17,7 +17,7 @@ export function createAnalyticUnitFromObject(obj: any): AnalyticUnit {
case DetectorType.PATTERN: case DetectorType.PATTERN:
return PatternAnalyticUnit.fromObject(obj); return PatternAnalyticUnit.fromObject(obj);
case DetectorType.ANOMALY: case DetectorType.ANOMALY:
return AnomalyAnalyticUnit.fromObject(obj) return AnomalyAnalyticUnit.fromObject(obj);
case DetectorType.THRESHOLD: case DetectorType.THRESHOLD:
return ThresholdAnalyticUnit.fromObject(obj); 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) { async function updateUnit(ctx: Router.IRouterContext) {
const analyticUnit = ctx.request.body as AnalyticUnit.AnalyticUnit; const analyticUnitObj = ctx.request.body as AnalyticUnit.AnalyticUnit;
if(analyticUnit.id === undefined) { if(analyticUnitObj.id === undefined) {
throw new Error('Cannot update undefined id'); 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 = { ctx.response.body = {
code: 200, code: 200,
message: 'Success' message: 'Success'

Loading…
Cancel
Save