From 812cd04c507953771404b4a5addf041fcba198a2 Mon Sep 17 00:00:00 2001 From: Melissa Alvarez Date: Mon, 10 Aug 2020 10:34:11 -0400 Subject: [PATCH] keep groupIds as array of strings and jobId as single string --- .../analytics_list/analytics_list.tsx | 14 ++++++---- .../components/analytics_list/use_columns.tsx | 4 +-- .../job_filter_bar/job_filter_bar.js | 8 ++++-- .../components/jobs_list/job_description.js | 4 +-- .../components/jobs_list/jobs_list.js | 4 +-- .../jobs/jobs_list/components/utils.d.ts | 2 +- .../jobs/jobs_list/components/utils.js | 12 +++++--- .../application/util/get_selected_ids_url.ts | 28 +++++++++++++------ 8 files changed, 48 insertions(+), 28 deletions(-) diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/analytics_list/analytics_list.tsx b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/analytics_list/analytics_list.tsx index 521f783a05976..0652ec5f8acb1 100644 --- a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/analytics_list/analytics_list.tsx +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/analytics_list/analytics_list.tsx @@ -105,13 +105,17 @@ export const DataFrameAnalyticsList: FC = ({ const [selectedIdFromUrlInitialized, setSelectedIdFromUrlInitialized] = useState(false); useEffect(() => { if (selectedIdFromUrlInitialized === false && analytics.length > 0) { - const urlValues = getSelectedIdFromUrl(window.location.href); + const { jobId, groupIds } = getSelectedIdFromUrl(window.location.href); + let queryText = ''; - if (urlValues.ids !== undefined) { - const queryText = urlValues.isGroup ? getGroupQueryText(urlValues.ids) : urlValues.ids[0]; - setSelectedIdFromUrlInitialized(true); - setSearchQueryText(queryText); + if (groupIds !== undefined) { + queryText = getGroupQueryText(groupIds); + } else if (jobId !== undefined) { + queryText = jobId; } + + setSelectedIdFromUrlInitialized(true); + setSearchQueryText(queryText); } }, [selectedIdFromUrlInitialized, analytics]); diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/analytics_list/use_columns.tsx b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/analytics_list/use_columns.tsx index 74f592ae27409..b49ebcbad47ac 100644 --- a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/analytics_list/use_columns.tsx +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/analytics_list/use_columns.tsx @@ -19,7 +19,7 @@ import { EuiLink, RIGHT_ALIGNMENT, } from '@elastic/eui'; -import { getSelectedIdsUrl } from '../../../../../util/get_selected_ids_url'; +import { getJobIdUrl } from '../../../../../util/get_selected_ids_url'; import { getAnalysisType, DataFrameAnalyticsId } from '../../../../common'; import { @@ -137,7 +137,7 @@ export const progressColumn = { }; export const getDFAnalyticsJobIdLink = (item: DataFrameAnalyticsListRow) => ( - {item.id} + {item.id} ); export const useColumns = ( diff --git a/x-pack/plugins/ml/public/application/jobs/jobs_list/components/job_filter_bar/job_filter_bar.js b/x-pack/plugins/ml/public/application/jobs/jobs_list/components/job_filter_bar/job_filter_bar.js index ad2f188f1e370..6eb7b00e5620c 100644 --- a/x-pack/plugins/ml/public/application/jobs/jobs_list/components/job_filter_bar/job_filter_bar.js +++ b/x-pack/plugins/ml/public/application/jobs/jobs_list/components/job_filter_bar/job_filter_bar.js @@ -55,10 +55,12 @@ export class JobFilterBar extends Component { componentDidMount() { // If job id is selected in url, filter table to that id let defaultQueryText; - const urlValues = getSelectedIdFromUrl(window.location.href); + const { jobId, groupIds } = getSelectedIdFromUrl(window.location.href); - if (urlValues.ids !== undefined) { - defaultQueryText = urlValues.isGroup ? getGroupQueryText(urlValues.ids) : urlValues.ids[0]; + if (groupIds !== undefined) { + defaultQueryText = getGroupQueryText(groupIds); + } else if (jobId !== undefined) { + defaultQueryText = jobId; } if (defaultQueryText !== undefined) { diff --git a/x-pack/plugins/ml/public/application/jobs/jobs_list/components/jobs_list/job_description.js b/x-pack/plugins/ml/public/application/jobs/jobs_list/components/jobs_list/job_description.js index de4c8e279bdab..efa4940cebb11 100644 --- a/x-pack/plugins/ml/public/application/jobs/jobs_list/components/jobs_list/job_description.js +++ b/x-pack/plugins/ml/public/application/jobs/jobs_list/components/jobs_list/job_description.js @@ -8,7 +8,7 @@ import PropTypes from 'prop-types'; import React from 'react'; import { JobGroup } from '../job_group'; -import { getSelectedIdsUrl } from '../../../../util/get_selected_ids_url'; +import { getGroupIdsUrl } from '../../../../util/get_selected_ids_url'; export function JobDescription({ job, isManagementTable }) { return ( @@ -18,7 +18,7 @@ export function JobDescription({ job, isManagementTable }) { {job.groups.map((group) => { if (isManagementTable === true) { return ( - + ); diff --git a/x-pack/plugins/ml/public/application/jobs/jobs_list/components/jobs_list/jobs_list.js b/x-pack/plugins/ml/public/application/jobs/jobs_list/components/jobs_list/jobs_list.js index 8754fa7b97785..17b87f04d162f 100644 --- a/x-pack/plugins/ml/public/application/jobs/jobs_list/components/jobs_list/jobs_list.js +++ b/x-pack/plugins/ml/public/application/jobs/jobs_list/components/jobs_list/jobs_list.js @@ -14,7 +14,7 @@ import { toLocaleString } from '../../../../util/string_utils'; import { ResultLinks, actionsMenuContent } from '../job_actions'; import { JobDescription } from './job_description'; import { JobIcon } from '../../../../components/job_message_icon'; -import { getSelectedIdsUrl } from '../../../../util/get_selected_ids_url'; +import { getJobIdUrl } from '../../../../util/get_selected_ids_url'; import { TIME_FORMAT } from '../../../../../../common/constants/time_format'; import { EuiBadge, EuiBasicTable, EuiButtonIcon, EuiLink, EuiScreenReaderOnly } from '@elastic/eui'; @@ -71,7 +71,7 @@ export class JobsList extends Component { return id; } - return {id}; + return {id}; } getPageOfJobs(index, size, sortField, sortDirection) { diff --git a/x-pack/plugins/ml/public/application/jobs/jobs_list/components/utils.d.ts b/x-pack/plugins/ml/public/application/jobs/jobs_list/components/utils.d.ts index 04710e1d4de97..a201b0812e4e9 100644 --- a/x-pack/plugins/ml/public/application/jobs/jobs_list/components/utils.d.ts +++ b/x-pack/plugins/ml/public/application/jobs/jobs_list/components/utils.d.ts @@ -3,6 +3,6 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -export function getSelectedIdFromUrl(str: string): { ids: string[]; isGroup: boolean }; +export function getSelectedIdFromUrl(str: string): { groupIds: string[] } | { jobId: string }; export function getGroupQueryText(arr: string[]): string; export function clearSelectedJobIdFromUrl(str: string): void; diff --git a/x-pack/plugins/ml/public/application/jobs/jobs_list/components/utils.js b/x-pack/plugins/ml/public/application/jobs/jobs_list/components/utils.js index 8d5bfbeacfa6d..913727bda67df 100644 --- a/x-pack/plugins/ml/public/application/jobs/jobs_list/components/utils.js +++ b/x-pack/plugins/ml/public/application/jobs/jobs_list/components/utils.js @@ -369,18 +369,22 @@ function getUrlVars(url) { }); return vars; } + export function getSelectedIdFromUrl(url) { const result = {}; if (typeof url === 'string') { const isGroup = url.includes('groupIds'); - url = decodeURIComponent(url); - if (url.includes('mlManagement') && (url.includes('jobId') || isGroup)) { + + if (url.includes('mlManagement')) { const urlParams = getUrlVars(url); const decodedJson = rison.decode(urlParams.mlManagement); - result.ids = isGroup ? decodedJson.groupIds : decodedJson.jobId; - result.isGroup = isGroup; + if (isGroup) { + result.groupIds = decodedJson.groupIds; + } else { + result.jobId = decodedJson.jobId; + } } } return result; diff --git a/x-pack/plugins/ml/public/application/util/get_selected_ids_url.ts b/x-pack/plugins/ml/public/application/util/get_selected_ids_url.ts index a3323e2d2bca7..0f53d93e584f8 100644 --- a/x-pack/plugins/ml/public/application/util/get_selected_ids_url.ts +++ b/x-pack/plugins/ml/public/application/util/get_selected_ids_url.ts @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ import rison from 'rison-node'; - import { getBasePath } from './dependency_cache'; export enum TAB_IDS { @@ -12,18 +11,29 @@ export enum TAB_IDS { ANOMALY_DETECTION = 'jobs', } -export function getSelectedIdsUrl( - tabId: string, - ids: string | string[], - isGroup: boolean = false -): string { +function getSelectedIdsUrl(tabId, settings: any): string { // Create url for filtering by job id or group ids for kibana management table - const settings = { - [isGroup ? 'groupIds' : 'jobId']: Array.isArray(ids) ? ids : [ids], - }; const encoded = rison.encode(settings); const url = `?mlManagement=${encoded}`; const basePath = getBasePath(); return `${basePath.get()}/app/ml#/${tabId}${url}`; } + +// Create url for filtering by group ids for kibana management table +export function getGroupIdsUrl(tabId: string, ids: string[]): string { + const settings = { + groupIds: ids, + }; + + return getSelectedIdsUrl(tabId, settings); +} + +// Create url for filtering by job id for kibana management table +export function getJobIdUrl(tabId: string, id: string): string { + const settings = { + jobId: id, + }; + + return getSelectedIdsUrl(tabId, settings); +}