Skip to content

Commit

Permalink
Fixed due to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
YulNaumenko committed Mar 19, 2020
1 parent 7fdfee6 commit 198ca72
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
32 changes: 31 additions & 1 deletion x-pack/plugins/alerting/common/parse_duration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { parseDuration } from './parse_duration';
import { parseDuration, getDurationNumberInItsUnit, getDurationUnitValue } from './parse_duration';

test('parses seconds', () => {
const result = parseDuration('10s');
Expand Down Expand Up @@ -52,3 +52,33 @@ test('throws error when 0 based', () => {
`"Invalid duration \\"0d\\". Durations must be of the form {number}x. Example: 5s, 5m, 5h or 5d\\""`
);
});

test('getDurationNumberInItsUnit days', () => {
const result = getDurationNumberInItsUnit('10d');
expect(result).toEqual(10);
});

test('getDurationNumberInItsUnit minutes', () => {
const result = getDurationNumberInItsUnit('1m');
expect(result).toEqual(1);
});

test('getDurationNumberInItsUnit seconds', () => {
const result = getDurationNumberInItsUnit('123s');
expect(result).toEqual(123);
});

test('getDurationUnitValue minutes', () => {
const result = getDurationUnitValue('1m');
expect(result).toEqual('m');
});

test('getDurationUnitValue days', () => {
const result = getDurationUnitValue('23d');
expect(result).toEqual('d');
});

test('getDurationUnitValue hours', () => {
const result = getDurationUnitValue('100h');
expect(result).toEqual('h');
});
9 changes: 9 additions & 0 deletions x-pack/plugins/alerting/common/parse_duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ export function parseDuration(duration: string): number {
);
}

export function getDurationNumberInItsUnit(duration: string): number {
return parseInt(duration.replace(/[^0-9.]/g, ''), 0);
}

export function getDurationUnitValue(duration: string): string {
const durationNumber = getDurationNumberInItsUnit(duration);
return duration.replace(durationNumber.toString(), '');
}

export function validateDurationSchema(duration: string) {
if (duration.match(SECONDS_REGEX)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import {
EuiButtonIcon,
EuiHorizontalRule,
} from '@elastic/eui';
import {
getDurationNumberInItsUnit,
getDurationUnitValue,
} from '../../../../../alerting/common/parse_duration';
import { loadAlertTypes } from '../../lib/alert_api';
import { actionVariablesFromAlertType } from '../../lib/action_variables';
import { AlertReducerAction } from './alert_reducer';
Expand Down Expand Up @@ -82,20 +86,16 @@ export const AlertForm = ({ alert, canChangeTrigger = true, dispatch, errors }:

const [alertTypesIndex, setAlertTypesIndex] = useState<AlertTypeIndex | undefined>(undefined);
const [alertInterval, setAlertInterval] = useState<number | undefined>(
alert.schedule.interval
? parseInt(alert.schedule.interval.replace(/^[A-Za-z]+$/, ''), 0)
: undefined
alert.schedule.interval ? getDurationNumberInItsUnit(alert.schedule.interval) : undefined
);
const [alertIntervalUnit, setAlertIntervalUnit] = useState<string>(
alert.schedule.interval
? alert.schedule.interval.replace((alertInterval ?? '').toString(), '')
: 'm'
alert.schedule.interval ? getDurationUnitValue(alert.schedule.interval) : 'm'
);
const [alertThrottle, setAlertThrottle] = useState<number | null>(
alert.throttle ? parseInt(alert.throttle.replace(/^[A-Za-z]+$/, ''), 0) : null
alert.throttle ? getDurationNumberInItsUnit(alert.throttle) : null
);
const [alertThrottleUnit, setAlertThrottleUnit] = useState<string>(
alert.throttle ? alert.throttle.replace((alertThrottle ?? '').toString(), '') : 'm'
alert.throttle ? getDurationUnitValue(alert.throttle) : 'm'
);
const [defaultActionGroupId, setDefaultActionGroupId] = useState<string | undefined>(undefined);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
await fieldOptions[1].click();
// need this two out of popup clicks to close them
await nameInput.click();
const intervalInput = await testSubjects.find('intervalInput');
await intervalInput.click();
await intervalInput.clearValue();
const validationError = await find.byCssSelector(`.euiFormErrorText`);
expect(validationError.isDisplayed).to.be(true);
await intervalInput.type(1);

await testSubjects.click('.slack-ActionTypeSelectOption');
await testSubjects.click('createActionConnectorButton');
Expand Down

0 comments on commit 198ca72

Please sign in to comment.