[bugfix] Fix incorrect expression parsing in alert setting component (#3497)

Co-authored-by: Calvin <zhengqiwei@apache.org>
Co-authored-by: tomsun28 <tomsun28@outlook.com>
This commit is contained in:
Yang Chen
2025-06-24 22:56:16 +08:00
committed by GitHub
co-authored by Calvin tomsun28
parent 198c190ca7
commit e45c31abfb
@@ -959,7 +959,6 @@ export class AlertSettingComponent implements OnInit {
selectedLabels: Set<string> = new Set(); // Selected labels
inputLabelValue = '';
labelInputElement!: ElementRef<HTMLInputElement>;
monitorDataByLabel: any[] = [];
$asTransferItems = (data: unknown): TransferItem[] => data as TransferItem[];
onConnectModalCancel() {
@@ -1000,7 +999,6 @@ export class AlertSettingComponent implements OnInit {
handleDeleteLabel(removedLabel: string): void {
this.selectedLabels.delete(removedLabel);
this.showMonitorByLabel();
}
showLabelInput(): void {
@@ -1014,23 +1012,10 @@ export class AlertSettingComponent implements OnInit {
if (this.inputLabelValue && !this.selectedLabels.has(this.inputLabelValue)) {
this.selectedLabels.add(this.inputLabelValue);
}
this.showMonitorByLabel();
this.inputLabelValue = '';
this.labelInputVisible = false;
}
showMonitorByLabel(): void {
const monitorsByLabel = this.transferData.filter(item => item.labels.some((label: string) => this.selectedLabels.has(label)));
this.monitorDataByLabel = monitorsByLabel.map(item => {
return {
key: item.key,
title: item.title,
description: item.description,
labels: item.labels
};
});
}
onFilterLabelsChange(newLabels: string[], direction: string): void {
if (direction == 'left') {
this.leftFilterLabels = newLabels;
@@ -1188,8 +1173,8 @@ export class AlertSettingComponent implements OnInit {
.replace(/^\s*&&\s*/, '')
.replace(/\s*&&\s*$/, '')
// Remove monitor label binding expressions
.replace(/&&\s*\(?(equals\(__labels__,\s*"[^"]+"\)(\s*or\s*equals\(__labels__,\s*"[^"]+"\))*)\)?/, '')
.replace(/\(?(equals\(__labels__,\s*"[^"]+"\)(\s*or\s*equals\(__labels__,\s*"[^"]+"\))*)\)?\s*&&\s*/, '')
.replace(/&&\s*\(?(contains\(__labels__,\s*"[^"]+"\)(\s*or\s*contains\(__labels__,\s*"[^"]+"\))*)\)?/, '')
.replace(/\(?(contains\(__labels__,\s*"[^"]+"\)(\s*or\s*contains\(__labels__,\s*"[^"]+"\))*)\)?\s*&&\s*/, '')
);
}
@@ -1301,7 +1286,7 @@ export class AlertSettingComponent implements OnInit {
// Parse label from expression
private parseLabelFromExpr(expr: string) {
const labelPattern = /equals\(__labels__,\s*"([^"]+)"\)/g;
const labelPattern = /contains\(__labels__,\s*"([^"]+)"\)/g;
let match;
this.selectedLabels.clear();
while ((match = labelPattern.exec(expr)) !== null) {
@@ -1424,4 +1409,15 @@ export class AlertSettingComponent implements OnInit {
onTextareaDragLeave(event: DragEvent): void {
(event.target as HTMLElement).classList.remove('drag-over');
}
get monitorDataByLabel(): any[] {
return this.transferData
.filter(item => item.labels.some((label: string) => this.selectedLabels.has(label)))
.map(item => ({
key: item.key,
title: item.title,
description: item.description,
labels: item.labels
}));
}
}