Limit fetching index patterns (#56603)

Should address #56352.

I did a look up and it seems like only id and title a really used in case that savedObjectsCache is used. So I simply limited that request to fetch only title
This commit is contained in:
Anton Dosov 2020-02-05 20:21:14 +01:00 committed by GitHub
parent 900af8262b
commit dcaee36498
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 6 deletions

View file

@ -19,7 +19,12 @@
// eslint-disable-next-line max-classes-per-file
import { IndexPatterns } from './index_patterns';
import { SavedObjectsClientContract, IUiSettingsClient, HttpSetup } from 'kibana/public';
import {
SavedObjectsClientContract,
IUiSettingsClient,
HttpSetup,
SavedObjectsFindResponsePublic,
} from 'kibana/public';
jest.mock('./index_pattern', () => {
class IndexPattern {
@ -45,9 +50,17 @@ jest.mock('./index_patterns_api_client', () => {
describe('IndexPatterns', () => {
let indexPatterns: IndexPatterns;
let savedObjectsClient: SavedObjectsClientContract;
beforeEach(() => {
const savedObjectsClient = {} as SavedObjectsClientContract;
savedObjectsClient = {} as SavedObjectsClientContract;
savedObjectsClient.find = jest.fn(
() =>
Promise.resolve({
savedObjects: [{ id: 'id', attributes: { title: 'title' } }],
}) as Promise<SavedObjectsFindResponsePublic<any>>
);
const uiSettings = {} as IUiSettingsClient;
const http = {} as HttpSetup;
@ -61,4 +74,27 @@ describe('IndexPatterns', () => {
expect(indexPattern).toBeDefined();
expect(indexPattern).toBe(await indexPatterns.get(id));
});
test('savedObjectCache pre-fetches only title', async () => {
expect(await indexPatterns.getIds()).toEqual(['id']);
expect(savedObjectsClient.find).toHaveBeenCalledWith({
type: 'index-pattern',
fields: ['title'],
perPage: 10000,
});
});
test('caches saved objects', async () => {
await indexPatterns.getIds();
await indexPatterns.getTitles();
await indexPatterns.getFields(['id', 'title']);
expect(savedObjectsClient.find).toHaveBeenCalledTimes(1);
});
test('can refresh the saved objects caches', async () => {
await indexPatterns.getIds();
await indexPatterns.getTitles(true);
await indexPatterns.getFields(['id', 'title'], true);
expect(savedObjectsClient.find).toHaveBeenCalledTimes(3);
});
});

View file

@ -30,6 +30,8 @@ import { IndexPatternsApiClient, GetFieldsOptions } from './index_patterns_api_c
const indexPatternCache = createIndexPatternCache();
type IndexPatternCachedFieldType = 'id' | 'title';
export class IndexPatterns {
private config: IUiSettingsClient;
private savedObjectsClient: SavedObjectsClientContract;
@ -50,7 +52,7 @@ export class IndexPatterns {
this.savedObjectsCache = (
await this.savedObjectsClient.find({
type: 'index-pattern',
fields: [],
fields: ['title'],
perPage: 10000,
})
).savedObjects;
@ -76,7 +78,7 @@ export class IndexPatterns {
return this.savedObjectsCache.map(obj => obj?.attributes?.title);
};
getFields = async (fields: string[], refresh: boolean = false) => {
getFields = async (fields: IndexPatternCachedFieldType[], refresh: boolean = false) => {
if (!this.savedObjectsCache || refresh) {
await this.refreshSavedObjectsCache();
}
@ -84,8 +86,10 @@ export class IndexPatterns {
return [];
}
return this.savedObjectsCache.map((obj: Record<string, any>) => {
const result: Record<string, any> = {};
fields.forEach((f: string) => (result[f] = obj[f] || obj?.attributes?.[f]));
const result: Partial<Record<IndexPatternCachedFieldType, string>> = {};
fields.forEach(
(f: IndexPatternCachedFieldType) => (result[f] = obj[f] || obj?.attributes?.[f])
);
return result;
});
};