-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.ts
144 lines (134 loc) · 4.19 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
import {
FILTER_TYPES,
UNITS,
Detector,
UIFilter,
} from '../../../models/interfaces';
import { get, cloneDeep, isEmpty } from 'lodash';
import { DetectorDefinitionFormikValues } from '../../DefineDetector/models/interfaces';
import { INITIAL_DETECTOR_DEFINITION_VALUES } from '../../DefineDetector/utils/constants';
import {
formikToIndices,
formikToFilterQuery,
} from '../../ReviewAndCreate/utils/helpers';
import { DEFAULT_SHINGLE_SIZE } from '../../../utils/constants';
export function detectorDefinitionToFormik(
ad: Detector
): DetectorDefinitionFormikValues {
const initialValues = cloneDeep(INITIAL_DETECTOR_DEFINITION_VALUES);
if (isEmpty(ad)) return initialValues;
return {
...initialValues,
name: ad.name,
description: ad.description,
index: [{ label: ad.indices[0] }], // Currently we support only one index
resultIndex: ad.resultIndex,
filters: filtersToFormik(ad),
filterQuery: JSON.stringify(
get(ad, 'filterQuery', { match_all: {} }),
null,
4
),
timeField: ad.timeField,
interval: get(ad, 'detectionInterval.period.interval', 10),
windowDelay: get(ad, 'windowDelay.period.interval', 0),
resultIndexMinAge: get(ad, 'resultIndexMinAge', undefined),
resultIndexMinSize:get(ad, 'resultIndexMinSize', undefined),
resultIndexTtl: get(ad, 'resultIndexTtl', undefined),
};
}
export function filtersToFormik(detector: Detector): UIFilter[] {
// Detectors created or updated using the API will not have metadata - create a custom filter in this case.
const noMetadata =
get(detector, 'uiMetadata.filterType') === undefined &&
get(detector, 'uiMetadata.filters') === undefined;
const isOldDetector = get(detector, 'uiMetadata.filterType') !== undefined;
if (noMetadata) {
return [
{
filterType: FILTER_TYPES.CUSTOM,
query: JSON.stringify(
get(detector, 'filterQuery', { match_all: {} }),
null,
4
),
},
];
}
const curFilterType = get(detector, 'uiMetadata.filterType');
const curFilters = get(detector, 'uiMetadata.filters', []);
// If this is an old detector:
// If filters is empty: it means it was a custom filter. Convert to a single filter array with a
// query value equal to the existing filterQuery
// If a set of simple filters: modify them by injecting the filter type
// into each existing filter
if (isOldDetector) {
if (isEmpty(curFilters)) {
return [
{
filterType: FILTER_TYPES.CUSTOM,
query: JSON.stringify(
get(detector, 'filterQuery', { match_all: {} }),
null,
4
),
},
];
} else {
curFilters.forEach(
(filter: UIFilter) => (filter.filterType = curFilterType)
);
}
}
return curFilters;
}
export function formikToDetectorDefinition(
values: DetectorDefinitionFormikValues,
detector: Detector
): Detector {
let detectorBody = {
...detector,
name: values.name,
description: values.description,
indices: formikToIndices(values.index),
resultIndex: values.resultIndex,
filterQuery: formikToFilterQuery(values),
uiMetadata: {
features: get(detector, 'uiMetadata.features', {}),
filters: get(values, 'filters', []),
},
timeField: values.timeField,
detectionInterval: {
period: { interval: values.interval, unit: UNITS.MINUTES },
},
windowDelay: {
period: { interval: values.windowDelay, unit: UNITS.MINUTES },
},
resultIndexMinAge: values.resultIndexMinAge,
resultIndexMinSize: values.resultIndexMinSize,
resultIndexTtl: values.resultIndexTtl,
} as Detector;
return detectorBody;
}
export function clearModelConfiguration(ad: Detector): Detector {
return {
...ad,
featureAttributes: [],
uiMetadata: {
...ad.uiMetadata,
features: {},
},
categoryField: undefined,
shingleSize: DEFAULT_SHINGLE_SIZE,
};
}