[watcher] Use ES Watcher API instead of search query (#204296)

## Summary

Simply uses the ES Watcher API to load watches instead of a search query
against the `.watches` index. This appears to be the last direct query
against that index.

Part of https://github.com/elastic/kibana/issues/152142
This commit is contained in:
Matthew Kime 2024-12-16 06:04:15 -06:00 committed by GitHub
parent d0270e57ce
commit bc5c097184
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'],