Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

add window delay field of anomaly detector #4

Merged
merged 1 commit into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions public/pages/PreviewDetector/containers/AdjustModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
isInvalid,
getError,
validatePositiveInteger,
validateNonNegativeInteger,
getErrorMessage,
} from '../../../utils/utils';
import { Detector } from '../../../models/interfaces';
Expand All @@ -65,11 +66,13 @@ type AdjustModelProps = {
export type AdjustModelFormikValues = {
formikFeatures: FeaturesFormikValues[];
detectionInterval: number;
windowDelay: number;
};

const getInitialValues = (detector: Detector) => ({
formikFeatures: Object.values(featuresToFormik(detector)),
detectionInterval: get(detector, 'detectionInterval.period.interval', 10),
windowDelay: get(detector, 'windowDelay.period.interval', 0),
});

export function AdjustModel(props: AdjustModelProps) {
Expand Down Expand Up @@ -177,6 +180,27 @@ export function AdjustModel(props: AdjustModelProps) {
</EuiFormRow>
)}
</Field>
<Field
name="windowDelay"
validate={validateNonNegativeInteger}
>
{({ field, form }: FieldProps) => (
<EuiFormRow
label="Window delay"
helpText="Specify data delay in minutes"
isInvalid={isInvalid(field.name, form)}
error={getError(field.name, form)}
>
<EuiFieldNumber
name="windowDelay"
id="windowDelay"
placeholder="Window delay"
data-test-subj="windowDelay"
{...field}
/>
</EuiFormRow>
)}
</Field>
</EuiFlyoutBody>
<EuiFlyoutFooter className="flyout">
<EuiFlexGroup alignItems="center" justifyContent="flexEnd">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,20 @@ describe('<ModelDefinition /> spec', () => {
unit: UNITS.MINUTES,
},
},
windowDelay: {
period: {
interval: 1,
unit: UNITS.MINUTES,
},
},
},
},
});
const { getByTestId } = renderWithRouter(randomDetector);
fireEvent.click(getByTestId('adjustModel'));
await wait();
userEvent.type(getByTestId('detectionInterval'), '20');
userEvent.type(getByTestId('windowDelay'), '2');
fireEvent.click(getByTestId('updateAdjustModel'));
await wait();
expect(httpClientMock.put).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ describe('prepareTunedDetector', () => {
const randomDetector = getRandomDetector(false);
const tuneValue: AdjustModelFormikValues = {
detectionInterval: 20,
windowDelay: 3,
formikFeatures: Object.values(featuresToFormik(randomDetector)),
};
const apiRequest = prepareTunedDetector(tuneValue, randomDetector);
expect(apiRequest.detectionInterval).toEqual({
period: { interval: 20, unit: UNITS.MINUTES },
});
expect(apiRequest.windowDelay).toEqual({
period: { interval: 3, unit: UNITS.MINUTES },
});
});
test('should able to update state of a feature interval', () => {
const randomDetector = getRandomDetector(false);
Expand All @@ -45,12 +49,16 @@ describe('prepareTunedDetector', () => {
const formikFeatures = Object.values(updatedFeatures);
const tuneValue: AdjustModelFormikValues = {
detectionInterval: 20,
windowDelay: 3,
formikFeatures: formikFeatures,
};
const apiRequest = prepareTunedDetector(tuneValue, randomDetector);
expect(apiRequest.detectionInterval).toEqual({
period: { interval: 20, unit: UNITS.MINUTES },
});
expect(apiRequest.windowDelay).toEqual({
period: { interval: 3, unit: UNITS.MINUTES },
});
expect(apiRequest.featureAttributes[0].featureEnabled).toBe(
!formikFeaturesMap[editFeatureId].enabled
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export function prepareTunedDetector(
detectionInterval: {
period: { interval: values.detectionInterval, unit: UNITS.MINUTES },
},
windowDelay: {
period: { interval: values.windowDelay, unit: UNITS.MINUTES },
},
featureAttributes: featureAttributes,
};
}
36 changes: 36 additions & 0 deletions public/utils/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import {validateNonNegativeInteger} from '../utils';

describe('validateNonNegativeInteger', () => {

test('should pass for positive', () => {
expect(validateNonNegativeInteger(1)).toBeUndefined();
});

test('should pass for zero', () => {
expect(validateNonNegativeInteger(0)).toBeUndefined();
});

test('should raise for negative', () => {
expect(validateNonNegativeInteger(-1)).not.toBeUndefined();
});

test('should raise for floating-point', () => {
expect(validateNonNegativeInteger(1.1)).not.toBeUndefined();
});

});
5 changes: 5 additions & 0 deletions public/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export const validatePositiveInteger = (value: any) => {
return 'Must be a positive integer';
};

export const validateNonNegativeInteger = (value: any) => {
if (!Number.isInteger(value) || value < 0)
return 'Must be a non-negative integer';
};

export const getErrorMessage = (err: any, defaultMessage: string) => {
if (typeof err === 'string') return err;
if (err && err.message) return err.message;
Expand Down