Browse Source

detectorType field is missing in old analytic units #632 (#633)

* Add migration

* Use detectorType instead of getDetectorByType

* Move getDetectorByType to migration
pull/1/head
rozetko 5 years ago committed by GitHub
parent
commit
367f165595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      server/src/controllers/analytics_controller.ts
  2. 35
      server/src/migrations.ts
  3. 4
      server/src/models/analytic_units/index.ts
  4. 14
      server/src/models/analytic_units/utils.ts
  5. 2
      server/src/services/alert_service.ts
  6. 2
      server/src/services/data_puller.ts

4
server/src/controllers/analytics_controller.ts

@ -212,7 +212,7 @@ export async function runLearning(id: AnalyticUnit.AnalyticUnitId, from?: number
// TODO: create an analytics serialization method in AnalyticUnit
let analyticUnitType = analyticUnit.type;
let detector = AnalyticUnit.getDetectorByType(analyticUnitType);
const detector = analyticUnit.detectorType;
let taskPayload: any = { detector, analyticUnitType, cache: oldCache };
if(detector === AnalyticUnit.DetectorType.PATTERN) {
@ -267,7 +267,7 @@ export async function runDetect(id: AnalyticUnit.AnalyticUnitId, from?: number,
let unit = await AnalyticUnit.findById(id);
previousLastDetectionTime = unit.lastDetectionTime;
let analyticUnitType = unit.type;
let detector = AnalyticUnit.getDetectorByType(analyticUnitType);
const detector = unit.detectorType;
let range: TimeRange;
if(from !== undefined && to !== undefined) {

35
server/src/migrations.ts

@ -3,6 +3,8 @@
- create migration function
- add it with the next revision number to REVISIONS Map
It will be automatically applied if actual DB revision < added revision
Note: do not import code from other modules here because it can be changed
*/
import { Collection, makeDBQ } from './services/data_service';
@ -24,7 +26,8 @@ type DbMeta = {
const REVISIONS = new Map<number, Function>([
[1, convertPanelUrlToPanelId],
[2, convertUnderscoreToCamelCase],
[3, integrateThresholdsIntoAnalyticUnits]
[3, integrateThresholdsIntoAnalyticUnits],
[4, addDetectorTypes]
]);
export async function applyDBMigrations() {
@ -115,3 +118,33 @@ async function integrateThresholdsIntoAnalyticUnits() {
await Promise.all(promises);
await thresholdsDB.removeMany({});
}
async function addDetectorTypes() {
const analyticUnits = await analyticUnitsDB.findMany({ detectorType: { $exists: false } });
const promises = analyticUnits.map(analyticUnit =>
analyticUnitsDB.updateOne(analyticUnit._id, { detectorType: getDetectorByType(analyticUnit.type) })
);
await Promise.all(promises);
}
function getDetectorByType(analyticUnitType: string): string {
const analyticUnitTypesMapping = {
pattern: [ 'GENERAL', 'PEAK', 'TROUGH', 'JUMP', 'DROP' ],
anomaly: [ 'ANOMALY' ],
threshold: [ 'THRESHOLD' ]
};
let detector;
_.forOwn(analyticUnitTypesMapping, (types, detectorType) => {
if(types.includes(analyticUnitType)) {
detector = detectorType;
}
});
if(detector === undefined) {
throw new Error(`Can't find detector for analytic unit of type "${analyticUnitType}"`);
}
return detector;
}

4
server/src/models/analytic_units/index.ts

@ -1,4 +1,4 @@
import { createAnalyticUnitFromObject, getDetectorByType } from './utils';
import { createAnalyticUnitFromObject } from './utils';
import { AnalyticUnitId, AnalyticUnitStatus, DetectorType, ANALYTIC_UNIT_TYPES } from './types';
import { AnalyticUnit } from './analytic_unit_model';
import { PatternAnalyticUnit } from './pattern_analytic_unit_model';
@ -19,7 +19,7 @@ import {
export {
AnalyticUnit, PatternAnalyticUnit, ThresholdAnalyticUnit,
AnalyticUnitId, AnalyticUnitStatus, DetectorType, ANALYTIC_UNIT_TYPES,
createAnalyticUnitFromObject, getDetectorByType,
createAnalyticUnitFromObject,
findById, findMany,
create, remove, update,
setStatus, setDetectionTime, setAlert, setMetric

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

@ -25,17 +25,3 @@ export function createAnalyticUnitFromObject(obj: any): AnalyticUnit {
throw new Error(`Can't create analytic unit with type "${detectorType}"`);
}
}
export function getDetectorByType(analyticUnitType: string): DetectorType {
let detector;
_.forOwn(ANALYTIC_UNIT_TYPES, (types, detectorType) => {
if(_.find(types, { value: analyticUnitType }) !== undefined) {
detector = detectorType;
}
});
if(detector === undefined) {
throw new Error(`Can't find detector for analytic unit of type "${analyticUnitType}"`);
}
return detector;
}

2
server/src/services/alert_service.ts

@ -131,7 +131,7 @@ export class AlertService {
}
public addAnalyticUnit(analyticUnit: AnalyticUnit.AnalyticUnit) {
let detector = AnalyticUnit.getDetectorByType(analyticUnit.type);
const detector = analyticUnit.detectorType;
let alertsType = {};
alertsType[AnalyticUnit.DetectorType.THRESHOLD] = ThresholdAlert;

2
server/src/services/data_puller.ts

@ -135,7 +135,7 @@ export class DataPuller {
if(cache !== null) {
cache = cache.data
}
const detector = AnalyticUnit.getDetectorByType(analyticUnit.type);
const detector = analyticUnit.detectorType;
let payload = {
data: payloadValues,
from: this._unitTimes[analyticUnit.id],

Loading…
Cancel
Save