[8.x] [watcher] Use ES Watcher API instead of search query (#204296) (#204372)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[watcher] Use ES Watcher API instead of search query
(#204296)](https://github.com/elastic/kibana/pull/204296)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Matthew
Kime","email":"matt@mattki.me"},"sourceCommit":{"committedDate":"2024-12-16T12:04:15Z","message":"[watcher]
Use ES Watcher API instead of search query (#204296)\n\n##
Summary\r\n\r\nSimply uses the ES Watcher API to load watches instead of
a search query\r\nagainst the `.watches` index. This appears to be the
last direct query\r\nagainst that index.\r\n\r\nPart of
https://github.com/elastic/kibana/issues/152142","sha":"bc5c0971843f06f250c77a08d60f2a507e099bf4","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Watcher","Team:Kibana
Management","release_note:skip","v9.0.0","backport:prev-minor"],"title":"[watcher]
Use ES Watcher API instead of search
query","number":204296,"url":"https://github.com/elastic/kibana/pull/204296","mergeCommit":{"message":"[watcher]
Use ES Watcher API instead of search query (#204296)\n\n##
Summary\r\n\r\nSimply uses the ES Watcher API to load watches instead of
a search query\r\nagainst the `.watches` index. This appears to be the
last direct query\r\nagainst that index.\r\n\r\nPart of
https://github.com/elastic/kibana/issues/152142","sha":"bc5c0971843f06f250c77a08d60f2a507e099bf4"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/204296","number":204296,"mergeCommit":{"message":"[watcher]
Use ES Watcher API instead of search query (#204296)\n\n##
Summary\r\n\r\nSimply uses the ES Watcher API to load watches instead of
a search query\r\nagainst the `.watches` index. This appears to be the
last direct query\r\nagainst that index.\r\n\r\nPart of
https://github.com/elastic/kibana/issues/152142","sha":"bc5c0971843f06f250c77a08d60f2a507e099bf4"}}]}]
BACKPORT-->

Co-authored-by: Matthew Kime <matt@mattki.me>
This commit is contained in:
Kibana Machine 2024-12-17 01:04:19 +11:00 committed by GitHub
parent a7ce45a3c6
commit 018a2a55fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 7 additions and 34 deletions

View file

@ -5,7 +5,6 @@
* 2.0.
*/
export const INDEX_NAMES: { [key: string]: string } = {
WATCHES: '.watches',
export const INDEX_NAMES = {
WATCHER_HISTORY: '.watcher-history-*',
};

View file

@ -60,7 +60,6 @@ export class WatcherServerPlugin implements Plugin<void, void, any, any> {
{
requiredClusterPrivileges: ['manage_watcher'],
requiredIndexPrivileges: {
[INDEX_NAMES.WATCHES]: ['read'],
[INDEX_NAMES.WATCHER_HISTORY]: ['read'],
},
ui: [],
@ -68,7 +67,6 @@ export class WatcherServerPlugin implements Plugin<void, void, any, any> {
{
requiredClusterPrivileges: ['monitor_watcher'],
requiredIndexPrivileges: {
[INDEX_NAMES.WATCHES]: ['read'],
[INDEX_NAMES.WATCHER_HISTORY]: ['read'],
},
ui: [],

View file

@ -5,29 +5,9 @@
* 2.0.
*/
import { IScopedClusterClient } from '@kbn/core/server';
import { get } from 'lodash';
import { fetchAllFromScroll } from '../../../lib/fetch_all_from_scroll';
import { INDEX_NAMES, ES_SCROLL_SETTINGS } from '../../../../common/constants';
import { RouteDependencies } from '../../../types';
// @ts-ignore
import { Watch } from '../../../models/watch';
function fetchWatches(dataClient: IScopedClusterClient) {
return dataClient.asCurrentUser
.search(
{
index: INDEX_NAMES.WATCHES,
scroll: ES_SCROLL_SETTINGS.KEEPALIVE,
body: {
size: ES_SCROLL_SETTINGS.PAGE_SIZE,
},
},
{ ignore: [404] }
)
.then((body) => fetchAllFromScroll(body, dataClient));
}
export function registerListRoute({ router, license, lib: { handleEsError } }: RouteDependencies) {
router.get(
{
@ -37,17 +17,13 @@ export function registerListRoute({ router, license, lib: { handleEsError } }: R
license.guardApiRoute(async (ctx, request, response) => {
try {
const esClient = (await ctx.core).elasticsearch.client;
const hits = await fetchWatches(esClient);
const watches = hits.map((hit: any) => {
const id = get(hit, '_id');
const watchJson = get(hit, '_source');
const watchStatusJson = get(hit, '_source.status');
const { watches: hits } = await esClient.asCurrentUser.watcher.queryWatches();
const watches = hits.map(({ _id, watch, status }) => {
return Watch.fromUpstreamJson(
{
id,
watchJson,
watchStatusJson,
id: _id,
watchJson: watch,
watchStatusJson: status,
},
{
throwExceptions: {

View file

@ -73,7 +73,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
// The index_management_user has been given permissions to advanced settings for Stack Management Tests.
// https://github.com/elastic/kibana/pull/113078/
expect(sections).to.have.length(2);
expect(sections).to.have.length(3);
expect(sections[0]).to.eql({
sectionId: 'data',
sectionLinks: ['index_management', 'transform'],