Skip to content

Commit a8d5b77

Browse files
authored
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
1 parent a82ebf8 commit a8d5b77

File tree

7 files changed

+59
-59
lines changed

7 files changed

+59
-59
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [IndexPatternsService](./kibana-plugin-plugins-data-public.indexpatternsservice.md) &gt; [find](./kibana-plugin-plugins-data-public.indexpatternsservice.find.md)
4+
5+
## IndexPatternsService.find property
6+
7+
<b>Signature:</b>
8+
9+
```typescript
10+
find: (search: string, size?: number) => Promise<IndexPattern[]>;
11+
```

docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export declare class IndexPatternsService
2323
| [clearCache](./kibana-plugin-plugins-data-public.indexpatternsservice.clearcache.md) | | <code>(id?: string &#124; undefined) =&gt; void</code> | Clear index pattern list cache |
2424
| [ensureDefaultIndexPattern](./kibana-plugin-plugins-data-public.indexpatternsservice.ensuredefaultindexpattern.md) | | <code>EnsureDefaultIndexPattern</code> | |
2525
| [fieldArrayToMap](./kibana-plugin-plugins-data-public.indexpatternsservice.fieldarraytomap.md) | | <code>(fields: FieldSpec[], fieldAttrs?: FieldAttrs &#124; undefined) =&gt; Record&lt;string, FieldSpec&gt;</code> | Converts field array to map |
26+
| [find](./kibana-plugin-plugins-data-public.indexpatternsservice.find.md) | | <code>(search: string, size?: number) =&gt; Promise&lt;IndexPattern[]&gt;</code> | |
2627
| [get](./kibana-plugin-plugins-data-public.indexpatternsservice.get.md) | | <code>(id: string) =&gt; Promise&lt;IndexPattern&gt;</code> | Get an index pattern by id. Cache optimized |
2728
| [getCache](./kibana-plugin-plugins-data-public.indexpatternsservice.getcache.md) | | <code>() =&gt; Promise&lt;SavedObject&lt;IndexPatternSavedObjectAttrs&gt;[] &#124; null &#124; undefined&gt;</code> | |
2829
| [getDefault](./kibana-plugin-plugins-data-public.indexpatternsservice.getdefault.md) | | <code>() =&gt; Promise&lt;IndexPattern &#124; null&gt;</code> | Get default index pattern |

src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts

+14
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ export class IndexPatternsService {
135135
return this.savedObjectsCache.map((obj) => obj?.attributes?.title);
136136
};
137137

138+
find = async (search: string, size: number = 10): Promise<IndexPattern[]> => {
139+
const savedObjects = await this.savedObjectsClient.find<IndexPatternSavedObjectAttrs>({
140+
type: 'index-pattern',
141+
fields: ['title'],
142+
search,
143+
searchFields: ['title'],
144+
perPage: size,
145+
});
146+
const getIndexPatternPromises = savedObjects.map(async (savedObject) => {
147+
return await this.get(savedObject.id);
148+
});
149+
return await Promise.all(getIndexPatternPromises);
150+
};
151+
138152
/**
139153
* Get list of index pattern ids with titles
140154
* @param refresh Force refresh of index pattern list

src/plugins/data/public/plugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export class DataPublicPlugin
229229
return {
230230
...dataServices,
231231
ui: {
232-
IndexPatternSelect: createIndexPatternSelect(core.savedObjects.client),
232+
IndexPatternSelect: createIndexPatternSelect(indexPatterns),
233233
SearchBar,
234234
},
235235
};

src/plugins/data/public/public.api.md

+2
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,8 @@ export class IndexPatternsService {
13961396
// (undocumented)
13971397
ensureDefaultIndexPattern: EnsureDefaultIndexPattern;
13981398
fieldArrayToMap: (fields: FieldSpec[], fieldAttrs?: FieldAttrs | undefined) => Record<string, FieldSpec>;
1399+
// (undocumented)
1400+
find: (search: string, size?: number) => Promise<IndexPattern[]>;
13991401
get: (id: string) => Promise<IndexPattern>;
14001402
// Warning: (ae-forgotten-export) The symbol "IndexPatternSavedObjectAttrs" needs to be exported by the entry point index.d.ts
14011403
//

src/plugins/data/public/ui/index_pattern_select/create_index_pattern_select.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
import _ from 'lodash';
2121
import React from 'react';
2222

23-
import { SavedObjectsClientContract } from 'src/core/public';
23+
import { IndexPatternsContract } from 'src/plugins/data/public';
2424
import { IndexPatternSelect, IndexPatternSelectProps } from './';
2525

2626
// Takes in stateful runtime dependencies and pre-wires them to the component
27-
export function createIndexPatternSelect(savedObjectsClient: SavedObjectsClientContract) {
27+
export function createIndexPatternSelect(indexPatternService: IndexPatternsContract) {
2828
return (props: IndexPatternSelectProps) => (
29-
<IndexPatternSelect {...props} savedObjectsClient={savedObjectsClient} />
29+
<IndexPatternSelect {...props} indexPatternService={indexPatternService} />
3030
);
3131
}

src/plugins/data/public/ui/index_pattern_select/index_pattern_select.tsx

+27-55
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ import React, { Component } from 'react';
2323
import { Required } from '@kbn/utility-types';
2424
import { EuiComboBox, EuiComboBoxProps } from '@elastic/eui';
2525

26-
import { SavedObjectsClientContract, SimpleSavedObject } from 'src/core/public';
27-
import { getTitle } from '../../../common/index_patterns/lib';
26+
import { IndexPatternsContract } from 'src/plugins/data/public';
2827

2928
export type IndexPatternSelectProps = Required<
3029
Omit<
@@ -40,7 +39,7 @@ export type IndexPatternSelectProps = Required<
4039
};
4140

4241
export type IndexPatternSelectInternalProps = IndexPatternSelectProps & {
43-
savedObjectsClient: SavedObjectsClientContract;
42+
indexPatternService: IndexPatternsContract;
4443
};
4544

4645
interface IndexPatternSelectState {
@@ -50,21 +49,6 @@ interface IndexPatternSelectState {
5049
searchValue: string | undefined;
5150
}
5251

53-
const getIndexPatterns = async (
54-
client: SavedObjectsClientContract,
55-
search: string,
56-
fields: string[]
57-
) => {
58-
const resp = await client.find({
59-
type: 'index-pattern',
60-
fields,
61-
search: `${search}*`,
62-
searchFields: ['title'],
63-
perPage: 100,
64-
});
65-
return resp.savedObjects;
66-
};
67-
6852
// Needed for React.lazy
6953
// eslint-disable-next-line import/no-default-export
7054
export default class IndexPatternSelect extends Component<IndexPatternSelectInternalProps> {
@@ -109,7 +93,8 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectInte
10993

11094
let indexPatternTitle;
11195
try {
112-
indexPatternTitle = await getTitle(this.props.savedObjectsClient, indexPatternId);
96+
const indexPattern = await this.props.indexPatternService.get(indexPatternId);
97+
indexPatternTitle = indexPattern.title;
11398
} catch (err) {
11499
// index pattern no longer exists
115100
return;
@@ -128,49 +113,36 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectInte
128113
};
129114

130115
debouncedFetch = _.debounce(async (searchValue: string) => {
131-
const { fieldTypes, onNoIndexPatterns, savedObjectsClient } = this.props;
132-
133-
const savedObjectFields = ['title'];
134-
if (fieldTypes) {
135-
savedObjectFields.push('fields');
136-
}
137-
let savedObjects = await getIndexPatterns(savedObjectsClient, searchValue, savedObjectFields);
138-
139-
if (fieldTypes) {
140-
savedObjects = savedObjects.filter((savedObject: SimpleSavedObject<any>) => {
141-
try {
142-
const indexPatternFields = JSON.parse(savedObject.attributes.fields as any);
143-
return indexPatternFields.some((field: any) => {
144-
return fieldTypes?.includes(field.type);
145-
});
146-
} catch (err) {
147-
// Unable to parse fields JSON, invalid index pattern
148-
return false;
149-
}
150-
});
151-
}
116+
const { fieldTypes, onNoIndexPatterns, indexPatternService } = this.props;
117+
const indexPatterns = await indexPatternService.find(`${searchValue}*`, 100);
152118

153-
if (!this.isMounted) {
119+
// We need this check to handle the case where search results come back in a different
120+
// order than they were sent out. Only load results for the most recent search.
121+
if (searchValue !== this.state.searchValue || !this.isMounted) {
154122
return;
155123
}
156124

157-
// We need this check to handle the case where search results come back in a different
158-
// order than they were sent out. Only load results for the most recent search.
159-
if (searchValue === this.state.searchValue) {
160-
const options = savedObjects.map((indexPatternSavedObject: SimpleSavedObject<any>) => {
125+
const options = indexPatterns
126+
.filter((indexPattern) => {
127+
return fieldTypes
128+
? indexPattern.fields.some((field) => {
129+
return fieldTypes.includes(field.type);
130+
})
131+
: true;
132+
})
133+
.map((indexPattern) => {
161134
return {
162-
label: indexPatternSavedObject.attributes.title,
163-
value: indexPatternSavedObject.id,
135+
label: indexPattern.title,
136+
value: indexPattern.id,
164137
};
165138
});
166-
this.setState({
167-
isLoading: false,
168-
options,
169-
});
139+
this.setState({
140+
isLoading: false,
141+
options,
142+
});
170143

171-
if (onNoIndexPatterns && searchValue === '' && options.length === 0) {
172-
onNoIndexPatterns();
173-
}
144+
if (onNoIndexPatterns && searchValue === '' && options.length === 0) {
145+
onNoIndexPatterns();
174146
}
175147
}, 300);
176148

@@ -195,7 +167,7 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectInte
195167
indexPatternId,
196168
placeholder,
197169
onNoIndexPatterns,
198-
savedObjectsClient,
170+
indexPatternService,
199171
...rest
200172
} = this.props;
201173

0 commit comments

Comments
 (0)