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] Move nested property utilities and url state to packages #147912

Merged
merged 14 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
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
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -1071,4 +1071,6 @@ x-pack/packages/ml/agg_utils @elastic/ml-ui
x-pack/packages/ml/aiops_components @elastic/ml-ui
x-pack/packages/ml/aiops_utils @elastic/ml-ui
x-pack/packages/ml/is_populated_object @elastic/ml-ui
x-pack/packages/ml/nested_property @elastic/ml-ui
x-pack/packages/ml/string_hash @elastic/ml-ui
x-pack/packages/ml/url_state @elastic/ml-ui
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,9 @@
"@kbn/mapbox-gl": "link:packages/kbn-mapbox-gl",
"@kbn/ml-agg-utils": "link:x-pack/packages/ml/agg_utils",
"@kbn/ml-is-populated-object": "link:x-pack/packages/ml/is_populated_object",
"@kbn/ml-nested-property": "link:x-pack/packages/ml/nested_property",
"@kbn/ml-string-hash": "link:x-pack/packages/ml/string_hash",
"@kbn/ml-url-state": "link:x-pack/packages/ml/url_state",
"@kbn/monaco": "link:packages/kbn-monaco",
"@kbn/osquery-io-ts-types": "link:packages/kbn-osquery-io-ts-types",
"@kbn/plugin-discovery": "link:packages/kbn-plugin-discovery",
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -816,10 +816,14 @@
"@kbn/ml-agg-utils/*": ["x-pack/packages/ml/agg_utils/*"],
"@kbn/ml-is-populated-object": ["x-pack/packages/ml/is_populated_object"],
"@kbn/ml-is-populated-object/*": ["x-pack/packages/ml/is_populated_object/*"],
"@kbn/ml-nested-property": ["x-pack/packages/ml/nested_property"],
"@kbn/ml-nested-property/*": ["x-pack/packages/ml/nested_property/*"],
"@kbn/ml-plugin": ["x-pack/plugins/ml"],
"@kbn/ml-plugin/*": ["x-pack/plugins/ml/*"],
"@kbn/ml-string-hash": ["x-pack/packages/ml/string_hash"],
"@kbn/ml-string-hash/*": ["x-pack/packages/ml/string_hash/*"],
"@kbn/ml-url-state": ["x-pack/packages/ml/url_state"],
"@kbn/ml-url-state/*": ["x-pack/packages/ml/url_state/*"],
"@kbn/monaco": ["packages/kbn-monaco"],
"@kbn/monaco/*": ["packages/kbn-monaco/*"],
"@kbn/monitoring-collection-plugin": ["x-pack/plugins/monitoring_collection"],
Expand Down
3 changes: 3 additions & 0 deletions x-pack/packages/ml/nested_property/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @kbn/ml-nested-property

Provides functionality similar to lodash's get() except that it's TypeScript aware and able to infer return types.
9 changes: 9 additions & 0 deletions x-pack/packages/ml/nested_property/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 { getNestedProperty } from './src/get_nested_property';
export { setNestedProperty } from './src/set_nested_property';
12 changes: 12 additions & 0 deletions x-pack/packages/ml/nested_property/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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.
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../../../..',
roots: ['<rootDir>/x-pack/packages/ml/nested_property'],
};
5 changes: 5 additions & 0 deletions x-pack/packages/ml/nested_property/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "shared-common",
"id": "@kbn/ml-nested-property",
"owner": "@elastic/ml-ui"
}
9 changes: 9 additions & 0 deletions x-pack/packages/ml/nested_property/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@kbn/ml-nested-property",
"description": "TypeScript-aware utility functions to get/set attributes from objects.",
"author": "Machine Learning UI",
"homepage": "https://docs.elastic.dev/kibana-dev-docs/api/kbn-ml-nested-property",
"private": true,
"version": "1.0.0",
"license": "SSPL-1.0 OR Elastic License 2.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { getNestedProperty } from './object_utils';
import { getNestedProperty } from './get_nested_property';

describe('object_utils', () => {
test('getNestedProperty()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,3 @@ export function getNestedProperty(

return o;
}

export const setNestedProperty = (obj: Record<string, any>, accessor: string, value: any) => {
let ref = obj;
const accessors = accessor.split('.');
const len = accessors.length;
for (let i = 0; i < len - 1; i++) {
const attribute = accessors[i];
if (ref[attribute] === undefined) {
ref[attribute] = {};
}

ref = ref[attribute];
}

ref[accessors[len - 1]] = value;

return obj;
};
24 changes: 24 additions & 0 deletions x-pack/packages/ml/nested_property/src/set_nested_property.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 const setNestedProperty = (obj: Record<string, any>, accessor: string, value: any) => {
let ref = obj;
const accessors = accessor.split('.');
const len = accessors.length;
for (let i = 0; i < len - 1; i++) {
const attribute = accessors[i];
if (ref[attribute] === undefined) {
ref[attribute] = {};
}

ref = ref[attribute];
}

ref[accessors[len - 1]] = value;

return obj;
};
18 changes: 18 additions & 0 deletions x-pack/packages/ml/nested_property/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target/types",
"types": [
"jest",
"node",
"react"
]
},
"include": [
"**/*.ts",
"**/*.tsx",
],
"exclude": [
"target/**/*",
]
}
3 changes: 3 additions & 0 deletions x-pack/packages/ml/url_state/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @kbn/ml-url-state

URL state management.
19 changes: 19 additions & 0 deletions x-pack/packages/ml/url_state/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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 {
isRisonSerializationRequired,
parseUrlState,
usePageUrlState,
useUrlState,
PageUrlStateService,
Provider,
UrlStateProvider,
type Accessor,
type Dictionary,
type SetUrlState,
} from './src/url_state';
12 changes: 12 additions & 0 deletions x-pack/packages/ml/url_state/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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.
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../../../..',
roots: ['<rootDir>/x-pack/packages/ml/url_state'],
};
5 changes: 5 additions & 0 deletions x-pack/packages/ml/url_state/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "shared-common",
"id": "@kbn/ml-url-state",
"owner": "@elastic/ml-ui"
}
9 changes: 9 additions & 0 deletions x-pack/packages/ml/url_state/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@kbn/ml-url-state",
"description": "Url state management utilities.",
"author": "Machine Learning UI",
"homepage": "https://docs.elastic.dev/kibana-dev-docs/api/kbn-ml-url-state",
"private": true,
"version": "1.0.0",
"license": "SSPL-1.0 OR Elastic License 2.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@ import React, {
useCallback,
useContext,
useMemo,
FC,
useRef,
useEffect,
type FC,
} from 'react';
import { useHistory, useLocation } from 'react-router-dom';
import { isEqual } from 'lodash';

import { getNestedProperty } from '@kbn/ml-nested-property';
import { decode, encode } from '@kbn/rison';
import { useHistory, useLocation } from 'react-router-dom';

import { BehaviorSubject, Observable } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';
import { isPopulatedObject } from '@kbn/ml-is-populated-object';
import { Dictionary } from '../../../common/types/common';

import { getNestedProperty } from './object_utils';
import { MlPages } from '../../../common/constants/locator';
export interface Dictionary<TValue> {
[id: string]: TValue;
}

type Accessor = '_a' | '_g';
export type Accessor = '_a' | '_g';
export type SetUrlState = (
accessor: Accessor,
attribute: string | Dictionary<any>,
Expand All @@ -48,7 +50,7 @@ const risonSerializedParams = new Set(['_a', '_g']);
* Checks if the URL query parameter requires rison serialization.
* @param queryParam
*/
function isRisonSerializationRequired(queryParam: string): boolean {
export function isRisonSerializationRequired(queryParam: string): boolean {
return risonSerializedParams.has(queryParam);
}

Expand Down Expand Up @@ -86,7 +88,7 @@ export const urlStateStore = createContext<UrlState>({
setUrlState: () => {},
});

const { Provider } = urlStateStore;
export const { Provider } = urlStateStore;

export const UrlStateProvider: FC = ({ children }) => {
const history = useHistory();
Expand Down Expand Up @@ -183,15 +185,6 @@ export const useUrlState = (
return [urlState, setUrlState];
};

type LegacyUrlKeys = 'mlExplorerSwimlane';

export type AppStateKey =
| 'mlSelectSeverity'
| 'mlSelectInterval'
| 'mlAnomaliesTable'
| MlPages
| LegacyUrlKeys;

/**
* Service for managing URL state of particular page.
*/
Expand Down Expand Up @@ -239,7 +232,7 @@ export class PageUrlStateService<T> {
* Hook for managing the URL state of the page.
*/
export const usePageUrlState = <PageUrlState extends object>(
pageKey: AppStateKey,
pageKey: string,
Copy link
Contributor

Choose a reason for hiding this comment

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

I reckon it's important to validate the page id here. We could also infer PageUrlState from the page key. I guess it's fine to merge AppStateKey and the appropriate URL states from all plugins. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd like to avoid having too plugin specific code in the packages which are used by multiple plugins. Instead I now did it the other way around and changed the required generic for usePageUrlState to be a combo of the pageKey definition and the original url state interface in 2f84808. Hope that's good enough for this iteration.

defaultState?: PageUrlState
): [
PageUrlState,
Expand Down
23 changes: 23 additions & 0 deletions x-pack/packages/ml/url_state/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target/types",
"types": [
"jest",
"node",
"react"
]
},
"include": [
"**/*.ts",
"**/*.tsx",
],
"exclude": [
"target/**/*",
],
"kbn_references": [
"@kbn/ml-nested-property",
"@kbn/rison",
"@kbn/ml-is-populated-object",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { type DataViewField } from '@kbn/data-views-plugin/public';
import { startWith } from 'rxjs';
import useMount from 'react-use/lib/useMount';
import type { Query, Filter } from '@kbn/es-query';
import { usePageUrlState } from '@kbn/ml-url-state';
import {
createMergedEsQuery,
getEsQueryFromSavedSearch,
Expand All @@ -27,7 +28,6 @@ import { useTimefilter, useTimeRangeUpdates } from '../../hooks/use_time_filter'
import { useChangePointResults } from './use_change_point_agg_request';
import { type TimeBuckets, TimeBucketsInterval } from '../../../common/time_buckets';
import { useDataSource } from '../../hooks/use_data_source';
import { usePageUrlState } from '../../hooks/use_url_state';
import { useTimeBuckets } from '../../hooks/use_time_buckets';

export interface ChangePointDetectionRequestParams {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import { DataView } from '@kbn/data-views-plugin/common';
import { SavedSearch } from '@kbn/saved-search-plugin/public';
import React, { FC } from 'react';
import { UrlStateProvider } from '@kbn/ml-url-state';
import { PageHeader } from '../page_header';
import { ChangePointDetectionContextProvider } from './change_point_detection_context';
import { DataSourceContext } from '../../hooks/use_data_source';
import { UrlStateProvider } from '../../hooks/use_url_state';
import { SavedSearchSavedObject } from '../../application/utils/search_utils';
import { AiopsAppContext, AiopsAppDependencies } from '../../hooks/use_aiops_app_context';
import { ChangePointDetectionPage } from './change_point_detection_page';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import {
OnRefreshProps,
OnTimeChangeProps,
} from '@elastic/eui';

import type { TimeRange } from '@kbn/es-query';
import { TimeHistoryContract, UI_SETTINGS } from '@kbn/data-plugin/public';

import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { toMountPoint, wrapWithTheme } from '@kbn/kibana-react-plugin/public';
import { useUrlState } from '@kbn/ml-url-state';
import { useRefreshIntervalUpdates, useTimeRangeUpdates } from '../../hooks/use_time_filter';
import { useUrlState } from '../../hooks/use_url_state';
import { useAiopsAppContext } from '../../hooks/use_aiops_app_context';
import { aiopsRefresh$ } from '../../application/services/timefilter_refresh_service';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import { i18n } from '@kbn/i18n';
import type { SavedSearch } from '@kbn/discover-plugin/public';
import type { DataView } from '@kbn/data-views-plugin/public';

import { UrlStateProvider } from '@kbn/ml-url-state';
import {
SEARCH_QUERY_LANGUAGE,
SearchQueryLanguage,
SavedSearchSavedObject,
} from '../../application/utils/search_utils';
import { UrlStateProvider } from '../../hooks/use_url_state';
import type { AiopsAppDependencies } from '../../hooks/use_aiops_app_context';
import { AiopsAppContext } from '../../hooks/use_aiops_app_context';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import { Filter, FilterStateStore, Query } from '@kbn/es-query';
import { FormattedMessage } from '@kbn/i18n-react';
import { SavedSearch } from '@kbn/discover-plugin/public';

import { useUrlState, usePageUrlState } from '@kbn/ml-url-state';
import { useAiopsAppContext } from '../../hooks/use_aiops_app_context';
import { SearchQueryLanguage, SavedSearchSavedObject } from '../../application/utils/search_utils';
import { useUrlState, usePageUrlState, AppStateKey } from '../../hooks/use_url_state';
import { useData } from '../../hooks/use_data';
import { FullTimeRangeSelector } from '../full_time_range_selector';
import { DocumentCountContent } from '../document_count_content/document_count_content';
Expand Down Expand Up @@ -79,7 +79,10 @@ export const ExplainLogRateSpikesPage: FC<ExplainLogRateSpikesPageProps> = ({
setSelectedGroup,
} = useSpikeAnalysisTableRowContext();

const [aiopsListState, setAiopsListState] = usePageUrlState(AppStateKey, restorableDefaults);
const [aiopsListState, setAiopsListState] = usePageUrlState(
'AIOPS_INDEX_VIEWER',
restorableDefaults
);
const [globalState, setGlobalState] = useUrlState('_g');

const [currentSavedSearch, setCurrentSavedSearch] = useState(savedSearch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import React, { FC } from 'react';
import type { SavedSearch } from '@kbn/discover-plugin/public';
import type { DataView } from '@kbn/data-views-plugin/public';
import { UrlStateProvider } from '@kbn/ml-url-state';
import { LogCategorizationPage } from './log_categorization_page';
import { SavedSearchSavedObject } from '../../application/utils/search_utils';
import type { AiopsAppDependencies } from '../../hooks/use_aiops_app_context';
import { AiopsAppContext } from '../../hooks/use_aiops_app_context';
import { UrlStateProvider } from '../../hooks/use_url_state';

export interface LogCategorizationAppStateProps {
dataView: DataView;
Expand Down
Loading