Skip to content

Commit

Permalink
Remove type awareness from rule UI, handle undefined frequencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacqary committed Jan 24, 2024
1 parent 4479ce6 commit 51bc88b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { HttpSetup } from '@kbn/core/public';
import { AsApiContract, RewriteResponseCase } from '@kbn/actions-plugin/common';
import { isSystemAction, SanitizedDefaultRuleAction } from '@kbn/alerting-plugin/common';
import { SanitizedDefaultRuleAction } from '@kbn/alerting-plugin/common';
import { Rule, RuleUpdates } from '../../../types';
import { BASE_ALERTING_API_PATH } from '../../constants';
import { transformRule } from './common_transformations';
Expand All @@ -29,27 +29,19 @@ const rewriteBodyRequest: RewriteResponseCase<RuleCreateBody> = ({
...res,
rule_type_id: ruleTypeId,
actions: actions.map((action) => {
if (isSystemAction(action)) {
const { id, params, useAlertDataForTemplate } = action;
return {
id,
params,
...(typeof useAlertDataForTemplate !== 'undefined'
? { use_alert_data_for_template: useAlertDataForTemplate }
: {}),
};
}
const { group, id, params, frequency, alertsFilter, useAlertDataForTemplate } =
action as SanitizedDefaultRuleAction;
return {
group,
id,
params,
frequency: {
notify_when: frequency!.notifyWhen,
throttle: frequency!.throttle,
summary: frequency!.summary,
},
frequency: frequency
? {
notify_when: frequency!.notifyWhen,
throttle: frequency!.throttle,
summary: frequency!.summary,
}
: undefined,
alerts_filter: alertsFilter,
...(typeof useAlertDataForTemplate !== 'undefined'
? { use_alert_data_for_template: useAlertDataForTemplate }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { HttpSetup } from '@kbn/core/public';
import { pick } from 'lodash';
import { RewriteResponseCase, AsApiContract } from '@kbn/actions-plugin/common';
import { isSystemAction, SanitizedDefaultRuleAction } from '@kbn/alerting-plugin/common';
import { SanitizedDefaultRuleAction } from '@kbn/alerting-plugin/common';
import { BASE_ALERTING_API_PATH } from '../../constants';
import { Rule, RuleUpdates } from '../../../types';
import { transformRule } from './common_transformations';
Expand All @@ -19,28 +19,19 @@ type RuleUpdatesBody = Pick<
const rewriteBodyRequest: RewriteResponseCase<RuleUpdatesBody> = ({ actions, ...res }): any => ({
...res,
actions: actions.map((action) => {
if (isSystemAction(action)) {
const { id, params, uuid, useAlertDataForTemplate } = action;
return {
id,
params,
...(typeof useAlertDataForTemplate !== 'undefined'
? { use_alert_data_for_template: useAlertDataForTemplate }
: {}),
...(uuid && { uuid }),
};
}
const { group, id, params, frequency, uuid, alertsFilter, useAlertDataForTemplate } =
action as SanitizedDefaultRuleAction;
return {
group,
id,
params,
frequency: {
notify_when: frequency!.notifyWhen,
throttle: frequency!.throttle,
summary: frequency!.summary,
},
frequency: frequency
? {
notify_when: frequency!.notifyWhen,
throttle: frequency!.throttle,
summary: frequency!.summary,
}
: undefined,
alerts_filter: alertsFilter,
...(typeof useAlertDataForTemplate !== 'undefined'
? { use_alert_data_for_template: useAlertDataForTemplate }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@

import React, { useReducer, useState, useEffect, useCallback } from 'react';
import { FormattedMessage } from '@kbn/i18n-react';
import {
isSystemAction,
RuleActionTypes,
RuleNotifyWhen,
SanitizedRuleAction,
} from '@kbn/alerting-plugin/common';
import { RuleNotifyWhen, SanitizedRuleAction } from '@kbn/alerting-plugin/common';
import {
EuiTitle,
EuiFlyoutHeader,
Expand Down Expand Up @@ -86,45 +81,15 @@ const cloneAndMigrateRule = (initialRule: Rule) => {
initialRule.notifyWhen === RuleNotifyWhen.THROTTLE ? initialRule.throttle! : null,
}
: { summary: false, notifyWhen: RuleNotifyWhen.THROTTLE, throttle: initialRule.throttle! };
clonedRule.actions = clonedRule.actions.map(
// Clone untyped actions and add a type property to them. This is necessary for the downstream rewrite functions.
(action: SanitizedRuleAction | Omit<SanitizedRuleAction, 'type'>) => {
if ('type' in action) {
return isSystemAction(action)
? action
: ({
...action,
frequency,
} as SanitizedRuleAction);
}
if (actionHasDefinedGroup(action as SanitizedRuleAction)) {
return {
...action,
frequency,
type: RuleActionTypes.DEFAULT,
} as SanitizedRuleAction;
}
clonedRule.actions = clonedRule.actions.map((action: SanitizedRuleAction) => {
if (actionHasDefinedGroup(action as SanitizedRuleAction)) {
return {
...action,
type: RuleActionTypes.SYSTEM,
frequency,
} as SanitizedRuleAction;
}
);
} else {
// Clone untyped actions and add a type property to them. This is necessary for the downstream rewrite functions.
// Note that this type is supposed to be stripped before sending to the server. This type property is used
// to tell the downstream rewrite functions whether to include properties such as frequency, alertsFilter, etc.
clonedRule.actions = clonedRule.actions.map(
(action: SanitizedRuleAction | Omit<SanitizedRuleAction, 'type'>) =>
'type' in action
? action
: ({
...action,
type: actionHasDefinedGroup(action as SanitizedRuleAction)
? RuleActionTypes.DEFAULT
: RuleActionTypes.SYSTEM,
} as SanitizedRuleAction)
);
return action as SanitizedRuleAction;
});
}
return clonedRule;
};
Expand Down

0 comments on commit 51bc88b

Please sign in to comment.