From b4c241cc42dbda37bf14b9f8d32634d4dbdf7ebe Mon Sep 17 00:00:00 2001 From: Dima Arnautov Date: Wed, 30 Oct 2024 11:56:07 +0100 Subject: [PATCH 1/2] chunk overall bucket request --- .../services/ml_api_service/index.ts | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/ml/public/application/services/ml_api_service/index.ts b/x-pack/plugins/ml/public/application/services/ml_api_service/index.ts index f69e60453bfd4..20e3fd1c64c36 100644 --- a/x-pack/plugins/ml/public/application/services/ml_api_service/index.ts +++ b/x-pack/plugins/ml/public/application/services/ml_api_service/index.ts @@ -11,7 +11,7 @@ import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import type { RuntimeMappings } from '@kbn/ml-runtime-field-utils'; -import { isNumber } from 'lodash'; +import { chunk, isNumber } from 'lodash'; import { ML_INTERNAL_BASE_PATH } from '../../../../common/constants/app'; import type { MlServerDefaults, @@ -397,13 +397,13 @@ export function mlApiProvider(httpService: HttpService) { end, overallScore, }: { - jobId: string; + jobId: string[]; topN: string; bucketSpan: string; start: number; end: number; overallScore?: number; - }) { + }): Promise { const body = JSON.stringify({ topN, bucketSpan, @@ -411,11 +411,31 @@ export function mlApiProvider(httpService: HttpService) { end, ...(overallScore ? { overall_score: overallScore } : {}), }); - return httpService.http({ - path: `${ML_INTERNAL_BASE_PATH}/anomaly_detectors/${jobId}/results/overall_buckets`, - method: 'POST', - body, - version: '1', + + // Max permitted job_id is 64 characters, so we can fit around 30 jobs per request + const maxJobsPerRequest = 30; + + return Promise.all( + chunk(jobId, maxJobsPerRequest).map((jobIdsChunk) => { + return httpService.http({ + path: `${ML_INTERNAL_BASE_PATH}/anomaly_detectors/${jobIdsChunk.join( + ',' + )}/results/overall_buckets`, + method: 'POST', + body, + version: '1', + }); + }) + ).then((responses) => { + // Merge responses + return responses.reduce( + (acc, response) => { + acc.count += response.count; + acc.overall_buckets.push(...response.overall_buckets); + return acc; + }, + { count: 0, overall_buckets: [] } as estypes.MlGetOverallBucketsResponse + ); }); }, From 59edb0f4e0754ae94d93625a7fba45c33eaf6f26 Mon Sep 17 00:00:00 2001 From: Dima Arnautov Date: Wed, 30 Oct 2024 12:32:33 +0100 Subject: [PATCH 2/2] refactor --- .../ml/public/application/services/ml_api_service/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/ml/public/application/services/ml_api_service/index.ts b/x-pack/plugins/ml/public/application/services/ml_api_service/index.ts index 20e3fd1c64c36..f21e67fe450f4 100644 --- a/x-pack/plugins/ml/public/application/services/ml_api_service/index.ts +++ b/x-pack/plugins/ml/public/application/services/ml_api_service/index.ts @@ -428,13 +428,13 @@ export function mlApiProvider(httpService: HttpService) { }) ).then((responses) => { // Merge responses - return responses.reduce( + return responses.reduce( (acc, response) => { acc.count += response.count; acc.overall_buckets.push(...response.overall_buckets); return acc; }, - { count: 0, overall_buckets: [] } as estypes.MlGetOverallBucketsResponse + { count: 0, overall_buckets: [] } ); }); },