Skip to content

Commit

Permalink
Limit fetching index patterns (elastic#56603)
Browse files Browse the repository at this point in the history
Should address elastic#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
  • Loading branch information
Dosant committed Feb 5, 2020
1 parent 4d66c14 commit 6e52440
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;

Expand All @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,7 +52,7 @@ export class IndexPatterns {
this.savedObjectsCache = (
await this.savedObjectsClient.find({
type: 'index-pattern',
fields: [],
fields: ['title'],
perPage: 10000,
})
).savedObjects;
Expand All @@ -76,16 +78,18 @@ 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();
}
if (!this.savedObjectsCache) {
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;
});
};
Expand Down

0 comments on commit 6e52440

Please sign in to comment.