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] Single Metric Viewer embeddable: fixes job refetch on error #199726

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { MlJob, MlJobStats } from '@elastic/elasticsearch/lib/api/types';
import { DatePickerContextProvider, type DatePickerDependencies } from '@kbn/ml-date-picker';
import type { TimeRangeBounds } from '@kbn/ml-time-buckets';
import usePrevious from 'react-use/lib/usePrevious';
import { extractErrorProperties } from '@kbn/ml-error-utils';
import { tz } from 'moment';
import { pick, throttle } from 'lodash';
import type { MlDependencies } from '../../application/app';
Expand All @@ -39,10 +40,17 @@ interface AppStateZoom {
to?: string;
}

const errorMessage = i18n.translate('xpack.ml.singleMetricViewerEmbeddable.errorMessage"', {
const basicErrorMessage = i18n.translate('xpack.ml.singleMetricViewerEmbeddable.errorMessage"', {
defaultMessage: 'Unable to load the ML single metric viewer data',
});

const jobNotFoundErrorMessage = i18n.translate(
'xpack.ml.singleMetricViewerEmbeddable.jobNotFoundErrorMessage"',
{
defaultMessage: 'No known job with the selected id',
}
);

export type SingleMetricViewerSharedComponent = FC<SingleMetricViewerProps>;

/**
Expand Down Expand Up @@ -106,6 +114,7 @@ const SingleMetricViewerWrapper: FC<SingleMetricViewerPropsWithDeps> = ({
const [selectedJobWrapper, setSelectedJobWrapper] = useState<
{ job: MlJob; stats: MlJobStats } | undefined
>();
const [errorEncountered, setErrorEncountered] = useState<boolean>(false);

const isMounted = useMountedState();
const { mlApi, mlTimeSeriesExplorerService, toastNotificationService } = mlServices;
Expand All @@ -130,18 +139,24 @@ const SingleMetricViewerWrapper: FC<SingleMetricViewerPropsWithDeps> = ({
]);
setSelectedJobWrapper({ job: jobs[0], stats: jobStats[0] });
} catch (e) {
const error = extractErrorProperties(e);
// Could get 404 because job has been deleted
setErrorEncountered(true);

if (onError) {
onError(new Error(errorMessage));
onError(
new Error(error.statusCode === 404 ? jobNotFoundErrorMessage : basicErrorMessage)
);
}
}
}
}
if (isMounted() === false) {
if (isMounted() === false || errorEncountered) {
return;
}
fetchSelectedJob();
},
[selectedJobId, mlApi, isMounted, onError]
[errorEncountered, selectedJobId, mlApi, isMounted, onError]
peteharverson marked this conversation as resolved.
Show resolved Hide resolved
);
// eslint-disable-next-line react-hooks/exhaustive-deps
const resizeHandler = useCallback(
Expand Down