Skip to content

Commit

Permalink
move toSimpleRuleSchedule out to reduce bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
maximpn committed Jan 15, 2025
1 parent 7149d81 commit a32193b
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 133 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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(),
};
}
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();
});
});
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(),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a32193b

Please sign in to comment.