From 1f6547bcb13bb479f1dfe7232077473e33cafed6 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Fri, 1 Nov 2024 00:39:08 +1100 Subject: [PATCH] [8.x] [ML] Fixes link to anomaly explorer from anomaly embeddables when viewing by job group (#198256) (#198515) # Backport This will backport the following commits from `main` to `8.x`: - [[ML] Fixes link to anomaly explorer from anomaly embeddables when viewing by job group (#198256)](https://github.com/elastic/kibana/pull/198256) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Robert Jaszczurek <92210485+rbrtj@users.noreply.github.com> --- .../job_selector/use_job_selection.ts | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/ml/public/application/components/job_selector/use_job_selection.ts b/x-pack/plugins/ml/public/application/components/job_selector/use_job_selection.ts index 7fcc1e71e180..51d1882084d3 100644 --- a/x-pack/plugins/ml/public/application/components/job_selector/use_job_selection.ts +++ b/x-pack/plugins/ml/public/application/components/job_selector/use_job_selection.ts @@ -25,6 +25,24 @@ function getInvalidJobIds(jobs: MlJobWithTimeRange[], ids: string[]) { }); } +// This is useful when redirecting from dashboards where groupIds are treated as jobIds +const getJobIdsFromGroups = (jobIds: string[], jobs: MlJobWithTimeRange[]) => { + const result = new Set(); + + jobIds.forEach((id) => { + const jobsInGroup = jobs.filter((job) => job.groups?.includes(id)); + + if (jobsInGroup.length > 0) { + jobsInGroup.forEach((job) => result.add(job.job_id)); + } else { + // If it's not a group ID, keep it (regardless of whether it's valid or not) + result.add(id); + } + }); + + return Array.from(result); +}; + export interface JobSelection { jobIds: string[]; selectedGroups: string[]; @@ -37,9 +55,9 @@ export const useJobSelection = (jobs: MlJobWithTimeRange[]) => { const getJobSelection = useJobSelectionFlyout(); const tmpIds = useMemo(() => { - const ids = globalState?.ml?.jobIds || []; + const ids = getJobIdsFromGroups(globalState?.ml?.jobIds || [], jobs); return (typeof ids === 'string' ? [ids] : ids).map((id: string) => String(id)); - }, [globalState?.ml?.jobIds]); + }, [globalState?.ml?.jobIds, jobs]); const invalidIds = useMemo(() => { return getInvalidJobIds(jobs, tmpIds);