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

[7.x] [Indexpattern management] Use indexPatterns Service instead of savedObjects client (#91839) #91985

Merged
merged 3 commits into from
Feb 19, 2021
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 @@ -7,7 +7,6 @@
*/

import React from 'react';
import { SavedObjectsFindResponsePublic } from 'kibana/public';
import { StepIndexPattern, canPreselectTimeField } from './step_index_pattern';
import { Header } from './components/header';
import { IndexPatternCreationConfig } from '../../../../../../../plugins/index_pattern_management/public';
Expand Down Expand Up @@ -43,8 +42,7 @@ const goToNextStep = () => {};

const mockContext = mockManagementPlugin.createIndexPatternManagmentContext();

mockContext.savedObjects.client.find = async () =>
Promise.resolve(({ savedObjects: [] } as unknown) as SavedObjectsFindResponsePublic<any>);
mockContext.data.indexPatterns.getTitles = async () => Promise.resolve([]);
mockContext.uiSettings.get.mockReturnValue('');

describe('StepIndexPattern', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import React, { Component } from 'react';
import { EuiSpacer, EuiCallOut, EuiSwitchEvent } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import {
indexPatterns,
IndexPatternAttributes,
UI_SETTINGS,
} from '../../../../../../../plugins/data/public';
import { indexPatterns, UI_SETTINGS } from '../../../../../../../plugins/data/public';
import {
getIndices,
containsIllegalCharacters,
Expand Down Expand Up @@ -118,18 +114,7 @@ export class StepIndexPattern extends Component<StepIndexPatternProps, StepIndex
}

fetchExistingIndexPatterns = async () => {
const {
savedObjects,
} = await this.context.services.savedObjects.client.find<IndexPatternAttributes>({
type: 'index-pattern',
fields: ['title'],
perPage: 10000,
});

const existingIndexPatterns = savedObjects.map((obj) =>
obj && obj.attributes ? obj.attributes.title : ''
) as string[];

const existingIndexPatterns = await this.context.services.data.indexPatterns.getTitles();
this.setState({ existingIndexPatterns });
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import { useKibana } from '../../../../../plugins/kibana_react/public';
import { IndexPatternManagmentContext } from '../../types';
import { Tabs } from './tabs';
import { IndexHeader } from './index_header';
import { IndexPatternTableItem } from '../types';
import { getIndexPatterns } from '../utils';

export interface EditIndexPatternProps extends RouteComponentProps {
indexPattern: IndexPattern;
Expand Down Expand Up @@ -62,7 +60,6 @@ export const EditIndexPattern = withRouter(
uiSettings,
indexPatternManagementStart,
overlays,
savedObjects,
chrome,
data,
} = useKibana<IndexPatternManagmentContext>().services;
Expand Down Expand Up @@ -97,11 +94,7 @@ export const EditIndexPattern = withRouter(
const removePattern = () => {
async function doRemove() {
if (indexPattern.id === defaultIndex) {
const indexPatterns: IndexPatternTableItem[] = await getIndexPatterns(
savedObjects.client,
uiSettings.get('defaultIndex'),
indexPatternManagementStart
);
const indexPatterns = await data.indexPatterns.getIdsWithTitle();
uiSettings.remove('defaultIndex');
const otherPatterns = filter(indexPatterns, (pattern) => {
return pattern.id !== indexPattern.id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ interface Props extends RouteComponentProps {
export const IndexPatternTable = ({ canSave, history }: Props) => {
const {
setBreadcrumbs,
savedObjects,
uiSettings,
indexPatternManagementStart,
chrome,
Expand All @@ -93,21 +92,15 @@ export const IndexPatternTable = ({ canSave, history }: Props) => {
history.push
);
const gettedIndexPatterns: IndexPatternTableItem[] = await getIndexPatterns(
savedObjects.client,
uiSettings.get('defaultIndex'),
indexPatternManagementStart
indexPatternManagementStart,
data.indexPatterns
);
setIsLoadingIndexPatterns(false);
setCreationOptions(options);
setIndexPatterns(gettedIndexPatterns);
})();
}, [
history.push,
indexPatterns.length,
indexPatternManagementStart,
uiSettings,
savedObjects.client,
]);
}, [history.push, indexPatterns.length, indexPatternManagementStart, uiSettings, data]);

const removeAliases = (item: MatchedItem) =>
!((item as unknown) as ResolveIndexResponseItemAlias).indices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,33 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { IndexPatternsContract } from 'src/plugins/data/public';
import { getIndexPatterns } from './utils';
import { coreMock } from '../../../../core/public/mocks';
import { mockManagementPlugin } from '../mocks';

const { savedObjects } = coreMock.createStart();
const mockManagementPluginStart = mockManagementPlugin.createStartContract();

(savedObjects.client.find as jest.Mock).mockResolvedValue({
savedObjects: [
{
id: 'test',
get: () => {
return 'test name';
const indexPatternContractMock = ({
getIdsWithTitle: jest.fn().mockReturnValue(
Promise.resolve([
{
id: 'test',
title: 'test name',
},
},
{
id: 'test1',
get: () => {
return 'test name 1';
{
id: 'test1',
title: 'test name 1',
},
},
],
});
])
),
get: jest.fn().mockReturnValue(Promise.resolve({})),
} as unknown) as jest.Mocked<IndexPatternsContract>;

const mockManagementPluginStart = mockManagementPlugin.createStartContract();

test('getting index patterns', async () => {
const indexPatterns = await getIndexPatterns(
savedObjects.client,
'test',
mockManagementPluginStart
mockManagementPluginStart,
indexPatternContractMock
);
expect(indexPatterns).toMatchSnapshot();
});
76 changes: 34 additions & 42 deletions src/plugins/index_pattern_management/public/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,46 @@
* Side Public License, v 1.
*/

import { IIndexPattern } from 'src/plugins/data/public';
import { SavedObjectsClientContract } from 'src/core/public';
import { IndexPatternsContract } from 'src/plugins/data/public';
import { IndexPatternManagementStart } from '../plugin';

export async function getIndexPatterns(
savedObjectsClient: SavedObjectsClientContract,
defaultIndex: string,
indexPatternManagementStart: IndexPatternManagementStart
indexPatternManagementStart: IndexPatternManagementStart,
indexPatternsService: IndexPatternsContract
) {
return (
savedObjectsClient
.find<IIndexPattern>({
type: 'index-pattern',
fields: ['title', 'type'],
perPage: 10000,
})
.then((response) =>
response.savedObjects
.map((pattern) => {
const id = pattern.id;
const title = pattern.get('title');
const isDefault = defaultIndex === id;
const existingIndexPatterns = await indexPatternsService.getIdsWithTitle();
const indexPatternsListItems = await Promise.all(
existingIndexPatterns.map(async ({ id, title }) => {
const isDefault = defaultIndex === id;
const pattern = await indexPatternsService.get(id);
const tags = (indexPatternManagementStart as IndexPatternManagementStart).list.getIndexPatternTags(
pattern,
isDefault
);

const tags = (indexPatternManagementStart as IndexPatternManagementStart).list.getIndexPatternTags(
pattern,
isDefault
);
return {
id,
title,
default: isDefault,
tags,
// the prepending of 0 at the default pattern takes care of prioritization
// so the sorting will but the default index on top
// or on bottom of a the table
sort: `${isDefault ? '0' : '1'}${title}`,
};
})
);

return {
id,
title,
default: isDefault,
tags,
// the prepending of 0 at the default pattern takes care of prioritization
// so the sorting will but the default index on top
// or on bottom of a the table
sort: `${isDefault ? '0' : '1'}${title}`,
};
})
.sort((a, b) => {
if (a.sort < b.sort) {
return -1;
} else if (a.sort > b.sort) {
return 1;
} else {
return 0;
}
})
) || []
return (
indexPatternsListItems.sort((a, b) => {
if (a.sort < b.sort) {
return -1;
} else if (a.sort > b.sort) {
return 1;
} else {
return 0;
}
}) || []
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function mountManagementSection(
getMlCardState: () => MlCardState
) {
const [
{ chrome, application, savedObjects, uiSettings, notifications, overlays, http, docLinks },
{ chrome, application, uiSettings, notifications, overlays, http, docLinks },
{ data, indexPatternFieldEditor },
indexPatternManagementStart,
] = await getStartServices();
Expand All @@ -54,7 +54,6 @@ export async function mountManagementSection(
const deps: IndexPatternManagmentContext = {
chrome,
application,
savedObjects,
uiSettings,
notifications,
overlays,
Expand Down
10 changes: 1 addition & 9 deletions src/plugins/index_pattern_management/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,14 @@ const docLinks = {
const createIndexPatternManagmentContext = (): {
[key in keyof IndexPatternManagmentContext]: any;
} => {
const {
chrome,
application,
savedObjects,
uiSettings,
notifications,
overlays,
} = coreMock.createStart();
const { chrome, application, uiSettings, notifications, overlays } = coreMock.createStart();
const { http } = coreMock.createSetup();
const data = dataPluginMock.createStartContract();
const indexPatternFieldEditor = indexPatternFieldEditorPluginMock.createStartContract();

return {
chrome,
application,
savedObjects,
uiSettings,
notifications,
overlays,
Expand Down
2 changes: 0 additions & 2 deletions src/plugins/index_pattern_management/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
ApplicationStart,
IUiSettingsClient,
OverlayStart,
SavedObjectsStart,
NotificationsStart,
DocLinksStart,
HttpSetup,
Expand All @@ -25,7 +24,6 @@ import { IndexPatternFieldEditorStart } from '../../index_pattern_field_editor/p
export interface IndexPatternManagmentContext {
chrome: ChromeStart;
application: ApplicationStart;
savedObjects: SavedObjectsStart;
uiSettings: IUiSettingsClient;
notifications: NotificationsStart;
overlays: OverlayStart;
Expand Down