From 38eacb9f7fb2ae4188ecfa01b3910395ac09afc8 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Fri, 8 Feb 2019 11:46:48 +0100 Subject: [PATCH 1/2] [ML] Fix Job Audit Messages filter. --- .../job_audit_messages/job_audit_messages.js | 42 +++++++++++++------ .../ml/server/models/job_service/jobs.js | 3 +- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/x-pack/plugins/ml/server/models/job_audit_messages/job_audit_messages.js b/x-pack/plugins/ml/server/models/job_audit_messages/job_audit_messages.js index a2728d134a5a2..891990a84756a 100644 --- a/x-pack/plugins/ml/server/models/job_audit_messages/job_audit_messages.js +++ b/x-pack/plugins/ml/server/models/job_audit_messages/job_audit_messages.js @@ -103,34 +103,50 @@ export function jobAuditMessagesProvider(callWithRequest) { } // search highest, most recent audit messages for all jobs for the last 24hrs. - async function getAuditMessagesSummary(jobAggregationSize = 10) { + async function getAuditMessagesSummary(jobIds) { // TODO This is the current default value of the cluster setting `search.max_buckets`. // This should possibly consider the real settings in a future update. const maxBuckets = 10000; + let levelsPerJobAggSize = maxBuckets; try { + const query = { + bool: { + filter: [ + { + range: { + timestamp: { + gte: 'now-1d' + } + } + } + ] + } + }; + + // If the jobIds arg is supplied, add a query filter + // to only include those jobIds in the aggregations. + if (Array.isArray(jobIds) && jobIds.length > 0) { + query.bool.filter.push({ + terms: { + job_id: jobIds + } + }); + levelsPerJobAggSize = jobIds.length; + } + const resp = await callWithRequest('search', { index: ML_NOTIFICATION_INDEX_PATTERN, ignore_unavailable: true, rest_total_hits_as_int: true, size: 0, body: { - query: { - bool: { - filter: { - range: { - timestamp: { - gte: 'now-1d' - } - } - } - } - }, + query, aggs: { levelsPerJob: { terms: { field: 'job_id', - size: (Math.min(maxBuckets, jobAggregationSize) || 1), // don't allow a value of 0 + size: levelsPerJobAggSize }, aggs: { levels: { diff --git a/x-pack/plugins/ml/server/models/job_service/jobs.js b/x-pack/plugins/ml/server/models/job_service/jobs.js index 6faac1cdc4aa7..9aec992b7664f 100644 --- a/x-pack/plugins/ml/server/models/job_service/jobs.js +++ b/x-pack/plugins/ml/server/models/job_service/jobs.js @@ -90,7 +90,8 @@ export function jobsProvider(callWithRequest) { async function jobsSummary(jobIds = []) { const fullJobsList = await createFullJobsList(); - const auditMessages = await getAuditMessagesSummary(fullJobsList.length); + const fullJobsIds = fullJobsList.map(job => job.job_id); + const auditMessages = await getAuditMessagesSummary(fullJobsIds); const auditMessagesByJob = auditMessages.reduce((p, c) => { p[c.job_id] = c; return p; From 46175ce53412ed58f2642eef1503c8a77ce779a4 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Fri, 8 Feb 2019 13:53:41 +0100 Subject: [PATCH 2/2] [ML] Only add auditMessage to jobSummary if it's been created after job.create_time. --- x-pack/plugins/ml/server/models/job_service/jobs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/ml/server/models/job_service/jobs.js b/x-pack/plugins/ml/server/models/job_service/jobs.js index 9aec992b7664f..dd50f1511fc62 100644 --- a/x-pack/plugins/ml/server/models/job_service/jobs.js +++ b/x-pack/plugins/ml/server/models/job_service/jobs.js @@ -128,7 +128,7 @@ export function jobsProvider(callWithRequest) { tempJob.fullJob = job; } const auditMessage = auditMessagesByJob[tempJob.id]; - if (auditMessage !== undefined) { + if (auditMessage !== undefined && job.create_time <= auditMessage.msgTime) { tempJob.auditMessage = { level: auditMessage.highestLevel, text: auditMessage.highestLevelText