-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Watcher] Prevent expensive queries on ES by using PointInTimeFinder …
…to get indexPatterns (#119717) * Use PointInTimeFinder to paginate through indexPatterns in a more performant way * commit using @elastic.co * Start working on tests * Add tests * Add missing types * Fix tests * Update tests responses * Remove unnecessary mocks * Fix type Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
c6e9da2
commit fd741b4
Showing
9 changed files
with
116 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
x-pack/plugins/watcher/server/routes/api/indices/register_get_index_patterns_route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { 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: string[] = []; | ||
|
||
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 }); | ||
} | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); | ||
} |