Skip to content

Commit

Permalink
[ML] Add ability to query text/job id from url for DFA page
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed May 14, 2020
1 parent 56b740a commit 1ec4fde
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
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 { DataFrameAnalyticsId, useRefreshAnalyticsList } from '../../../../common';
Expand Down Expand Up @@ -46,6 +47,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 @@ -86,6 +88,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 @@ -102,6 +106,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 @@ -114,6 +119,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 @@ -129,6 +146,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 @@ -286,6 +304,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 @@ -23,7 +23,6 @@ import {
import { getJobIdUrl } from '../../../../../jobs/jobs_list/components/utils';

import { getAnalysisType, DataFrameAnalyticsId } from '../../../../common';
import { getResultsUrl } from './common';
import { CreateAnalyticsFormProps } from '../../hooks/use_create_analytics_form';
import {
getDataFrameAnalyticsProgress,
Expand Down Expand Up @@ -199,7 +198,6 @@ export const getColumns = (
'data-test-subj': 'mlAnalyticsTableRowDetailsToggle',
},
{
// field: DataFrameAnalyticsListColumn.id,
name: 'ID',
sortable: (item: DataFrameAnalyticsListRow) => item.id,
truncateText: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,15 @@ 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);
}
}
}

0 comments on commit 1ec4fde

Please sign in to comment.