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

[Watcher] Prevent expensive queries on ES by using PointInTimeFinder to get indexPatterns #119717

Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ jest.mock('../../public/application/lib/api', () => {
return {
...original,
loadIndexPatterns: async () => {
const INDEX_PATTERNS = [
{ attributes: { title: 'index1' } },
{ attributes: { title: 'index2' } },
{ attributes: { title: 'index3' } },
];
return await INDEX_PATTERNS;
return ['index1', 'index2', 'index3'];
},
getHttpClient: () => mockHttpClient,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ jest.mock('../../public/application/lib/api', () => {
return {
...original,
loadIndexPatterns: async () => {
const INDEX_PATTERNS = [
{ attributes: { title: 'index1' } },
{ attributes: { title: 'index2' } },
{ attributes: { title: 'index3' } },
];
return await INDEX_PATTERNS;
return ['index1', 'index2', 'index3'];
sabarasaba marked this conversation as resolved.
Show resolved Hide resolved
},
getHttpClient: () => mockHttpClient,
};
Expand Down
8 changes: 3 additions & 5 deletions x-pack/plugins/watcher/public/application/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,10 @@ export const executeWatch = async (executeWatchDetails: ExecutedWatchDetails, wa
};

export const loadIndexPatterns = async () => {
const { savedObjects } = await getSavedObjectsClient().find({
type: 'index-pattern',
fields: ['title'],
perPage: 10000,
return sendRequest({
path: `${basePath}/indices/index_patterns`,
method: 'get',
});
return savedObjects;
};

const getWatchVisualizationDataDeserializer = (data: { visualizeData: any }) => data?.visualizeData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,8 @@ export const ThresholdWatchEdit = ({ pageTitle }: { pageTitle: string }) => {

useEffect(() => {
const getIndexPatterns = async () => {
const indexPatternObjects = await loadIndexPatterns();
const titles = indexPatternObjects.map((indexPattern: any) => indexPattern.attributes.title);
setIndexPatterns(titles);
const { data: indexPatternTitles } = await loadIndexPatterns();
setIndexPatterns(indexPatternTitles);
};

const loadData = async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { SavedObject, SavedObjectsFindResult } from 'src/core/server';
import { RouteDependencies } from '../../../types';

export function registerGetIndexPatternsRoute({
router,
license,
lib: { handleEsError },
}: RouteDependencies) {
router.get(
{
path: '/api/watcher/indices/index_patterns',
validate: false,
},
license.guardApiRoute(async ({ core: { savedObjects } }, request, response) => {
try {
const finder = savedObjects.client.createPointInTimeFinder({
type: 'index-pattern',
fields: ['title'],
perPage: 1000,
});

const responses: SavedObject[] = [];
sabarasaba marked this conversation as resolved.
Show resolved Hide resolved

for await (const result of finder.find()) {
responses.push(
...result.saved_objects.map(
(indexPattern: SavedObjectsFindResult<any>) => indexPattern.attributes.title
)
);
}

await finder.close();

return response.ok({ body: responses });
} catch (error) {
return handleEsError({ error, response });
}
})
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
*/

import { registerGetRoute } from './register_get_route';
import { registerGetIndexPatternsRoute } from './register_get_index_patterns_route';
import { RouteDependencies } from '../../../types';

export function registerIndicesRoutes(deps: RouteDependencies) {
registerGetRoute(deps);
registerGetIndexPatternsRoute(deps);
}
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./painless_lab'));
loadTestFile(require.resolve('./file_upload'));
loadTestFile(require.resolve('./ml'));
loadTestFile(require.resolve('./watcher'));
});
}
14 changes: 14 additions & 0 deletions x-pack/test/api_integration/apis/watcher/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { FtrProviderContext } from '../../ftr_provider_context';

export default function ({ loadTestFile }: FtrProviderContext) {
describe('Watcher', () => {
loadTestFile(require.resolve('./watcher'));
});
}
47 changes: 47 additions & 0 deletions x-pack/test/api_integration/apis/watcher/watcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import expect from '@kbn/expect';

import { FtrProviderContext } from '../../ftr_provider_context';

export default function ({ getService }: FtrProviderContext) {
const log = getService('log');
const supertest = getService('supertest');
const transform = getService('transform');

describe('watcher', () => {
before(async () => {
try {
await transform.testResources.createIndexPatternIfNeeded('ft_ecommerce', 'order_date');
} catch (error) {
log.debug('[Setup error] Error creating index pattern');
throw error;
}
});

after(async () => {
try {
await transform.testResources.deleteIndexPatternByTitle('ft_ecommerce');
} catch (error) {
log.debug('[Cleanup error] Error deleting index pattern');
throw error;
}
});

describe('POST /api/watcher/indices/index_patterns', () => {
it('returns list of index patterns', async () => {
const response = await supertest
.get('/api/watcher/indices/index_patterns')
.set('kbn-xsrf', 'kibana')
.expect(200);

expect(response.body).to.contain('ft_ecommerce');
});
});
});
}