You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.2 KiB
39 lines
1.2 KiB
5 years ago
|
import { DetectorType, ANALYTIC_UNIT_TYPES } from './types';
|
||
|
import { AnalyticUnit } from './analytic_unit_model';
|
||
|
import { PatternAnalyticUnit } from './pattern_analytic_unit_model';
|
||
|
import { ThresholdAnalyticUnit } from './threshold_analytic_unit_model';
|
||
|
|
||
|
import * as _ from 'lodash';
|
||
|
|
||
|
|
||
|
export function createAnalyticUnitFromObject(obj: any): AnalyticUnit {
|
||
|
if (obj === undefined) {
|
||
|
throw new Error('obj is undefined');
|
||
|
}
|
||
|
|
||
|
const detectorType: DetectorType = obj.detectorType;
|
||
|
switch (detectorType) {
|
||
|
case DetectorType.PATTERN:
|
||
|
return PatternAnalyticUnit.fromObject(obj);
|
||
|
case DetectorType.THRESHOLD:
|
||
|
return ThresholdAnalyticUnit.fromObject(obj);
|
||
|
|
||
|
default:
|
||
|
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;
|
||
|
}
|