From 045fa80397149159369d452722508242e0ca2db9 Mon Sep 17 00:00:00 2001 From: Dima Arnautov Date: Mon, 16 Nov 2020 16:49:34 +0100 Subject: [PATCH] [ML] update types and tests --- x-pack/plugins/ml/common/constants/ml_url_generator.ts | 3 +++ x-pack/plugins/ml/common/types/common.ts | 6 ++++++ .../components/models_management/models_list.tsx | 2 +- .../pages/analytics_management/page.tsx | 6 +++++- x-pack/plugins/ml/public/application/util/url_state.tsx | 3 ++- .../data_frame_analytics_urls_generator.ts | 9 ++++----- .../ml/public/ml_url_generator/ml_url_generator.test.ts | 8 ++++++-- 7 files changed, 27 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/ml/common/constants/ml_url_generator.ts b/x-pack/plugins/ml/common/constants/ml_url_generator.ts index a79e72a84c08e..0c931d281d2d5 100644 --- a/x-pack/plugins/ml/common/constants/ml_url_generator.ts +++ b/x-pack/plugins/ml/common/constants/ml_url_generator.ts @@ -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', /** @@ -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]; diff --git a/x-pack/plugins/ml/common/types/common.ts b/x-pack/plugins/ml/common/types/common.ts index 5ea5b1c376f31..4ae542c510a26 100644 --- a/x-pack/plugins/ml/common/types/common.ts +++ b/x-pack/plugins/ml/common/types/common.ts @@ -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 { [id: string]: TValue; } @@ -39,3 +41,7 @@ export interface ListingPageUrlState { sortDirection: string; queryText?: string; } + +export type AppPageState = { + [key in MlPages]?: Partial; +}; diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/models_management/models_list.tsx b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/models_management/models_list.tsx index 1099f59c1f4f2..2d74d08c4550c 100644 --- a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/models_management/models_list.tsx +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/models_management/models_list.tsx @@ -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() ); diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/page.tsx b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/page.tsx index 5be222d9a8507..2c97b0549350e 100644 --- a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/page.tsx +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/page.tsx @@ -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, @@ -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); diff --git a/x-pack/plugins/ml/public/application/util/url_state.tsx b/x-pack/plugins/ml/public/application/util/url_state.tsx index dc9a39fea6846..448a888ab32c2 100644 --- a/x-pack/plugins/ml/public/application/util/url_state.tsx +++ b/x-pack/plugins/ml/public/application/util/url_state.tsx @@ -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 = ( @@ -155,7 +156,7 @@ export const useUrlState = (accessor: Accessor) => { * Hook for managing the URL state of the page. */ export const usePageUrlState = ( - pageKey: string, + pageKey: MlPages, defaultState: PageUrlState ): [PageUrlState, (update: Partial) => void] => { const [appState, setAppState] = useUrlState('_a'); diff --git a/x-pack/plugins/ml/public/ml_url_generator/data_frame_analytics_urls_generator.ts b/x-pack/plugins/ml/public/ml_url_generator/data_frame_analytics_urls_generator.ts index a1689febab612..dc9c3bd86cc63 100644 --- a/x-pack/plugins/ml/public/ml_url_generator/data_frame_analytics_urls_generator.ts +++ b/x-pack/plugins/ml/public/ml_url_generator/data_frame_analytics_urls_generator.ts @@ -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, @@ -39,11 +38,11 @@ export function createDataFrameAnalyticsJobManagementUrl( ...(queryTextArr.length > 0 ? { queryText: queryTextArr.join(' ') } : {}), }; - const queryState: Record> = { - dfa_jobs: jobsListState, + const queryState: AppPageState = { + [ML_PAGES.DATA_FRAME_ANALYTICS_JOBS_MANAGE]: jobsListState, }; - url = setStateToKbnUrl>( + url = setStateToKbnUrl>( '_a', queryState, { useHash: false, storeInHashQuery: false }, diff --git a/x-pack/plugins/ml/public/ml_url_generator/ml_url_generator.test.ts b/x-pack/plugins/ml/public/ml_url_generator/ml_url_generator.test.ts index e7f12ead3ffe9..38fd9de9fecd3 100644 --- a/x-pack/plugins/ml/public/ml_url_generator/ml_url_generator.test.ts +++ b/x-pack/plugins/ml/public/ml_url_generator/ml_url_generator.test.ts @@ -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 () => { @@ -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)'))" + ); }); });