Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Add linking to dataframe from job management tab #65778

Merged
merged 11 commits into from
May 15, 2020
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { Fragment, FC, useState } from 'react';
import React, { Fragment, FC, useState, useEffect } from 'react';

import { i18n } from '@kbn/i18n';

Expand All @@ -15,6 +15,7 @@ import {
EuiFlexGroup,
EuiFlexItem,
EuiSpacer,
EuiSearchBar,
} from '@elastic/eui';

import {
Expand Down Expand Up @@ -51,6 +52,7 @@ import { RefreshAnalyticsListButton } from '../refresh_analytics_list_button';
import { CreateAnalyticsButton } from '../create_analytics_button';
import { CreateAnalyticsFormProps } from '../../hooks/use_create_analytics_form';
import { CreateAnalyticsFlyoutWrapper } from '../create_analytics_flyout_wrapper';
import { getSelectedJobIdFromUrl } from '../../../../../jobs/jobs_list/components/utils';

function getItemIdToExpandedRowMap(
itemIds: DataFrameAnalyticsId[],
Expand Down Expand Up @@ -91,6 +93,8 @@ export const DataFrameAnalyticsList: FC<Props> = ({
const [isLoading, setIsLoading] = useState(false);
const [filterActive, setFilterActive] = useState(false);

const [queryText, setQueryText] = useState('');

const [analytics, setAnalytics] = useState<DataFrameAnalyticsListRow[]>([]);
const [analyticsStats, setAnalyticsStats] = useState<AnalyticStatsBarStats | undefined>(
undefined
Expand All @@ -107,6 +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 disabled =
!checkPermission('canCreateDataFrameAnalytics') ||
!checkPermission('canStartStopDataFrameAnalytics');
Expand All @@ -119,6 +124,18 @@ export const DataFrameAnalyticsList: FC<Props> = ({
blockRefresh
);

const selectedId = getSelectedJobIdFromUrl(window.location.href);
useEffect(() => {
if (urlFilterIdCleared === false && analytics.length > 0) {
if (selectedId !== undefined) {
setUrlFilterIdCleared(true);
setQueryText(selectedId);
const selectedIdQuery: Query = EuiSearchBar.Query.parse(selectedId);
onQueryChange({ query: selectedIdQuery, error: undefined });
}
}
}, [urlFilterIdCleared, analytics]);

// Subscribe to the refresh observable to trigger reloading the analytics list.
useRefreshAnalyticsList({
isLoading: setIsLoading,
Expand All @@ -134,6 +151,7 @@ export const DataFrameAnalyticsList: FC<Props> = ({
clauses = query.ast.clauses;
}
if (clauses.length > 0) {
setQueryText(query.text);
setFilterActive(true);
filterAnalytics(clauses as Array<TermClause | FieldClause>);
} else {
Expand Down Expand Up @@ -297,6 +315,7 @@ export const DataFrameAnalyticsList: FC<Props> = ({
};

const search = {
query: queryText,
onChange: onQueryChange,
box: {
incremental: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import {
EuiScreenReaderOnly,
EuiText,
EuiToolTip,
EuiLink,
RIGHT_ALIGNMENT,
} from '@elastic/eui';
// @ts-ignore
import { getJobIdUrl } from '../../../../../jobs/jobs_list/components/utils';

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

export const getDFAnalyticsJobIdLink = (item: DataFrameAnalyticsListRow) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this could be implicit return

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to implicit return in f3c8367#diff-5c7fbd428261fc2fd9567311e2e7046cL141.

return <EuiLink href={getJobIdUrl('data_frame_analytics', item.id)}>{item.id}</EuiLink>;
};

export const getColumns = (
expandedRowItemIds: DataFrameAnalyticsId[],
setExpandedRowItemIds: React.Dispatch<React.SetStateAction<DataFrameAnalyticsId[]>>,
Expand Down Expand Up @@ -193,12 +200,13 @@ export const getColumns = (
'data-test-subj': 'mlAnalyticsTableRowDetailsToggle',
},
{
field: DataFrameAnalyticsListColumn.id,
name: 'ID',
sortable: true,
sortable: (item: DataFrameAnalyticsListRow) => item.id,
truncateText: true,
'data-test-subj': 'mlAnalyticsTableColumnId',
scope: 'row',
render: (item: DataFrameAnalyticsListRow) =>
isManagementTable ? getDFAnalyticsJobIdLink(item) : item.id,
},
{
field: DataFrameAnalyticsListColumn.description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class JobsList extends Component {
return id;
}

return <EuiLink href={getJobIdUrl(id)}>{id}</EuiLink>;
return <EuiLink href={getJobIdUrl('jobs', id)}>{id}</EuiLink>;
}

getPageOfJobs(index, size, sortField, sortDirection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ function jobProperty(job, prop) {
return job[propMap[prop]];
}

export function getJobIdUrl(jobId) {
export function getJobIdUrl(tabId, jobId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be moved to a common directory since it's shared (maybe public/application/util?).

Could just create a new ts file with the name of the function with only that function in that file (like job_selection_change.ts, has_matching_points.ts). Then you wouldn't have to worry about jobs_list/components/utils.js not being in ts yet.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this getJobIdUrl function to x-pack/plugins/ml/public/application/util/get_job_id_url.ts in f3c8367. Much cleaner 😄 !

// Create url for filtering by job id for kibana management table
const settings = {
jobId,
Expand All @@ -376,7 +376,7 @@ export function getJobIdUrl(jobId) {
const url = `?mlManagement=${encoded}`;
const basePath = getBasePath();

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

function getUrlVars(url) {
Expand Down Expand Up @@ -408,3 +408,15 @@ export function clearSelectedJobIdFromUrl(url) {
}
}
}

export function resetMlJobUrl(tabId, url) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this being used anywhere. Unless I'm missing something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally did this in case we want to reset the window's url after navigating to the page, but since it's not needed, I have deleted this in f3c8367.

// 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);
}
}
}