Skip to content

Commit

Permalink
[7.x] [Metrics UI] Fix No Data in Inventory alerts/Snapshot API (elas…
Browse files Browse the repository at this point in the history
…tic#72513) (elastic#73700)

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
Zacqary and elasticmachine authored Jul 31, 2020
1 parent c3dbbe5 commit 455835f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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;
5 changes: 3 additions & 2 deletions x-pack/plugins/infra/server/lib/snapshot/response_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 455835f

Please sign in to comment.