Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] [Uptime] Ml anomaly alert edit (#76909) #78749

Merged
merged 1 commit into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import { Alert, AlertEdit } from '../../../../../../plugins/triggers_actions_ui/public';

interface Props {
alertFlyoutVisible: boolean;
initialAlert: Alert;
setAlertFlyoutVisibility: React.Dispatch<React.SetStateAction<boolean>>;
}

export const UptimeEditAlertFlyoutComponent = ({
alertFlyoutVisible,
initialAlert,
setAlertFlyoutVisibility,
}: Props) => {
const onClose = () => {
setAlertFlyoutVisibility(false);
};
return alertFlyoutVisible ? <AlertEdit initialAlert={initialAlert} onClose={onClose} /> : null;
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ import { useMonitorId } from '../../../hooks';
import { setAlertFlyoutType, setAlertFlyoutVisible } from '../../../state/actions';
import { useAnomalyAlert } from './use_anomaly_alert';
import { ConfirmAlertDeletion } from './confirm_alert_delete';
import { deleteAnomalyAlertAction } from '../../../state/alerts/alerts';
import {
deleteAnomalyAlertAction,
getAnomalyAlertAction,
isAnomalyAlertDeleting,
} from '../../../state/alerts/alerts';
import { UptimeEditAlertFlyoutComponent } from '../../common/alerts/uptime_edit_alert_flyout';

interface Props {
hasMLJob: boolean;
Expand All @@ -33,11 +38,14 @@ interface Props {
export const ManageMLJobComponent = ({ hasMLJob, onEnableJob, onJobDelete }: Props) => {
const [isPopOverOpen, setIsPopOverOpen] = useState(false);

const [isFlyoutOpen, setIsFlyoutOpen] = useState(false);

const { basePath } = useContext(UptimeSettingsContext);

const canDeleteMLJob = useSelector(canDeleteMLJobSelector);

const isMLJobCreating = useSelector(isMLJobCreatingSelector);
const isAlertDeleting = useSelector(isAnomalyAlertDeleting);

const { loading: isMLJobLoading } = useSelector(hasMLJobSelector);

Expand All @@ -54,7 +62,7 @@ export const ManageMLJobComponent = ({ hasMLJob, onEnableJob, onJobDelete }: Pro
const deleteAnomalyAlert = () =>
dispatch(deleteAnomalyAlertAction.get({ alertId: anomalyAlert?.id as string }));

const showLoading = isMLJobCreating || isMLJobLoading;
const showLoading = isMLJobCreating || isMLJobLoading || isAlertDeleting;

const btnText = hasMLJob ? labels.ANOMALY_DETECTION : labels.ENABLE_ANOMALY_DETECTION;

Expand All @@ -63,7 +71,7 @@ export const ManageMLJobComponent = ({ hasMLJob, onEnableJob, onJobDelete }: Pro
data-test-subj={hasMLJob ? 'uptimeManageMLJobBtn' : 'uptimeEnableAnomalyBtn'}
onClick={hasMLJob ? () => setIsPopOverOpen(true) : onEnableJob}
disabled={hasMLJob && !canDeleteMLJob}
isLoading={isMLJobCreating || isMLJobLoading}
isLoading={showLoading}
size="s"
aria-label={labels.ENABLE_MANAGE_JOB}
>
Expand All @@ -85,21 +93,27 @@ export const ManageMLJobComponent = ({ hasMLJob, onEnableJob, onJobDelete }: Pro
dateRange: { from: dateRangeStart, to: dateRangeEnd },
}),
},
{
name: anomalyAlert ? labels.DISABLE_ANOMALY_ALERT : labels.ENABLE_ANOMALY_ALERT,
'data-test-subj': anomalyAlert
? 'uptimeDisableAnomalyAlertBtn'
: 'uptimeEnableAnomalyAlertBtn',
icon: <EuiIcon type={anomalyAlert ? 'bellSlash' : 'bell'} size="m" />,
onClick: () => {
if (anomalyAlert) {
setIsConfirmAlertDeleteOpen(true);
} else {
dispatch(setAlertFlyoutType(CLIENT_ALERT_TYPES.DURATION_ANOMALY));
dispatch(setAlertFlyoutVisible(true));
}
},
},
...(anomalyAlert
? [
{
name: 'Anomaly alert',
icon: 'bell',
'data-test-subj': 'uptimeManageAnomalyAlertBtn',
panel: 1,
},
]
: [
{
name: labels.ENABLE_ANOMALY_ALERT,
'data-test-subj': 'uptimeEnableAnomalyAlertBtn',
icon: 'bell',
onClick: () => {
dispatch(setAlertFlyoutType(CLIENT_ALERT_TYPES.DURATION_ANOMALY));
dispatch(setAlertFlyoutVisible(true));
setIsPopOverOpen(false);
},
},
]),
{
name: labels.DISABLE_ANOMALY_DETECTION,
'data-test-subj': 'uptimeDeleteMLJobBtn',
Expand All @@ -111,6 +125,27 @@ export const ManageMLJobComponent = ({ hasMLJob, onEnableJob, onJobDelete }: Pro
},
],
},
{
id: 1,
title: 'Anomaly alert',
items: [
{
name: 'Edit',
'data-test-subj': 'uptimeEditAnomalyAlertBtn',
onClick: () => {
setIsFlyoutOpen(true);
setIsPopOverOpen(false);
},
},
{
name: 'Disable',
'data-test-subj': 'uptimeDisableAnomalyAlertBtn',
onClick: () => {
setIsConfirmAlertDeleteOpen(true);
},
},
],
},
];

return (
Expand Down Expand Up @@ -138,6 +173,14 @@ export const ManageMLJobComponent = ({ hasMLJob, onEnableJob, onJobDelete }: Pro
}}
/>
)}
<UptimeEditAlertFlyoutComponent
initialAlert={anomalyAlert!}
alertFlyoutVisible={isFlyoutOpen}
setAlertFlyoutVisibility={() => {
setIsFlyoutOpen(false);
dispatch(getAnomalyAlertAction.get({ monitorId }));
}}
/>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useSelector } from 'react-redux';
import React, { useEffect, useState } from 'react';
import { AnomalyTranslations } from './translations';
import { AlertExpressionPopover } from '../alert_expression_popover';
import { DEFAULT_SEVERITY, SelectSeverity } from './select_severity';
import { DEFAULT_SEVERITY, SelectSeverity, SEVERITY_OPTIONS } from './select_severity';
import { monitorIdSelector } from '../../../../state/selectors';
import { getSeverityColor, getSeverityType } from '../../../../../../ml/public';

Expand All @@ -40,6 +40,14 @@ export function AnomalyAlertComponent({ setAlertParams, alertParams }: Props) {
setAlertParams('severity', severity.val);
}, [severity, setAlertParams]);

useEffect(() => {
if (alertParams.severity !== undefined) {
setSeverity(SEVERITY_OPTIONS.find(({ val }) => val === alertParams.severity)!);
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
<>
<EuiSpacer size="l" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ export const initDurationAnomalyAlertType: AlertTypeInitializer = ({
name,
validate: () => ({ errors: {} }),
defaultActionMessage,
requiresAppContext: false,
requiresAppContext: true,
});
1 change: 1 addition & 0 deletions x-pack/plugins/uptime/public/state/alerts/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,4 @@ export const alertsSelector = ({ alerts }: AppState) => alerts.alerts;
export const isAlertDeletedSelector = ({ alerts }: AppState) => alerts.alertDeletion;

export const anomalyAlertSelector = ({ alerts }: AppState) => alerts.anomalyAlert;
export const isAnomalyAlertDeleting = ({ alerts }: AppState) => alerts.anomalyAlertDeletion.loading;
1 change: 0 additions & 1 deletion x-pack/test/functional/apps/uptime/ml_anomaly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export default ({ getService }: FtrProviderContext) => {

it('can create job successfully', async () => {
await uptime.ml.createMLJob();
// await uptime.navigation.refreshApp();
});

it('can open ML Manage Menu', async () => {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/test/functional/services/uptime/ml_anomaly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export function UptimeMLAnomalyProvider({ getService }: FtrProviderContext) {
return await testSubjects.click('uptimeEnableAnomalyAlertBtn');
},

async disableAnomalyAlertIsVisible() {
return await testSubjects.exists('uptimeDisableAnomalyAlertBtn');
async manageAnomalyAlertIsVisible() {
return await testSubjects.exists('uptimeManageAnomalyAlertBtn');
},

async changeAlertThreshold(level: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {

it('change button to disable anomaly alert', async () => {
await uptime.ml.openMLManageMenu();
expect(uptime.ml.disableAnomalyAlertIsVisible()).to.eql(true);
expect(uptime.ml.manageAnomalyAlertIsVisible()).to.eql(true);
});

it('can delete job successfully', async () => {
Expand Down