[Metrics UI] Fix No Data in Inventory alerts/Snapshot API (#72513)

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Zacqary Adam Xeper 2020-07-29 11:07:41 -05:00 committed by GitHub
parent 10cbaf5ca1
commit e202a830d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 3 deletions

View file

@ -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);

View file

@ -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;

View file

@ -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(