Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

add throttle on action level #45

Merged
merged 11 commits into from
May 14, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import React from 'react';
import { EuiFlexItem, EuiFlexGroup } from '@elastic/eui';

import { FormikFieldNumber, FormikSelect } from '../../../../../components/FormControls';
import { isInvalid, hasError, validateInterval, validateUnit } from '../../../../../utils/validate';
import {
isInvalid,
hasError,
validatePositiveInteger,
validateUnit,
} from '../../../../../utils/validate';

const unitOptions = [
{ value: 'MINUTES', text: 'Minutes' },
Expand All @@ -35,7 +40,7 @@ const Interval = () => (
<FormikFieldNumber
name="period.interval"
formRow
fieldProps={{ validate: validateInterval }}
fieldProps={{ validate: validatePositiveInteger }}
rowProps={{
label: 'Every',
isInvalid,
Expand Down
1 change: 0 additions & 1 deletion public/pages/CreateTrigger/components/Action/Action.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ const Action = ({
helpText: 'Choose destination for an action.',
isInvalid,
error: hasError,
style: { marginTop: '0px' },
}}
inputProps={{
placeholder: 'Select a destination',
Expand Down
59 changes: 57 additions & 2 deletions public/pages/CreateTrigger/components/Action/actions/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

import React from 'react';
import _ from 'lodash';
import Mustache from 'mustache';
import {
EuiFlexGroup,
Expand All @@ -25,8 +26,18 @@ import {
EuiTextArea,
} from '@elastic/eui';

import { FormikTextArea, FormikFieldText } from '../../../../../components/FormControls';
import { isInvalid, hasError, required } from '../../../../../utils/validate';
import {
FormikTextArea,
FormikFieldText,
FormikSwitch,
FormikFieldNumber,
} from '../../../../../components/FormControls';
import {
isInvalid,
validateActionThrottle,
hasError,
required,
} from '../../../../../utils/validate';
import { URL } from '../../../../../../utils/constants';

const messageHelpText = (index, sendTestMessage) => (
Expand Down Expand Up @@ -122,6 +133,50 @@ const Message = ({
</EuiFormRow>

<EuiSpacer size="s" />

<EuiFormRow
label={
<div>
<span>Action throttling</span>
<EuiButtonEmpty
size="s"
onClick={() => {
setFlyout({ type: 'messageFrequency' });
}}
>
Info
</EuiButtonEmpty>
</div>
}
style={{ maxWidth: '100%' }}
>
<EuiFlexGroup alignItems="center">
<EuiFlexItem grow={false} style={{ marginRight: '0px' }}>
<FormikSwitch
name={`actions.${index}.throttle_enabled`}
inputProps={{ label: 'Maximum Frequency' }}
/>
</EuiFlexItem>
<EuiFlexItem grow={false} style={{ marginLeft: '4px', marginRight: '4px' }}>
<FormikFieldNumber
name={`actions.${index}.throttle.value`}
fieldProps={{ validate: validateActionThrottle(action) }}
inputProps={{
style: { width: '100px' },
min: 1,
compressed: true,
disabled: !_.get(action, `throttle_enabled`) ? 'disabled' : '',
isInvalid: (name, form) =>
_.get(form.values, `actions.${index}.throttle_enabled`) &&
!_.get(form.values, name),
}}
/>
</EuiFlexItem>
<EuiFlexItem grow={false} style={{ marginLeft: '0px' }}>
<EuiText size="s">minutes.</EuiText>
</EuiFlexItem>
</EuiFlexGroup>
</EuiFormRow>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import React from 'react';
import { render, shallow, mount } from 'enzyme';
import { Formik } from 'formik';
import Message from './Message';
import Mustache from 'mustache';

jest.mock('@elastic/eui/lib/components/form/form_row/make_id', () => () => 'testing-id');

function getRenderWrapper(customProps = {}) {
return render(
<Formik
render={() => (
<Message
action={{
message_template: {
source:
'Monitor {{ctx.monitor.name}} just entered alert status. Please investigate the issue.\n- Trigger: {{ctx.trigger.name}}\n- Severity: {{ctx.trigger.severity}}\n- Period start: {{ctx.periodStart}}\n- Period end: {{ctx.periodEnd}}',
lang: 'mustache',
},
}}
context={{}}
index={0}
sendTestMessage={jest.fn()}
setFlyout={jest.fn()}
/>
)}
/>
);
}

describe('Message', () => {
test('renders', () => {
const wrapper = getRenderWrapper();
expect(wrapper).toMatchSnapshot();
});
});
Loading