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

[8.16] [ML] Fix overall bucket request for large number of job IDs (#198292) #198415

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -388,25 +388,45 @@ export function mlApiProvider(httpService: HttpService) {
end,
overallScore,
}: {
jobId: string;
jobId: string[];
topN: string;
bucketSpan: string;
start: number;
end: number;
overallScore?: number;
}) {
}): Promise<estypes.MlGetOverallBucketsResponse> {
const body = JSON.stringify({
topN,
bucketSpan,
start,
end,
...(overallScore ? { overall_score: overallScore } : {}),
});
return httpService.http<any>({
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<estypes.MlGetOverallBucketsResponse>({
path: `${ML_INTERNAL_BASE_PATH}/anomaly_detectors/${jobIdsChunk.join(
','
)}/results/overall_buckets`,
method: 'POST',
body,
version: '1',
});
})
).then((responses) => {
// Merge responses
return responses.reduce<estypes.MlGetOverallBucketsResponse>(
(acc, response) => {
acc.count += response.count;
acc.overall_buckets.push(...response.overall_buckets);
return acc;
},
{ count: 0, overall_buckets: [] }
);
});
},

Expand Down