Skip to content

Commit

Permalink
[Custom threhsold] Fix preview chart for multi char aggregation name (#…
Browse files Browse the repository at this point in the history
…177163)

Fixes #176437

## Summary

Fixes preview chart for multi-char aggregation name

<img
src="https://github.com/elastic/kibana/assets/12370520/fc6b30d2-d6b2-4de4-b163-770e556e9bea"
width=500 />

(cherry picked from commit b8dded0)
  • Loading branch information
maryam-saeidi committed Feb 19, 2024
1 parent 67eae74 commit 7adf212
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ describe('PainlessTinyMathParser', () => {
// ✅ checked with Lens Formula editor
expect(parser.parse()).toEqual('100*average(system.cpu.system.pct)');
});
it('should parse a simple equation with multi-char aggregation name', () => {
const equation = '100 * ABC-abc';
const parser = new PainlessTinyMathParser({
equation,
aggMap: {
'ABC-abc': 'average(system.cpu.system.pct)',
},
});
expect(parser.parse()).toEqual('100*average(system.cpu.system.pct)');
});
it('should parse a simple equation with two aggregations A and B', () => {
const equation = '100 * A + B / 100';
const parser = new PainlessTinyMathParser({
Expand Down Expand Up @@ -162,6 +172,20 @@ describe('PainlessTinyMathParser', () => {
'ifelse(ifelse(average(system.cpu.system.pct)>0,0,1) + ifelse(average(system.cpu.user.pct)==10,0,1) * ifelse(average(system.cpu.system.pct)<200,0,1) + ifelse(average(system.cpu.user.pct)==2,1,0) > 0, 100, ifelse(average(system.cpu.system.pct)==10, 200, 300))'
);
});
it('should parse a complex equation with multi char aggregation name', () => {
const equation =
'!(aa > 0) || baa !== 10 && !(aa < 200 || baa == 2) ? 100 : aa == 10 ? 200 : 300';
const parser = new PainlessTinyMathParser({
equation,
aggMap: {
aa: 'average(system.cpu.system.pct)',
baa: 'average(system.cpu.user.pct)',
},
});
expect(parser.parse()).toEqual(
'ifelse(ifelse(average(system.cpu.system.pct)>0,0,1) + ifelse(average(system.cpu.user.pct)==10,0,1) * ifelse(average(system.cpu.system.pct)<200,0,1) + ifelse(average(system.cpu.user.pct)==2,1,0) > 0, 100, ifelse(average(system.cpu.system.pct)==10, 200, 300))'
);
});

it('should parse a complex equation with many nested conditions and many aggregations', () => {
const equation =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,16 @@ export class PainlessTinyMathParser {
}

replaceCharactersWithAggMap(inputString: string, aggMap: AggMap): string {
// Use a regular expression to match any character from 'A' to 'Z'
const regex = /[A-Z]/g;

return inputString.replace(regex, (match) => {
const replacement = aggMap[match]; // Get the replacement from the map
return replacement ? replacement : match; // Use the replacement or the original character
});
let parsedInputString = inputString;
// Iterate over aggregation names and replace them with equation
Object.keys(aggMap)
.sort()
.reverse()
.forEach((metricName) => {
parsedInputString = parsedInputString.replaceAll(metricName, aggMap[metricName]);
});

return parsedInputString;
}

parseCondition(condition: string): string {
Expand Down

0 comments on commit 7adf212

Please sign in to comment.