-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move toSimpleRuleSchedule out to reduce bundle size
- Loading branch information
Showing
11 changed files
with
145 additions
and
133 deletions.
There are no files selected for viewing
99 changes: 0 additions & 99 deletions
99
...ins/security_solution/common/api/detection_engine/model/rule_schema/rule_schedule.test.ts
This file was deleted.
Oops, something went wrong.
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
105 changes: 105 additions & 0 deletions
105
...ty_solution/common/api/detection_engine/model/rule_schema/to_simple_rule_schedule.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,105 @@ | ||
/* | ||
* 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 { toSimpleRuleSchedule } from './to_simple_rule_schedule'; | ||
|
||
describe('toSimpleRuleSchedule', () => { | ||
it.each([ | ||
[ | ||
{ interval: '10s', from: 'now-20s', to: 'now' }, | ||
{ interval: '10s', lookback: '10s' }, | ||
], | ||
[ | ||
{ interval: '10m', from: 'now-30m', to: 'now' }, | ||
{ interval: '10m', lookback: '20m' }, | ||
], | ||
[ | ||
{ interval: '1h', from: 'now-3h', to: 'now' }, | ||
{ interval: '1h', lookback: '2h' }, | ||
], | ||
[ | ||
{ interval: '60s', from: 'now-2m', to: 'now' }, | ||
{ interval: '60s', lookback: '1m' }, | ||
], | ||
[ | ||
{ interval: '60s', from: 'now-2h', to: 'now' }, | ||
{ interval: '60s', lookback: '119m' }, | ||
], | ||
[ | ||
{ interval: '60m', from: 'now-3h', to: 'now' }, | ||
{ interval: '60m', lookback: '2h' }, | ||
], | ||
[ | ||
{ interval: '3600s', from: 'now-5h', to: 'now' }, | ||
{ interval: '3600s', lookback: '4h' }, | ||
], | ||
[ | ||
{ interval: '1m', from: 'now-120s', to: 'now' }, | ||
{ interval: '1m', lookback: '1m' }, | ||
], | ||
[ | ||
{ interval: '1h', from: 'now-7200s', to: 'now' }, | ||
{ interval: '1h', lookback: '1h' }, | ||
], | ||
[ | ||
{ interval: '1h', from: 'now-120m', to: 'now' }, | ||
{ interval: '1h', lookback: '1h' }, | ||
], | ||
[ | ||
{ interval: '90s', from: 'now-90s', to: 'now' }, | ||
{ interval: '90s', lookback: '0s' }, | ||
], | ||
[ | ||
{ interval: '30m', from: 'now-30m', to: 'now' }, | ||
{ interval: '30m', lookback: '0s' }, | ||
], | ||
[ | ||
{ interval: '1h', from: 'now-1h', to: 'now' }, | ||
{ interval: '1h', lookback: '0s' }, | ||
], | ||
[ | ||
{ interval: '60s', from: 'now-1m', to: 'now' }, | ||
{ interval: '60s', lookback: '0s' }, | ||
], | ||
[ | ||
{ interval: '60m', from: 'now-1h', to: 'now' }, | ||
{ interval: '60m', lookback: '0s' }, | ||
], | ||
[ | ||
{ interval: '1m', from: 'now-60s', to: 'now' }, | ||
{ interval: '1m', lookback: '0s' }, | ||
], | ||
[ | ||
{ interval: '1h', from: 'now-60m', to: 'now' }, | ||
{ interval: '1h', lookback: '0s' }, | ||
], | ||
[ | ||
{ interval: '1h', from: 'now-3600s', to: 'now' }, | ||
{ interval: '1h', lookback: '0s' }, | ||
], | ||
[ | ||
{ interval: '0s', from: 'now', to: 'now' }, | ||
{ interval: '0s', lookback: '0s' }, | ||
], | ||
])('transforms %j to simple rule schedule', (fullRuleSchedule, expected) => { | ||
const result = toSimpleRuleSchedule(fullRuleSchedule); | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it.each([ | ||
[{ interval: 'invalid', from: 'now-11m', to: 'now' }], | ||
[{ interval: '10m', from: 'invalid', to: 'now' }], | ||
[{ interval: '10m', from: 'now-11m', to: 'invalid' }], | ||
[{ interval: '10m', from: 'now-11m', to: 'now-1m' }], | ||
[{ interval: '10m', from: 'now-5m', to: 'now' }], | ||
])('returns "undefined" for %j', (fullRuleSchedule) => { | ||
const result = toSimpleRuleSchedule(fullRuleSchedule); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
...ecurity_solution/common/api/detection_engine/model/rule_schema/to_simple_rule_schedule.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,32 @@ | ||
/* | ||
* 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 { calcDateMathDiff } from '@kbn/securitysolution-utils/date_math'; | ||
import { TimeDuration as TimeDurationUtil } from '@kbn/securitysolution-utils/time_duration'; | ||
import type { RuleSchedule, SimpleRuleSchedule } from './rule_schedule'; | ||
|
||
/** | ||
* Transforms RuleSchedule to SimpleRuleSchedule by replacing `from` and `to` with `lookback`. | ||
* | ||
* The transformation is only possible when `to` equals to `now` and result `lookback` is non-negative. | ||
*/ | ||
export function toSimpleRuleSchedule(ruleSchedule: RuleSchedule): SimpleRuleSchedule | undefined { | ||
if (ruleSchedule.to !== 'now') { | ||
return undefined; | ||
} | ||
|
||
const lookBackMs = calcDateMathDiff(ruleSchedule.from, `now-${ruleSchedule.interval}`); | ||
|
||
if (lookBackMs === undefined || lookBackMs < 0) { | ||
return undefined; | ||
} | ||
|
||
return { | ||
interval: ruleSchedule.interval, | ||
lookback: TimeDurationUtil.fromMilliseconds(lookBackMs).toString(), | ||
}; | ||
} |
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