From a32193b6a21679e575a64f64235a4e9e8113b425 Mon Sep 17 00:00:00 2001 From: Maxim Palenov Date: Fri, 10 Jan 2025 20:48:20 +0100 Subject: [PATCH] move toSimpleRuleSchedule out to reduce bundle size --- .../model/rule_schema/rule_schedule.test.ts | 99 ----------------- .../model/rule_schema/rule_schedule.ts | 24 ---- .../to_simple_rule_schedule.test.ts | 105 ++++++++++++++++++ .../rule_schema/to_simple_rule_schedule.ts | 32 ++++++ .../get_field_diffs_for_grouped_fields.ts | 2 +- .../rule_details/rule_schedule_section.tsx | 2 +- .../get_subfield_changes/rule_schedule.ts | 2 +- .../rule_schedule/rule_schedule_form.tsx | 6 +- .../simple_rule_schedule_form.tsx | 2 +- .../fields/rule_schedule/rule_schedule.tsx | 2 +- .../pages/detection_engine/rules/helpers.tsx | 2 +- 11 files changed, 145 insertions(+), 133 deletions(-) delete mode 100644 x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/rule_schedule.test.ts create mode 100644 x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/to_simple_rule_schedule.test.ts create mode 100644 x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/to_simple_rule_schedule.ts diff --git a/x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/rule_schedule.test.ts b/x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/rule_schedule.test.ts deleted file mode 100644 index b951d88061754..0000000000000 --- a/x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/rule_schedule.test.ts +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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 './rule_schedule'; - -describe('toSimpleRuleSchedule', () => { - it.each([['10s'], ['3m'], ['5h']])('returns interval "%s" without changes', (interval) => { - const result = toSimpleRuleSchedule({ - interval, - from: 'now-10h', - to: 'now', - }); - - expect(result?.interval).toBe(interval); - }); - - it.each([ - ['now-20s', '10s', '10s'], - ['now-30m', '10m', '20m'], - ['now-3h', '1h', '2h'], - ['now-120s', '1m', '1m'], - ['now-180m', '2h', '1h'], - ['now-7200s', '90m', '30m'], - ])('returns lookback (from=%s and interval=%s)', (from, interval, expected) => { - const result = toSimpleRuleSchedule({ - interval, - from, - to: 'now', - }); - - expect(result?.lookback).toBe(expected); - }); - - it('returns zero lookback', () => { - const result = toSimpleRuleSchedule({ - interval: '10m', - from: 'now-10m', - to: 'now', - }); - - expect(result?.lookback).toBe('0s'); - }); - - describe('for invalid rule schedule', () => { - it('returns "undefined" when interval is invalid', () => { - const result = toSimpleRuleSchedule({ - interval: 'invalid', - from: 'now-11m', - to: 'now', - }); - - expect(result).toBeUndefined(); - }); - - it('returns "undefined" when from is invalid', () => { - const result = toSimpleRuleSchedule({ - interval: '10m', - from: 'invalid', - to: 'now', - }); - - expect(result).toBeUndefined(); - }); - - it('returns "undefined" when to is invalid', () => { - const result = toSimpleRuleSchedule({ - interval: '10m', - from: 'now-11m', - to: 'invalid', - }); - - expect(result).toBeUndefined(); - }); - - it('returns "undefined" when to is not equal "now"', () => { - const result = toSimpleRuleSchedule({ - interval: '10m', - from: 'now-11m', - to: 'now-1m', - }); - - expect(result).toBeUndefined(); - }); - - it('returns "undefined" when result lookback is negative', () => { - const result = toSimpleRuleSchedule({ - interval: '10m', - from: 'now-5m', - to: 'now', - }); - - expect(result).toBeUndefined(); - }); - }); -}); diff --git a/x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/rule_schedule.ts b/x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/rule_schedule.ts index 21efeeac05974..0177dd0b96db3 100644 --- a/x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/rule_schedule.ts +++ b/x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/rule_schedule.ts @@ -6,8 +6,6 @@ */ import { z } from '@kbn/zod'; -import { calcDateMathDiff } from '@kbn/securitysolution-utils/date_math'; -import { TimeDuration as TimeDurationUtil } from '@kbn/securitysolution-utils/time_duration'; import { RuleIntervalFrom, RuleIntervalTo } from './common_attributes.gen'; import { TimeDuration as TimeDurationSchema } from './time_duration'; @@ -50,25 +48,3 @@ export const SimpleRuleSchedule = z.object({ */ lookback: TimeDurationSchema({ allowedUnits: ['s', 'm', 'h'] }), }); - -/** - * 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(), - }; -} diff --git a/x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/to_simple_rule_schedule.test.ts b/x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/to_simple_rule_schedule.test.ts new file mode 100644 index 0000000000000..c2383090a466a --- /dev/null +++ b/x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/to_simple_rule_schedule.test.ts @@ -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(); + }); +}); diff --git a/x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/to_simple_rule_schedule.ts b/x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/to_simple_rule_schedule.ts new file mode 100644 index 0000000000000..5227099e63111 --- /dev/null +++ b/x-pack/solutions/security/plugins/security_solution/common/api/detection_engine/model/rule_schema/to_simple_rule_schedule.ts @@ -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(), + }; +} diff --git a/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/per_field_diff/get_field_diffs_for_grouped_fields.ts b/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/per_field_diff/get_field_diffs_for_grouped_fields.ts index ba8f39fff1f90..7dffd3a593b15 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/per_field_diff/get_field_diffs_for_grouped_fields.ts +++ b/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/per_field_diff/get_field_diffs_for_grouped_fields.ts @@ -10,7 +10,7 @@ import type { RuleSchedule, SimpleRuleSchedule, } from '../../../../../../common/api/detection_engine/model/rule_schema/rule_schedule'; -import { toSimpleRuleSchedule } from '../../../../../../common/api/detection_engine/model/rule_schema/rule_schedule'; +import { toSimpleRuleSchedule } from '../../../../../../common/api/detection_engine/model/rule_schema/to_simple_rule_schedule'; import type { AllFieldsDiff, RuleFieldsDiffWithDataSource, diff --git a/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/rule_schedule_section.tsx b/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/rule_schedule_section.tsx index 0726aec9dbe37..9eba3395b36fa 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/rule_schedule_section.tsx +++ b/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/rule_schedule_section.tsx @@ -8,7 +8,7 @@ import React from 'react'; import { EuiDescriptionList, EuiText } from '@elastic/eui'; import type { EuiDescriptionListProps } from '@elastic/eui'; -import { toSimpleRuleSchedule } from '../../../../../common/api/detection_engine/model/rule_schema/rule_schedule'; +import { toSimpleRuleSchedule } from '../../../../../common/api/detection_engine/model/rule_schema/to_simple_rule_schedule'; import { IntervalAbbrScreenReader } from '../../../../common/components/accessibility'; import type { RuleResponse } from '../../../../../common/api/detection_engine/model/rule_schema'; import { DEFAULT_DESCRIPTION_LIST_COLUMN_WIDTHS } from './constants'; diff --git a/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/comparison_side/get_subfield_changes/rule_schedule.ts b/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/comparison_side/get_subfield_changes/rule_schedule.ts index 0606ab517a8fb..2f15eff892752 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/comparison_side/get_subfield_changes/rule_schedule.ts +++ b/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/comparison_side/get_subfield_changes/rule_schedule.ts @@ -6,7 +6,7 @@ */ import type { SimpleRuleSchedule } from '../../../../../../../../common/api/detection_engine/model/rule_schema/rule_schedule'; -import { toSimpleRuleSchedule } from '../../../../../../../../common/api/detection_engine/model/rule_schema/rule_schedule'; +import { toSimpleRuleSchedule } from '../../../../../../../../common/api/detection_engine/model/rule_schema/to_simple_rule_schedule'; import { stringifyToSortedJson } from '../utils'; import type { DiffableAllFields } from '../../../../../../../../common/api/detection_engine'; import type { SubfieldChange } from '../types'; diff --git a/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/final_edit/fields/rule_schedule/rule_schedule_form.tsx b/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/final_edit/fields/rule_schedule/rule_schedule_form.tsx index 17d2bf5e5515f..ca9cb0db316f1 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/final_edit/fields/rule_schedule/rule_schedule_form.tsx +++ b/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/final_edit/fields/rule_schedule/rule_schedule_form.tsx @@ -6,10 +6,8 @@ */ import React, { useMemo } from 'react'; -import { - toSimpleRuleSchedule, - type RuleSchedule, -} from '../../../../../../../../../common/api/detection_engine/model/rule_schema/rule_schedule'; +import { type RuleSchedule } from '../../../../../../../../../common/api/detection_engine/model/rule_schema/rule_schedule'; +import { toSimpleRuleSchedule } from '../../../../../../../../../common/api/detection_engine/model/rule_schema/to_simple_rule_schedule'; import { SimpleRuleScheduleForm } from './simple_rule_schedule_form'; import { useFieldUpgradeContext } from '../../../rule_upgrade/field_upgrade_context'; import { FullRuleScheduleForm } from './full_rule_schedule_form'; diff --git a/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/final_edit/fields/rule_schedule/simple_rule_schedule_form.tsx b/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/final_edit/fields/rule_schedule/simple_rule_schedule_form.tsx index 0814fd73739e9..28f995dd9afde 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/final_edit/fields/rule_schedule/simple_rule_schedule_form.tsx +++ b/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/final_edit/fields/rule_schedule/simple_rule_schedule_form.tsx @@ -11,7 +11,7 @@ import type { RuleSchedule, SimpleRuleSchedule, } from '../../../../../../../../../common/api/detection_engine/model/rule_schema/rule_schedule'; -import { toSimpleRuleSchedule } from '../../../../../../../../../common/api/detection_engine/model/rule_schema/rule_schedule'; +import { toSimpleRuleSchedule } from '../../../../../../../../../common/api/detection_engine/model/rule_schema/to_simple_rule_schedule'; import { type FormData } from '../../../../../../../../shared_imports'; import type { DiffableRule } from '../../../../../../../../../common/api/detection_engine'; import { RuleFieldEditFormWrapper } from '../../../field_final_side'; diff --git a/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/final_readonly/fields/rule_schedule/rule_schedule.tsx b/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/final_readonly/fields/rule_schedule/rule_schedule.tsx index a2731cf7b1256..e9ee329cfe1be 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/final_readonly/fields/rule_schedule/rule_schedule.tsx +++ b/x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/three_way_diff/final_readonly/fields/rule_schedule/rule_schedule.tsx @@ -8,7 +8,7 @@ import React, { useMemo } from 'react'; import { EuiDescriptionList } from '@elastic/eui'; import type { RuleSchedule } from '../../../../../../../../../common/api/detection_engine/model/rule_schema/rule_schedule'; -import { toSimpleRuleSchedule } from '../../../../../../../../../common/api/detection_engine/model/rule_schema/rule_schedule'; +import { toSimpleRuleSchedule } from '../../../../../../../../../common/api/detection_engine/model/rule_schema/to_simple_rule_schedule'; import * as i18n from '../../../../translations'; import { AccessibleTimeValue } from '../../../../rule_schedule_section'; diff --git a/x-pack/solutions/security/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.tsx b/x-pack/solutions/security/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.tsx index 68fa52ac354b0..1dff767667450 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.tsx +++ b/x-pack/solutions/security/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.tsx @@ -20,7 +20,7 @@ import { ENDPOINT_LIST_ID } from '@kbn/securitysolution-list-constants'; import type { Filter } from '@kbn/es-query'; import type { ActionVariables } from '@kbn/triggers-actions-ui-plugin/public'; import { requiredOptional } from '@kbn/zod-helpers'; -import { toSimpleRuleSchedule } from '../../../../../common/api/detection_engine/model/rule_schema/rule_schedule'; +import { toSimpleRuleSchedule } from '../../../../../common/api/detection_engine/model/rule_schema/to_simple_rule_schedule'; import { ALERT_SUPPRESSION_FIELDS_FIELD_NAME, ALERT_SUPPRESSION_DURATION_TYPE_FIELD_NAME,