Skip to content

Commit

Permalink
Lock throttle interval to check interval
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacqary committed Dec 19, 2022
1 parent dfdf80f commit 40ce55a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface ActionAccordionFormProps {
isActionGroupDisabledForActionType?: (actionGroupId: string, actionTypeId: string) => boolean;
hideActionHeader?: boolean;
hideNotifyWhen?: boolean;
minimumThrottleInterval?: [number | undefined, string];
}

interface ActiveActionConnectorState {
Expand All @@ -91,6 +92,7 @@ export const ActionForm = ({
isActionGroupDisabledForActionType,
hideActionHeader,
hideNotifyWhen,
minimumThrottleInterval,
}: ActionAccordionFormProps) => {
const {
http,
Expand Down Expand Up @@ -396,6 +398,7 @@ export const ActionForm = ({
setActiveActionItem(undefined);
}}
hideNotifyWhen={hideNotifyWhen}
minimumThrottleInterval={minimumThrottleInterval}
/>
);
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { useState, useEffect, useCallback } from 'react';
import React, { useState, useEffect, useCallback, useMemo } from 'react';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import {
Expand Down Expand Up @@ -130,7 +130,6 @@ export const ActionNotifyWhen = ({
onNotifyWhenChange,
onThrottleChange,
}: RuleNotifyWhenProps) => {
const [ruleThrottle, setRuleThrottle] = useState<number>(throttle || 1);
const [showCustomThrottleOpts, setShowCustomThrottleOpts] = useState<boolean>(false);
const [notifyWhenValue, setNotifyWhenValue] =
useState<RuleNotifyWhenType>(DEFAULT_NOTIFY_WHEN_VALUE);
Expand All @@ -154,11 +153,11 @@ export const ActionNotifyWhen = ({
setNotifyWhenValue(newValue);
setTimeout(
() =>
onThrottleChange(newValue === 'onThrottleInterval' ? ruleThrottle : null, throttleUnit),
onThrottleChange(newValue === 'onThrottleInterval' ? throttle ?? 1 : null, throttleUnit),
100
);
},
[onNotifyWhenChange, setNotifyWhenValue, onThrottleChange, ruleThrottle, throttleUnit]
[onNotifyWhenChange, setNotifyWhenValue, onThrottleChange, throttle, throttleUnit]
);

const labelForRuleRenotify = [
Expand Down Expand Up @@ -194,7 +193,7 @@ export const ActionNotifyWhen = ({
<EuiFlexItem grow={2}>
<EuiFieldNumber
min={1}
value={ruleThrottle}
value={throttle ?? 1}
name="throttle"
data-test-subj="throttleInput"
prepend={i18n.translate(
Expand All @@ -210,7 +209,6 @@ export const ActionNotifyWhen = ({
map((value) => parseInt(value, 10)),
filter((value) => !isNaN(value)),
map((value) => {
setRuleThrottle(value);
onThrottleChange(value, throttleUnit);
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { ActionVariable, RuleActionParam } from '@kbn/alerting-plugin/common';
import {
getDurationNumberInItsUnit,
getDurationUnitValue,
parseDuration,
} from '@kbn/alerting-plugin/common/parse_duration';
import { betaBadgeProps } from './beta_badge_props';
import {
Expand Down Expand Up @@ -65,6 +66,7 @@ export type ActionTypeFormProps = {
recoveryActionGroup?: string;
isActionGroupDisabledForActionType?: (actionGroupId: string, actionTypeId: string) => boolean;
hideNotifyWhen?: boolean;
minimumThrottleInterval?: [number | undefined, string];
} & Pick<
ActionAccordionFormProps,
| 'defaultActionGroupId'
Expand Down Expand Up @@ -102,6 +104,7 @@ export const ActionTypeForm = ({
isActionGroupDisabledForActionType,
recoveryActionGroup,
hideNotifyWhen = false,
minimumThrottleInterval,
}: ActionTypeFormProps) => {
const {
application: { capabilities },
Expand All @@ -123,6 +126,10 @@ export const ActionTypeForm = ({
const [actionThrottleUnit, setActionThrottleUnit] = useState<string>(
actionItem.frequency?.throttle ? getDurationUnitValue(actionItem.frequency?.throttle) : 'h'
);
const [minimumActionThrottle = -1, minimumActionThrottleUnit] = minimumThrottleInterval ?? [
-1,
's',
];

const getDefaultParams = async () => {
const connectorType = await actionTypeRegistry.get(actionItem.actionTypeId);
Expand All @@ -138,6 +145,40 @@ export const ActionTypeForm = ({
return defaultParams;
};

const getBoundThrottle = useCallback(
(throttle: number | null, throttleUnit: string) => {
try {
const throttleUnitDuration = parseDuration(`1${throttleUnit}`);
const minThrottleUnitDuration = parseDuration(`1${minimumActionThrottleUnit}`);
const boundedThrottle =
throttleUnitDuration > minThrottleUnitDuration || !throttle
? throttle
: Math.max(throttle, minimumActionThrottle);
const boundedThrottleUnit =
throttleUnitDuration >= minThrottleUnitDuration
? throttleUnit
: minimumActionThrottleUnit;
return [boundedThrottle, boundedThrottleUnit] as [number | null, string];
} catch (e) {
return [throttle, throttleUnit] as [number | null, string];
}
},
[minimumActionThrottle, minimumActionThrottleUnit]
);

useEffect(() => {
const [boundThrottle, boundThrottleUnit] = getBoundThrottle(actionThrottle, actionThrottleUnit);
if (boundThrottle !== actionThrottle) setActionThrottle(boundThrottle);
if (boundThrottleUnit !== actionThrottleUnit) setActionThrottleUnit(boundThrottleUnit);
}, [
getBoundThrottle,
actionThrottle,
actionThrottleUnit,
setActionThrottle,
setActionThrottleUnit,
minimumThrottleInterval,
]);

useEffect(() => {
(async () => {
setAvailableActionVariables(
Expand Down Expand Up @@ -178,13 +219,6 @@ export const ActionTypeForm = ({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [actionItem]);

// useEffect(() => {
// if (!actionItem.frequency) {
// setActionFrequency(DEFAULT_FREQUENCY, index);
// }
// // eslint-disable-next-line react-hooks/exhaustive-deps
// }, [actionItem.frequency]);

const canSave = hasSaveActionsCapability(capabilities);

const actionGroupDisplay = (
Expand Down Expand Up @@ -222,15 +256,16 @@ export const ActionTypeForm = ({
)}
onThrottleChange={useCallback(
(throttle: number | null, throttleUnit: string) => {
setActionThrottle(throttle);
setActionThrottleUnit(throttleUnit);
const [boundedThrottle, boundedThrottleUnit] = getBoundThrottle(throttle, throttleUnit);
setActionThrottle(boundedThrottle);
setActionThrottleUnit(boundedThrottleUnit);
setActionFrequencyProperty(
'throttle',
throttle ? `${throttle}${throttleUnit}` : null,
boundedThrottle ? `${boundedThrottle}${boundedThrottleUnit}` : null,
index
);
},
[setActionFrequencyProperty, index]
[setActionFrequencyProperty, index, getBoundThrottle]
)}
/>
);
Expand All @@ -254,7 +289,6 @@ export const ActionTypeForm = ({
<>
{showSelectActionGroup && (
<>
<EuiSpacer size="xs" />
<EuiSuperSelect
prepend={
<EuiFormLabel htmlFor={`addNewActionConnectorActionGroup-${actionItem.actionTypeId}`}>
Expand All @@ -279,6 +313,7 @@ export const ActionTypeForm = ({
setActionGroup(group);
}}
/>
{!hideNotifyWhen && <EuiSpacer size="xs" />}
</>
)}
{!hideNotifyWhen && actionNotifyWhen}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ export const RuleForm = ({
setActionParamsProperty={setActionParamsProperty}
actionTypeRegistry={actionTypeRegistry}
setActionFrequencyProperty={setActionFrequencyProperty}
minimumThrottleInterval={[ruleInterval, ruleIntervalUnit]}
/>
</>
) : null}
Expand Down

0 comments on commit 40ce55a

Please sign in to comment.