Skip to content

Commit

Permalink
[ML] update types and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Nov 16, 2020
1 parent 2c5e336 commit 045fa80
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 10 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/ml/common/constants/ml_url_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const ML_PAGES = {
ANOMALY_EXPLORER: 'explorer',
SINGLE_METRIC_VIEWER: 'timeseriesexplorer',
DATA_FRAME_ANALYTICS_JOBS_MANAGE: 'data_frame_analytics',
DATA_FRAME_ANALYTICS_MODELS_MANAGE: 'data_frame_analytics/models',
DATA_FRAME_ANALYTICS_EXPLORATION: 'data_frame_analytics/exploration',
DATA_FRAME_ANALYTICS_MAP: 'data_frame_analytics/map',
/**
Expand Down Expand Up @@ -45,3 +46,5 @@ export const ML_PAGES = {
ACCESS_DENIED: 'access-denied',
OVERVIEW: 'overview',
} as const;

export type MlPages = typeof ML_PAGES[keyof typeof ML_PAGES];
6 changes: 6 additions & 0 deletions x-pack/plugins/ml/common/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { MlPages } from '../constants/ml_url_generator';

export interface Dictionary<TValue> {
[id: string]: TValue;
}
Expand Down Expand Up @@ -39,3 +41,7 @@ export interface ListingPageUrlState {
sortDirection: string;
queryText?: string;
}

export type AppPageState<T> = {
[key in MlPages]?: Partial<T>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const ModelsList: FC = () => {
const urlGenerator = useMlUrlGenerator();

const [pageState, updatePageState] = usePageUrlState(
'trained_models',
ML_PAGES.DATA_FRAME_ANALYTICS_MODELS_MANAGE,
getDefaultModelsListState()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { JobMap } from '../job_map';
import { usePageUrlState } from '../../../util/url_state';
import { ListingPageUrlState } from '../../../../../common/types/common';
import { DataFrameAnalyticsListColumn } from './components/analytics_list/common';
import { ML_PAGES } from '../../../../../common/constants/ml_url_generator';

export const getDefaultDFAListState = (): ListingPageUrlState => ({
pageIndex: 0,
Expand All @@ -48,7 +49,10 @@ export const Page: FC = () => {
const [blockRefresh, setBlockRefresh] = useState(false);
const [globalState] = useUrlState('_g');

const [dfaPageState, setDfaPageState] = usePageUrlState('dfa_jobs', getDefaultDFAListState());
const [dfaPageState, setDfaPageState] = usePageUrlState(
ML_PAGES.DATA_FRAME_ANALYTICS_JOBS_MANAGE,
getDefaultDFAListState()
);

useRefreshInterval(setBlockRefresh);

Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/ml/public/application/util/url_state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useHistory, useLocation } from 'react-router-dom';
import { Dictionary } from '../../../common/types/common';

import { getNestedProperty } from './object_utils';
import { MlPages } from '../../../common/constants/ml_url_generator';

type Accessor = '_a' | '_g';
export type SetUrlState = (
Expand Down Expand Up @@ -155,7 +156,7 @@ export const useUrlState = (accessor: Accessor) => {
* Hook for managing the URL state of the page.
*/
export const usePageUrlState = <PageUrlState extends {}>(
pageKey: string,
pageKey: MlPages,
defaultState: PageUrlState
): [PageUrlState, (update: Partial<PageUrlState>) => void] => {
const [appState, setAppState] = useUrlState('_a');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
import {
DataFrameAnalyticsExplorationQueryState,
DataFrameAnalyticsExplorationUrlState,
DataFrameAnalyticsQueryState,
DataFrameAnalyticsUrlState,
MlCommonGlobalState,
} from '../../common/types/ml_url_generator';
import { ML_PAGES } from '../../common/constants/ml_url_generator';
import { setStateToKbnUrl } from '../../../../../src/plugins/kibana_utils/public';
import { getGroupQueryText, getJobQueryText } from '../../common/util/string_utils';
import { ListingPageUrlState } from '../../common/types/common';
import { AppPageState, ListingPageUrlState } from '../../common/types/common';

export function createDataFrameAnalyticsJobManagementUrl(
appBasePath: string,
Expand All @@ -39,11 +38,11 @@ export function createDataFrameAnalyticsJobManagementUrl(
...(queryTextArr.length > 0 ? { queryText: queryTextArr.join(' ') } : {}),
};

const queryState: Record<string, Partial<ListingPageUrlState>> = {
dfa_jobs: jobsListState,
const queryState: AppPageState<ListingPageUrlState> = {
[ML_PAGES.DATA_FRAME_ANALYTICS_JOBS_MANAGE]: jobsListState,
};

url = setStateToKbnUrl<Partial<DataFrameAnalyticsQueryState>>(
url = setStateToKbnUrl<AppPageState<ListingPageUrlState>>(
'_a',
queryState,
{ useHash: false, storeInHashQuery: false },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ describe('MlUrlGenerator', () => {
jobId: 'grid_regression_1',
},
});
expect(url).toBe('/app/ml/data_frame_analytics?mlManagement=(jobId:grid_regression_1)');
expect(url).toBe(
'/app/ml/data_frame_analytics?_a=(data_frame_analytics:(queryText:grid_regression_1))'
);
});

it('should generate valid URL for the Data Frame Analytics job management page with groupIds', async () => {
Expand All @@ -190,7 +192,9 @@ describe('MlUrlGenerator', () => {
groupIds: ['group_1', 'group_2'],
},
});
expect(url).toBe('/app/ml/data_frame_analytics?mlManagement=(groupIds:!(group_1,group_2))');
expect(url).toBe(
"/app/ml/data_frame_analytics?_a=(data_frame_analytics:(queryText:'groups:(group_1%20or%20group_2)'))"
);
});
});

Expand Down

0 comments on commit 045fa80

Please sign in to comment.