-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Metrics UI] Support percentage format in threshold alerts (#72701)
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
644e9c2
commit 0d8a461
Showing
6 changed files
with
232 additions
and
8 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
x-pack/plugins/infra/common/utils/corrected_percent_convert.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { decimalToPct, pctToDecimal } from './corrected_percent_convert'; | ||
|
||
describe('decimalToPct', () => { | ||
test('should retain correct floating point precision up to 10 decimal places', () => { | ||
// Most of these cases would still work fine just doing x * 100 instead of passing it through | ||
// decimalToPct, but the function still needs to work regardless | ||
expect(decimalToPct(0)).toBe(0); | ||
expect(decimalToPct(0.1)).toBe(10); | ||
expect(decimalToPct(0.01)).toBe(1); | ||
expect(decimalToPct(0.014)).toBe(1.4); | ||
expect(decimalToPct(0.0141)).toBe(1.41); | ||
expect(decimalToPct(0.01414)).toBe(1.414); | ||
// This case is known to fail without decimalToPct; vanilla JS 0.014141 * 100 === 1.4141000000000001 | ||
expect(decimalToPct(0.014141)).toBe(1.4141); | ||
expect(decimalToPct(0.0141414)).toBe(1.41414); | ||
expect(decimalToPct(0.01414141)).toBe(1.414141); | ||
expect(decimalToPct(0.014141414)).toBe(1.4141414); | ||
}); | ||
test('should also work with values greater than 1', () => { | ||
expect(decimalToPct(2)).toBe(200); | ||
expect(decimalToPct(2.1)).toBe(210); | ||
expect(decimalToPct(2.14)).toBe(214); | ||
expect(decimalToPct(2.14141414)).toBe(214.141414); | ||
}); | ||
}); | ||
|
||
describe('pctToDecimal', () => { | ||
test('should retain correct floating point precision up to 10 decimal places', () => { | ||
expect(pctToDecimal(0)).toBe(0); | ||
expect(pctToDecimal(10)).toBe(0.1); | ||
expect(pctToDecimal(1)).toBe(0.01); | ||
expect(pctToDecimal(1.4)).toBe(0.014); | ||
expect(pctToDecimal(1.41)).toBe(0.0141); | ||
expect(pctToDecimal(1.414)).toBe(0.01414); | ||
expect(pctToDecimal(1.4141)).toBe(0.014141); | ||
expect(pctToDecimal(1.41414)).toBe(0.0141414); | ||
expect(pctToDecimal(1.414141)).toBe(0.01414141); | ||
expect(pctToDecimal(1.4141414)).toBe(0.014141414); | ||
}); | ||
test('should also work with values greater than 100%', () => { | ||
expect(pctToDecimal(200)).toBe(2); | ||
expect(pctToDecimal(210)).toBe(2.1); | ||
expect(pctToDecimal(214)).toBe(2.14); | ||
expect(pctToDecimal(214.141414)).toBe(2.14141414); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
x-pack/plugins/infra/common/utils/corrected_percent_convert.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
const correctedPctConvert = (v: number, decimalToPct: boolean) => { | ||
// Correct floating point precision | ||
const replacementPattern = decimalToPct ? new RegExp(/0?\./) : '.'; | ||
const numberOfDigits = String(v).replace(replacementPattern, '').length; | ||
const multipliedValue = decimalToPct ? v * 100 : v / 100; | ||
return parseFloat(multipliedValue.toPrecision(numberOfDigits)); | ||
}; | ||
|
||
export const decimalToPct = (v: number) => correctedPctConvert(v, true); | ||
export const pctToDecimal = (v: number) => correctedPctConvert(v, false); |
84 changes: 84 additions & 0 deletions
84
x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_row.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { mountWithIntl, nextTick } from 'test_utils/enzyme_helpers'; | ||
import { MetricExpression } from '../types'; | ||
import React from 'react'; | ||
import { ExpressionRow } from './expression_row'; | ||
import { act } from 'react-dom/test-utils'; | ||
// eslint-disable-next-line @kbn/eslint/no-restricted-paths | ||
import { Comparator } from '../../../../server/lib/alerting/metric_threshold/types'; | ||
|
||
jest.mock('../../../containers/source/use_source_via_http', () => ({ | ||
useSourceViaHttp: () => ({ | ||
source: { id: 'default' }, | ||
createDerivedIndexPattern: () => ({ fields: [], title: 'metricbeat-*' }), | ||
}), | ||
})); | ||
|
||
describe('ExpressionRow', () => { | ||
async function setup(expression: MetricExpression) { | ||
const wrapper = mountWithIntl( | ||
<ExpressionRow | ||
canDelete={false} | ||
fields={[ | ||
{ name: 'system.cpu.user.pct', type: 'test' }, | ||
{ name: 'system.load.1', type: 'test' }, | ||
]} | ||
remove={() => {}} | ||
addExpression={() => {}} | ||
key={1} | ||
expressionId={1} | ||
setAlertParams={() => {}} | ||
errors={{ | ||
aggField: [], | ||
timeSizeUnit: [], | ||
timeWindowSize: [], | ||
}} | ||
expression={expression} | ||
/> | ||
); | ||
|
||
const update = async () => | ||
await act(async () => { | ||
await nextTick(); | ||
wrapper.update(); | ||
}); | ||
|
||
await update(); | ||
|
||
return { wrapper, update }; | ||
} | ||
|
||
it('should display thresholds as a percentage for pct metrics', async () => { | ||
const expression = { | ||
metric: 'system.cpu.user.pct', | ||
comparator: Comparator.GT, | ||
threshold: [0.5], | ||
timeSize: 1, | ||
timeUnit: 'm', | ||
aggType: 'avg', | ||
}; | ||
const { wrapper } = await setup(expression as MetricExpression); | ||
const [valueMatch] = wrapper.html().match('<span class="euiExpression__value">50</span>') ?? []; | ||
expect(valueMatch).toBeTruthy(); | ||
}); | ||
|
||
it('should display thresholds as a decimal for all other metrics', async () => { | ||
const expression = { | ||
metric: 'system.load.1', | ||
comparator: Comparator.GT, | ||
threshold: [0.5], | ||
timeSize: 1, | ||
timeUnit: 'm', | ||
aggType: 'avg', | ||
}; | ||
const { wrapper } = await setup(expression as MetricExpression); | ||
const [valueMatch] = | ||
wrapper.html().match('<span class="euiExpression__value">0.5</span>') ?? []; | ||
expect(valueMatch).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters