[8.16] [ML] Single Metric Viewer embeddable: fix job refetch on error (#199726) (#200766)

# Backport

This will backport the following commits from `main` to `8.16`:
- [[ML] Single Metric Viewer embeddable: fix job refetch on error
(#199726)](https://github.com/elastic/kibana/pull/199726)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Melissa
Alvarez","email":"melissa.alvarez@elastic.co"},"sourceCommit":{"committedDate":"2024-11-19T15:57:56Z","message":"[ML]
Single Metric Viewer embeddable: fix job refetch on error
(#199726)\n\n## Summary\r\n\r\nThis PR ensures that the job fetch is not
reattempted continuously when\r\nerrors are encountered.\r\n\r\nTo test,
add a Single Metric Viewer panel to a dashboard, then delete\r\nthe job
the panel depends on. You can then refresh the browser and see\r\nthe
error message about the job not being found.\r\n\r\nError panel for
generic error and for no known job error: \r\n<img width=\"1308\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/6d6d6972-ac1a-479c-a5dc-8d770026ca4e\">\r\n\r\n\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] Any UI touched in this PR is
usable by keyboard only (learn more\r\nabout [keyboard
accessibility](https://webaim.org/techniques/keyboard/))\r\n- [ ] Any UI
touched in this PR does not create any new axe failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n---------\r\n\r\nCo-authored-by:
Elastic Machine
<elasticmachine@users.noreply.github.com>","sha":"ddf324a9a055bf9ab5a6ba3e9a5f7c36d1e7f9ae","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix",":ml","Feature:Anomaly
Detection","v9.0.0","Feature:Embeddables","backport:version","v8.17.0","v8.16.1"],"title":"[ML]
Single Metric Viewer embeddable: fix job refetch on
error","number":199726,"url":"https://github.com/elastic/kibana/pull/199726","mergeCommit":{"message":"[ML]
Single Metric Viewer embeddable: fix job refetch on error
(#199726)\n\n## Summary\r\n\r\nThis PR ensures that the job fetch is not
reattempted continuously when\r\nerrors are encountered.\r\n\r\nTo test,
add a Single Metric Viewer panel to a dashboard, then delete\r\nthe job
the panel depends on. You can then refresh the browser and see\r\nthe
error message about the job not being found.\r\n\r\nError panel for
generic error and for no known job error: \r\n<img width=\"1308\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/6d6d6972-ac1a-479c-a5dc-8d770026ca4e\">\r\n\r\n\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] Any UI touched in this PR is
usable by keyboard only (learn more\r\nabout [keyboard
accessibility](https://webaim.org/techniques/keyboard/))\r\n- [ ] Any UI
touched in this PR does not create any new axe failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n---------\r\n\r\nCo-authored-by:
Elastic Machine
<elasticmachine@users.noreply.github.com>","sha":"ddf324a9a055bf9ab5a6ba3e9a5f7c36d1e7f9ae"}},"sourceBranch":"main","suggestedTargetBranches":["8.x","8.16"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/199726","number":199726,"mergeCommit":{"message":"[ML]
Single Metric Viewer embeddable: fix job refetch on error
(#199726)\n\n## Summary\r\n\r\nThis PR ensures that the job fetch is not
reattempted continuously when\r\nerrors are encountered.\r\n\r\nTo test,
add a Single Metric Viewer panel to a dashboard, then delete\r\nthe job
the panel depends on. You can then refresh the browser and see\r\nthe
error message about the job not being found.\r\n\r\nError panel for
generic error and for no known job error: \r\n<img width=\"1308\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/6d6d6972-ac1a-479c-a5dc-8d770026ca4e\">\r\n\r\n\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] Any UI touched in this PR is
usable by keyboard only (learn more\r\nabout [keyboard
accessibility](https://webaim.org/techniques/keyboard/))\r\n- [ ] Any UI
touched in this PR does not create any new axe failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n---------\r\n\r\nCo-authored-by:
Elastic Machine
<elasticmachine@users.noreply.github.com>","sha":"ddf324a9a055bf9ab5a6ba3e9a5f7c36d1e7f9ae"}},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.16","label":"v8.16.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Melissa Alvarez <melissa.alvarez@elastic.co>
This commit is contained in:
Kibana Machine 2024-11-20 04:47:05 +11:00 committed by GitHub
parent 5ea363173d
commit d2fc28cf92
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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';
@ -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>;
/**
@ -67,7 +75,7 @@ export interface SingleMetricViewerProps {
*/
lastRefresh?: number;
onRenderComplete?: () => void;
onError?: (error: Error) => void;
onError?: (error?: Error) => void;
onForecastIdChange?: (forecastId: string | undefined) => void;
uuid: string;
}
@ -106,6 +114,7 @@ const SingleMetricViewerWrapper: FC<SingleMetricViewerPropsWithDeps> = ({
const [selectedJobWrapper, setSelectedJobWrapper] = useState<
{ job: MlJob; stats: MlJobStats } | undefined
>();
const [errorEncountered, setErrorEncountered] = useState<number | undefined>();
const isMounted = useMountedState();
const { mlApi, mlTimeSeriesExplorerService, toastNotificationService } = mlServices;
@ -119,6 +128,16 @@ const SingleMetricViewerWrapper: FC<SingleMetricViewerPropsWithDeps> = ({
const previousRefresh = usePrevious(lastRefresh ?? 0);
useEffect(
function resetErrorOnJobChange() {
// Calling onError to clear any previous error
setErrorEncountered(undefined);
onError?.();
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[selectedJobId]
);
useEffect(
function setUpSelectedJob() {
async function fetchSelectedJob() {
@ -130,19 +149,26 @@ 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 and also avoid infinite refetches on any error
setErrorEncountered(error.statusCode);
if (onError) {
onError(new Error(errorMessage));
onError(
new Error(errorEncountered === 404 ? jobNotFoundErrorMessage : basicErrorMessage)
);
}
}
}
}
if (isMounted() === false) {
if (isMounted() === false || errorEncountered !== undefined) {
return;
}
fetchSelectedJob();
},
[selectedJobId, mlApi, isMounted, onError]
// eslint-disable-next-line react-hooks/exhaustive-deps
[selectedJobId, isMounted, errorEncountered]
);
// eslint-disable-next-line react-hooks/exhaustive-deps
const resizeHandler = useCallback(
throttle((e: { width: number; height: number }) => {