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

[ML] Improving model snapshot revert UI experience #88588

Merged
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { FC, useEffect, useCallback, useState } from 'react';
import React, { FC, useEffect, useCallback, useState, useRef } from 'react';
import { i18n } from '@kbn/i18n';

import {
Expand Down Expand Up @@ -50,15 +50,24 @@ export const ModelSnapshotTable: FC<Props> = ({ job, refreshJobList }) => {
const [closeJobModalVisible, setCloseJobModalVisible] = useState<ModelSnapshot | null>(null);
const [combinedJobState, setCombinedJobState] = useState<COMBINED_JOB_STATE | null>(null);

const isMounted = useRef(true);
useEffect(() => {
loadModelSnapshots();
return () => {
isMounted.current = false;
};
}, []);

const loadModelSnapshots = useCallback(async () => {
async function loadModelSnapshots() {
if (isMounted.current === false) {
// table refresh can be triggered a while after a snapshot revert has been triggered.
// ensure the table is still visible before attempted to refresh it.
return;
}
const { model_snapshots: ms } = await ml.getModelSnapshots(job.job_id);
setSnapshots(ms);
setSnapshotsLoaded(true);
}, [job]);
}

const checkJobIsClosed = useCallback(
async (snapshot: ModelSnapshot) => {
Expand Down Expand Up @@ -107,13 +116,14 @@ export const ModelSnapshotTable: FC<Props> = ({ job, refreshJobList }) => {
}
}, []);

const closeRevertFlyout = useCallback((reload: boolean) => {
const closeRevertFlyout = useCallback(() => {
setRevertSnapshot(null);
if (reload) {
loadModelSnapshots();
// wait half a second before refreshing the jobs list
setTimeout(refreshJobList, 500);
}
}, []);

const refresh = useCallback(() => {
loadModelSnapshots();
// wait half a second before refreshing the jobs list
setTimeout(refreshJobList, 500);
}, []);

const columns: Array<EuiBasicTableColumn<any>> = [
Expand Down Expand Up @@ -231,6 +241,7 @@ export const ModelSnapshotTable: FC<Props> = ({ job, refreshJobList }) => {
snapshots={snapshots}
job={job}
closeFlyout={closeRevertFlyout}
refresh={refresh}
/>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,17 @@ interface Props {
snapshot: ModelSnapshot;
snapshots: ModelSnapshot[];
job: CombinedJobWithStats;
closeFlyout(reload: boolean): void;
closeFlyout(): void;
refresh(): void;
}

export const RevertModelSnapshotFlyout: FC<Props> = ({ snapshot, snapshots, job, closeFlyout }) => {
export const RevertModelSnapshotFlyout: FC<Props> = ({
snapshot,
snapshots,
job,
closeFlyout,
refresh,
}) => {
const { toasts } = useNotifications();
const { loadAnomalyDataForJob, loadEventRateForJob } = useMemo(
() => chartLoaderProvider(mlResultsService),
Expand All @@ -73,7 +80,6 @@ export const RevertModelSnapshotFlyout: FC<Props> = ({ snapshot, snapshots, job,
const [eventRateData, setEventRateData] = useState<LineChartPoint[]>([]);
const [anomalies, setAnomalies] = useState<Anomaly[]>([]);
const [chartReady, setChartReady] = useState(false);
const [applying, setApplying] = useState(false);

useEffect(() => {
createChartData();
Expand Down Expand Up @@ -110,13 +116,6 @@ export const RevertModelSnapshotFlyout: FC<Props> = ({ snapshot, snapshots, job,
setChartReady(true);
}, [job]);

function closeWithReload() {
closeFlyout(true);
}
function closeWithoutReload() {
closeFlyout(false);
}

function showRevertModal() {
setRevertModalVisible(true);
}
Expand All @@ -125,7 +124,6 @@ export const RevertModelSnapshotFlyout: FC<Props> = ({ snapshot, snapshots, job,
}

async function applyRevert() {
setApplying(true);
const end =
replay && runInRealTime === false ? job.data_counts.latest_record_timestamp : undefined;
try {
Expand All @@ -138,17 +136,19 @@ export const RevertModelSnapshotFlyout: FC<Props> = ({ snapshot, snapshots, job,
}))
: undefined;

await ml.jobs.revertModelSnapshot(
job.job_id,
currentSnapshot.snapshot_id,
replay,
end,
events
);
ml.jobs
.revertModelSnapshot(job.job_id, currentSnapshot.snapshot_id, replay, end, events)
.then(() => {
toasts.addSuccess(
i18n.translate('xpack.ml.revertModelSnapshotFlyout.revertSuccessTitle', {
defaultMessage: 'Model snapshot revert successful',
})
);
refresh();
});
hideRevertModal();
closeWithReload();
closeFlyout();
} catch (error) {
setApplying(false);
toasts.addError(new Error(error.body.message), {
title: i18n.translate('xpack.ml.revertModelSnapshotFlyout.revertErrorTitle', {
defaultMessage: 'Model snapshot revert failed',
Expand All @@ -166,7 +166,7 @@ export const RevertModelSnapshotFlyout: FC<Props> = ({ snapshot, snapshots, job,

return (
<>
<EuiFlyout onClose={closeWithoutReload} hideCloseButton size="m">
<EuiFlyout onClose={closeFlyout} hideCloseButton size="m">
<EuiFlyoutHeader hasBorder>
<EuiTitle size="s">
<h5>
Expand Down Expand Up @@ -347,7 +347,7 @@ export const RevertModelSnapshotFlyout: FC<Props> = ({ snapshot, snapshots, job,
<EuiFlyoutFooter>
<EuiFlexGroup justifyContent="spaceBetween">
<EuiFlexItem grow={false}>
<EuiButtonEmpty iconType="cross" onClick={closeWithoutReload} flush="left">
<EuiButtonEmpty iconType="cross" onClick={closeFlyout} flush="left">
<FormattedMessage
id="xpack.ml.newJob.wizard.revertModelSnapshotFlyout.closeButton"
defaultMessage="Close"
Expand Down Expand Up @@ -392,10 +392,14 @@ export const RevertModelSnapshotFlyout: FC<Props> = ({ snapshot, snapshots, job,
defaultMessage: 'Apply',
}
)}
confirmButtonDisabled={applying}
buttonColor="danger"
defaultFocusedButton="confirm"
/>
>
<FormattedMessage
id="xpack.ml.newJob.wizard.revertModelSnapshotFlyout.modalBody"
defaultMessage="The snapshot revert will be carried out in the background and may take some time."
/>
</EuiConfirmModal>
</EuiOverlayMask>
)}
</>
Expand Down