diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts index 4d0c13e366f21..3b795810b39f0 100644 --- a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts +++ b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts @@ -112,6 +112,8 @@ const getData = async ( try { const { nodes } = await snapshot.getNodes(esClient, options); + if (!nodes.length) return { [UNGROUPED_FACTORY_KEY]: null }; // No Data state + return nodes.reduce((acc, n) => { const nodePathItem = last(n.path) as any; const m = first(n.metrics); diff --git a/x-pack/plugins/infra/server/lib/snapshot/response_helpers.test.ts b/x-pack/plugins/infra/server/lib/snapshot/response_helpers.test.ts index 20220aef1133d..74840afc157d2 100644 --- a/x-pack/plugins/infra/server/lib/snapshot/response_helpers.test.ts +++ b/x-pack/plugins/infra/server/lib/snapshot/response_helpers.test.ts @@ -4,7 +4,13 @@ * you may not use this file except in compliance with the Elastic License. */ -import { isIPv4, getIPFromBucket, InfraSnapshotNodeGroupByBucket } from './response_helpers'; +import { + isIPv4, + getIPFromBucket, + InfraSnapshotNodeGroupByBucket, + getMetricValueFromBucket, + InfraSnapshotMetricsBucket, +} from './response_helpers'; describe('InfraOps ResponseHelpers', () => { describe('isIPv4', () => { @@ -74,4 +80,40 @@ describe('InfraOps ResponseHelpers', () => { expect(getIPFromBucket('host', bucket)).toBe(null); }); }); + + describe('getMetricValueFromBucket', () => { + it('should return the value of a bucket with data', () => { + expect(getMetricValueFromBucket('custom', testBucket, 1)).toBe(0.5); + }); + it('should return the normalized value of a bucket with data', () => { + expect(getMetricValueFromBucket('cpu', testNormalizedBucket, 1)).toBe(50); + }); + it('should return null for a bucket with no data', () => { + expect(getMetricValueFromBucket('custom', testEmptyBucket, 1)).toBe(null); + }); + }); }); + +// Hack to get around TypeScript +const buckets = [ + { + key: 'a', + doc_count: 1, + custom_1: { + value: 0.5, + }, + }, + { + key: 'b', + doc_count: 1, + cpu: { + value: 0.5, + normalized_value: 50, + }, + }, + { + key: 'c', + doc_count: 0, + }, +] as InfraSnapshotMetricsBucket[]; +const [testBucket, testNormalizedBucket, testEmptyBucket] = buckets; diff --git a/x-pack/plugins/infra/server/lib/snapshot/response_helpers.ts b/x-pack/plugins/infra/server/lib/snapshot/response_helpers.ts index 317a7da95ce6b..646ce9f2409af 100644 --- a/x-pack/plugins/infra/server/lib/snapshot/response_helpers.ts +++ b/x-pack/plugins/infra/server/lib/snapshot/response_helpers.ts @@ -158,14 +158,15 @@ const findLastFullBucket = ( }, last(buckets)); }; -const getMetricValueFromBucket = ( +export const getMetricValueFromBucket = ( type: SnapshotMetricType, bucket: InfraSnapshotMetricsBucket, index: number ) => { const key = type === 'custom' ? `custom_${index}` : type; const metric = bucket[key]; - return (metric && (metric.normalized_value || metric.value)) || 0; + const value = metric && (metric.normalized_value || metric.value); + return isFinite(value) ? value : null; }; function calculateMax(