diff --git a/x-pack/plugins/ml/public/ml_url_generator/anomaly_detection_urls_generator.ts b/x-pack/plugins/ml/public/ml_url_generator/anomaly_detection_urls_generator.ts new file mode 100644 index 00000000000000..4aa7266398c5b8 --- /dev/null +++ b/x-pack/plugins/ml/public/ml_url_generator/anomaly_detection_urls_generator.ts @@ -0,0 +1,159 @@ +/* + * 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 isEmpty from 'lodash/isEmpty'; +import { + AnomalyDetectionQueryState, + AnomalyDetectionUrlState, + ExplorerAppState, + ExplorerGlobalState, + ExplorerUrlState, + MlGenericUrlState, + TimeSeriesExplorerAppState, + TimeSeriesExplorerGlobalState, + TimeSeriesExplorerUrlState, +} from '../../common/types/ml_url_generator'; +import { ML_PAGES } from '../../common/constants/ml_url_generator'; +import { extractParams, createIndexBasedMlUrl } from './common'; +import { setStateToKbnUrl } from '../../../../../src/plugins/kibana_utils/public'; +/** + * Creates URL to the Anomaly Detection Job management page + */ +export function createAnomalyDetectionJobManagementUrl( + appBasePath: string, + mlUrlGeneratorState: AnomalyDetectionUrlState +): string { + const { params } = extractParams(mlUrlGeneratorState); + let url = `${appBasePath}/${ML_PAGES.ANOMALY_DETECTION_JOBS_MANAGE}`; + if (isEmpty(params)) { + return url; + } + const { jobId, groupIds } = params; + const queryState: AnomalyDetectionQueryState = { + jobId, + groupIds, + }; + + url = setStateToKbnUrl( + 'mlManagement', + queryState, + { useHash: false, storeInHashQuery: false }, + url + ); + return url; +} + +export function createAnomalyDetectionCreatJobSelectType( + appBasePath: string, + mlGenericUrlState: MlGenericUrlState +): string { + return createIndexBasedMlUrl(appBasePath, mlGenericUrlState); +} + +/** + * Creates URL to the Anomaly Explorer page + */ +export function createExplorerUrl( + appBasePath: string, + { + refreshInterval, + timeRange, + jobIds, + query, + mlExplorerSwimlane = {}, + mlExplorerFilter = {}, + }: ExplorerUrlState +): string { + let url = `${appBasePath}/${ML_PAGES.ANOMALY_EXPLORER}`; + + const appState: Partial = { + mlExplorerSwimlane, + mlExplorerFilter, + }; + if (query) appState.query = query; + + if (jobIds) { + const queryState: Partial = { + ml: { + jobIds, + }, + }; + + if (timeRange) queryState.time = timeRange; + if (refreshInterval) queryState.refreshInterval = refreshInterval; + + url = setStateToKbnUrl>( + '_g', + queryState, + { useHash: false, storeInHashQuery: false }, + url + ); + url = setStateToKbnUrl>( + '_a', + appState, + { useHash: false, storeInHashQuery: false }, + url + ); + } + + return url; +} + +/** + * Creates URL to the SingleMetricViewer page + */ +export function createSingleMetricViewerUrl( + appBasePath: string, + { + timeRange, + jobIds, + refreshInterval, + zoom, + query, + detectorIndex, + entities, + }: TimeSeriesExplorerUrlState +): string { + let url = `${appBasePath}/${ML_PAGES.SINGLE_METRIC_VIEWER}`; + const queryState: TimeSeriesExplorerGlobalState = { + ml: { + jobIds, + }, + refreshInterval, + time: timeRange, + }; + + const appState: Partial = {}; + const mlTimeSeriesExplorer: Partial = {}; + + if (detectorIndex !== undefined) { + mlTimeSeriesExplorer.detectorIndex = detectorIndex; + } + if (entities !== undefined) { + mlTimeSeriesExplorer.entities = entities; + } + appState.mlTimeSeriesExplorer = mlTimeSeriesExplorer; + + if (zoom) appState.zoom = zoom; + if (query) + appState.query = { + query_string: query, + }; + url = setStateToKbnUrl( + '_g', + queryState, + { useHash: false, storeInHashQuery: false }, + url + ); + url = setStateToKbnUrl( + '_a', + appState, + { useHash: false, storeInHashQuery: false }, + url + ); + + return url; +} diff --git a/x-pack/plugins/ml/public/ml_url_generator/common.ts b/x-pack/plugins/ml/public/ml_url_generator/common.ts new file mode 100644 index 00000000000000..34a7fac9c305f4 --- /dev/null +++ b/x-pack/plugins/ml/public/ml_url_generator/common.ts @@ -0,0 +1,54 @@ +/* + * 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 isEmpty from 'lodash/isEmpty'; +import { MlGenericUrlState } from '../../common/types/ml_url_generator'; +import { setStateToKbnUrl } from '../../../../../src/plugins/kibana_utils/public'; + +export function extractParams(urlState: UrlState) { + // page should be guaranteed to exist here but is unknown + // @ts-ignore + const { page, ...params } = urlState; + return { page, params }; +} + +/** + * Creates generic index based search ML url + * e.g. `jobs/new_job/datavisualizer?index=3da93760-e0af-11ea-9ad3-3bcfc330e42a` + */ +export function createIndexBasedMlUrl( + appBasePath: string, + mlGenericUrlState: MlGenericUrlState +): string { + const { globalState, appState, page, index, savedSearchId, ...restParams } = mlGenericUrlState; + let url = `${appBasePath}/${page}`; + + if (index !== undefined && savedSearchId === undefined) { + url = `${url}?index=${index}`; + } + if (index === undefined && savedSearchId !== undefined) { + url = `${url}?savedSearchId=${savedSearchId}`; + } + + if (!isEmpty(restParams)) { + Object.keys(restParams).forEach((key) => { + url = setStateToKbnUrl( + key, + restParams[key], + { useHash: false, storeInHashQuery: false }, + url + ); + }); + } + + if (globalState) { + url = setStateToKbnUrl('_g', globalState, { useHash: false, storeInHashQuery: false }, url); + } + if (appState) { + url = setStateToKbnUrl('_a', appState, { useHash: false, storeInHashQuery: false }, url); + } + return url; +} diff --git a/x-pack/plugins/ml/public/ml_url_generator/data_visualizer_urls_generator.ts b/x-pack/plugins/ml/public/ml_url_generator/data_visualizer_urls_generator.ts new file mode 100644 index 00000000000000..15d932c8c2f86c --- /dev/null +++ b/x-pack/plugins/ml/public/ml_url_generator/data_visualizer_urls_generator.ts @@ -0,0 +1,28 @@ +/* + * 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. + */ + +/** + * Creates URL to the Data Visualizer page + */ +import { DataVisualizerUrlState, MlGenericUrlState } from '../../common/types/ml_url_generator'; +import { createIndexBasedMlUrl } from './common'; + +export function createDataVisualizerUrl( + appBasePath: string, + { page }: DataVisualizerUrlState +): string { + return `${appBasePath}/${page}`; +} + +/** + * Creates URL to the Index Data Visualizer + */ +export function createIndexDataVisualizerUrl( + appBasePath: string, + mlGenericUrlState: MlGenericUrlState +): string { + return createIndexBasedMlUrl(appBasePath, mlGenericUrlState); +} diff --git a/x-pack/plugins/ml/public/ml_url_generator/dataframe_analytics_urls_generator.ts b/x-pack/plugins/ml/public/ml_url_generator/dataframe_analytics_urls_generator.ts new file mode 100644 index 00000000000000..9d8c8057adb6fd --- /dev/null +++ b/x-pack/plugins/ml/public/ml_url_generator/dataframe_analytics_urls_generator.ts @@ -0,0 +1,74 @@ +/* + * 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. + */ + +/** + * Creates URL to the DataFrameAnalytics page + */ +import isEmpty from 'lodash/isEmpty'; +import { + DataFrameAnalyticsExplorationQueryState, + DataFrameAnalyticsExplorationUrlState, + DataFrameAnalyticsQueryState, + DataFrameAnalyticsUrlState, +} from '../../common/types/ml_url_generator'; +import { ML_PAGES } from '../../common/constants/ml_url_generator'; +import { extractParams } from './common'; +import { setStateToKbnUrl } from '../../../../../src/plugins/kibana_utils/public'; + +export function createDataframeAnalyticsUrl( + appBasePath: string, + mlUrlGeneratorState: DataFrameAnalyticsUrlState +): string { + let url = `${appBasePath}/${ML_PAGES.DATA_FRAME_ANALYTICS_JOBS_MANAGE}`; + const { params } = extractParams(mlUrlGeneratorState); + + if (!isEmpty(params)) { + const { jobId, groupIds } = params; + const queryState: Partial = { + jobId, + groupIds, + }; + + url = setStateToKbnUrl>( + 'mlManagement', + queryState, + { useHash: false, storeInHashQuery: false }, + url + ); + } + + return url; +} + +/** + * Creates URL to the DataFrameAnalytics Exploration page + */ +export function createDataframeAnalyticsExplorationUrl( + appBasePath: string, + mlUrlGeneratorState: DataFrameAnalyticsExplorationUrlState +): string { + let url = `${appBasePath}/${ML_PAGES.DATA_FRAME_ANALYTICS_EXPLORATION}`; + const { params } = extractParams(mlUrlGeneratorState); + + if (!isEmpty(params)) { + const { jobId, analysisType } = params; + const queryState: DataFrameAnalyticsExplorationQueryState = { + ml: { + jobId, + analysisType, + }, + }; + + url = setStateToKbnUrl( + '_g', + queryState, + { useHash: false, storeInHashQuery: false }, + url + ); + } + + return url; +} diff --git a/x-pack/plugins/ml/public/ml_url_generator/index.ts b/x-pack/plugins/ml/public/ml_url_generator/index.ts new file mode 100644 index 00000000000000..1579b187278c4f --- /dev/null +++ b/x-pack/plugins/ml/public/ml_url_generator/index.ts @@ -0,0 +1,6 @@ +/* + * 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. + */ +export { MlUrlGenerator, registerUrlGenerator } from './ml_url_generator'; diff --git a/x-pack/plugins/ml/public/url_generator.test.ts b/x-pack/plugins/ml/public/ml_url_generator/ml_url_generator.test.ts similarity index 97% rename from x-pack/plugins/ml/public/url_generator.test.ts rename to x-pack/plugins/ml/public/ml_url_generator/ml_url_generator.test.ts index a82c98994d9a52..68b222c7cd4891 100644 --- a/x-pack/plugins/ml/public/url_generator.test.ts +++ b/x-pack/plugins/ml/public/ml_url_generator/ml_url_generator.test.ts @@ -4,9 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { MlUrlGenerator } from './url_generator'; -import { ML_PAGES } from '../common/constants/ml_url_generator'; -import { ANALYSIS_CONFIG_TYPE } from './application/data_frame_analytics/common'; +import { MlUrlGenerator } from './ml_url_generator'; +import { ML_PAGES } from '../../common/constants/ml_url_generator'; +import { ANALYSIS_CONFIG_TYPE } from '../application/data_frame_analytics/common'; describe('MlUrlGenerator', () => { const urlGenerator = new MlUrlGenerator({ diff --git a/x-pack/plugins/ml/public/ml_url_generator/ml_url_generator.ts b/x-pack/plugins/ml/public/ml_url_generator/ml_url_generator.ts new file mode 100644 index 00000000000000..40ae58651bb2a7 --- /dev/null +++ b/x-pack/plugins/ml/public/ml_url_generator/ml_url_generator.ts @@ -0,0 +1,112 @@ +/* + * 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 { CoreSetup } from 'kibana/public'; +import { + SharePluginSetup, + UrlGeneratorsDefinition, + UrlGeneratorState, +} from '../../../../../src/plugins/share/public'; +import { MlStartDependencies } from '../plugin'; +import { ML_PAGES, ML_APP_URL_GENERATOR } from '../../common/constants/ml_url_generator'; +import { + MlUrlGeneratorState, + AnomalyDetectionUrlState, + ExplorerUrlState, + TimeSeriesExplorerUrlState, + DataFrameAnalyticsUrlState, + DataFrameAnalyticsExplorationUrlState, + DataVisualizerUrlState, + MlGenericUrlState, +} from '../../common/types/ml_url_generator'; +import { + createAnomalyDetectionJobManagementUrl, + createAnomalyDetectionCreatJobSelectType, + createExplorerUrl, + createSingleMetricViewerUrl, +} from './anomaly_detection_urls_generator'; +import { + createDataframeAnalyticsUrl, + createDataframeAnalyticsExplorationUrl, +} from './dataframe_analytics_urls_generator'; +import { + createIndexDataVisualizerUrl, + createDataVisualizerUrl, +} from './data_visualizer_urls_generator'; + +declare module '../../../../../src/plugins/share/public' { + export interface UrlGeneratorStateMapping { + [ML_APP_URL_GENERATOR]: UrlGeneratorState; + } +} + +interface Params { + appBasePath: string; + useHash: boolean; +} + +export class MlUrlGenerator implements UrlGeneratorsDefinition { + constructor(private readonly params: Params) {} + + public readonly id = ML_APP_URL_GENERATOR; + + public readonly createUrl = async (mlUrlGeneratorState: MlUrlGeneratorState): Promise => { + const appBasePath = this.params.appBasePath; + switch (mlUrlGeneratorState.page) { + case ML_PAGES.ANOMALY_DETECTION_JOBS_MANAGE: + return createAnomalyDetectionJobManagementUrl( + appBasePath, + mlUrlGeneratorState as AnomalyDetectionUrlState + ); + case ML_PAGES.ANOMALY_EXPLORER: + return createExplorerUrl(appBasePath, mlUrlGeneratorState as ExplorerUrlState); + case ML_PAGES.ANOMALY_DETECTION_CREATE_JOB_SELECT_TYPE: + return createAnomalyDetectionCreatJobSelectType( + appBasePath, + mlUrlGeneratorState as MlGenericUrlState + ); + case ML_PAGES.SINGLE_METRIC_VIEWER: + return createSingleMetricViewerUrl( + appBasePath, + mlUrlGeneratorState as TimeSeriesExplorerUrlState + ); + case ML_PAGES.DATA_FRAME_ANALYTICS_JOBS_MANAGE: + return createDataframeAnalyticsUrl( + appBasePath, + mlUrlGeneratorState as DataFrameAnalyticsUrlState + ); + case ML_PAGES.DATA_FRAME_ANALYTICS_EXPLORATION: + return createDataframeAnalyticsExplorationUrl( + appBasePath, + mlUrlGeneratorState as DataFrameAnalyticsExplorationUrlState + ); + case ML_PAGES.DATA_VISUALIZER: + case ML_PAGES.DATA_VISUALIZER_FILE: + case ML_PAGES.DATA_VISUALIZER_INDEX_SELECT: + return createDataVisualizerUrl(appBasePath, mlUrlGeneratorState as DataVisualizerUrlState); + case ML_PAGES.DATA_VISUALIZER_INDEX_VIEWER: + return createIndexDataVisualizerUrl(appBasePath, mlUrlGeneratorState as MlGenericUrlState); + default: + throw new Error('Page type is not provided or unknown'); + } + }; +} + +/** + * Registers the URL generator + */ +export function registerUrlGenerator( + share: SharePluginSetup, + core: CoreSetup +) { + const baseUrl = core.http.basePath.prepend('/app/ml'); + share.urlGenerators.registerUrlGenerator( + new MlUrlGenerator({ + appBasePath: baseUrl, + useHash: core.uiSettings.get('state:storeInSessionStorage'), + }) + ); +} diff --git a/x-pack/plugins/ml/public/plugin.ts b/x-pack/plugins/ml/public/plugin.ts index 4f8ceb8effe982..a1a127cc2baed3 100644 --- a/x-pack/plugins/ml/public/plugin.ts +++ b/x-pack/plugins/ml/public/plugin.ts @@ -34,7 +34,7 @@ import { registerFeature } from './register_feature'; import { UiActionsSetup, UiActionsStart } from '../../../../src/plugins/ui_actions/public'; import { registerMlUiActions } from './ui_actions'; import { KibanaLegacyStart } from '../../../../src/plugins/kibana_legacy/public'; -import { registerUrlGenerator } from './url_generator'; +import { registerUrlGenerator } from './ml_url_generator'; import { isFullLicense, isMlEnabled } from '../common/license'; import { registerEmbeddables } from './embeddables'; diff --git a/x-pack/plugins/ml/public/url_generator.ts b/x-pack/plugins/ml/public/url_generator.ts deleted file mode 100644 index 2ef0c7208dc093..00000000000000 --- a/x-pack/plugins/ml/public/url_generator.ts +++ /dev/null @@ -1,341 +0,0 @@ -/* - * 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 { CoreSetup } from 'kibana/public'; -import isEmpty from 'lodash/isEmpty'; -import { - SharePluginSetup, - UrlGeneratorsDefinition, - UrlGeneratorState, -} from '../../../../src/plugins/share/public'; -import { setStateToKbnUrl } from '../../../../src/plugins/kibana_utils/public'; -import { MlStartDependencies } from './plugin'; -import { ML_PAGES, ML_APP_URL_GENERATOR } from '../common/constants/ml_url_generator'; -import { - MlUrlGeneratorState, - AnomalyDetectionUrlState, - ExplorerUrlState, - TimeSeriesExplorerUrlState, - DataFrameAnalyticsUrlState, - DataFrameAnalyticsExplorationUrlState, - DataVisualizerUrlState, - MlGenericUrlState, - AnomalyDetectionQueryState, - ExplorerGlobalState, - TimeSeriesExplorerGlobalState, - TimeSeriesExplorerAppState, - DataFrameAnalyticsExplorationQueryState, - DataFrameAnalyticsQueryState, - ExplorerAppState, -} from '../common/types/ml_url_generator'; - -declare module '../../../../src/plugins/share/public' { - export interface UrlGeneratorStateMapping { - [ML_APP_URL_GENERATOR]: UrlGeneratorState; - } -} - -interface Params { - appBasePath: string; - useHash: boolean; -} - -export class MlUrlGenerator implements UrlGeneratorsDefinition { - constructor(private readonly params: Params) {} - - public readonly id = ML_APP_URL_GENERATOR; - - public readonly createUrl = async (mlUrlGeneratorState: MlUrlGeneratorState): Promise => { - switch (mlUrlGeneratorState.page) { - case ML_PAGES.ANOMALY_DETECTION_JOBS_MANAGE: - return this.createAnomalyDetectionJobManagementUrl( - mlUrlGeneratorState as AnomalyDetectionUrlState - ); - case ML_PAGES.ANOMALY_EXPLORER: - return this.createExplorerUrl(mlUrlGeneratorState as ExplorerUrlState); - case ML_PAGES.ANOMALY_DETECTION_CREATE_JOB_SELECT_TYPE: - return this.createAnomalyDetectionCreatJobSelectType( - mlUrlGeneratorState as MlGenericUrlState - ); - case ML_PAGES.SINGLE_METRIC_VIEWER: - return this.createSingleMetricViewerUrl(mlUrlGeneratorState as TimeSeriesExplorerUrlState); - case ML_PAGES.DATA_FRAME_ANALYTICS_JOBS_MANAGE: - return this.createDataframeAnalyticsUrl(mlUrlGeneratorState as DataFrameAnalyticsUrlState); - case ML_PAGES.DATA_FRAME_ANALYTICS_EXPLORATION: - return this.createDataframeAnalyticsExplorationUrl( - mlUrlGeneratorState as DataFrameAnalyticsExplorationUrlState - ); - case ML_PAGES.DATA_VISUALIZER: - return this.createDataVisualizerUrl(mlUrlGeneratorState as DataVisualizerUrlState); - case ML_PAGES.DATA_VISUALIZER_FILE: - return this.createDataVisualizerUrl(mlUrlGeneratorState as DataVisualizerUrlState); - case ML_PAGES.DATA_VISUALIZER_INDEX_SELECT: - return this.createDataVisualizerUrl(mlUrlGeneratorState as DataVisualizerUrlState); - case ML_PAGES.DATA_VISUALIZER_INDEX_VIEWER: - return this.createIndexDataVisualizerUrl(mlUrlGeneratorState as MlGenericUrlState); - default: - throw new Error('Page type is not provided or unknown'); - } - }; - - private static extractParams(urlState: UrlState) { - // page should be guaranteed to exist here but is unknown - // @ts-ignore - const { page, ...params } = urlState; - return { page, params }; - } - /** - * Creates URL to the Anomaly Detection Job management page - */ - private createAnomalyDetectionJobManagementUrl( - mlUrlGeneratorState: AnomalyDetectionUrlState - ): string { - const { params } = MlUrlGenerator.extractParams(mlUrlGeneratorState); - let url = `${this.params.appBasePath}/${ML_PAGES.ANOMALY_DETECTION_JOBS_MANAGE}`; - if (isEmpty(params)) { - return url; - } - const { jobId, groupIds } = params; - const queryState: AnomalyDetectionQueryState = { - jobId, - groupIds, - }; - - url = setStateToKbnUrl( - 'mlManagement', - queryState, - { useHash: false, storeInHashQuery: false }, - url - ); - return url; - } - - private createAnomalyDetectionCreatJobSelectType(mlGenericUrlState: MlGenericUrlState): string { - return this.createIndexBasedMlUrl(mlGenericUrlState); - } - - /** - * Creates URL to the Anomaly Explorer page - */ - private createExplorerUrl({ - refreshInterval, - timeRange, - jobIds, - query, - mlExplorerSwimlane = {}, - mlExplorerFilter = {}, - }: ExplorerUrlState): string { - let url = `${this.params.appBasePath}/${ML_PAGES.ANOMALY_EXPLORER}`; - - const appState: Partial = { - mlExplorerSwimlane, - mlExplorerFilter, - }; - if (query) appState.query = query; - - if (jobIds) { - const queryState: Partial = { - ml: { - jobIds, - }, - }; - - if (timeRange) queryState.time = timeRange; - if (refreshInterval) queryState.refreshInterval = refreshInterval; - - url = setStateToKbnUrl>( - '_g', - queryState, - { useHash: false, storeInHashQuery: false }, - url - ); - url = setStateToKbnUrl>( - '_a', - appState, - { useHash: false, storeInHashQuery: false }, - url - ); - } - - return url; - } - - /** - * Creates URL to the SingleMetricViewer page - */ - private createSingleMetricViewerUrl({ - timeRange, - jobIds, - refreshInterval, - zoom, - query, - detectorIndex, - entities, - }: TimeSeriesExplorerUrlState): string { - let url = `${this.params.appBasePath}/${ML_PAGES.SINGLE_METRIC_VIEWER}`; - const queryState: TimeSeriesExplorerGlobalState = { - ml: { - jobIds, - }, - refreshInterval, - time: timeRange, - }; - - const appState: Partial = {}; - const mlTimeSeriesExplorer: Partial = {}; - - if (detectorIndex !== undefined) { - mlTimeSeriesExplorer.detectorIndex = detectorIndex; - } - if (entities !== undefined) { - mlTimeSeriesExplorer.entities = entities; - } - appState.mlTimeSeriesExplorer = mlTimeSeriesExplorer; - - if (zoom) appState.zoom = zoom; - if (query) - appState.query = { - query_string: query, - }; - url = setStateToKbnUrl( - '_g', - queryState, - { useHash: false, storeInHashQuery: false }, - url - ); - url = setStateToKbnUrl( - '_a', - appState, - { useHash: false, storeInHashQuery: false }, - url - ); - - return url; - } - - /** - * Creates URL to the DataFrameAnalytics Exploration page - */ - private createDataframeAnalyticsExplorationUrl( - mlUrlGeneratorState: DataFrameAnalyticsExplorationUrlState - ): string { - let url = `${this.params.appBasePath}/${ML_PAGES.DATA_FRAME_ANALYTICS_EXPLORATION}`; - const { params } = MlUrlGenerator.extractParams( - mlUrlGeneratorState - ); - - if (!isEmpty(params)) { - const { jobId, analysisType } = params; - const queryState: DataFrameAnalyticsExplorationQueryState = { - ml: { - jobId, - analysisType, - }, - }; - - url = setStateToKbnUrl( - '_g', - queryState, - { useHash: false, storeInHashQuery: false }, - url - ); - } - - return url; - } - - /** - * Creates URL to the DataFrameAnalytics page - */ - private createDataframeAnalyticsUrl(mlUrlGeneratorState: DataFrameAnalyticsUrlState): string { - let url = `${this.params.appBasePath}/${ML_PAGES.DATA_FRAME_ANALYTICS_JOBS_MANAGE}`; - const { params } = MlUrlGenerator.extractParams( - mlUrlGeneratorState - ); - - if (!isEmpty(params)) { - const { jobId, groupIds } = params; - const queryState: Partial = { - jobId, - groupIds, - }; - - url = setStateToKbnUrl>( - 'mlManagement', - queryState, - { useHash: false, storeInHashQuery: false }, - url - ); - } - - return url; - } - - /** - * Creates URL to the Data Visualizer page - */ - private createDataVisualizerUrl({ page }: DataVisualizerUrlState): string { - return `${this.params.appBasePath}/${page}`; - } - - /** - * Creates URL to the Index Data Visualizer - */ - private createIndexDataVisualizerUrl(mlGenericUrlState: MlGenericUrlState): string { - return this.createIndexBasedMlUrl(mlGenericUrlState); - } - - /** - * Creates generic index based search ML url - * e.g. `jobs/new_job/datavisualizer?index=3da93760-e0af-11ea-9ad3-3bcfc330e42a` - */ - private createIndexBasedMlUrl(mlGenericUrlState: MlGenericUrlState): string { - const { globalState, appState, page, index, savedSearchId, ...restParams } = mlGenericUrlState; - let url = `${this.params.appBasePath}/${page}`; - - if (index !== undefined && savedSearchId === undefined) { - url = `${url}?index=${index}`; - } - if (index === undefined && savedSearchId !== undefined) { - url = `${url}?savedSearchId=${savedSearchId}`; - } - - if (!isEmpty(restParams)) { - Object.keys(restParams).forEach((key) => { - url = setStateToKbnUrl( - key, - restParams[key], - { useHash: false, storeInHashQuery: false }, - url - ); - }); - } - - if (globalState) { - url = setStateToKbnUrl('_g', globalState, { useHash: false, storeInHashQuery: false }, url); - } - if (appState) { - url = setStateToKbnUrl('_a', appState, { useHash: false, storeInHashQuery: false }, url); - } - return url; - } -} - -/** - * Registers the URL generator - */ -export function registerUrlGenerator( - share: SharePluginSetup, - core: CoreSetup -) { - const baseUrl = core.http.basePath.prepend('/app/ml'); - share.urlGenerators.registerUrlGenerator( - new MlUrlGenerator({ - appBasePath: baseUrl, - useHash: core.uiSettings.get('state:storeInSessionStorage'), - }) - ); -}