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

[Response Ops] Using data view service to fetch data views #131330

Merged
merged 4 commits into from
May 6, 2022
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 @@ -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
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