From 1855708c5ad9fa687ce11bfa993c2d1a415ec72e Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Mon, 28 Feb 2022 17:16:27 -0700 Subject: [PATCH 1/4] [Infrastructure UI] Optimization for Metric Threshold Rule for 7.17 --- .../metric_threshold/lib/evaluate_alert.ts | 58 ++++++++----------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts index a794bb3d60450..b3aae3b3ee909 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts @@ -7,7 +7,7 @@ import moment from 'moment'; import { ElasticsearchClient } from 'kibana/server'; -import { mapValues, first, last, isNaN, isNumber, isObject, has } from 'lodash'; +import { difference, mapValues, first, last, isNaN, isNumber, isObject, has } from 'lodash'; import { isTooManyBucketsPreviewException, TOO_MANY_BUCKETS_PREVIEW_EXCEPTION, @@ -97,33 +97,27 @@ export const evaluateAlert = !currentGroups.includes(g)); + const missingGroups = difference(prevGroups, currentGroups); if (currentGroups.length === 0 && missingGroups.length === 0) { missingGroups.push(UNGROUPED_FACTORY_KEY); } const backfillTimestamp = last(last(Object.values(currentValues)))?.key ?? new Date().toISOString(); - const backfilledPrevGroups: Record< - string, - Array<{ key: string; value: number }> - > = missingGroups.reduce( - (result, group) => ({ - ...result, - [group]: [ - { - key: backfillTimestamp, - value: criterion.aggType === Aggregators.COUNT ? 0 : null, - }, - ], - }), - {} - ); + const backfilledPrevGroups: Record> = {}; + for (const group of missingGroups) { + backfilledPrevGroups[group] = [ + { + key: backfillTimestamp, + value: criterion.aggType === Aggregators.COUNT ? 0 : null, + }, + ]; + } const currentValuesWithBackfilledPrevGroups = { ...currentValues, ...backfilledPrevGroups, }; - return mapValues( + const finalValues = mapValues( currentValuesWithBackfilledPrevGroups, (points: any[] | typeof NaN | null) => { if (isTooManyBucketsPreviewException(points)) throw points; @@ -141,6 +135,7 @@ export const evaluateAlert = ; doc_count: number }>; - const groupedResults = compositeBuckets.reduce( - (result, bucket) => ({ - ...result, - [Object.values(bucket.key) - .map((value) => value) - .join(', ')]: getValuesFromAggregations( - bucket, - aggType, - dropPartialBucketsOptions, - calculatedTimerange, - bucket.doc_count - ), - }), - {} - ); + const groupedResults: Record = {}; + for (const bucket of compositeBuckets) { + const key = Object.values(bucket.key).join(', '); + const value = getValuesFromAggregations( + bucket, + aggType, + dropPartialBucketsOptions, + calculatedTimerange, + bucket.doc_count + ); + groupedResults[key] = value; + } return groupedResults; } const { body: result } = await esClient.search({ From 847a2e1cabe2bcbf5012edf586355fae64a30ba0 Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Tue, 1 Mar 2022 07:59:06 -0700 Subject: [PATCH 2/4] Removing the unnecessary assignment --- .../server/lib/alerting/metric_threshold/lib/evaluate_alert.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts index b3aae3b3ee909..a6ded107fe6fe 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts @@ -117,7 +117,7 @@ export const evaluateAlert = { if (isTooManyBucketsPreviewException(points)) throw points; @@ -135,7 +135,6 @@ export const evaluateAlert = Date: Mon, 7 Mar 2022 15:32:16 -0700 Subject: [PATCH 3/4] Removing any type --- .../alerting/metric_threshold/lib/evaluate_alert.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts index a6ded107fe6fe..cefd1be40d628 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts @@ -139,6 +139,11 @@ export const evaluateAlert = Promise>> = async function ( +) => Promise< + Record | null | TooManyBucketsErrorObject> +> = async function ( esClient, params, index, @@ -194,13 +201,12 @@ const getMetric: ( (response) => response.aggregations?.groupings?.after_key ); const compositeBuckets = (await getAllCompositeData( - // @ts-expect-error @elastic/elasticsearch SearchResponse.body.timeout is not required (body) => esClient.search({ body, index }), searchBody, bucketSelector, afterKeyHandler )) as Array; doc_count: number }>; - const groupedResults: Record = {}; + const groupedResults: Record> = {}; for (const bucket of compositeBuckets) { const key = Object.values(bucket.key).join(', '); const value = getValuesFromAggregations( @@ -215,7 +221,6 @@ const getMetric: ( return groupedResults; } const { body: result } = await esClient.search({ - // @ts-expect-error buckets_path is not compatible with @elastic/elasticsearch body: searchBody, index, }); From e77dc1fac2cf6dbcefaa0cc13efbf8c13b2261c5 Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Tue, 8 Mar 2022 14:36:19 -0700 Subject: [PATCH 4/4] Revert "Removing any type" This reverts commit 9f73db7af39ae0ea95308fb6ba26030504cc4c22. --- .../alerting/metric_threshold/lib/evaluate_alert.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts index cefd1be40d628..a6ded107fe6fe 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts @@ -139,11 +139,6 @@ export const evaluateAlert = Promise< - Record | null | TooManyBucketsErrorObject> -> = async function ( +) => Promise>> = async function ( esClient, params, index, @@ -201,12 +194,13 @@ const getMetric: ( (response) => response.aggregations?.groupings?.after_key ); const compositeBuckets = (await getAllCompositeData( + // @ts-expect-error @elastic/elasticsearch SearchResponse.body.timeout is not required (body) => esClient.search({ body, index }), searchBody, bucketSelector, afterKeyHandler )) as Array; doc_count: number }>; - const groupedResults: Record> = {}; + const groupedResults: Record = {}; for (const bucket of compositeBuckets) { const key = Object.values(bucket.key).join(', '); const value = getValuesFromAggregations( @@ -221,6 +215,7 @@ const getMetric: ( return groupedResults; } const { body: result } = await esClient.search({ + // @ts-expect-error buckets_path is not compatible with @elastic/elasticsearch body: searchBody, index, });