Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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