Skip to content
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

Update IndexPatternSelect to get fields from indexPatternService instead of savedObject attributes #84376

Merged
merged 7 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[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)

## IndexPatternsService.find property

<b>Signature:</b>

```typescript
find: (search: string, size?: number) => Promise<IndexPattern[]>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export declare class IndexPatternsService
| [clearCache](./kibana-plugin-plugins-data-public.indexpatternsservice.clearcache.md) | | <code>(id?: string &#124; undefined) =&gt; 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 &#124; undefined) =&gt; Record&lt;string, FieldSpec&gt;</code> | Converts field array to map |
| [find](./kibana-plugin-plugins-data-public.indexpatternsservice.find.md) | | <code>(search: string, size?: number) =&gt; Promise&lt;IndexPattern[]&gt;</code> | |
| [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 |
| [getCache](./kibana-plugin-plugins-data-public.indexpatternsservice.getcache.md) | | <code>() =&gt; Promise&lt;SavedObject&lt;IndexPatternSavedObjectAttrs&gt;[] &#124; null &#124; undefined&gt;</code> | |
| [getDefault](./kibana-plugin-plugins-data-public.indexpatternsservice.getdefault.md) | | <code>() =&gt; Promise&lt;IndexPattern &#124; null&gt;</code> | Get default index pattern |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: `${search}*`,
nreese marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export class DataPublicPlugin
return {
...dataServices,
ui: {
IndexPatternSelect: createIndexPatternSelect(core.savedObjects.client),
IndexPatternSelect: createIndexPatternSelect(indexPatterns),
SearchBar,
},
};
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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} />
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand All @@ -40,7 +39,7 @@ export type IndexPatternSelectProps = Required<
};

export type IndexPatternSelectInternalProps = IndexPatternSelectProps & {
savedObjectsClient: SavedObjectsClientContract;
indexPatternService: IndexPatternsContract;
};

interface IndexPatternSelectState {
Expand All @@ -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> {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
});
}
const { fieldTypes, onNoIndexPatterns, indexPatternService } = this.props;
const indexPatterns = await indexPatternService.find(searchValue, 100);

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<any>) => {
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);

Expand All @@ -195,7 +167,7 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectInte
indexPatternId,
placeholder,
onNoIndexPatterns,
savedObjectsClient,
indexPatternService,
...rest
} = this.props;

Expand Down