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
This commit is contained in:
parent
a82ebf8b25
commit
a8d5b778ee
|
@ -0,0 +1,11 @@
|
|||
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
|
||||
|
||||
[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
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```typescript
|
||||
find: (search: string, size?: number) => Promise<IndexPattern[]>;
|
||||
```
|
|
@ -23,6 +23,7 @@ export declare class IndexPatternsService
|
|||
| [clearCache](./kibana-plugin-plugins-data-public.indexpatternsservice.clearcache.md) | | <code>(id?: string | undefined) => void</code> | Clear index pattern list cache |
|
||||
| [ensureDefaultIndexPattern](./kibana-plugin-plugins-data-public.indexpatternsservice.ensuredefaultindexpattern.md) | | <code>EnsureDefaultIndexPattern</code> | |
|
||||
| [fieldArrayToMap](./kibana-plugin-plugins-data-public.indexpatternsservice.fieldarraytomap.md) | | <code>(fields: FieldSpec[], fieldAttrs?: FieldAttrs | undefined) => Record<string, FieldSpec></code> | Converts field array to map |
|
||||
| [find](./kibana-plugin-plugins-data-public.indexpatternsservice.find.md) | | <code>(search: string, size?: number) => Promise<IndexPattern[]></code> | |
|
||||
| [get](./kibana-plugin-plugins-data-public.indexpatternsservice.get.md) | | <code>(id: string) => Promise<IndexPattern></code> | Get an index pattern by id. Cache optimized |
|
||||
| [getCache](./kibana-plugin-plugins-data-public.indexpatternsservice.getcache.md) | | <code>() => Promise<SavedObject<IndexPatternSavedObjectAttrs>[] | null | undefined></code> | |
|
||||
| [getDefault](./kibana-plugin-plugins-data-public.indexpatternsservice.getdefault.md) | | <code>() => Promise<IndexPattern | null></code> | Get default index pattern |
|
||||
|
|
|
@ -135,6 +135,20 @@ export class IndexPatternsService {
|
|||
return this.savedObjectsCache.map((obj) => obj?.attributes?.title);
|
||||
};
|
||||
|
||||
find = async (search: string, size: number = 10): Promise<IndexPattern[]> => {
|
||||
const savedObjects = await this.savedObjectsClient.find<IndexPatternSavedObjectAttrs>({
|
||||
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
|
||||
|
|
|
@ -229,7 +229,7 @@ export class DataPublicPlugin
|
|||
return {
|
||||
...dataServices,
|
||||
ui: {
|
||||
IndexPatternSelect: createIndexPatternSelect(core.savedObjects.client),
|
||||
IndexPatternSelect: createIndexPatternSelect(indexPatterns),
|
||||
SearchBar,
|
||||
},
|
||||
};
|
||||
|
|
|
@ -1396,6 +1396,8 @@ export class IndexPatternsService {
|
|||
// (undocumented)
|
||||
ensureDefaultIndexPattern: EnsureDefaultIndexPattern;
|
||||
fieldArrayToMap: (fields: FieldSpec[], fieldAttrs?: FieldAttrs | undefined) => Record<string, FieldSpec>;
|
||||
// (undocumented)
|
||||
find: (search: string, size?: number) => Promise<IndexPattern[]>;
|
||||
get: (id: string) => Promise<IndexPattern>;
|
||||
// Warning: (ae-forgotten-export) The symbol "IndexPatternSavedObjectAttrs" needs to be exported by the entry point index.d.ts
|
||||
//
|
||||
|
|
|
@ -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) => (
|
||||
<IndexPatternSelect {...props} savedObjectsClient={savedObjectsClient} />
|
||||
<IndexPatternSelect {...props} indexPatternService={indexPatternService} />
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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<IndexPatternSelectInternalProps> {
|
||||
|
@ -109,7 +93,8 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectInte
|
|||
|
||||
let indexPatternTitle;
|
||||
try {
|
||||
indexPatternTitle = await getTitle(this.props.savedObjectsClient, indexPatternId);
|
||||
const indexPattern = await this.props.indexPatternService.get(indexPatternId);
|
||||
indexPatternTitle = indexPattern.title;
|
||||
} catch (err) {
|
||||
// index pattern no longer exists
|
||||
return;
|
||||
|
@ -128,49 +113,36 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectInte
|
|||
};
|
||||
|
||||
debouncedFetch = _.debounce(async (searchValue: string) => {
|
||||
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<any>) => {
|
||||
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<any>) => {
|
||||
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<IndexPatternSelectInte
|
|||
indexPatternId,
|
||||
placeholder,
|
||||
onNoIndexPatterns,
|
||||
savedObjectsClient,
|
||||
indexPatternService,
|
||||
...rest
|
||||
} = this.props;
|
||||
|
||||
|
|
Loading…
Reference in a new issue