Skip to content

Commit

Permalink
package @kbn/ml-locator
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Sep 24, 2024
1 parent 4f93f54 commit 4f8243a
Show file tree
Hide file tree
Showing 175 changed files with 367 additions and 250 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -609,13 +609,15 @@ x-pack/packages/ml/data_grid @elastic/ml-ui
x-pack/packages/ml/data_view_utils @elastic/ml-ui
x-pack/packages/ml/date_picker @elastic/ml-ui
x-pack/packages/ml/date_utils @elastic/ml-ui
x-pack/packages/ml/embeddables @elastic/ml-ui
x-pack/packages/ml/error_utils @elastic/ml-ui
x-pack/packages/ml/field_stats_flyout @elastic/ml-ui
x-pack/packages/ml/in_memory_table @elastic/ml-ui
x-pack/packages/ml/is_defined @elastic/ml-ui
x-pack/packages/ml/is_populated_object @elastic/ml-ui
x-pack/packages/ml/kibana_theme @elastic/ml-ui
x-pack/packages/ml/local_storage @elastic/ml-ui
x-pack/packages/ml/locator @elastic/ml-ui
x-pack/packages/ml/nested_property @elastic/ml-ui
x-pack/packages/ml/number_utils @elastic/ml-ui
x-pack/packages/ml/parse_interval @elastic/ml-ui
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@
"@kbn/ml-is-populated-object": "link:x-pack/packages/ml/is_populated_object",
"@kbn/ml-kibana-theme": "link:x-pack/packages/ml/kibana_theme",
"@kbn/ml-local-storage": "link:x-pack/packages/ml/local_storage",
"@kbn/ml-locator": "link:x-pack/packages/ml/locator",
"@kbn/ml-nested-property": "link:x-pack/packages/ml/nested_property",
"@kbn/ml-number-utils": "link:x-pack/packages/ml/number_utils",
"@kbn/ml-parse-interval": "link:x-pack/packages/ml/parse_interval",
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,8 @@
"@kbn/ml-kibana-theme/*": ["x-pack/packages/ml/kibana_theme/*"],
"@kbn/ml-local-storage": ["x-pack/packages/ml/local_storage"],
"@kbn/ml-local-storage/*": ["x-pack/packages/ml/local_storage/*"],
"@kbn/ml-locator": ["x-pack/packages/ml/locator"],
"@kbn/ml-locator/*": ["x-pack/packages/ml/locator/*"],
"@kbn/ml-nested-property": ["x-pack/packages/ml/nested_property"],
"@kbn/ml-nested-property/*": ["x-pack/packages/ml/nested_property/*"],
"@kbn/ml-number-utils": ["x-pack/packages/ml/number_utils"],
Expand Down
3 changes: 3 additions & 0 deletions x-pack/packages/ml/embeddables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ export {
ANOMALY_SINGLE_METRIC_VIEWER_EMBEDDABLE_TYPE,
ANOMALY_SWIMLANE_EMBEDDABLE_TYPE,
ANOMALY_EXPLORER_CHARTS_EMBEDDABLE_TYPE,
type AnomalyExplorerChartsEmbeddableType,
type AnomalySwimLaneEmbeddableType,
type MlEmbeddableTypes,
} from './constants';
2 changes: 1 addition & 1 deletion x-pack/packages/ml/embeddables/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"name": "@kbn/ml-embeddables",
"private": true,
"version": "1.0.0",
"license": "Elastic License 2.0 OR AGPL-3.0-only OR SSPL-1.0"
"license": "Elastic License 2.0"
}
3 changes: 3 additions & 0 deletions x-pack/packages/ml/locator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @kbn/ml-locator

Empty package generated by @kbn/generate
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

import { setStateToKbnUrl } from '@kbn/kibana-utils-plugin/public';
import { ML_PAGES } from '../../../common/constants/locator';
import type { ChangePointDetectionUrlState } from '../../../common/types/locator';
import { ML_PAGES } from '../constants';
import type { ChangePointDetectionUrlState } from '../types';

/**
* Creates URL to the Change Point Detection page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isEmpty } from 'lodash';
import { setStateToKbnUrl } from '@kbn/kibana-utils-plugin/public';
import type { ListingPageUrlState } from '@kbn/ml-url-state';
import type {
AppPageState,
AnomalyDetectionUrlState,
ExplorerAppState,
ExplorerGlobalState,
Expand All @@ -18,11 +19,10 @@ import type {
TimeSeriesExplorerAppState,
TimeSeriesExplorerGlobalState,
TimeSeriesExplorerUrlState,
} from '../../../common/types/locator';
import { ML_PAGES } from '../../../common/constants/locator';
} from '../types';
import { ML_PAGES } from '../constants';
import { formatGenericMlUrl } from './common';
import { getGroupQueryText, getJobQueryText } from '../../../common/util/string_utils';
import type { AppPageState } from '../../../common/types/common';
import { getGroupQueryText, getJobQueryText } from './string_utils';

/**
* Creates URL to the Anomaly Detection Job management page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { isEmpty } from 'lodash';
import { setStateToKbnUrl } from '@kbn/kibana-utils-plugin/public';
import type { MlGenericUrlState } from '../../../common/types/locator';
import type { MlGenericUrlState } from '../types';

export function extractParams<UrlState>(urlState: UrlState) {
// page should be guaranteed to exist here but <UrlState> is unknown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import { setStateToKbnUrl } from '@kbn/kibana-utils-plugin/public';
import type { ListingPageUrlState } from '@kbn/ml-url-state';
import { formatGenericMlUrl } from './common';
import type {
AppPageState,
DataFrameAnalyticsExplorationQueryState,
DataFrameAnalyticsExplorationUrlState,
DataFrameAnalyticsUrlState,
ExplorationPageUrlState,
MlGenericUrlState,
MlCommonGlobalState,
} from '../../../common/types/locator';
import { ML_PAGES } from '../../../common/constants/locator';
import { getGroupQueryText, getJobQueryText } from '../../../common/util/string_utils';
import type { AppPageState } from '../../../common/types/common';
} from '../types';
import { ML_PAGES } from '../constants';
import { getGroupQueryText, getJobQueryText } from './string_utils';

export function formatDataFrameAnalyticsJobManagementUrl(
appBasePath: string,
Expand Down
24 changes: 24 additions & 0 deletions x-pack/packages/ml/locator/formatters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export { extractParams, formatGenericMlUrl } from './common';
export {
formatAnomalyDetectionJobManagementUrl,
formatAnomalyDetectionCreateJobSelectType,
formatAnomalyDetectionCreateJobSelectIndex,
formatSuppliedConfigurationsManagementUrl,
formatExplorerUrl,
formatSingleMetricViewerUrl,
} from './anomaly_detection';
export {
formatDataFrameAnalyticsJobManagementUrl,
formatDataFrameAnalyticsExplorationUrl,
formatDataFrameAnalyticsCreateJobUrl,
formatDataFrameAnalyticsMapUrl,
} from './data_frame_analytics';
export { formatNotificationsUrl } from './notifications';
export { formatEditCalendarUrl, formatEditFilterUrl } from './settings';
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
import { setStateToKbnUrl } from '@kbn/kibana-utils-plugin/public';
import { isPopulatedObject } from '@kbn/ml-is-populated-object';
import { ML_PAGES } from '../../../common/constants/locator';
import type { NotificationsUrlState } from '../../../common/types/locator';
import { ML_PAGES } from '../constants';
import type { NotificationsUrlState } from '../types';

export interface NotificationsAppState {
level?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

import { setStateToKbnUrl } from '@kbn/kibana-utils-plugin/public';
import type { CalendarEditUrlState, FilterEditUrlState } from '../../../common/types/locator';
import { ML_PAGES } from '../../../common/constants/locator';
import type { CalendarEditUrlState, FilterEditUrlState } from '../types';
import { ML_PAGES } from '../constants';

export function formatEditCalendarUrl(
appBasePath: string,
Expand Down
23 changes: 23 additions & 0 deletions x-pack/packages/ml/locator/formatters/string_utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { getGroupQueryText } from './string_utils';

describe('getGroupQueryText', () => {
const groupIdOne = 'test_group_id_1';
const groupIdTwo = 'test_group_id_2';

it('should get query string for selected group ids', () => {
const actual = getGroupQueryText([groupIdOne, groupIdTwo]);
expect(actual).toBe(`groups:(${groupIdOne} or ${groupIdTwo})`);
});

it('should get query string for selected group id', () => {
const actual = getGroupQueryText([groupIdOne]);
expect(actual).toBe(`groups:(${groupIdOne})`);
});
});
14 changes: 14 additions & 0 deletions x-pack/packages/ml/locator/formatters/string_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export function getGroupQueryText(groupIds: string[]): string {
return `groups:(${groupIds.join(' or ')})`;
}

export function getJobQueryText(jobIds: string | string[]): string {
return Array.isArray(jobIds) ? `id:(${jobIds.join(' OR ')})` : `id:${jobIds}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

import { setStateToKbnUrl } from '@kbn/kibana-utils-plugin/public';
import type { ListingPageUrlState } from '@kbn/ml-url-state';
import type { MemoryUsageUrlState, TrainedModelsUrlState } from '../../../common/types/locator';
import { ML_PAGES } from '../../../common/constants/locator';
import type { AppPageState } from '../../../common/types/common';
import type { AppPageState, MemoryUsageUrlState, TrainedModelsUrlState } from '../types';
import { ML_PAGES } from '../constants';

export function formatTrainedModelsManagementUrl(
appBasePath: string,
Expand Down
20 changes: 20 additions & 0 deletions x-pack/packages/ml/locator/index.ts
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
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export { formatExplorerUrl } from './formatters';
export { ML_APP_LOCATOR, ML_PAGES, type MlPages } from './constants';
export { MlLocatorDefinition, type MlLocatorParams, type MlLocator } from './ml_locator';
export { useMlHref } from './use_ml_href';
export type {
AnomalyExplorerFilterUrlState,
AnomalyExplorerSwimLaneUrlState,
AnomalyDetectionQueryState,
ExpandablePanels,
ExplorationPageUrlState,
ExplorerAppState,
TimeSeriesExplorerAppState,
} from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/

export * from './common';
export * from './anomaly_detection';
export * from './data_frame_analytics';
export * from './settings';
module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../../../..',
roots: ['<rootDir>/x-pack/packages/ml/locator'],
};
5 changes: 5 additions & 0 deletions x-pack/packages/ml/locator/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "shared-browser",
"id": "@kbn/ml-locator",
"owner": "@elastic/ml-ui"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { MlLocatorDefinition } from './ml_locator';
import { ML_PAGES } from '../../common/constants/locator';
import { ML_PAGES } from './constants';
import { ANALYSIS_CONFIG_TYPE } from '@kbn/ml-data-frame-analytics-utils';

describe('ML locator', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import type {
MlLocatorParams,
MlLocator,
ChangePointDetectionQueryState,
} from '../../common/types/locator';
import { ML_APP_LOCATOR, ML_PAGES } from '../../common/constants/locator';
} from './types';
import { ML_APP_LOCATOR, ML_PAGES } from './constants';
import {
formatAnomalyDetectionCreateJobSelectIndex,
formatAnomalyDetectionCreateJobSelectType,
Expand Down
6 changes: 6 additions & 0 deletions x-pack/packages/ml/locator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@kbn/ml-locator",
"private": true,
"version": "1.0.0",
"license": "Elastic License 2.0"
}
30 changes: 30 additions & 0 deletions x-pack/packages/ml/locator/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target/types",
"types": [
"jest",
"node",
"react",
"@emotion/react/types/css-prop"
]
},
"include": [
"**/*.ts",
"**/*.tsx",
],
"exclude": [
"target/**/*"
],
"kbn_references": [
"@kbn/kibana-utils-plugin",
"@kbn/ml-url-state",
"@kbn/ml-is-populated-object",
"@kbn/share-plugin",
"@kbn/ml-data-frame-analytics-utils",
"@kbn/utility-types",
"@kbn/data-plugin",
"@kbn/ml-anomaly-utils",
"@kbn/ml-query-utils",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import type { DataFrameAnalysisConfigType } from '@kbn/ml-data-frame-analytics-u
import type { InfluencersFilterQuery } from '@kbn/ml-anomaly-utils';
import type { SearchQueryLanguage } from '@kbn/ml-query-utils';
import type { ListingPageUrlState } from '@kbn/ml-url-state';
import type { JobId } from './anomaly_detection_jobs/job';
import type { ML_PAGES } from '../constants/locator';
import type { MlPages, ML_PAGES } from './constants';

type JobId = string;

type OptionalPageState = object | undefined;

export type AppPageState<T> = {
[key in MlPages]?: Partial<T>;
};

export type MLPageState<PageType, PageState> = PageState extends OptionalPageState
? { page: PageType; pageState?: PageState }
: PageState extends object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
*/

import type { DependencyList } from 'react';
import type { MlPluginSetup } from '..';
import type { MlLocatorParams } from '../../common/types/locator';

import type { LocatorPublic } from '@kbn/share-plugin/public';

import type { MlLocatorParams } from './types';

/**
* Provides a URL to ML plugin page
* TODO remove basePath parameter
*/
export const useMlHref = (
ml: MlPluginSetup | undefined,
mlLocator: LocatorPublic<MlLocatorParams> | undefined,
basePath: string | undefined,
params: MlLocatorParams,
dependencies?: DependencyList
) => {
return ml && ml.locator
? ml.locator.useUrl(params, undefined, dependencies)
return mlLocator
? mlLocator.useUrl(params, undefined, dependencies)
: basePath !== undefined
? `${basePath}/app/ml/${params.page}`
: '';
Expand Down
6 changes: 0 additions & 6 deletions x-pack/plugins/ml/common/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* 2.0.
*/

import type { MlPages } from '../constants/locator';

export interface Dictionary<TValue> {
[id: string]: TValue;
}
Expand Down Expand Up @@ -35,10 +33,6 @@ type DeepReadonlyObject<T> = {
readonly [P in keyof T]: DeepReadonly<T[P]>;
};

export type AppPageState<T> = {
[key in MlPages]?: Partial<T>;
};

type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };

export type XOR<T, U> = T | U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
Expand Down
Loading

0 comments on commit 4f8243a

Please sign in to comment.