[AO] Fix custom equations to support repeating the same variable (#166482)

## Summary

This PR fixes #166476 by fixing a bug where you can't repeat a variable
twice in the equation. This PR changes the code that converts the
variables from `A` to `params.A` by changing a `replace` to a
`replaceAll`.

The work around would be to duplicate the aggregation that needs to be
repeated so it has a unique variable name.

### Checklist

- [X] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Chris Cowan 2023-09-20 02:02:02 -06:00 committed by GitHub
parent 172de682c5
commit ca1df9ec4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 9 additions and 9 deletions

View file

@ -74,6 +74,6 @@ export const createCustomMetricsAggregations = (
const convertEquationToPainless = (bucketsPath: { [id: string]: string }, equation?: string) => {
const workingEquation = equation || Object.keys(bucketsPath).join(' + ');
return Object.keys(bucketsPath).reduce((acc, key) => {
return acc.replace(key, `params.${key}`);
return acc.replaceAll(key, `params.${key}`);
}, workingEquation);
};

View file

@ -74,6 +74,6 @@ export const createCustomMetricsAggregations = (
const convertEquationToPainless = (bucketsPath: { [id: string]: string }, equation?: string) => {
const workingEquation = equation || Object.keys(bucketsPath).join(' + ');
return Object.keys(bucketsPath).reduce((acc, key) => {
return acc.replace(key, `params.${key}`);
return acc.replaceAll(key, `params.${key}`);
}, workingEquation);
};

View file

@ -94,7 +94,7 @@ export default function ({ getService }: FtrProviderContext) {
{ name: 'A', field: 'system.network.in.bytes', aggType: Aggregators.AVERAGE },
{ name: 'B', field: 'system.network.out.bytes', aggType: Aggregators.AVERAGE },
],
equation: 'A / B ',
equation: '(A + A) / (B + B)',
},
],
alertOnNoData: true,
@ -187,7 +187,7 @@ export default function ({ getService }: FtrProviderContext) {
{ name: 'A', field: 'system.network.in.bytes', aggType: Aggregators.AVERAGE },
{ name: 'B', field: 'system.network.out.bytes', aggType: Aggregators.AVERAGE },
],
equation: 'A / B ',
equation: '(A + A) / (B + B)',
},
],
alertOnNoData: true,

View file

@ -153,7 +153,7 @@ export default function ({ getService }: FtrProviderContext) {
{ name: 'A', aggType: 'count', filter: 'event.dataset: "apache2.error"' },
{ name: 'B', aggType: 'count' },
],
equation: '(A / B) * 100',
equation: '((A + A) / (B + B)) * 100',
label: 'apache2 error ratio',
} as CustomMetricExpressionParams,
],
@ -187,7 +187,7 @@ export default function ({ getService }: FtrProviderContext) {
{ name: 'A', aggType: 'count', filter: 'event.dataset: "apache2.error"' },
{ name: 'B', aggType: 'count' },
],
equation: '(A / B) * 100',
equation: '((A + A) / (B + B)) * 100',
currentValue: 36.195262024407754,
timestamp: '2021-10-19T00:53:59.997Z',
shouldFire: true,

View file

@ -134,7 +134,7 @@ export default function ({ getService }: FtrProviderContext) {
{ name: 'A', aggregation: 'avg', field: 'system.cpu.user.pct' },
{ name: 'B', aggregation: 'avg', field: 'system.cpu.user.pct' },
],
equation: '(A + B) * 100',
equation: '((A + A + B + B) / 2) * 100',
},
],
};

View file

@ -95,7 +95,7 @@ export default function ({ getService }: FtrProviderContext) {
{ name: 'A', field: 'system.network.in.bytes', aggType: Aggregators.AVERAGE },
{ name: 'B', field: 'system.network.out.bytes', aggType: Aggregators.AVERAGE },
],
equation: 'A / B ',
equation: '((A + A) / (B + B))',
},
],
alertOnNoData: true,
@ -186,7 +186,7 @@ export default function ({ getService }: FtrProviderContext) {
{ name: 'A', field: 'system.network.in.bytes', aggType: Aggregators.AVERAGE },
{ name: 'B', field: 'system.network.out.bytes', aggType: Aggregators.AVERAGE },
],
equation: 'A / B ',
equation: '((A + A) / (B + B))',
},
],
alertOnNoData: true,