Skip to content

Commit

Permalink
IndexPatternService - remove getFields function (elastic#77571)
Browse files Browse the repository at this point in the history
* remove getFields fn
  • Loading branch information
mattkime committed Sep 16, 2020
1 parent cb7ba1f commit 330512b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,13 @@ describe('IndexPatterns', () => {
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);
expect(savedObjectsClient.find).toHaveBeenCalledTimes(2);
});

test('deletes the index pattern', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ const indexPatternCache = createIndexPatternCache();
const MAX_ATTEMPTS_TO_RESOLVE_CONFLICTS = 3;
const savedObjectType = 'index-pattern';

type IndexPatternCachedFieldType = 'id' | 'title';

export interface IndexPatternSavedObjectAttrs {
title: string;
}
Expand Down Expand Up @@ -121,22 +119,6 @@ export class IndexPatternsService {
return this.savedObjectsCache.map((obj) => obj?.attributes?.title);
};

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: Partial<Record<IndexPatternCachedFieldType, string>> = {};
fields.forEach(
(f: IndexPatternCachedFieldType) => (result[f] = obj[f] || obj?.attributes?.[f])
);
return result;
});
};

getFieldsForTimePattern = (options: GetFieldsOptions = {}) => {
return this.apiClient.getFieldsForTimePattern(options);
};
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ describe('Flyout', () => {
done: jest.fn(),
newIndexPatternUrl: '',
indexPatterns: {
getFields: jest.fn().mockImplementation(() => [{ id: '1' }, { id: '2' }]),
getCache: jest.fn().mockImplementation(() => [
{ id: '1', attributes: {} },
{ id: '2', attributes: {} },
]),
} as any,
overlays,
http,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ export class Flyout extends Component<FlyoutProps, FlyoutState> {
}

fetchIndexPatterns = async () => {
const indexPatterns = await this.props.indexPatterns.getFields(['id', 'title']);
const indexPatterns = (await this.props.indexPatterns.getCache())?.map((savedObject) => ({
id: savedObject.id,
title: savedObject.attributes.title,
}));
this.setState({ indexPatterns } as any);
};

Expand Down

0 comments on commit 330512b

Please sign in to comment.