Skip to content

Commit

Permalink
[ML] Refactor to own folder
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Aug 31, 2020
1 parent 689f0f6 commit 4d6a5a0
Show file tree
Hide file tree
Showing 9 changed files with 437 additions and 345 deletions.
Original file line number Diff line number Diff line change
@@ -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<AnomalyDetectionUrlState>(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<AnomalyDetectionQueryState>(
'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<ExplorerAppState> = {
mlExplorerSwimlane,
mlExplorerFilter,
};
if (query) appState.query = query;

if (jobIds) {
const queryState: Partial<ExplorerGlobalState> = {
ml: {
jobIds,
},
};

if (timeRange) queryState.time = timeRange;
if (refreshInterval) queryState.refreshInterval = refreshInterval;

url = setStateToKbnUrl<Partial<ExplorerGlobalState>>(
'_g',
queryState,
{ useHash: false, storeInHashQuery: false },
url
);
url = setStateToKbnUrl<Partial<ExplorerAppState>>(
'_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<TimeSeriesExplorerAppState> = {};
const mlTimeSeriesExplorer: Partial<TimeSeriesExplorerAppState['mlTimeSeriesExplorer']> = {};

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<TimeSeriesExplorerGlobalState>(
'_g',
queryState,
{ useHash: false, storeInHashQuery: false },
url
);
url = setStateToKbnUrl<TimeSeriesExplorerAppState>(
'_a',
appState,
{ useHash: false, storeInHashQuery: false },
url
);

return url;
}
54 changes: 54 additions & 0 deletions x-pack/plugins/ml/public/ml_url_generator/common.ts
Original file line number Diff line number Diff line change
@@ -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: UrlState) {
// page should be guaranteed to exist here but <UrlState> 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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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<DataFrameAnalyticsUrlState>(mlUrlGeneratorState);

if (!isEmpty(params)) {
const { jobId, groupIds } = params;
const queryState: Partial<DataFrameAnalyticsQueryState> = {
jobId,
groupIds,
};

url = setStateToKbnUrl<Partial<DataFrameAnalyticsQueryState>>(
'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<DataFrameAnalyticsExplorationUrlState>(mlUrlGeneratorState);

if (!isEmpty(params)) {
const { jobId, analysisType } = params;
const queryState: DataFrameAnalyticsExplorationQueryState = {
ml: {
jobId,
analysisType,
},
};

url = setStateToKbnUrl<DataFrameAnalyticsExplorationQueryState>(
'_g',
queryState,
{ useHash: false, storeInHashQuery: false },
url
);
}

return url;
}
6 changes: 6 additions & 0 deletions x-pack/plugins/ml/public/ml_url_generator/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading

0 comments on commit 4d6a5a0

Please sign in to comment.