forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RAM][SECURITYSOLUTION][ALERTS] - Show warning that custom action int…
…ervals cannot be shorter than the rule's check interval (elastic#155502)
- Loading branch information
Showing
10 changed files
with
149 additions
and
73 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
x-pack/plugins/security_solution/common/utils/time_type_value.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { getTimeTypeValue } from './time_type_value'; | ||
|
||
describe('getTimeTypeValue', () => { | ||
[ | ||
{ interval: '1ms', value: 1, unit: 'ms' }, | ||
{ interval: '0s', value: 0, unit: 's' }, | ||
{ interval: '3s', value: 3, unit: 's' }, | ||
{ interval: '5m', value: 5, unit: 'm' }, | ||
{ interval: '7h', value: 7, unit: 'h' }, | ||
{ interval: '10d', value: 10, unit: 'd' }, | ||
].forEach(({ interval, value, unit }) => { | ||
it(`should correctly return time duration and time unit when 'interval' is ${interval}`, () => { | ||
const { value: actualValue, unit: actualUnit } = getTimeTypeValue(interval); | ||
expect(actualValue).toEqual(value); | ||
expect(actualUnit).toEqual(unit); | ||
}); | ||
}); | ||
|
||
[ | ||
{ interval: '-1ms', value: 1, unit: 'ms' }, | ||
{ interval: '-3s', value: 3, unit: 's' }, | ||
{ interval: '-5m', value: 5, unit: 'm' }, | ||
{ interval: '-7h', value: 7, unit: 'h' }, | ||
{ interval: '-10d', value: 10, unit: 'd' }, | ||
{ interval: '-1', value: 1, unit: 'ms' }, | ||
{ interval: '-3', value: 3, unit: 'ms' }, | ||
{ interval: '-5', value: 5, unit: 'ms' }, | ||
{ interval: '-7', value: 7, unit: 'ms' }, | ||
{ interval: '-10', value: 10, unit: 'ms' }, | ||
].forEach(({ interval, value, unit }) => { | ||
it(`should correctly return positive time duration and time unit when 'interval' is negative (${interval})`, () => { | ||
const { value: actualValue, unit: actualUnit } = getTimeTypeValue(interval); | ||
expect(actualValue).toEqual(value); | ||
expect(actualUnit).toEqual(unit); | ||
}); | ||
}); | ||
|
||
[ | ||
{ interval: 'ms', value: 0, unit: 'ms' }, | ||
{ interval: 's', value: 0, unit: 's' }, | ||
{ interval: 'm', value: 0, unit: 'm' }, | ||
{ interval: 'h', value: 0, unit: 'h' }, | ||
{ interval: 'd', value: 0, unit: 'd' }, | ||
{ interval: '-ms', value: 0, unit: 'ms' }, | ||
{ interval: '-s', value: 0, unit: 's' }, | ||
{ interval: '-m', value: 0, unit: 'm' }, | ||
{ interval: '-h', value: 0, unit: 'h' }, | ||
{ interval: '-d', value: 0, unit: 'd' }, | ||
].forEach(({ interval, value, unit }) => { | ||
it(`should correctly return time duration equal to '0' when 'interval' does not specify time duration (${interval})`, () => { | ||
const { value: actualValue, unit: actualUnit } = getTimeTypeValue(interval); | ||
expect(actualValue).toEqual(value); | ||
expect(actualUnit).toEqual(unit); | ||
}); | ||
}); | ||
|
||
[ | ||
{ interval: '0', value: 0, unit: 'ms' }, | ||
{ interval: '1', value: 1, unit: 'ms' }, | ||
{ interval: '3', value: 3, unit: 'ms' }, | ||
{ interval: '5', value: 5, unit: 'ms' }, | ||
{ interval: '7', value: 7, unit: 'ms' }, | ||
{ interval: '10', value: 10, unit: 'ms' }, | ||
].forEach(({ interval, value, unit }) => { | ||
it(`should correctly return time unit set to 'ms' as a default value when 'interval' does not specify it (${interval})`, () => { | ||
const { value: actualValue, unit: actualUnit } = getTimeTypeValue(interval); | ||
expect(actualValue).toEqual(value); | ||
expect(actualUnit).toEqual(unit); | ||
}); | ||
}); | ||
|
||
[ | ||
{ interval: '1f', value: 1, unit: 'ms' }, | ||
{ interval: '-3r', value: 3, unit: 'ms' }, | ||
{ interval: 'p', value: 0, unit: 'ms' }, | ||
].forEach(({ interval, value, unit }) => { | ||
it(`should correctly return time unit set to 'ms' as a default value when data is invalid (${interval})`, () => { | ||
const { value: actualValue, unit: actualUnit } = getTimeTypeValue(interval); | ||
expect(actualValue).toEqual(value); | ||
expect(actualUnit).toEqual(unit); | ||
}); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
x-pack/plugins/security_solution/common/utils/time_type_value.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { isEmpty } from 'lodash/fp'; | ||
import type { Unit } from '@kbn/datemath'; | ||
|
||
export const getTimeTypeValue = (time: string): { unit: Unit; value: number } => { | ||
const timeObj: { unit: Unit; value: number } = { | ||
unit: 'ms', | ||
value: 0, | ||
}; | ||
const filterTimeVal = time.match(/\d+/g); | ||
const filterTimeType = time.match(/[a-zA-Z]+/g); | ||
if (!isEmpty(filterTimeVal) && filterTimeVal != null && !isNaN(Number(filterTimeVal[0]))) { | ||
timeObj.value = Number(filterTimeVal[0]); | ||
} | ||
if ( | ||
!isEmpty(filterTimeType) && | ||
filterTimeType != null && | ||
['s', 'm', 'h', 'd'].includes(filterTimeType[0]) | ||
) { | ||
timeObj.unit = filterTimeType[0] as Unit; | ||
} | ||
return timeObj; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters