Skip to content

Commit

Permalink
Using data view service to fetch data views (#131330)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
ymao1 and kibanamachine authored May 6, 2022
1 parent ed70cb1 commit 43c4134
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 105 deletions.
6 changes: 3 additions & 3 deletions x-pack/plugins/triggers_actions_ui/public/application/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
} from '../types';
import { Section, routeToRuleDetails, legacyRouteToRuleDetails } from './constants';

import { setSavedObjectsClient } from '../common/lib/data_apis';
import { setDataViewsService } from '../common/lib/data_apis';
import { KibanaContextProvider } from '../common/lib/kibana';

const TriggersActionsUIHome = lazy(() => import('./home'));
Expand Down Expand Up @@ -67,12 +67,12 @@ export const renderApp = (deps: TriggersAndActionsUiServices) => {
};

export const App = ({ deps }: { deps: TriggersAndActionsUiServices }) => {
const { savedObjects, uiSettings, theme$ } = deps;
const { dataViews, uiSettings, theme$ } = deps;
const sections: Section[] = ['rules', 'connectors', 'alerts', '__components_sandbox'];
const isDarkMode = useObservable<boolean>(uiSettings.get$('theme:darkMode'));

const sectionsRegex = sections.join('|');
setSavedObjectsClient(savedObjects.client);
setDataViewsService(dataViews);
return (
<I18nProvider>
<EuiThemeProvider darkMode={isDarkMode}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import {
loadIndexPatterns,
setSavedObjectsClient,
setDataViewsService,
getMatchingIndices,
getESIndexFields,
} from './data_apis';
Expand All @@ -19,10 +19,8 @@ const http = httpServiceMock.createStartContract();
const pattern = 'test-pattern';
const indexes = ['test-index'];

const generateIndexPattern = (title: string) => ({
attributes: {
title,
},
const generateDataView = (title: string) => ({
title,
});

const mockIndices = { indices: ['indices1', 'indices2'] };
Expand Down Expand Up @@ -67,7 +65,7 @@ describe('Data API', () => {

describe('index patterns', () => {
beforeEach(() => {
setSavedObjectsClient({
setDataViewsService({
find: mockFind,
});
});
Expand All @@ -76,68 +74,15 @@ describe('Data API', () => {
});

test('fetches the index patterns', async () => {
mockFind.mockResolvedValueOnce({
savedObjects: [generateIndexPattern('index-1'), generateIndexPattern('index-2')],
total: 2,
});
mockFind.mockResolvedValueOnce([generateDataView('index-1'), generateDataView('index-2')]);
const results = await loadIndexPatterns(mockPattern);

expect(mockFind).toBeCalledTimes(1);
expect(mockFind).toBeCalledWith({
fields: ['title'],
page: 1,
perPage,
search: '*test-pattern*',
type: 'index-pattern',
});
expect(mockFind).toBeCalledWith('*test-pattern*', perPage);
expect(results).toEqual(['index-1', 'index-2']);
});

test(`fetches the index patterns as chunks and merges them, if the total number of index patterns more than ${perPage}`, async () => {
mockFind.mockResolvedValueOnce({
savedObjects: [generateIndexPattern('index-1'), generateIndexPattern('index-2')],
total: 2010,
});
mockFind.mockResolvedValueOnce({
savedObjects: [generateIndexPattern('index-3'), generateIndexPattern('index-4')],
total: 2010,
});
mockFind.mockResolvedValueOnce({
savedObjects: [generateIndexPattern('index-5'), generateIndexPattern('index-6')],
total: 2010,
});
const results = await loadIndexPatterns(mockPattern);

expect(mockFind).toBeCalledTimes(3);
expect(mockFind).toHaveBeenNthCalledWith(1, {
fields: ['title'],
page: 1,
perPage,
search: '*test-pattern*',
type: 'index-pattern',
});
expect(mockFind).toHaveBeenNthCalledWith(2, {
fields: ['title'],
page: 2,
perPage,
search: '*test-pattern*',
type: 'index-pattern',
});
expect(mockFind).toHaveBeenNthCalledWith(3, {
fields: ['title'],
page: 3,
perPage,
search: '*test-pattern*',
type: 'index-pattern',
});
expect(results).toEqual(['index-1', 'index-2', 'index-3', 'index-4', 'index-5', 'index-6']);
});

test('returns an empty array if one of the requests fails', async () => {
mockFind.mockResolvedValueOnce({
savedObjects: [generateIndexPattern('index-1'), generateIndexPattern('index-2')],
total: 1010,
});
test('returns an empty array if find requests fails', async () => {
mockFind.mockRejectedValueOnce(500);

const results = await loadIndexPatterns(mockPattern);
Expand Down
49 changes: 9 additions & 40 deletions x-pack/plugins/triggers_actions_ui/public/common/lib/data_apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { HttpSetup } from '@kbn/core/public';
import { DataViewsContract, DataView } from '@kbn/data-views-plugin/public';

const DATA_API_ROOT = '/api/triggers_actions_ui/data';

Expand Down Expand Up @@ -62,57 +63,25 @@ export async function getESIndexFields({
return fields;
}

let savedObjectsClient: any;
type DataViewsService = Pick<DataViewsContract, 'find'>;
let dataViewsService: DataViewsService;

export const setSavedObjectsClient = (aSavedObjectsClient: any) => {
savedObjectsClient = aSavedObjectsClient;
export const setDataViewsService = (aDataViewsService: DataViewsService) => {
dataViewsService = aDataViewsService;
};

export const getSavedObjectsClient = () => {
return savedObjectsClient;
export const getDataViewsService = () => {
return dataViewsService;
};

export const loadIndexPatterns = async (pattern: string) => {
let allSavedObjects = [];
const formattedPattern = formatPattern(pattern);
const perPage = 1000;

try {
const { savedObjects, total } = await getSavedObjectsClient().find({
type: 'index-pattern',
fields: ['title'],
page: 1,
search: formattedPattern,
perPage,
});
const dataViews: DataView[] = await getDataViewsService().find(formattedPattern, perPage);

allSavedObjects = savedObjects;

if (total > perPage) {
let currentPage = 2;
const numberOfPages = Math.ceil(total / perPage);
const promises = [];

while (currentPage <= numberOfPages) {
promises.push(
getSavedObjectsClient().find({
type: 'index-pattern',
page: currentPage,
fields: ['title'],
search: formattedPattern,
perPage,
})
);
currentPage++;
}

const paginatedResults = await Promise.all(promises);

allSavedObjects = paginatedResults.reduce((oldResult, result) => {
return oldResult.concat(result.savedObjects);
}, allSavedObjects);
}
return allSavedObjects.map((indexPattern: any) => indexPattern.attributes.title);
return dataViews.map((dataView: DataView) => dataView.title);
} catch (e) {
return [];
}
Expand Down

0 comments on commit 43c4134

Please sign in to comment.