[Fleet] Fix CPU metrics display when percentage < 0.1 (#154160)

This commit is contained in:
Nicolas Chaulet 2023-03-31 10:59:14 -04:00 committed by GitHub
parent 4ccdea43ff
commit 95253d7cc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 4 deletions

View file

@ -0,0 +1,81 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React from 'react';
import { render } from '@testing-library/react';
import { formatAgentCPU } from './agent_metrics';
jest.mock('../components/metric_non_available', () => {
return {
MetricNonAvailable: () => <>N/A</>,
};
});
jest.mock('@elastic/eui', () => {
return {
...jest.requireActual('@elastic/eui'),
EuiToolTip: (props: any) => <div data-tooltip-content={props.content}>{props.children}</div>,
};
});
describe('Agent metrics helper', () => {
describe('formatAgentCPU', () => {
it('should return 0% if cpu is 0.00002', () => {
const res = formatAgentCPU({
cpu_avg: 0.00002,
memory_size_byte_avg: 2000,
});
const result = render(<>{res}</>);
expect(result.asFragment()).toMatchInlineSnapshot(`
<DocumentFragment>
<div
data-tooltip-content="0.0020 %"
>
0.00 %
</div>
</DocumentFragment>
`);
});
it('should return 5% if cpu is 0.005', () => {
const res = formatAgentCPU({
cpu_avg: 0.005,
memory_size_byte_avg: 2000,
});
const result = render(<>{res}</>);
expect(result.asFragment()).toMatchInlineSnapshot(`
<DocumentFragment>
<div
data-tooltip-content="0.5000 %"
>
0.50 %
</div>
</DocumentFragment>
`);
});
it('should return N/A if cpu is undefined', () => {
const res = formatAgentCPU({
cpu_avg: undefined,
memory_size_byte_avg: 2000,
});
const result = render(<>{res}</>);
expect(result.asFragment()).toMatchInlineSnapshot(`
<DocumentFragment>
N/A
</DocumentFragment>
`);
});
});
});

View file

@ -6,14 +6,17 @@
*/
import React from 'react';
import { EuiToolTip } from '@elastic/eui';
import type { AgentMetrics, AgentPolicy } from '../../../../../../common/types';
import { MetricNonAvailable } from '../components';
export function formatAgentCPU(metrics?: AgentMetrics, agentPolicy?: AgentPolicy) {
return metrics?.cpu_avg && metrics?.cpu_avg !== 0 ? (
`${(metrics.cpu_avg * 100).toFixed(2)} %`
return typeof metrics?.cpu_avg !== 'undefined' ? (
<EuiToolTip content={`${(metrics.cpu_avg * 100).toFixed(4)} %`}>
<>{(metrics.cpu_avg * 100).toFixed(2)} %</>
</EuiToolTip>
) : (
<MetricNonAvailable agentPolicy={agentPolicy} />
);

View file

@ -54,7 +54,7 @@ async function _fetchAndAssignAgentMetrics(esClient: ElasticsearchClient, agents
return {
...agent,
metrics: {
cpu_avg: results?.sum_cpu ? Math.trunc(results.sum_cpu * 10000) / 10000 : undefined,
cpu_avg: results?.sum_cpu ? Math.trunc(results.sum_cpu * 100000) / 100000 : undefined,
memory_size_byte_avg: results?.sum_memory_size
? Math.trunc(results?.sum_memory_size)
: undefined,

View file

@ -192,7 +192,7 @@ export default function ({ getService }: FtrProviderContext) {
const agent1: Agent = apiResponse.items.find((agent: any) => agent.id === 'agent1');
expect(agent1.metrics?.memory_size_byte_avg).to.eql('25510920');
expect(agent1.metrics?.cpu_avg).to.eql('0.0166');
expect(agent1.metrics?.cpu_avg).to.eql('0.01666');
const agent2: Agent = apiResponse.items.find((agent: any) => agent.id === 'agent2');
expect(agent2.metrics?.memory_size_byte_avg).equal(undefined);