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] Extend MlUrlGenerator to support other ML pages #75696

Merged
merged 35 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5efa8cd
[ML] Extend MlUrlGenerator to support other pages
qn895 Aug 13, 2020
9faedfd
[ML] Change destructuring & index based ml generator
qn895 Aug 21, 2020
7d8aba8
[ML] Make createIndexBasedMlUrl check more explicit
qn895 Aug 21, 2020
40cc3c5
[ML] Remove commented code
qn895 Aug 21, 2020
c1f8cde
[ML] Update type for TimeRange
qn895 Aug 21, 2020
98dd2e5
[ML] Update redirect for dfa wizard to use constants
qn895 Aug 21, 2020
0e4ddd6
Merge remote-tracking branch 'upstream/master' into ml-url-generator-…
qn895 Aug 21, 2020
7c9318e
[ML] Rename ML_TABS to ML_PAGES
qn895 Aug 24, 2020
aa9398d
Merge remote-tracking branch 'upstream/master' into new-ml-url-generator
qn895 Aug 24, 2020
7360f32
[ML] Update core API docs
qn895 Aug 24, 2020
7ce5a5b
[ML] Fix perPartitionStopOnWarn typo
qn895 Aug 24, 2020
9e93c9d
Merge remote-tracking branch 'upstream/master' into new-ml-url-generator
qn895 Aug 25, 2020
8179168
[ML] Update redirect func name & fix for url with base path
qn895 Aug 25, 2020
c552644
[ML] Change ml url state types to its own
qn895 Aug 25, 2020
d8c5716
[ML] Change timeseriesexplorer to SINGLE_METRIC_VIEWER
qn895 Aug 25, 2020
3a1a7cb
[ML] Rename/update descriptions
qn895 Aug 25, 2020
ff528e5
[ML] Update calendar and filter manage
qn895 Aug 25, 2020
60d6799
[ML] Rename ANOMALY_DETECTION -> ANOMALY_DETECTION_JOBS_MANAGE
qn895 Aug 25, 2020
18b9093
[ML] Rename DATA_FRAME_ANALYTICS -> DATA_FRAME_ANALYTICS_JOBS_MANAGE
qn895 Aug 25, 2020
b7f1920
[ML] Update imports after moving
qn895 Aug 25, 2020
b7cd7fd
[ML] Update import from file instead of from context
qn895 Aug 25, 2020
49e56ed
[ML] Have dedicated functions for createIndexDataVisualizerUrl and cr…
qn895 Aug 25, 2020
64b936b
[ML] Remove hash based path in initSampleDataSets
qn895 Aug 25, 2020
689f0f6
Merge remote-tracking branch 'upstream/master' into new-ml-url-generator
qn895 Aug 27, 2020
4d6a5a0
[ML] Refactor to own folder
qn895 Aug 31, 2020
3751f4a
Merge remote-tracking branch 'upstream/master' into new-ml-url-generator
qn895 Aug 31, 2020
5eefb05
[ML] Revert changes to data/common/types
qn895 Aug 31, 2020
a1e9118
[ML] Fix createAnomalyDetectionCreateJobSelectType name and update ap…
qn895 Sep 1, 2020
2abee97
[ML] Change shape to use `page` and `pageState`
qn895 Sep 1, 2020
8148a6c
[ML] Remove commented block code
qn895 Sep 1, 2020
9962c25
[ML] Rename dataframe -> data frame, update types for MLPagestype
qn895 Sep 2, 2020
8248d7f
[ML] Rename DataFrameAnalyticsType
qn895 Sep 2, 2020
67214cd
Merge remote-tracking branch 'upstream/master' into new-ml-url-generator
qn895 Sep 2, 2020
a6cb7da
[ML] Update typings
qn895 Sep 2, 2020
8489ded
[ML] fix base type
darnautov Sep 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/plugins/data/common/query/timefilter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import { Moment } from 'moment';

export interface RefreshInterval {
display?: string;
pause: boolean;
value: number;
}

export interface TimeRange {
from: string;
to: string;
mode?: 'absolute' | 'relative';
mode?: 'absolute' | 'relative' | 'quick';
Copy link
Contributor

@lizozom lizozom Aug 27, 2020

Choose a reason for hiding this comment

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

@qn895 I'm slightly confused on how both these fields get used.

I don't even see any place in the code using them (nor here or in the data plugin), to the point I would consider removing mode 🤨

I'd love to sync on this.

Copy link
Member Author

Choose a reason for hiding this comment

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

@lizozom Thanks for the feedback. Our team decided it would be good to take those parameters out as they are outdated. I have reverted the additions to the data plugin here: 5eefb05

}

export interface TimeRangeBounds {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { useNavigateToPath, NavigateToPath } from './use_navigate_to_path';
export { useUiSettings } from './use_ui_settings_context';
export { useTimefilter } from './use_timefilter';
export { useNotifications } from './use_notifications_context';
export { useMlUrlGenerator } from './use_create_url';
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 { useMlKibana } from './kibana_context';
import { ML_APP_URL_GENERATOR } from '../../../url_generator';

export const useMlUrlGenerator = () => {
const {
services: {
share: {
urlGenerators: { getUrlGenerator },
},
},
} = useMlKibana();

return getUrlGenerator(ML_APP_URL_GENERATOR);
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ export const useNavigateToPath = () => {

const location = useLocation();

return useMemo(
() => (path: string | undefined, preserveSearch = false) => {
navigateToUrl(
getUrlForApp(PLUGIN_ID, {
path: `${path}${preserveSearch === true ? location.search : ''}`,
})
);
},
[location]
);
return useMemo(() => {
return (path: string | undefined, preserveSearch = false) => {
if (path === undefined) return;
const modifiedPath = `${path}${preserveSearch === true ? location.search : ''}`;
/**
* Handle urls generated by MlUrlGenerator where '/app/ml' is automatically prepended
*/
const url = modifiedPath.startsWith('/app/ml')
? modifiedPath
: getUrlForApp(PLUGIN_ID, {
path: modifiedPath,
});
navigateToUrl(url);
};
}, [location]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
import React, { FC, Fragment } from 'react';
import { EuiCard, EuiIcon } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { useNavigateToPath } from '../../../../../contexts/kibana';
import { useMlKibana, useMlUrlGenerator } from '../../../../../contexts/kibana';
import { ML_TABS } from '../../../../../../url_generator';

export const BackToListPanel: FC = () => {
const navigateToPath = useNavigateToPath();
const urlGenerator = useMlUrlGenerator();
const {
services: {
application: { navigateToUrl },
},
} = useMlKibana();

const redirectToAnalyticsManagementPage = async () => {
await navigateToPath('/data_frame_analytics');
const url = await urlGenerator.createUrl({ page: ML_TABS.DATA_FRAME_ANALYTICS });
await navigateToUrl(url);
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@
import React, { FC, Fragment } from 'react';
import { EuiCard, EuiIcon } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { useNavigateToPath } from '../../../../../contexts/kibana';
import { getResultsUrl } from '../../../analytics_management/components/analytics_list/common';
import { useMlUrlGenerator } from '../../../../../contexts/kibana';
import { ANALYSIS_CONFIG_TYPE } from '../../../../common/analytics';
import { useNavigateToPath } from '../../../../../contexts/kibana';
import { ML_TABS } from '../../../../../../url_generator';

interface Props {
jobId: string;
analysisType: ANALYSIS_CONFIG_TYPE;
}

export const ViewResultsPanel: FC<Props> = ({ jobId, analysisType }) => {
const urlGenerator = useMlUrlGenerator();
const navigateToPath = useNavigateToPath();

const redirectToAnalyticsManagementPage = async () => {
const path = getResultsUrl(jobId, analysisType);
const redirectToAnalyticsJobPage = async () => {
peteharverson marked this conversation as resolved.
Show resolved Hide resolved
const path = await urlGenerator.createUrl({
peteharverson marked this conversation as resolved.
Show resolved Hide resolved
page: ML_TABS.DATA_FRAME_ANALYTICS_EXPLORATION,
jobId,
analysisType,
});
await navigateToPath(path);
};

Expand All @@ -38,7 +44,7 @@ export const ViewResultsPanel: FC<Props> = ({ jobId, analysisType }) => {
defaultMessage: 'View results for the analytics job.',
}
)}
onClick={redirectToAnalyticsManagementPage}
onClick={redirectToAnalyticsJobPage}
data-test-subj="analyticsWizardViewResultsCard"
/>
</Fragment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface ExplorerAppState {
filteredFields?: string[];
queryString?: string;
};
query?: any;
}

const explorerAppState$: Observable<ExplorerAppState> = explorerState$.pipe(
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/ml/public/application/routing/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const MlRoutes: FC<{
pageDeps: PageDependencies;
}> = ({ pageDeps }) => {
const navigateToPath = useNavigateToPath();

return (
<>
{Object.entries(routes).map(([name, routeFactory]) => {
Expand Down
228 changes: 219 additions & 9 deletions x-pack/plugins/ml/public/url_generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,233 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { MlUrlGenerator } from './url_generator';
import { MlUrlGenerator, ML_TABS } from './url_generator';
import { ANALYSIS_CONFIG_TYPE } from './application/data_frame_analytics/common';

describe('MlUrlGenerator', () => {
const urlGenerator = new MlUrlGenerator({
appBasePath: '/app/ml',
useHash: false,
});

it('should generate valid URL for the Anomaly Explorer page', async () => {
const url = await urlGenerator.createUrl({
page: 'explorer',
jobIds: ['test-job'],
mlExplorerSwimlane: { viewByFromPage: 2, viewByPerPage: 20 },
describe('AnomalyDetection', () => {
describe('Job Management Page', () => {
it('should generate valid URL for the Anomaly Detection job management page', async () => {
const url = await urlGenerator.createUrl({
page: ML_TABS.ANOMALY_DETECTION,
});
expect(url).toBe('/app/ml/jobs');
});

it('should generate valid URL for the Anomaly Detection job management page for job', async () => {
const url = await urlGenerator.createUrl({
page: ML_TABS.ANOMALY_DETECTION,
jobId: 'fq_single_1',
});
expect(url).toBe('/app/ml/jobs?mlManagement=(jobId:fq_single_1)');
});

it('should generate valid URL for the Anomaly Detection job management page for groupIds', async () => {
const url = await urlGenerator.createUrl({
page: ML_TABS.ANOMALY_DETECTION,
groupIds: ['farequote', 'categorization'],
});
expect(url).toBe('/app/ml/jobs?mlManagement=(groupIds:!(farequote,categorization))');
});

it('should generate valid URL for the Data Visualizer Viewer page', async () => {
const url = await urlGenerator.createUrl({
page: ML_TABS.DATA_VISUALIZER_NEW_JOB,
index: `3da93760-e0af-11ea-9ad3-3bcfc330e42a`,
globalState: {
time: {
from: 'now-30m',
to: 'now',
mode: 'quick',
},
},
});
expect(url).toBe(
'/app/ml/jobs/new_job/step/job_type?index=3da93760-e0af-11ea-9ad3-3bcfc330e42a&_g=(time:(from:now-30m,mode:quick,to:now))'
);
});
});

describe('Anomaly Explorer Page', () => {
it('should generate valid URL for the Anomaly Explorer page', async () => {
const url = await urlGenerator.createUrl({
page: ML_TABS.ANOMALY_EXPLORER,
jobIds: ['fq_single_1'],
mlExplorerSwimlane: { viewByFromPage: 2, viewByPerPage: 20 },
refreshInterval: {
display: 'Off',
pause: false,
value: 0,
},
timeRange: {
from: '2019-02-07T00:00:00.000Z',
to: '2020-08-13T17:15:00.000Z',
mode: 'absolute',
},
query: {
analyze_wildcard: true,
query: '*',
},
});
expect(url).toBe(
"/app/ml/explorer?_g=(ml:(jobIds:!(fq_single_1)),refreshInterval:(display:Off,pause:!f,value:0),time:(from:'2019-02-07T00:00:00.000Z',mode:absolute,to:'2020-08-13T17:15:00.000Z'))&_a=(mlExplorerFilter:(),mlExplorerSwimlane:(viewByFromPage:2,viewByPerPage:20),query:(analyze_wildcard:!t,query:'*'))"
);
});
it('should generate valid URL for the Anomaly Explorer page for multiple jobIds', async () => {
const url = await urlGenerator.createUrl({
page: ML_TABS.ANOMALY_EXPLORER,
jobIds: ['fq_single_1', 'logs_categorization_1'],
timeRange: {
from: '2019-02-07T00:00:00.000Z',
to: '2020-08-13T17:15:00.000Z',
mode: 'absolute',
},
});
expect(url).toBe(
"/app/ml/explorer?_g=(ml:(jobIds:!(fq_single_1,logs_categorization_1)),time:(from:'2019-02-07T00:00:00.000Z',mode:absolute,to:'2020-08-13T17:15:00.000Z'))&_a=(mlExplorerFilter:(),mlExplorerSwimlane:())"
);
});
});

describe('Single Metric Viewer Page', () => {
it('should generate valid URL for the Single Metric Viewer page', async () => {
const url = await urlGenerator.createUrl({
page: ML_TABS.TIME_SERIES_EXPLORER,
jobIds: ['logs_categorization_1'],
refreshInterval: {
display: 'Off',
pause: false,
value: 0,
},
timeRange: {
from: '2020-07-12T00:39:02.912Z',
to: '2020-07-22T15:52:18.613Z',
mode: 'absolute',
},
query: {
analyze_wildcard: true,
query: '*',
},
});
expect(url).toBe(
"/app/ml/timeseriesexplorer?_g=(ml:(jobIds:!(logs_categorization_1)),refreshInterval:(display:Off,pause:!f,value:0),time:(from:'2020-07-12T00:39:02.912Z',mode:absolute,to:'2020-07-22T15:52:18.613Z'))&_a=(mlTimeSeriesExplorer:(),query:(query_string:(analyze_wildcard:!t,query:'*')))"
);
});

it('should generate valid URL for the Single Metric Viewer page with extra settings', async () => {
const url = await urlGenerator.createUrl({
page: ML_TABS.TIME_SERIES_EXPLORER,
jobIds: ['logs_categorization_1'],
detectorIndex: 0,
entities: { mlcategory: '2' },
refreshInterval: {
display: 'Off',
pause: false,
value: 0,
},
timeRange: {
from: '2020-07-12T00:39:02.912Z',
to: '2020-07-22T15:52:18.613Z',
mode: 'absolute',
},
zoom: {
from: '2020-07-20T23:58:29.367Z',
to: '2020-07-21T11:00:13.173Z',
},
query: {
analyze_wildcard: true,
query: '*',
},
});
expect(url).toBe(
"/app/ml/timeseriesexplorer?_g=(ml:(jobIds:!(logs_categorization_1)),refreshInterval:(display:Off,pause:!f,value:0),time:(from:'2020-07-12T00:39:02.912Z',mode:absolute,to:'2020-07-22T15:52:18.613Z'))&_a=(mlTimeSeriesExplorer:(detectorIndex:0,entities:(mlcategory:'2')),query:(query_string:(analyze_wildcard:!t,query:'*')),zoom:(from:'2020-07-20T23:58:29.367Z',to:'2020-07-21T11:00:13.173Z'))"
);
});
});
});

describe('DataFrameAnalytics', () => {
describe('JobManagement Page', () => {
it('should generate valid URL for the Data Frame Analytics job management page', async () => {
const url = await urlGenerator.createUrl({
page: ML_TABS.DATA_FRAME_ANALYTICS,
});
expect(url).toBe('/app/ml/data_frame_analytics');
});
it('should generate valid URL for the Data Frame Analytics job management page with jobId', async () => {
const url = await urlGenerator.createUrl({
page: ML_TABS.DATA_FRAME_ANALYTICS,
jobId: 'grid_regression_1',
});
expect(url).toBe('/app/ml/data_frame_analytics?mlManagement=(jobId:grid_regression_1)');
});

it('should generate valid URL for the Data Frame Analytics job management page with groupIds', async () => {
const url = await urlGenerator.createUrl({
page: ML_TABS.DATA_FRAME_ANALYTICS,
groupIds: ['group_1', 'group_2'],
});
expect(url).toBe('/app/ml/data_frame_analytics?mlManagement=(groupIds:!(group_1,group_2))');
});
});

describe('ExplorationPage', () => {
it('should generate valid URL for the Data Frame Analytics exploration page for job', async () => {
const url = await urlGenerator.createUrl({
page: ML_TABS.DATA_FRAME_ANALYTICS_EXPLORATION,
jobId: 'grid_regression_1',
analysisType: ANALYSIS_CONFIG_TYPE.REGRESSION,
});
expect(url).toBe(
'/app/ml/data_frame_analytics/exploration?_g=(ml:(analysisType:regression,jobId:grid_regression_1))'
);
});
});
});

describe('DataVisualizer', () => {
it('should generate valid URL for the Data Visualizer page', async () => {
const url = await urlGenerator.createUrl({
page: ML_TABS.DATA_VISUALIZER,
});
expect(url).toBe('/app/ml/datavisualizer');
});

it('should generate valid URL for the Data Visualizer import file page', async () => {
peteharverson marked this conversation as resolved.
Show resolved Hide resolved
const url = await urlGenerator.createUrl({
page: ML_TABS.DATA_VISUALIZER_FILE,
});
expect(url).toBe('/app/ml/filedatavisualizer');
});

it('should generate valid URL for the Data Visualizer import by index page', async () => {
peteharverson marked this conversation as resolved.
Show resolved Hide resolved
const url = await urlGenerator.createUrl({
page: ML_TABS.DATA_VISUALIZER_INDEX_SELECT,
});
expect(url).toBe('/app/ml/datavisualizer_index_select');
});

it('should generate valid URL for the Data Visualizer Viewer page', async () => {
peteharverson marked this conversation as resolved.
Show resolved Hide resolved
const url = await urlGenerator.createUrl({
page: ML_TABS.DATA_VISUALIZER_VIEWER,
index: '3da93760-e0af-11ea-9ad3-3bcfc330e42a',
globalState: {
time: {
from: 'now-30m',
to: 'now',
mode: 'quick',
},
},
});
expect(url).toBe(
'/app/ml/jobs/new_job/datavisualizer?index=3da93760-e0af-11ea-9ad3-3bcfc330e42a&_g=(time:(from:now-30m,mode:quick,to:now))'
);
});
expect(url).toBe(
'/app/ml/explorer#?_g=(ml:(jobIds:!(test-job)))&_a=(mlExplorerFilter:(),mlExplorerSwimlane:(viewByFromPage:2,viewByPerPage:20))'
);
});

it('should throw an error in case the page is not provided', async () => {
Expand Down
Loading