Skip to content

Commit ccd4853

Browse files
committed
make custom result index lifecycle management in AD optional
Signed-off-by: Jackie Han <jkhanjob@gmail.com>
1 parent 75bad74 commit ccd4853

File tree

7 files changed

+90
-32
lines changed

7 files changed

+90
-32
lines changed

public/pages/DefineDetector/components/CustomResultIndex/CustomResultIndex.tsx

+62-14
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,48 @@ import {
2323
EuiIcon,
2424
EuiFieldNumber,
2525
} from '@elastic/eui';
26-
import { Field, FieldProps } from 'formik';
27-
import React, { useState } from 'react';
26+
import { Field, FieldProps, FormikProps, useFormikContext } from 'formik';
27+
import React, { useEffect, useState } from 'react';
2828
import ContentPanel from '../../../../components/ContentPanel/ContentPanel';
2929
import { CUSTOM_AD_RESULT_INDEX_PREFIX } from '../../../../../server/utils/constants';
3030
import { BASE_DOCS_LINK } from '../../../../utils/constants';
3131
import {
3232
isInvalid,
3333
getError,
3434
validateCustomResultIndex,
35-
validatePositiveInteger,
35+
validateEmptyOrPositiveInteger,
3636
} from '../../../../utils/utils';
3737
import { FormattedFormRow } from '../../../../components/FormattedFormRow/FormattedFormRow';
38+
import { DetectorDefinitionFormikValues } from '../../models/interfaces';
39+
import { get } from 'lodash';
3840

3941
interface CustomResultIndexProps {
4042
isEdit: boolean;
4143
useDefaultResultIndex?: boolean;
4244
resultIndex?: string;
45+
formikProps: FormikProps<DetectorDefinitionFormikValues>;
4346
}
4447

4548
function CustomResultIndex(props: CustomResultIndexProps) {
4649
const [enabled, setEnabled] = useState<boolean>(!!props.resultIndex);
50+
const [customResultIndexConditionsEnabled, setCustomResultIndexConditionsEnabled] = useState<boolean>(true);
51+
const customResultIndexMinAge = get(props.formikProps, 'values.resultIndexMinAge');
52+
const customResultIndexMinSize = get(props.formikProps, 'values.resultIndexMinSize');
53+
const customResultIndexTTL = get(props.formikProps, 'values.resultIndexTtl');
54+
const { setFieldValue } = useFormikContext();
55+
56+
useEffect(() => {
57+
if (props.isEdit) {
58+
if (customResultIndexMinAge === undefined && customResultIndexMinSize === undefined && customResultIndexTTL === undefined) {
59+
setCustomResultIndexConditionsEnabled(false);
60+
}
61+
}
62+
if (!customResultIndexConditionsEnabled) {
63+
setFieldValue('resultIndexMinAge', '');
64+
setFieldValue('resultIndexMinSize', '');
65+
setFieldValue('resultIndexTtl', '');
66+
}
67+
},[customResultIndexConditionsEnabled])
4768

4869
return (
4970
<ContentPanel
@@ -89,7 +110,7 @@ function CustomResultIndex(props: CustomResultIndexProps) {
89110
<EuiFlexItem>
90111
<EuiCallOut
91112
data-test-subj="cannotEditResultIndexCallout"
92-
title="You can't change the custom result index after creating the detector. You can manage the result index using the following three settings."
113+
title="You can't change the custom result index after creating the detector. You can manage the result index using the following three settings inside Anomaly Detection plugin or with the Index Management plugin."
93114
color="warning"
94115
iconType="alert"
95116
size="s"
@@ -115,20 +136,37 @@ function CustomResultIndex(props: CustomResultIndexProps) {
115136
</EuiFormRow>
116137
</EuiFlexItem>
117138
) : null}
139+
140+
{enabled ? (
141+
<EuiFlexItem>
142+
<EuiCheckbox
143+
id={'resultIndexConditionCheckbox'}
144+
label="Enable custom result index lifecycle management"
145+
checked={customResultIndexConditionsEnabled}
146+
onChange={() => {
147+
setCustomResultIndexConditionsEnabled(!customResultIndexConditionsEnabled);
148+
}}
149+
/>
150+
</EuiFlexItem>
151+
) : null}
118152
</EuiFlexGroup>
119153
)}
120154
</Field>
121155

122-
{enabled ? (<Field
156+
{ (enabled && customResultIndexConditionsEnabled) ? (<Field
123157
name="resultIndexMinAge"
124-
validate={enabled ? validatePositiveInteger : null}
158+
validate={(enabled && customResultIndexConditionsEnabled) ? validateEmptyOrPositiveInteger : null}
125159
>
126160
{({ field, form }: FieldProps) => (
127161
<EuiFlexGroup>
128162
<EuiFlexItem style={{ maxWidth: '70%' }}>
129163
<FormattedFormRow
130164
fullWidth
131-
title="Max Index Age"
165+
formattedTitle={
166+
<p>
167+
Min Index Age <span className="optional">- optional</span>
168+
</p>
169+
}
132170
hint={[
133171
`This setting would define a specific threshold for the age of an index. When this threshold is surpassed, a rollover will be triggered automatically.`,
134172
]}
@@ -141,6 +179,7 @@ function CustomResultIndex(props: CustomResultIndexProps) {
141179
name="resultIndexMinAge"
142180
id="resultIndexMinAge"
143181
data-test-subj="resultIndexMinAge"
182+
placeholder="Min index age"
144183
min={1}
145184
{...field}
146185
/>
@@ -157,16 +196,20 @@ function CustomResultIndex(props: CustomResultIndexProps) {
157196
)}
158197
</Field>) : null}
159198

160-
{enabled ? (<Field
199+
{(enabled && customResultIndexConditionsEnabled) ? (<Field
161200
name="resultIndexMinSize"
162-
validate={enabled ? validatePositiveInteger : null}
201+
validate={(enabled && customResultIndexConditionsEnabled) ? validateEmptyOrPositiveInteger : null}
163202
>
164203
{({ field, form }: FieldProps) => (
165204
<EuiFlexGroup>
166205
<EuiFlexItem style={{ maxWidth: '70%' }}>
167206
<FormattedFormRow
168207
fullWidth
169-
title="Max Index Size"
208+
formattedTitle={
209+
<p>
210+
Min Index Size <span className="optional">- optional</span>
211+
</p>
212+
}
170213
hint={[
171214
`This setting would define a specific threshold for the size of an index. When this threshold is surpassed, a rollover will be triggered automatically.`,
172215
]}
@@ -178,7 +221,7 @@ function CustomResultIndex(props: CustomResultIndexProps) {
178221
<EuiFieldNumber
179222
name="resultIndexMinSize"
180223
id="resultIndexMinSize"
181-
placeholder="Max index size"
224+
placeholder="Min index size"
182225
data-test-subj="resultIndexMinSize"
183226
min={1000}
184227
{...field}
@@ -196,16 +239,20 @@ function CustomResultIndex(props: CustomResultIndexProps) {
196239
)}
197240
</Field>) : null}
198241

199-
{enabled ? (<Field
242+
{(enabled && customResultIndexConditionsEnabled) ? (<Field
200243
name="resultIndexTtl"
201-
validate={enabled ? validatePositiveInteger : null}
244+
validate={(enabled && customResultIndexConditionsEnabled) ? validateEmptyOrPositiveInteger : null}
202245
>
203246
{({ field, form }: FieldProps) => (
204247
<EuiFlexGroup>
205248
<EuiFlexItem style={{ maxWidth: '70%' }}>
206249
<FormattedFormRow
207250
fullWidth
208-
title="Index TTL"
251+
formattedTitle={
252+
<p>
253+
Index TTL <span className="optional">- optional</span>
254+
</p>
255+
}
209256
hint={[
210257
`This setting would define the duration after which an index is considered expired and eligible for deletion.`,
211258
]}
@@ -218,6 +265,7 @@ function CustomResultIndex(props: CustomResultIndexProps) {
218265
name="resultIndexTtl"
219266
id="resultIndexTtl"
220267
data-test-subj="resultIndexTtl"
268+
placeholder="Index TTL"
221269
min={1}
222270
{...field}
223271
/>

public/pages/DefineDetector/containers/DefineDetector.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ export const DefineDetector = (props: DefineDetectorProps) => {
392392
<CustomResultIndex
393393
isEdit={props.isEdit}
394394
resultIndex={get(formikProps, 'values.resultIndex')}
395+
formikProps={formikProps}
395396
/>
396397
</Fragment>
397398
</EuiPageBody>

public/pages/DefineDetector/models/interfaces.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface DetectorDefinitionFormikValues {
2222
timeField: string;
2323
interval: number;
2424
windowDelay: number;
25-
resultIndexMinAge?: number;
26-
resultIndexMinSize?: number;
27-
resultIndexTtl?:number;
25+
resultIndexMinAge?: number | string;
26+
resultIndexMinSize?: number | string;
27+
resultIndexTtl?:number | string;
2828
}

public/pages/DefineDetector/utils/helpers.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ export function detectorDefinitionToFormik(
4545
timeField: ad.timeField,
4646
interval: get(ad, 'detectionInterval.period.interval', 10),
4747
windowDelay: get(ad, 'windowDelay.period.interval', 0),
48-
resultIndexMinAge: get(ad, 'resultIndexMinAge', 7),
49-
resultIndexMinSize:get(ad, 'resultIndexMinSize', 51200),
50-
resultIndexTtl: get(ad, 'resultIndexTtl', 60),
48+
resultIndexMinAge: get(ad, 'resultIndexMinAge', undefined),
49+
resultIndexMinSize:get(ad, 'resultIndexMinSize', undefined),
50+
resultIndexTtl: get(ad, 'resultIndexTtl', undefined),
5151
};
5252
}
5353

public/pages/ReviewAndCreate/components/DetectorDefinitionFields/DetectorDefinitionFields.tsx

+10-6
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,13 @@ export const DetectorDefinitionFields = (
131131
}
132132
};
133133

134-
const minAge = get(props, 'detector.resultIndexMinAge', '-');
135-
const minSize = get(props, 'detector.resultIndexMinSize', '-');
136-
const ttl = get(props, 'detector.resultIndexTtl', '-');
134+
const minAgeValue = get(props, 'detector.resultIndexMinAge', undefined);
135+
const minAge = (minAgeValue === undefined) ? '- Days' : minAgeValue + " Days";
136+
const minSizeValue = get(props, 'detector.resultIndexMinSize', undefined);
137+
const minSize = (minSizeValue === undefined) ? '- MB' : minSizeValue + " MB";
138+
const ttlValue = get(props, 'detector.resultIndexTtl', undefined);
139+
const ttl = (ttlValue === undefined) ? '- Days' : ttlValue + " Days";
140+
137141

138142
return (
139143
<ContentPanel
@@ -224,19 +228,19 @@ export const DetectorDefinitionFields = (
224228
<EuiFlexItem>
225229
<ConfigCell
226230
title="Custom result index min age"
227-
description={minAge === '-' ? minAge : `${minAge} Days`}
231+
description={minAge}
228232
/>
229233
</EuiFlexItem>
230234
<EuiFlexItem>
231235
<ConfigCell
232236
title="Custom result index min size"
233-
description={minSize == '-' ? minSize : `${minSize} MB`}
237+
description={minSize}
234238
/>
235239
</EuiFlexItem>
236240
<EuiFlexItem>
237241
<ConfigCell
238242
title="Custom result index TTL"
239-
description={ttl == '-' ? ttl : `${ttl} Days`}
243+
description={ttl}
240244
/>
241245
</EuiFlexItem>
242246
</EuiFlexGrid>

public/pages/ReviewAndCreate/containers/__tests__/__snapshots__/ReviewAndCreate.test.tsx.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ exports[`<ReviewAndCreate /> spec renders the component, validation loading 1`]
424424
<p
425425
class="enabled"
426426
>
427-
-
427+
- Days
428428
</p>
429429
</div>
430430
</div>
@@ -458,7 +458,7 @@ exports[`<ReviewAndCreate /> spec renders the component, validation loading 1`]
458458
<p
459459
class="enabled"
460460
>
461-
-
461+
- MB
462462
</p>
463463
</div>
464464
</div>
@@ -492,7 +492,7 @@ exports[`<ReviewAndCreate /> spec renders the component, validation loading 1`]
492492
<p
493493
class="enabled"
494494
>
495-
-
495+
- Days
496496
</p>
497497
</div>
498498
</div>
@@ -1518,7 +1518,7 @@ exports[`issue in detector validation issues in feature query 1`] = `
15181518
<p
15191519
class="enabled"
15201520
>
1521-
-
1521+
- Days
15221522
</p>
15231523
</div>
15241524
</div>
@@ -1552,7 +1552,7 @@ exports[`issue in detector validation issues in feature query 1`] = `
15521552
<p
15531553
class="enabled"
15541554
>
1555-
-
1555+
- MB
15561556
</p>
15571557
</div>
15581558
</div>
@@ -1586,7 +1586,7 @@ exports[`issue in detector validation issues in feature query 1`] = `
15861586
<p
15871587
class="enabled"
15881588
>
1589-
-
1589+
- Days
15901590
</p>
15911591
</div>
15921592
</div>

public/utils/utils.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ export const validatePositiveInteger = (value: any) => {
110110
return 'Must be a positive integer';
111111
};
112112

113+
export const validateEmptyOrPositiveInteger = (value: any) => {
114+
if (Number.isInteger(value) && value < 1)
115+
return 'Must be a positive integer';
116+
};
117+
113118
export const validateNonNegativeInteger = (value: any) => {
114119
if (!Number.isInteger(value) || value < 0)
115120
return 'Must be a non-negative integer';

0 commit comments

Comments
 (0)