Compare commits

...

3 Commits

Author SHA1 Message Date
Ashwin Bhatkal
ed85fed711 chore: resolve comments 2026-03-23 21:54:48 +05:30
Ashwin Bhatkal
194e8474b3 chore: fix tests 2026-03-23 21:54:48 +05:30
Ashwin Bhatkal
895ef98be4 fix: bugs in dashboard filter expression 2026-03-23 21:54:48 +05:30
3 changed files with 66 additions and 2 deletions

View File

@@ -1191,6 +1191,49 @@ describe('removeKeysFromExpression', () => {
expect(pairs).toHaveLength(2);
});
});
describe('Parenthesised expressions', () => {
it('should not leave a dangling AND when removing the last filter inside parens', () => {
const expression =
'(deployment.environment = $deployment.environment AND service.name = $service.name AND operation IN $top_level_operation)';
const result = removeKeysFromExpression(expression, ['operation'], true);
expect(result).toBe(
'(deployment.environment = $deployment.environment AND service.name = $service.name)',
);
});
it('should not leave a dangling AND when removing the first filter inside parens', () => {
const expression =
'(deployment.environment = $deployment.environment AND service.name = $service.name AND operation IN $top_level_operation)';
const result = removeKeysFromExpression(
expression,
['deployment.environment'],
true,
);
expect(result).toBe(
'(service.name = $service.name AND operation IN $top_level_operation)',
);
});
it('should not leave a dangling AND when removing a middle filter inside parens', () => {
const expression =
'(deployment.environment = $deployment.environment AND service.name = $service.name AND operation IN $top_level_operation)';
const result = removeKeysFromExpression(expression, ['service.name'], true);
expect(result).toBe(
'(deployment.environment = $deployment.environment AND operation IN $top_level_operation)',
);
});
it('should return empty parens when removing the only filter inside parens', () => {
const expression = '(operation IN $top_level_operation)';
const result = removeKeysFromExpression(expression, ['operation'], true);
expect(result).toBe('()');
});
});
});
describe('formatValueForExpression', () => {

View File

@@ -569,7 +569,7 @@ export const removeKeysFromExpression = (
const currentQueryPair = queryPairsMap.get(`${key}`.trim().toLowerCase());
if (currentQueryPair && currentQueryPair.isComplete) {
// Determine the start index of the query pair (fallback order: key → operator → value)
const queryPairStart =
let queryPairStart =
currentQueryPair.position.keyStart ??
currentQueryPair.position.operatorStart ??
currentQueryPair.position.valueStart;
@@ -587,6 +587,15 @@ export const removeKeysFromExpression = (
// If match is found, extend the queryPairEnd to include the matched part
queryPairEnd += match[0].length;
}
// If no following conjunction was absorbed (e.g. removed pair is last in expression),
// absorb the preceding AND/OR instead to avoid leaving a dangling conjunction
if (!match?.[3]) {
const beforePair = updatedExpression.slice(0, queryPairStart);
const precedingConjunctionMatch = beforePair.match(/\s+(AND|OR)\s+$/i);
if (precedingConjunctionMatch) {
queryPairStart -= precedingConjunctionMatch[0].length;
}
}
// Remove the full query pair (including any conjunction/whitespace) from the expression
updatedExpression = `${updatedExpression.slice(
0,

View File

@@ -3,6 +3,7 @@ import { useCallback } from 'react';
import { useAddDynamicVariableToPanels } from 'hooks/dashboard/useAddDynamicVariableToPanels';
import { updateLocalStorageDashboardVariable } from 'hooks/dashboard/useDashboardFromLocalStorage';
import { useUpdateDashboard } from 'hooks/dashboard/useUpdateDashboard';
import { useNotifications } from 'hooks/useNotifications';
import { IDashboardVariables } from 'providers/Dashboard/store/dashboardVariables/dashboardVariablesStoreTypes';
import { useDashboardStore } from 'providers/Dashboard/store/useDashboardStore';
import { IDashboardVariable } from 'types/api/dashboard/getAll';
@@ -51,6 +52,7 @@ export const useDashboardVariableUpdate = (): UseDashboardVariableUpdateReturn =
);
const addDynamicVariableToPanels = useAddDynamicVariableToPanels();
const updateMutation = useUpdateDashboard();
const { notifications } = useNotifications();
const onValueUpdate = useCallback(
(
@@ -180,6 +182,15 @@ export const useDashboardVariableUpdate = (): UseDashboardVariableUpdateReturn =
// Get current dashboard variables
const currentVariables = selectedDashboard.data.variables || {};
// Prevent duplicate variable names
const nameExists = Object.values(currentVariables).some(
(v) => v.name === name,
);
if (nameExists) {
notifications.error({ message: `Variable "${name}" already exists` });
return;
}
// Create tableRowData like Dashboard Settings does
const tableRowData = [];
const variableOrderArr = [];
@@ -232,7 +243,8 @@ export const useDashboardVariableUpdate = (): UseDashboardVariableUpdateReturn =
// Convert to dashboard format and update
const updatedVariables = convertVariablesToDbFormat(tableRowData);
updateVariables(updatedVariables, newVariable.id, [], false);
// Don't pass currentRequestedId — variable creation should not modify widget filters.
updateVariables(updatedVariables);
},
[selectedDashboard, updateVariables],
);