Skip to content

Commit f3c8367

Browse files
committed
[ML] Refactor get_job_id_url util function & clean up
1 parent 0159072 commit f3c8367

File tree

5 files changed

+32
-35
lines changed

5 files changed

+32
-35
lines changed

x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/analytics_list/analytics_list.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export const DataFrameAnalyticsList: FC<Props> = ({
111111
const [sortField, setSortField] = useState<string>(DataFrameAnalyticsListColumn.id);
112112
const [sortDirection, setSortDirection] = useState<SortDirection>(SORT_DIRECTION.ASC);
113113

114-
const [urlFilterIdCleared, setUrlFilterIdCleared] = useState<boolean>(false);
114+
const [jobIdSelected, setJobIdSelected] = useState<boolean>(false);
115115
const disabled =
116116
!checkPermission('canCreateDataFrameAnalytics') ||
117117
!checkPermission('canStartStopDataFrameAnalytics');
@@ -124,17 +124,19 @@ export const DataFrameAnalyticsList: FC<Props> = ({
124124
blockRefresh
125125
);
126126

127+
// Query text/job_id based on url but only after getAnalytics is done first
128+
// jobIdSelected makes sure the query is only run once since analytics is being refreshed constantly
127129
const selectedId = getSelectedJobIdFromUrl(window.location.href);
128130
useEffect(() => {
129-
if (urlFilterIdCleared === false && analytics.length > 0) {
131+
if (jobIdSelected === false && analytics.length > 0) {
130132
if (selectedId !== undefined) {
131-
setUrlFilterIdCleared(true);
133+
setJobIdSelected(true);
132134
setQueryText(selectedId);
133135
const selectedIdQuery: Query = EuiSearchBar.Query.parse(selectedId);
134136
onQueryChange({ query: selectedIdQuery, error: undefined });
135137
}
136138
}
137-
}, [urlFilterIdCleared, analytics]);
139+
}, [jobIdSelected, analytics]);
138140

139141
// Subscribe to the refresh observable to trigger reloading the analytics list.
140142
useRefreshAnalyticsList({

x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/analytics_list/columns.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import {
1919
EuiLink,
2020
RIGHT_ALIGNMENT,
2121
} from '@elastic/eui';
22-
// @ts-ignore
23-
import { getJobIdUrl } from '../../../../../jobs/jobs_list/components/utils';
22+
import { getJobIdUrl } from '../../../../../util/get_job_id_url';
2423

2524
import { getAnalysisType, DataFrameAnalyticsId } from '../../../../common';
2625
import { CreateAnalyticsFormProps } from '../../hooks/use_create_analytics_form';
@@ -138,9 +137,9 @@ export const progressColumn = {
138137
'data-test-subj': 'mlAnalyticsTableColumnProgress',
139138
};
140139

141-
export const getDFAnalyticsJobIdLink = (item: DataFrameAnalyticsListRow) => {
142-
return <EuiLink href={getJobIdUrl('data_frame_analytics', item.id)}>{item.id}</EuiLink>;
143-
};
140+
export const getDFAnalyticsJobIdLink = (item: DataFrameAnalyticsListRow) => (
141+
<EuiLink href={getJobIdUrl('data_frame_analytics', item.id)}>{item.id}</EuiLink>
142+
);
144143

145144
export const getColumns = (
146145
expandedRowItemIds: DataFrameAnalyticsId[],

x-pack/plugins/ml/public/application/jobs/jobs_list/components/jobs_list/jobs_list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { toLocaleString } from '../../../../util/string_utils';
1414
import { ResultLinks, actionsMenuContent } from '../job_actions';
1515
import { JobDescription } from './job_description';
1616
import { JobIcon } from '../../../../components/job_message_icon';
17-
import { getJobIdUrl } from '../utils';
17+
import { getJobIdUrl } from '../../../../util/get_job_id_url';
1818

1919
import { EuiBadge, EuiBasicTable, EuiButtonIcon, EuiLink, EuiScreenReaderOnly } from '@elastic/eui';
2020
import { i18n } from '@kbn/i18n';

x-pack/plugins/ml/public/application/jobs/jobs_list/components/utils.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import rison from 'rison-node';
1010

1111
import { mlJobService } from '../../../services/job_service';
1212
import { ml } from '../../../services/ml_api_service';
13-
import { getToastNotifications, getBasePath } from '../../../util/dependency_cache';
13+
import { getToastNotifications } from '../../../util/dependency_cache';
1414
import { JOB_STATE, DATAFEED_STATE } from '../../../../../common/constants/states';
1515
import { parseInterval } from '../../../../../common/util/parse_interval';
1616
import { i18n } from '@kbn/i18n';
@@ -367,18 +367,6 @@ function jobProperty(job, prop) {
367367
return job[propMap[prop]];
368368
}
369369

370-
export function getJobIdUrl(tabId, jobId) {
371-
// Create url for filtering by job id for kibana management table
372-
const settings = {
373-
jobId,
374-
};
375-
const encoded = rison.encode(settings);
376-
const url = `?mlManagement=${encoded}`;
377-
const basePath = getBasePath();
378-
379-
return `${basePath.get()}/app/ml#/${tabId}${url}`;
380-
}
381-
382370
function getUrlVars(url) {
383371
const vars = {};
384372
url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(_, key, value) {
@@ -408,15 +396,3 @@ export function clearSelectedJobIdFromUrl(url) {
408396
}
409397
}
410398
}
411-
412-
export function resetMlJobUrl(tabId, url) {
413-
// Change current window's url to just the generic tab url without the job ID
414-
if (typeof url === 'string') {
415-
url = decodeURIComponent(url);
416-
if (url.includes('mlManagement') && url.includes('jobId')) {
417-
const urlParams = getUrlVars(url);
418-
const clearedParams = `ml#/${tabId}?_g=${urlParams._g}`;
419-
window.history.replaceState({}, document.title, clearedParams);
420-
}
421-
}
422-
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import rison from 'rison-node';
7+
8+
import { getBasePath } from './dependency_cache';
9+
10+
export function getJobIdUrl(tabId: string, jobId: string): string {
11+
// Create url for filtering by job id for kibana management table
12+
const settings = {
13+
jobId,
14+
};
15+
const encoded = rison.encode(settings);
16+
const url = `?mlManagement=${encoded}`;
17+
const basePath = getBasePath();
18+
19+
return `${basePath.get()}/app/ml#/${tabId}${url}`;
20+
}

0 commit comments

Comments
 (0)