-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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] Use context and custom hooks to manage legacy dependencies. #42244
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5906cb9
[ML] Move NavigationMenuContext to context directory.
walterra c73c1fe
[ML] Simplify context setup.
walterra 322c510
[ML] Replace KibanaContext with AngularContext.
walterra e04979f
[ML] Replace NavigationMenuContext with UiContext.
walterra 00f8c23
[ML] Use context instead of deep import.
walterra a771ca5
[ML] Import fix.
walterra 76792ba
Merge branch 'master' into ml-context-dir
walterra f168481
[ML] Fix export.
walterra 24a4483
[ML] Add context description.
walterra 5677cf1
[ML] Rename AngularContext to KibanaContext.
walterra 87c7519
Merge branch 'master' into ml-context-dir
walterra 8c3cd59
Merge branch 'master' into ml-context-dir
walterra 99c7a9d
Merge branch 'master' of github.com:elastic/kibana into ml-context-dir
walterra 83953aa
[ML] Fix comment typo.
walterra f446c08
Merge branch 'ml-context-dir' of github.com:walterra/kibana into ml-c…
walterra 64b198a
[ML] Improve KibanaContext types.
walterra 41068f5
[ML] Improve combinedQuery type.
walterra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
x-pack/legacy/plugins/ml/public/contexts/kibana/__mocks__/index_pattern.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* 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 { IndexPattern } from 'ui/index_patterns'; | ||
|
||
export const indexPatternMock = ({ | ||
id: 'the-index-pattern-id', | ||
title: 'the-index-pattern-title', | ||
fields: [], | ||
} as unknown) as IndexPattern; |
21 changes: 21 additions & 0 deletions
21
x-pack/legacy/plugins/ml/public/contexts/kibana/__mocks__/index_patterns.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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 { IndexPatterns } from 'ui/index_patterns'; | ||
|
||
export const indexPatternsMock = (new (class { | ||
fieldFormats = []; | ||
config = {}; | ||
savedObjectsClient = {}; | ||
refreshSavedObjectsCache = {}; | ||
clearCache = jest.fn(); | ||
get = jest.fn(); | ||
getDefault = jest.fn(); | ||
getFields = jest.fn(); | ||
getIds = jest.fn(); | ||
getTitles = jest.fn(); | ||
make = jest.fn(); | ||
})() as unknown) as IndexPatterns; |
11 changes: 11 additions & 0 deletions
11
x-pack/legacy/plugins/ml/public/contexts/kibana/__mocks__/kibana_config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* 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 const kibanaConfigMock = { | ||
get: <T>(key: string): T => ({} as T), | ||
has: (key: string) => false, | ||
set: (key: string, value: any) => {}, | ||
}; |
22 changes: 22 additions & 0 deletions
22
x-pack/legacy/plugins/ml/public/contexts/kibana/__mocks__/kibana_context_value.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* 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 { indexPatternMock } from './index_pattern'; | ||
import { indexPatternsMock } from './index_patterns'; | ||
import { kibanaConfigMock } from './kibana_config'; | ||
import { savedSearchMock } from './saved_search'; | ||
|
||
export const kibanaContextValueMock = { | ||
combinedQuery: { | ||
query: 'the-query-string', | ||
language: 'the-query-language', | ||
}, | ||
currentIndexPattern: indexPatternMock, | ||
currentSavedSearch: savedSearchMock, | ||
indexPatterns: indexPatternsMock, | ||
kbnBaseUrl: 'url', | ||
kibanaConfig: kibanaConfigMock, | ||
}; |
14 changes: 14 additions & 0 deletions
14
x-pack/legacy/plugins/ml/public/contexts/kibana/__mocks__/saved_search.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export const savedSearchMock = { | ||
id: 'the-saved-search-id', | ||
title: 'the-saved-search-title', | ||
searchSource: {}, | ||
columns: [], | ||
sort: [], | ||
destroy: () => {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* 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 { KibanaContext, KibanaContextValue, SavedSearchQuery } from './kibana_context'; | ||
export { useKibanaContext } from './use_kibana_context'; | ||
export { useCurrentIndexPattern } from './use_current_index_pattern'; | ||
export { useCurrentSavedSearch } from './use_current_saved_search'; |
45 changes: 45 additions & 0 deletions
45
x-pack/legacy/plugins/ml/public/contexts/kibana/kibana_context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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 React from 'react'; | ||
|
||
import { KibanaConfig } from 'src/legacy/server/kbn_server'; | ||
import { SavedSearch } from 'src/legacy/core_plugins/kibana/public/discover/types'; | ||
|
||
import { IndexPattern, IndexPatterns } from 'ui/index_patterns'; | ||
|
||
// set() method is missing in original d.ts | ||
export interface KibanaConfigTypeFix extends KibanaConfig { | ||
set(key: string, value: any): void; | ||
} | ||
|
||
export interface KibanaContextValue { | ||
combinedQuery: any; | ||
currentIndexPattern: IndexPattern; | ||
currentSavedSearch: SavedSearch; | ||
indexPatterns: IndexPatterns; | ||
kbnBaseUrl: string; | ||
kibanaConfig: KibanaConfigTypeFix; | ||
} | ||
|
||
export type SavedSearchQuery = object; | ||
|
||
// This context provides dependencies which can be injected | ||
// via angularjs only (like services, currentIndexPattern etc.). | ||
// Because we cannot just import these dependencies, the default value | ||
// for the context is just {} and of type `Partial<KibanaContextValue>` | ||
// for the angularjs based dependencies. Therefore, the | ||
// actual dependencies are set like we did previously with KibanaContext | ||
// in the wrapping angularjs directive. In the custom hook we check if | ||
// the dependencies are present with error reporting if they weren't | ||
// added properly. That's why in tests, these custom hooks must not | ||
// be mocked, instead <UiChrome.Provider value="mocked-value">` needs | ||
// to be used. This guarantees that we have both properly set up | ||
// TypeScript support and runtime checks for these dependencies. | ||
// Multiple custom hooks can be created to access subsets of | ||
// the overall context value if necessary too, | ||
// see useCurrentIndexPattern() for example. | ||
export const KibanaContext = React.createContext<Partial<KibanaContextValue>>({}); |
19 changes: 19 additions & 0 deletions
19
x-pack/legacy/plugins/ml/public/contexts/kibana/use_current_index_pattern.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { useContext } from 'react'; | ||
|
||
import { KibanaContext } from './kibana_context'; | ||
|
||
export const useCurrentIndexPattern = () => { | ||
const context = useContext(KibanaContext); | ||
|
||
if (context.currentIndexPattern === undefined) { | ||
throw new Error('currentIndexPattern is undefined'); | ||
} | ||
|
||
return context.currentIndexPattern; | ||
}; |
19 changes: 19 additions & 0 deletions
19
x-pack/legacy/plugins/ml/public/contexts/kibana/use_current_saved_search.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { useContext } from 'react'; | ||
|
||
import { KibanaContext } from './kibana_context'; | ||
|
||
export const useCurrentSavedSearch = () => { | ||
const context = useContext(KibanaContext); | ||
|
||
if (context.currentSavedSearch === undefined) { | ||
throw new Error('currentSavedSearch is undefined'); | ||
} | ||
|
||
return context.currentSavedSearch; | ||
}; |
26 changes: 26 additions & 0 deletions
26
x-pack/legacy/plugins/ml/public/contexts/kibana/use_kibana_context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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 { useContext } from 'react'; | ||
|
||
import { KibanaContext, KibanaContextValue } from './kibana_context'; | ||
|
||
export const useKibanaContext = () => { | ||
const context = useContext(KibanaContext); | ||
|
||
if ( | ||
context.combinedQuery === undefined || | ||
context.currentIndexPattern === undefined || | ||
context.currentSavedSearch === undefined || | ||
context.indexPatterns === undefined || | ||
context.kbnBaseUrl === undefined || | ||
context.kibanaConfig === undefined | ||
) { | ||
throw new Error('required attribute is undefined'); | ||
} | ||
|
||
return context as KibanaContextValue; | ||
}; |
34 changes: 34 additions & 0 deletions
34
x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/mocks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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 const uiChromeMock = { | ||
getBasePath: () => 'basePath', | ||
getUiSettingsClient: () => { | ||
return { | ||
get: (key: string) => { | ||
switch (key) { | ||
case 'dateFormat': | ||
case 'timepicker:timeDefaults': | ||
return {}; | ||
case 'timepicker:refreshIntervalDefaults': | ||
return { pause: false, value: 0 }; | ||
default: | ||
throw new Error(`Unexpected config key: ${key}`); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
export const uiTimefilterMock = { | ||
getRefreshInterval: () => '30s', | ||
getTime: () => ({ from: 0, to: 0 }), | ||
on: (event: string, reload: () => void) => {}, | ||
}; | ||
|
||
export const uiTimeHistoryMock = { | ||
get: () => [{ from: 0, to: 0 }], | ||
}; |
9 changes: 9 additions & 0 deletions
9
x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/use_ui_chrome_context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { uiChromeMock } from './mocks'; | ||
|
||
export const useUiChromeContext = () => uiChromeMock; |
13 changes: 13 additions & 0 deletions
13
x-pack/legacy/plugins/ml/public/contexts/ui/__mocks__/use_ui_context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* 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 { uiChromeMock, uiTimefilterMock, uiTimeHistoryMock } from './mocks'; | ||
|
||
export const useUiContext = () => ({ | ||
chrome: uiChromeMock, | ||
timefilter: uiTimefilterMock, | ||
timeHistory: uiTimeHistoryMock, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
// We only export UiContext but not any custom hooks, because if we'd import them | ||
// from here, mocking the hook from jest tests won't work as expected. | ||
export { UiContext } from './ui_context'; |
27 changes: 27 additions & 0 deletions
27
x-pack/legacy/plugins/ml/public/contexts/ui/ui_context.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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 React from 'react'; | ||
|
||
import chrome from 'ui/chrome'; | ||
import { timefilter } from 'ui/timefilter'; | ||
import { timeHistory } from 'ui/timefilter/time_history'; | ||
|
||
// This provides ui/* based imports via React Context. | ||
// Because these dependencies can use regular imports, | ||
// they are just passed on as the default value | ||
// of the Context which means it's not necessary | ||
// to add <UiContext.Provider value="..." />... to the | ||
// wrapping angular directive, reducing a lot of boilerplate. | ||
// The custom hooks like useUiContext() need to be mocked in | ||
// tests because we rely on the properly set up default value. | ||
// Different custom hooks can be created to access parts only | ||
// from the full context value, see useUiChromeContext() as an example. | ||
export const UiContext = React.createContext({ | ||
chrome, | ||
timefilter, | ||
timeHistory, | ||
}); |
13 changes: 13 additions & 0 deletions
13
x-pack/legacy/plugins/ml/public/contexts/ui/use_ui_chrome_context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* 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 { useContext } from 'react'; | ||
|
||
import { UiContext } from './ui_context'; | ||
|
||
export const useUiChromeContext = () => { | ||
return useContext(UiContext).chrome; | ||
}; |
13 changes: 13 additions & 0 deletions
13
x-pack/legacy/plugins/ml/public/contexts/ui/use_ui_context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* 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 { useContext } from 'react'; | ||
|
||
import { UiContext } from './ui_context'; | ||
|
||
export const useUiContext = () => { | ||
return useContext(UiContext); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be useful here to loop through the required attributes to check for undefined and then throw the error with the specific missing dependency. Just for future debugging purposes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would potentially throw multiple times if more than one property is missing unless the loop builds up an array of missing properties and joins them for the error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the second thing you said. Should've been more clear. Not a must-have but would be nice to have.