Skip to content

Commit

Permalink
[ML] Refactor get_job_id_url util function & clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed May 14, 2020
1 parent 0159072 commit f3c8367
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const DataFrameAnalyticsList: FC<Props> = ({
const [sortField, setSortField] = useState<string>(DataFrameAnalyticsListColumn.id);
const [sortDirection, setSortDirection] = useState<SortDirection>(SORT_DIRECTION.ASC);

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

// Query text/job_id based on url but only after getAnalytics is done first
// jobIdSelected makes sure the query is only run once since analytics is being refreshed constantly
const selectedId = getSelectedJobIdFromUrl(window.location.href);
useEffect(() => {
if (urlFilterIdCleared === false && analytics.length > 0) {
if (jobIdSelected === false && analytics.length > 0) {
if (selectedId !== undefined) {
setUrlFilterIdCleared(true);
setJobIdSelected(true);
setQueryText(selectedId);
const selectedIdQuery: Query = EuiSearchBar.Query.parse(selectedId);
onQueryChange({ query: selectedIdQuery, error: undefined });
}
}
}, [urlFilterIdCleared, analytics]);
}, [jobIdSelected, analytics]);

// Subscribe to the refresh observable to trigger reloading the analytics list.
useRefreshAnalyticsList({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import {
EuiLink,
RIGHT_ALIGNMENT,
} from '@elastic/eui';
// @ts-ignore
import { getJobIdUrl } from '../../../../../jobs/jobs_list/components/utils';
import { getJobIdUrl } from '../../../../../util/get_job_id_url';

import { getAnalysisType, DataFrameAnalyticsId } from '../../../../common';
import { CreateAnalyticsFormProps } from '../../hooks/use_create_analytics_form';
Expand Down Expand Up @@ -138,9 +137,9 @@ export const progressColumn = {
'data-test-subj': 'mlAnalyticsTableColumnProgress',
};

export const getDFAnalyticsJobIdLink = (item: DataFrameAnalyticsListRow) => {
return <EuiLink href={getJobIdUrl('data_frame_analytics', item.id)}>{item.id}</EuiLink>;
};
export const getDFAnalyticsJobIdLink = (item: DataFrameAnalyticsListRow) => (
<EuiLink href={getJobIdUrl('data_frame_analytics', item.id)}>{item.id}</EuiLink>
);

export const getColumns = (
expandedRowItemIds: DataFrameAnalyticsId[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 { getJobIdUrl } from '../utils';
import { getJobIdUrl } from '../../../../util/get_job_id_url';

import { EuiBadge, EuiBasicTable, EuiButtonIcon, EuiLink, EuiScreenReaderOnly } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import rison from 'rison-node';

import { mlJobService } from '../../../services/job_service';
import { ml } from '../../../services/ml_api_service';
import { getToastNotifications, getBasePath } from '../../../util/dependency_cache';
import { getToastNotifications } from '../../../util/dependency_cache';
import { JOB_STATE, DATAFEED_STATE } from '../../../../../common/constants/states';
import { parseInterval } from '../../../../../common/util/parse_interval';
import { i18n } from '@kbn/i18n';
Expand Down Expand Up @@ -367,18 +367,6 @@ function jobProperty(job, prop) {
return job[propMap[prop]];
}

export function getJobIdUrl(tabId, jobId) {
// Create url for filtering by job id for kibana management table
const settings = {
jobId,
};
const encoded = rison.encode(settings);
const url = `?mlManagement=${encoded}`;
const basePath = getBasePath();

return `${basePath.get()}/app/ml#/${tabId}${url}`;
}

function getUrlVars(url) {
const vars = {};
url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(_, key, value) {
Expand Down Expand Up @@ -408,15 +396,3 @@ export function clearSelectedJobIdFromUrl(url) {
}
}
}

export function resetMlJobUrl(tabId, url) {
// Change current window's url to just the generic tab url without the job ID
if (typeof url === 'string') {
url = decodeURIComponent(url);
if (url.includes('mlManagement') && url.includes('jobId')) {
const urlParams = getUrlVars(url);
const clearedParams = `ml#/${tabId}?_g=${urlParams._g}`;
window.history.replaceState({}, document.title, clearedParams);
}
}
}
20 changes: 20 additions & 0 deletions x-pack/plugins/ml/public/application/util/get_job_id_url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import rison from 'rison-node';

import { getBasePath } from './dependency_cache';

export function getJobIdUrl(tabId: string, jobId: string): string {
// Create url for filtering by job id for kibana management table
const settings = {
jobId,
};
const encoded = rison.encode(settings);
const url = `?mlManagement=${encoded}`;
const basePath = getBasePath();

return `${basePath.get()}/app/ml#/${tabId}${url}`;
}

0 comments on commit f3c8367

Please sign in to comment.