From a8d5b778eef36329ebe6275c2a9bf0a63073e85e Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Mon, 30 Nov 2020 09:40:45 -0700 Subject: [PATCH] Update IndexPatternSelect to get fields from indexPatternService instead of savedObject attributes (#84376) * Update indexPatternSelect to get fields from indexPatternService instead of savedObject attributes * keep original search implemenation * import from public * remove unused code * API updates * review feedback --- ...s-data-public.indexpatternsservice.find.md | 11 +++ ...lugins-data-public.indexpatternsservice.md | 1 + .../index_patterns/index_patterns.ts | 14 ++++ src/plugins/data/public/plugin.ts | 2 +- src/plugins/data/public/public.api.md | 2 + .../create_index_pattern_select.tsx | 6 +- .../index_pattern_select.tsx | 84 +++++++------------ 7 files changed, 60 insertions(+), 60 deletions(-) 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 000000000000..f642965c5da8 --- /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 48019fe410b9..30ce1fa1de38 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/common/index_patterns/index_patterns/index_patterns.ts b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts index e09246ae8cd3..4a266b3cad64 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 dded52310a99..8d40447a48ff 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/public.api.md b/src/plugins/data/public/public.api.md index a6daaf834a42..8ceb91269adb 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 // 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 a48c2dabf150..11cf8edee5aa 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,12 @@ import _ from 'lodash'; import React from 'react'; -import { SavedObjectsClientContract } from 'src/core/public'; +import { IndexPatternsContract } from 'src/plugins/data/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 1e0e8934778a..2388f3d7a504 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'; export type IndexPatternSelectProps = Required< Omit< @@ -40,7 +39,7 @@ export type IndexPatternSelectProps = Required< }; export type IndexPatternSelectInternalProps = IndexPatternSelectProps & { - savedObjectsClient: SavedObjectsClientContract; + indexPatternService: IndexPatternsContract; }; interface IndexPatternSelectState { @@ -50,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 { @@ -109,7 +93,8 @@ export default class IndexPatternSelect extends Component { - const { fieldTypes, onNoIndexPatterns, savedObjectsClient } = this.props; - - 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) { - return; - } + const { fieldTypes, onNoIndexPatterns, indexPatternService } = this.props; + 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. - if (searchValue === this.state.searchValue) { - const options = savedObjects.map((indexPatternSavedObject: SimpleSavedObject) => { + if (searchValue !== this.state.searchValue || !this.isMounted) { + return; + } + + 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 +167,7 @@ export default class IndexPatternSelect extends Component