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

Limit fetching index patterns #56603

Merged
merged 5 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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 () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding these 🙏

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) => {
Dosant marked this conversation as resolved.
Show resolved Hide resolved
if (!this.savedObjectsCache || refresh) {
Dosant marked this conversation as resolved.
Show resolved Hide resolved
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