mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[Indexpattern management] Use indexPatterns Service instead of savedObjects client (#91839)
* [Index pattern management] Use indexPatterns Service instead of savedObjects client * Minor fixes * Keep the same test setup
This commit is contained in:
parent
8d9ac0058f
commit
494d1decf5
9 changed files with 64 additions and 116 deletions
|
@ -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';
|
||||
|
@ -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', () => {
|
||||
|
|
|
@ -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,
|
||||
|
@ -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 });
|
||||
};
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
@ -62,7 +60,6 @@ export const EditIndexPattern = withRouter(
|
|||
uiSettings,
|
||||
indexPatternManagementStart,
|
||||
overlays,
|
||||
savedObjects,
|
||||
chrome,
|
||||
data,
|
||||
} = useKibana<IndexPatternManagmentContext>().services;
|
||||
|
@ -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;
|
||||
|
|
|
@ -69,13 +69,13 @@ interface Props extends RouteComponentProps {
|
|||
export const IndexPatternTable = ({ canSave, history }: Props) => {
|
||||
const {
|
||||
setBreadcrumbs,
|
||||
savedObjects,
|
||||
uiSettings,
|
||||
indexPatternManagementStart,
|
||||
chrome,
|
||||
docLinks,
|
||||
application,
|
||||
http,
|
||||
data,
|
||||
getMlCardState,
|
||||
} = useKibana<IndexPatternManagmentContext>().services;
|
||||
const [indexPatterns, setIndexPatterns] = useState<IndexPatternTableItem[]>([]);
|
||||
|
@ -92,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;
|
||||
|
|
|
@ -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();
|
||||
const indexPatternContractMock = ({
|
||||
getIdsWithTitle: jest.fn().mockReturnValue(
|
||||
Promise.resolve([
|
||||
{
|
||||
id: 'test',
|
||||
title: 'test name',
|
||||
},
|
||||
{
|
||||
id: 'test1',
|
||||
title: 'test name 1',
|
||||
},
|
||||
])
|
||||
),
|
||||
get: jest.fn().mockReturnValue(Promise.resolve({})),
|
||||
} as unknown) as jest.Mocked<IndexPatternsContract>;
|
||||
|
||||
(savedObjects.client.find as jest.Mock).mockResolvedValue({
|
||||
savedObjects: [
|
||||
{
|
||||
id: 'test',
|
||||
get: () => {
|
||||
return 'test name';
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'test1',
|
||||
get: () => {
|
||||
return 'test name 1';
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
const mockManagementPluginStart = mockManagementPlugin.createStartContract();
|
||||
|
||||
test('getting index patterns', async () => {
|
||||
const indexPatterns = await getIndexPatterns(
|
||||
savedObjects.client,
|
||||
'test',
|
||||
mockManagementPluginStart
|
||||
mockManagementPluginStart,
|
||||
indexPatternContractMock
|
||||
);
|
||||
expect(indexPatterns).toMatchSnapshot();
|
||||
});
|
||||
|
|
|
@ -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
|
||||
) {
|
||||
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
|
||||
);
|
||||
|
||||
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 (
|
||||
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 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}`,
|
||||
};
|
||||
})
|
||||
.sort((a, b) => {
|
||||
if (a.sort < b.sort) {
|
||||
return -1;
|
||||
} else if (a.sort > b.sort) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
})
|
||||
) || []
|
||||
indexPatternsListItems.sort((a, b) => {
|
||||
if (a.sort < b.sort) {
|
||||
return -1;
|
||||
} else if (a.sort > b.sort) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}) || []
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
@ -54,7 +54,6 @@ export async function mountManagementSection(
|
|||
const deps: IndexPatternManagmentContext = {
|
||||
chrome,
|
||||
application,
|
||||
savedObjects,
|
||||
uiSettings,
|
||||
notifications,
|
||||
overlays,
|
||||
|
|
|
@ -75,14 +75,7 @@ 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();
|
||||
|
@ -90,7 +83,6 @@ const createIndexPatternManagmentContext = (): {
|
|||
return {
|
||||
chrome,
|
||||
application,
|
||||
savedObjects,
|
||||
uiSettings,
|
||||
notifications,
|
||||
overlays,
|
||||
|
|
|
@ -11,7 +11,6 @@ import {
|
|||
ApplicationStart,
|
||||
IUiSettingsClient,
|
||||
OverlayStart,
|
||||
SavedObjectsStart,
|
||||
NotificationsStart,
|
||||
DocLinksStart,
|
||||
HttpSetup,
|
||||
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue