From 759ea3da73c129d4d2b92ea8db887053fe026bd7 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Wed, 25 Nov 2020 13:54:28 -0700 Subject: [PATCH 1/6] Update indexPatternSelect to get fields from indexPatternService instead of savedObject attributes --- .../index_patterns/index_patterns.ts | 14 ++++ src/plugins/data/public/plugin.ts | 2 +- .../create_index_pattern_select.tsx | 5 +- .../index_pattern_select.tsx | 67 ++++++++----------- 4 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts index e09246ae8cd3e7..4a266b3cad6492 100644 --- a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts +++ b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts @@ -135,6 +135,20 @@ export class IndexPatternsService { return this.savedObjectsCache.map((obj) => obj?.attributes?.title); }; + find = async (search: string, size: number = 10): Promise => { + const savedObjects = await this.savedObjectsClient.find({ + type: 'index-pattern', + fields: ['title'], + search, + searchFields: ['title'], + perPage: size, + }); + const getIndexPatternPromises = savedObjects.map(async (savedObject) => { + return await this.get(savedObject.id); + }); + return await Promise.all(getIndexPatternPromises); + }; + /** * Get list of index pattern ids with titles * @param refresh Force refresh of index pattern list diff --git a/src/plugins/data/public/plugin.ts b/src/plugins/data/public/plugin.ts index dded52310a99c6..8d40447a48ff00 100644 --- a/src/plugins/data/public/plugin.ts +++ b/src/plugins/data/public/plugin.ts @@ -229,7 +229,7 @@ export class DataPublicPlugin return { ...dataServices, ui: { - IndexPatternSelect: createIndexPatternSelect(core.savedObjects.client), + IndexPatternSelect: createIndexPatternSelect(indexPatterns), SearchBar, }, }; diff --git a/src/plugins/data/public/ui/index_pattern_select/create_index_pattern_select.tsx b/src/plugins/data/public/ui/index_pattern_select/create_index_pattern_select.tsx index a48c2dabf15067..258dfa20cce143 100644 --- a/src/plugins/data/public/ui/index_pattern_select/create_index_pattern_select.tsx +++ b/src/plugins/data/public/ui/index_pattern_select/create_index_pattern_select.tsx @@ -20,12 +20,13 @@ import _ from 'lodash'; import React from 'react'; +import { IndexPatternsContract } from 'src/plugins/data/public/index_patterns'; import { SavedObjectsClientContract } from 'src/core/public'; import { IndexPatternSelect, IndexPatternSelectProps } from './'; // Takes in stateful runtime dependencies and pre-wires them to the component -export function createIndexPatternSelect(savedObjectsClient: SavedObjectsClientContract) { +export function createIndexPatternSelect(indexPatternService: IndexPatternsContract) { return (props: IndexPatternSelectProps) => ( - + ); } diff --git a/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx b/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx index 1e0e8934778add..3ace944c95dcea 100644 --- a/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx +++ b/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx @@ -23,8 +23,7 @@ import React, { Component } from 'react'; import { Required } from '@kbn/utility-types'; import { EuiComboBox, EuiComboBoxProps } from '@elastic/eui'; -import { SavedObjectsClientContract, SimpleSavedObject } from 'src/core/public'; -import { getTitle } from '../../../common/index_patterns/lib'; +import { IndexPatternsContract } from 'src/plugins/data/public/index_patterns'; export type IndexPatternSelectProps = Required< Omit< @@ -40,7 +39,7 @@ export type IndexPatternSelectProps = Required< }; export type IndexPatternSelectInternalProps = IndexPatternSelectProps & { - savedObjectsClient: SavedObjectsClientContract; + indexPatternService: IndexPatternsContract; }; interface IndexPatternSelectState { @@ -109,7 +108,8 @@ export default class IndexPatternSelect extends Component { - const { fieldTypes, onNoIndexPatterns, savedObjectsClient } = this.props; + const { fieldTypes, onNoIndexPatterns, indexPatternService } = this.props; + const indexPatterns = await indexPatternService.find(searchValue, 100); - const savedObjectFields = ['title']; - if (fieldTypes) { - savedObjectFields.push('fields'); - } - let savedObjects = await getIndexPatterns(savedObjectsClient, searchValue, savedObjectFields); - - if (fieldTypes) { - savedObjects = savedObjects.filter((savedObject: SimpleSavedObject) => { - try { - const indexPatternFields = JSON.parse(savedObject.attributes.fields as any); - return indexPatternFields.some((field: any) => { - return fieldTypes?.includes(field.type); - }); - } catch (err) { - // Unable to parse fields JSON, invalid index pattern - return false; - } - }); - } - - if (!this.isMounted) { + // We need this check to handle the case where search results come back in a different + // order than they were sent out. Only load results for the most recent search. + if (searchValue !== this.state.searchValue || !this.isMounted) { return; } - // We need this check to handle the case where search results come back in a different - // order than they were sent out. Only load results for the most recent search. - if (searchValue === this.state.searchValue) { - const options = savedObjects.map((indexPatternSavedObject: SimpleSavedObject) => { + const options = indexPatterns + .filter((indexPattern) => { + return fieldTypes + ? indexPattern.fields.some((field) => { + return fieldTypes.includes(field.type); + }) + : true; + }) + .map((indexPattern) => { return { - label: indexPatternSavedObject.attributes.title, - value: indexPatternSavedObject.id, + label: indexPattern.title, + value: indexPattern.id, }; }); - this.setState({ - isLoading: false, - options, - }); + this.setState({ + isLoading: false, + options, + }); - if (onNoIndexPatterns && searchValue === '' && options.length === 0) { - onNoIndexPatterns(); - } + if (onNoIndexPatterns && searchValue === '' && options.length === 0) { + onNoIndexPatterns(); } }, 300); @@ -195,7 +182,7 @@ export default class IndexPatternSelect extends Component Date: Wed, 25 Nov 2020 13:55:00 -0700 Subject: [PATCH 2/6] keep original search implemenation --- .../data/common/index_patterns/index_patterns/index_patterns.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts index 4a266b3cad6492..c2cf4a988ecce9 100644 --- a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts +++ b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts @@ -139,7 +139,7 @@ export class IndexPatternsService { const savedObjects = await this.savedObjectsClient.find({ type: 'index-pattern', fields: ['title'], - search, + search: `${search}*`, searchFields: ['title'], perPage: size, }); From 906e2eac666392f170e0e617fee19af51b1a4182 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Wed, 25 Nov 2020 14:03:03 -0700 Subject: [PATCH 3/6] import from public --- .../ui/index_pattern_select/create_index_pattern_select.tsx | 2 +- .../public/ui/index_pattern_select/index_pattern_select.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/data/public/ui/index_pattern_select/create_index_pattern_select.tsx b/src/plugins/data/public/ui/index_pattern_select/create_index_pattern_select.tsx index 258dfa20cce143..1f720272596702 100644 --- a/src/plugins/data/public/ui/index_pattern_select/create_index_pattern_select.tsx +++ b/src/plugins/data/public/ui/index_pattern_select/create_index_pattern_select.tsx @@ -20,7 +20,7 @@ import _ from 'lodash'; import React from 'react'; -import { IndexPatternsContract } from 'src/plugins/data/public/index_patterns'; +import { IndexPatternsContract } from 'src/plugins/data/public'; import { SavedObjectsClientContract } from 'src/core/public'; import { IndexPatternSelect, IndexPatternSelectProps } from './'; diff --git a/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx b/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx index 3ace944c95dcea..982bfb5c535211 100644 --- a/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx +++ b/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx @@ -23,7 +23,7 @@ import React, { Component } from 'react'; import { Required } from '@kbn/utility-types'; import { EuiComboBox, EuiComboBoxProps } from '@elastic/eui'; -import { IndexPatternsContract } from 'src/plugins/data/public/index_patterns'; +import { IndexPatternsContract } from 'src/plugins/data/public'; export type IndexPatternSelectProps = Required< Omit< From 3ab8f251b0be2a8b0efa1656f4fe66326ce96bc6 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Wed, 25 Nov 2020 14:34:56 -0700 Subject: [PATCH 4/6] remove unused code --- .../create_index_pattern_select.tsx | 1 - .../index_pattern_select/index_pattern_select.tsx | 15 --------------- 2 files changed, 16 deletions(-) diff --git a/src/plugins/data/public/ui/index_pattern_select/create_index_pattern_select.tsx b/src/plugins/data/public/ui/index_pattern_select/create_index_pattern_select.tsx index 1f720272596702..11cf8edee5aaec 100644 --- a/src/plugins/data/public/ui/index_pattern_select/create_index_pattern_select.tsx +++ b/src/plugins/data/public/ui/index_pattern_select/create_index_pattern_select.tsx @@ -21,7 +21,6 @@ import _ from 'lodash'; import React from 'react'; import { IndexPatternsContract } from 'src/plugins/data/public'; -import { SavedObjectsClientContract } from 'src/core/public'; import { IndexPatternSelect, IndexPatternSelectProps } from './'; // Takes in stateful runtime dependencies and pre-wires them to the component diff --git a/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx b/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx index 982bfb5c535211..c5bcd57216de27 100644 --- a/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx +++ b/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx @@ -49,21 +49,6 @@ interface IndexPatternSelectState { searchValue: string | undefined; } -const getIndexPatterns = async ( - client: SavedObjectsClientContract, - search: string, - fields: string[] -) => { - const resp = await client.find({ - type: 'index-pattern', - fields, - search: `${search}*`, - searchFields: ['title'], - perPage: 100, - }); - return resp.savedObjects; -}; - // Needed for React.lazy // eslint-disable-next-line import/no-default-export export default class IndexPatternSelect extends Component { From 62483ce7428ec441d6d3a7d09282508d2d3080b3 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Wed, 25 Nov 2020 15:06:26 -0700 Subject: [PATCH 5/6] API updates --- ...n-plugins-data-public.indexpatternsservice.find.md | 11 +++++++++++ ...plugin-plugins-data-public.indexpatternsservice.md | 1 + src/plugins/data/public/public.api.md | 2 ++ 3 files changed, 14 insertions(+) create mode 100644 docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.find.md diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.find.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.find.md new file mode 100644 index 00000000000000..f642965c5da80f --- /dev/null +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.find.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [IndexPatternsService](./kibana-plugin-plugins-data-public.indexpatternsservice.md) > [find](./kibana-plugin-plugins-data-public.indexpatternsservice.find.md) + +## IndexPatternsService.find property + +Signature: + +```typescript +find: (search: string, size?: number) => Promise; +``` diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.md index 48019fe410b975..30ce1fa1de386b 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.md @@ -23,6 +23,7 @@ export declare class IndexPatternsService | [clearCache](./kibana-plugin-plugins-data-public.indexpatternsservice.clearcache.md) | | (id?: string | undefined) => void | Clear index pattern list cache | | [ensureDefaultIndexPattern](./kibana-plugin-plugins-data-public.indexpatternsservice.ensuredefaultindexpattern.md) | | EnsureDefaultIndexPattern | | | [fieldArrayToMap](./kibana-plugin-plugins-data-public.indexpatternsservice.fieldarraytomap.md) | | (fields: FieldSpec[], fieldAttrs?: FieldAttrs | undefined) => Record<string, FieldSpec> | Converts field array to map | +| [find](./kibana-plugin-plugins-data-public.indexpatternsservice.find.md) | | (search: string, size?: number) => Promise<IndexPattern[]> | | | [get](./kibana-plugin-plugins-data-public.indexpatternsservice.get.md) | | (id: string) => Promise<IndexPattern> | Get an index pattern by id. Cache optimized | | [getCache](./kibana-plugin-plugins-data-public.indexpatternsservice.getcache.md) | | () => Promise<SavedObject<IndexPatternSavedObjectAttrs>[] | null | undefined> | | | [getDefault](./kibana-plugin-plugins-data-public.indexpatternsservice.getdefault.md) | | () => Promise<IndexPattern | null> | Get default index pattern | diff --git a/src/plugins/data/public/public.api.md b/src/plugins/data/public/public.api.md index a6daaf834a4240..8ceb91269adbea 100644 --- a/src/plugins/data/public/public.api.md +++ b/src/plugins/data/public/public.api.md @@ -1396,6 +1396,8 @@ export class IndexPatternsService { // (undocumented) ensureDefaultIndexPattern: EnsureDefaultIndexPattern; fieldArrayToMap: (fields: FieldSpec[], fieldAttrs?: FieldAttrs | undefined) => Record; + // (undocumented) + find: (search: string, size?: number) => Promise; get: (id: string) => Promise; // Warning: (ae-forgotten-export) The symbol "IndexPatternSavedObjectAttrs" needs to be exported by the entry point index.d.ts // From ca1c0bb1f8a4ef7029f6c3b4114289ff1a4cc470 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Mon, 30 Nov 2020 07:54:31 -0700 Subject: [PATCH 6/6] review feedback --- .../data/common/index_patterns/index_patterns/index_patterns.ts | 2 +- .../public/ui/index_pattern_select/index_pattern_select.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts index c2cf4a988ecce9..4a266b3cad6492 100644 --- a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts +++ b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts @@ -139,7 +139,7 @@ export class IndexPatternsService { const savedObjects = await this.savedObjectsClient.find({ type: 'index-pattern', fields: ['title'], - search: `${search}*`, + search, searchFields: ['title'], perPage: size, }); diff --git a/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx b/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx index c5bcd57216de27..2388f3d7a504b6 100644 --- a/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx +++ b/src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx @@ -114,7 +114,7 @@ export default class IndexPatternSelect extends Component { const { fieldTypes, onNoIndexPatterns, indexPatternService } = this.props; - const indexPatterns = await indexPatternService.find(searchValue, 100); + const indexPatterns = await indexPatternService.find(`${searchValue}*`, 100); // We need this check to handle the case where search results come back in a different // order than they were sent out. Only load results for the most recent search.