Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.13] [Custom threhsold] Fix preview chart for multi char aggregation name (#177163) #177186

Merged
merged 1 commit into from
Feb 19, 2024
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 @@ -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
Loading