Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

Fix: dangling and syntax in dashboard filter ( #10674) (#11243)

Rinky Devi2517f69b655a
diff --git a/frontend/public/locales/en-GB/dashboard.json b/frontend/public/locales/en-GB/dashboard.json
index ec804ccfe8..231251e389 100644
--- a/frontend/public/locales/en-GB/dashboard.json
+++ b/frontend/public/locales/en-GB/dashboard.json
@@ -26,5 +26,6 @@
"dashboard_unsave_changes": "There are unsaved changes in the Query builder, please stage and run the query or the changes will be lost. Press OK to discard.",
"dashboard_save_changes": "Your graph built with {{queryTag}} query will be saved. Press OK to confirm.",
"your_graph_build_with": "Your graph built with",
- "dashboard_ok_confirm": "query will be saved. Press OK to confirm."
+ "dashboard_ok_confirm": "query will be saved. Press OK to confirm.",
+ "variable_name_already_exists": "Variable \"{{name}}\" already exists"
}
diff --git a/frontend/public/locales/en/dashboard.json b/frontend/public/locales/en/dashboard.json
index 26e8f289a9..ecb25b0dd5 100644
--- a/frontend/public/locales/en/dashboard.json
+++ b/frontend/public/locales/en/dashboard.json
@@ -30,5 +30,6 @@
"dashboard_unsave_changes": "There are unsaved changes in the Query builder, please stage and run the query or the changes will be lost. Press OK to discard.",
"dashboard_save_changes": "Your graph built with {{queryTag}} query will be saved. Press OK to confirm.",
"your_graph_build_with": "Your graph built with",
- "dashboard_ok_confirm": "query will be saved. Press OK to confirm."
+ "dashboard_ok_confirm": "query will be saved. Press OK to confirm.",
+ "variable_name_already_exists": "Variable \"{{name}}\" already exists"
}
diff --git a/frontend/src/components/QueryBuilderV2/__tests__/utils.test.ts b/frontend/src/components/QueryBuilderV2/__tests__/utils.test.ts
index 0d77c05aeb..1fd2164c21 100644
--- a/frontend/src/components/QueryBuilderV2/__tests__/utils.test.ts
+++ b/frontend/src/components/QueryBuilderV2/__tests__/utils.test.ts
@@ -1155,7 +1155,7 @@ describe('removeKeysFromExpression', () => {
});
describe('Real-world scenarios', () => {
- it('should handle multiple variable instances of same key', () => {
+ it('should remove at most one variable expression per key', () => {
const expression =
"deployment.environment = $env1 deployment.environment = $env2 deployment.environment = 'default'";
const result = removeKeysFromExpression(
@@ -1164,9 +1164,11 @@ describe('removeKeysFromExpression', () => {
true,
);
- // Should remove one occurence as this case in itself is invalid to have multiple variable expressions for the same key
+ // Should remove one occurrence — having multiple $-value clauses for the
+ // same key is invalid. The first is removed; subsequent $ clauses and
+ // literal-value clauses are preserved.
expect(result).toBe(
- "deployment.environment = $env1 deployment.environment = 'default'",
+ "deployment.environment = $env2 AND deployment.environment = 'default'",
);
});
@@ -1199,6 +1201,186 @@ describe('removeKeysFromExpression', () => {
expect(pairs).toHaveLength(2);
});
});
+
+ describe('ANTLR-based removal — operator precedence (AND binds tighter than OR)', () => {
+ it('preserves OR when removing from a mixed AND/OR expression', () => {
+ // a AND b OR c — grammar parses as (a AND b) OR c
+ // removing b collapses the AND group to just a, OR is preserved
+ expect(
+ removeKeysFromExpression("a = '1' AND b = '2' OR c = '3'", ['b']),
+ ).toBe("a = '1' OR c = '3'");
+ });
+
+ it('preserves correct conjunctions in a four-term mixed expression', () => {
+ // a AND b OR c AND d — removing b collapses first AND group to a
+ expect(
+ removeKeysFromExpression("a = '1' AND b = '2' OR c = '3' AND d = '4'", [
+ 'b',
+ ]),
+ ).toBe("a = '1' OR c = '3' AND d = '4'");
+ });
+
+ it('preserves correct conjunctions when removing from a trailing AND group', () => {
+ // a OR b AND c OR d — removing c collapses second AND group to b
+ expect(
+ removeKeysFromExpression("a = '1' OR b = '2' AND c = '3' OR d = '4'", [
+ 'c',
+ ]),
+ ).toBe("a = '1' OR b = '2' OR d = '4'");
+ });
+ });
+
+ describe('ANTLR-based removal — parenthesised expressions', () => {
+ it('removes last clause without leaving a dangling AND', () => {
+ const expression =
+ '(deployment.environment = $deployment.environment AND service.name = $service.name AND top_level_operation IN [$top_level_operation])';
+ expect(
+ removeKeysFromExpression(expression, ['top_level_operation'], true),
+ ).toBe(
+ '(deployment.environment = $deployment.environment AND service.name = $service.name)',
+ );
+ });
+
+ it('removes first clause without leaving a dangling AND', () => {
+ expect(
+ removeKeysFromExpression(
+ '(deployment.environment = $deployment.environment AND service.name = $service.name)',
+ ['deployment.environment'],
+ true,
+ ),
+ ).toBe('service.name = $service.name');
+ });
+
+ it('removes middle clause without disturbing surrounding AND', () => {
+ expect(
+ removeKeysFromExpression(
+ '(deployment.environment = $deployment.environment AND service.name = $service.name AND region = $region)',
+ ['service.name'],
+ true,
+ ),
+ ).toBe(
+ '(deployment.environment = $deployment.environment AND region = $region)',
+ );
+ });
+
+ it('drops the empty paren group when its only child is removed', () => {
+ // (a) OR (b) — removing a must not leave () OR (b = '2')
+ // The remaining single-clause group has its redundant parens stripped too
+ expect(removeKeysFromExpression("(a = '1') OR (b = '2')", ['a'])).toBe(
+ "b = '2'",
+ );
+ });
+
+ it('handles OR inside parentheses without leaving dangling OR', () => {
+ expect(
+ removeKeysFromExpression(
+ '(service.name = $service.name OR operation = $operation)',
+ ['operation'],
+ true,
+ ),
+ ).toBe('service.name = $service.name');
+ });
+ });
+
+ describe('ANTLR-based removal — BETWEEN, EXISTS, and other operators', () => {
+ it('removes a BETWEEN clause without treating its AND as a conjunction', () => {
+ // BETWEEN x AND y — the AND is part of the operator, not a conjunction
+ expect(
+ removeKeysFromExpression("a BETWEEN 1 AND 10 AND b = '2'", ['a']),
+ ).toBe("b = '2'");
+ });
+
+ it('removes a NOT BETWEEN clause without treating its AND as a conjunction', () => {
+ expect(
+ removeKeysFromExpression("a NOT BETWEEN 1 AND 10 AND b = '2'", ['a']),
+ ).toBe("b = '2'");
+ });
+
+ it('removes an EXISTS clause (no value token)', () => {
+ expect(removeKeysFromExpression("a = '1' AND b EXISTS", ['b'])).toBe(
+ "a = '1'",
+ );
+ });
+
+ it('removes a NOT EXISTS clause', () => {
+ expect(removeKeysFromExpression("a = '1' AND b NOT EXISTS", ['b'])).toBe(
+ "a = '1'",
+ );
+ });
+
+ it('removes an IN clause correctly', () => {
+ expect(
+ removeKeysFromExpression("service IN ['api', 'web'] AND status = 'ok'", [
+ 'service',
+ ]),
+ ).toBe("status = 'ok'");
+ });
+
+ it('removes a NOT IN clause correctly', () => {
+ expect(
+ removeKeysFromExpression(
+ "service NOT IN ['api', 'web'] AND status = 'ok'",
+ ['service'],
+ ),
+ ).toBe("status = 'ok'");
+ });
+
+ it('removes a CONTAINS clause correctly', () => {
+ expect(
+ removeKeysFromExpression("message CONTAINS 'error' AND service = 'api'", [
+ 'message',
+ ]),
+ ).toBe("service = 'api'");
+ });
+
+ it('removes a LIKE clause correctly', () => {
+ expect(
+ removeKeysFromExpression("message LIKE '%error%' AND service = 'api'", [
+ 'message',
+ ]),
+ ).toBe("service = 'api'");
+ });
+
+ it('removes a NOT LIKE clause correctly', () => {
+ expect(
+ removeKeysFromExpression("message NOT LIKE '%error%' AND service = 'api'", [
+ 'message',
+ ]),
+ ).toBe("service = 'api'");
+ });
+ });
+
+ describe('ANTLR-based removal — AND/OR precedence combinations', () => {
+ it('handles a AND b AND c OR d: removing b leaves a AND c OR d', () => {
+ // AND binds tighter than OR, so this parses as (a AND b AND c) OR d
+ expect(
+ removeKeysFromExpression("a = '1' AND b = '2' AND c = '3' OR d = '4'", [
+ 'b',
+ ]),
+ ).toBe("a = '1' AND c = '3' OR d = '4'");
+ });
+ });
+
+ describe('ANTLR-based removal — deeply nested expressions', () => {
+ const nestedExpr =
+ "((deployment.environment = $env1 OR deployment.environment = 'default') AND service.name = $svc1)";
+
+ it('removes service.name variable — outer and inner single-child parens both drop', () => {
+ // After removal: inner OR group keeps parens (2 items), outer group drops
+ // parens (1 item remains)
+ expect(removeKeysFromExpression(nestedExpr, ['service.name'], true)).toBe(
+ "(deployment.environment = $env1 OR deployment.environment = 'default')",
+ );
+ });
+
+ it('removes deployment.environment variable — inner OR collapses, outer parens kept', () => {
+ // Only the $env1 variable clause is removed; 'default' literal stays.
+ // Inner paren drops (single item left), outer paren stays (2 AND items remain).
+ expect(
+ removeKeysFromExpression(nestedExpr, ['deployment.environment'], true),
+ ).toBe("(deployment.environment = 'default' AND service.name = $svc1)");
+ });
+ });
});
describe('formatValueForExpression', () => {
diff --git a/frontend/src/components/QueryBuilderV2/utils.ts b/frontend/src/components/QueryBuilderV2/utils.ts
index 302f7fc308..0ce347391b 100644
--- a/frontend/src/components/QueryBuilderV2/utils.ts
+++ b/frontend/src/components/QueryBuilderV2/utils.ts
@@ -1,4 +1,7 @@
/* eslint-disable sonarjs/cognitive-complexity */
+import { CharStreams, CommonTokenStream, ParserRuleContext } from 'antlr4';
+import { cloneDeep, isEqual, sortBy } from 'lodash-es';
+import { v4 as uuid } from 'uuid';
import { createAggregation } from 'api/v5/queryRange/prepareQueryRangePayloadV5';
import {
DEPRECATED_OPERATORS_MAP,
@@ -6,7 +9,16 @@ import {
QUERY_BUILDER_FUNCTIONS,
} from 'constants/antlrQueryConstants';
import { getOperatorValue } from 'container/QueryBuilder/filters/QueryBuilderSearch/utils';
-import { cloneDeep, isEqual, sortBy } from 'lodash-es';
+import FilterQueryLexer from 'parser/FilterQueryLexer';
+import FilterQueryParser, {
+ AndExpressionContext,
+ ComparisonContext,
+ InClauseContext,
+ NotInClauseContext,
+ OrExpressionContext,
+ PrimaryContext,
+ UnaryExpressionContext,
+} from 'parser/FilterQueryParser';
import { IQueryPair } from 'types/antlrQueryTypes';
import { BaseAutocompleteData } from 'types/api/queryBuilder/queryAutocompleteResponse';
import {
@@ -26,7 +38,6 @@ import { DataSource, ReduceOperators } from 'types/common/queryBuilder';
import { extractQueryPairs } from 'utils/queryContextUtils';
import { isQuoted, unquote } from 'utils/stringUtils';
import { isFunctionOperator, isNonValueOperator } from 'utils/tokenUtils';
-import { v4 as uuid } from 'uuid';
/**
* Check if an operator requires array values (like IN, NOT IN)
@@ -513,97 +524,201 @@ export const convertFiltersToExpressionWithExistingQuery = (
};
/**
- * Removes specified key-value pairs from a logical query expression string.
+ * Removes clauses for specified keys from a filter query expression.
*
- * This function parses the given query expression and removes any query pairs
- * whose keys match those in the `keysToRemove` array. It also removes any trailing
- * logical conjunctions (e.g., `AND`, `OR`) and whitespace that follow the matched pairs,
- * ensuring that the resulting expression remains valid and clean.
+ * Uses an ANTLR parse-tree traversal over the existing FilterQuery grammar so that
+ * compound predicates like `BETWEEN x AND y`, `EXISTS`, and `IN (...)` are treated
+ * as atomic nodes — their internal tokens are never confused with top-level AND/OR
+ * conjunctions. Surviving siblings are rejoined with the correct operator at each
+ * level of the tree, producing no dangling operators regardless of expression shape.
+ * If the expression cannot be parsed, it is returned unchanged.
*
- * @param expression - The full query string.
- * @param keysToRemove - An array of keys (case-insensitive) that should be removed from the expression.
- * @param removeOnlyVariableExpressions - When true, only removes key-value pairs where the value is a variable (starts with $). When false, uses the original behavior.
- * @returns A new expression string with the specified keys and their associated clauses removed.
+ * @param expression - The full filter query string.
+ * @param keysToRemove - Keys (case-insensitive) whose clauses should be dropped.
+ * @param removeOnlyVariableExpressions - Controls which clauses are eligible for removal:
+ * - `false` (default): removes all clauses for the key regardless of value.
+ * - `true`: removes only the first clause whose value contains any `$`.
+ * - `string` (e.g. `"$service.name"`): removes only the clause whose value exactly
+ * matches that string — preferred when the specific variable reference is known.
+ * @returns The rewritten expression, or an empty string if all clauses were removed.
*/
export const removeKeysFromExpression = (
expression: string,
keysToRemove: string[],
- removeOnlyVariableExpressions = false,
+ removeOnlyVariableExpressions: string | boolean = false,
): string => {
if (!keysToRemove || keysToRemove.length === 0) {
return expression;
}
+ if (!expression.trim()) {
+ return expression;
+ }
+
+ const keysSet = new Set(keysToRemove.map((k) => k.trim().toLowerCase()));
+ // Tracks keys for which a variable expression has already been removed.
+ // Having multiple $-value clauses for the same key is invalid; we remove at most one.
+ const removedVariableKeys = new Set<string>();
+
+ const chars = CharStreams.fromString(expression);
+ const lexer = new FilterQueryLexer(chars);
+ lexer.removeErrorListeners();
+ const tokenStream = new CommonTokenStream(lexer);
+ const parser = new FilterQueryParser(tokenStream);
+ parser.removeErrorListeners();
+
+ const tree = parser.query();
+
+ // If the expression couldn't be parsed, return it unchanged rather than mangling it
+ if (parser.syntaxErrorsCount > 0) {
+ return expression;
+ }
+
+ // Extract original source text for a node, preserving the user's exact formatting
+ const src = (ctx: ParserRuleContext): string =>
+ expression.slice(ctx.start.start, (ctx.stop ?? ctx.start).stop + 1);
+
+ // Returns null when the entire node should be dropped.
+ // isSingle = true means the result is a single, non-compound expression at
+ // this level (no AND/OR between sibling clauses), which lets the paren
+ // visitor decide whether wrapping is still needed.
+ type VisitResult = { text: string; isSingle: boolean } | null;
+
+ function visitOrExpression(ctx: OrExpressionContext): VisitResult {
+ const parts = ctx
+ .andExpression_list()
+ .map(visitAndExpression)
+ .filter((r): r is NonNullable<VisitResult> => r !== null);
+ if (parts.length === 0) {
+ return null;
+ }
+ // Single surviving branch — pass its isSingle straight through so the
+ // paren visitor can decide whether to keep the outer parens.
+ if (parts.length === 1) {
+ return parts[0];
+ }
+ return { text: parts.map((p) => p.text).join(' OR '), isSingle: false };
+ }
+
+ function visitAndExpression(ctx: AndExpressionContext): VisitResult {
+ const parts = ctx
+ .unaryExpression_list()
+ .map(visitUnaryExpression)
+ .filter((r): r is NonNullable<VisitResult> => r !== null);
+ if (parts.length === 0) {
+ return null;
+ }
+ if (parts.length === 1) {
+ return { text: parts[0].text, isSingle: true };
+ }
+ return { text: parts.map((p) => p.text).join(' AND '), isSingle: false };
+ }
+
+ function visitUnaryExpression(ctx: UnaryExpressionContext): VisitResult {
+ const primaryResult = visitPrimary(ctx.primary());
+ if (primaryResult === null) {
+ return null;
+ }
+ return ctx.NOT()
+ ? { text: `NOT ${primaryResult.text}`, isSingle: true }
+ : primaryResult;
+ }
+
+ function visitPrimary(ctx: PrimaryContext): VisitResult {
+ // Parenthesised sub-expression: ( orExpression )
+ const orCtx = ctx.orExpression();
+ if (orCtx) {
+ const inner = visitOrExpression(orCtx);
+ if (inner === null) {
+ return null;
+ }
+ // Drop redundant parens when the group collapses to a single clause;
+ // keep them when multiple clauses remain (operator-precedence matters).
+ if (inner.isSingle) {
+ return { text: inner.text, isSingle: true };
+ }
+ return { text: `(${inner.text})`, isSingle: true };
+ }
+
+ const compCtx = ctx.comparison();
+ if (compCtx) {
+ const result = visitComparison(compCtx);
+ return result !== null ? { text: result, isSingle: true } : null;
+ }
+
+ // functionCall, fullText, bare key, bare value — keep verbatim
+ return { text: src(ctx), isSingle: true };
+ }
+
+ function visitComparison(ctx: ComparisonContext): string | null {
+ const keyText = ctx.key().getText().trim().toLowerCase();
+
+ if (!keysSet.has(keyText)) {
+ return src(ctx);
+ }
- let updatedExpression = expression;
-
- if (updatedExpression) {
- keysToRemove.forEach((key) => {
- // Extract key-value query pairs from the expression
- const existingQueryPairs = extractQueryPairs(updatedExpression);
-
- let queryPairsMap: Map<string, IQueryPair>;
-
- if (existingQueryPairs.length > 0) {
- // Filter query pairs based on the removeOnlyVariableExpressions flag
- const filteredQueryPairs = removeOnlyVariableExpressions
- ? existingQueryPairs.filter((pair) => {
- const pairKey = pair.key?.trim().toLowerCase();
- const matchesKey = pairKey === `${key}`.trim().toLowerCase();
- if (!matchesKey) {
- return false;
- }
- const value = pair.value?.toString().trim();
- return value && value.includes('$');
- })
- : existingQueryPairs;
-
- // Build a map for quick lookup of query pairs by their lowercase trimmed keys
- queryPairsMap = new Map(
- filteredQueryPairs.map((pair) => {
- const key = pair.key.trim().toLowerCase();
- return [key, pair];
- }),
- );
-
- // Lookup the current query pair using the attribute key (case-insensitive)
- 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 =
- currentQueryPair.position.keyStart ??
- currentQueryPair.position.operatorStart ??
- currentQueryPair.position.valueStart;
- // Determine the end index of the query pair (fallback order: value → operator → key)
- let queryPairEnd =
- currentQueryPair.position.valueEnd ??
- currentQueryPair.position.operatorEnd ??
- currentQueryPair.position.keyEnd;
- // Get the part of the expression that comes after the current query pair
- const expressionAfterPair = `${updatedExpression.slice(queryPairEnd + 1)}`;
- // Match optional spaces and an optional conjunction (AND/OR), case-insensitive
- const conjunctionOrSpacesRegex = /^(\s*((AND|OR)\s+)?)/i;
- const match = expressionAfterPair.match(conjunctionOrSpacesRegex);
- if (match && match.length > 0) {
- // If match is found, extend the queryPairEnd to include the matched part
- queryPairEnd += match[0].length;
+ if (removeOnlyVariableExpressions) {
+ // Scope the value check to value nodes only — not the full comparison text —
+ // so a key that contains '$' does not trigger removal when the value is a
+ // literal. The ANTLR4 runtime returns null from getTypedRuleContext when a
+ // rule is absent, despite the non-nullable TypeScript signatures.
+ const inCtx = ctx.inClause() as unknown as InClauseContext | null;
+ const notInCtx = ctx.notInClause() as unknown as NotInClauseContext | null;
+ // When a specific variable string is supplied, require an exact match so we
+ // never accidentally remove a different $-valued clause for the same key.
+ const matchesVariable = (text: string): boolean =>
+ typeof removeOnlyVariableExpressions === 'string'
+ ? text === removeOnlyVariableExpressions
+ : text.includes('$');
+
+ const valueHasVariable = (): boolean => {
+ // Simple comparisons: key = $var, BETWEEN $v1 AND $v2, etc.
+ if (ctx.value_list().some((v) => matchesVariable(v.getText()))) {
+ return true;
+ }
+ // IN $var (bare single value) or IN ($v1, $v2) (value list)
+ if (inCtx) {
+ const bare = inCtx.value() as unknown as { getText(): string } | null;
+ if (bare && matchesVariable(bare.getText())) {
+ return true;
+ }
+ const list = inCtx.valueList() as unknown as {
+ value_list(): { getText(): string }[];
+ } | null;
+ if (list && list.value_list().some((v) => matchesVariable(v.getText()))) {
+ return true;
+ }
+ }
+ // NOT IN $var or NOT IN ($v1, $v2)
+ if (notInCtx) {
+ const bare = notInCtx.value() as unknown as { getText(): string } | null;
+ if (bare && matchesVariable(bare.getText())) {
+ return true;
+ }
+ const list = notInCtx.valueList() as unknown as {
+ value_list(): { getText(): string }[];
+ } | null;
+ if (list && list.value_list().some((v) => matchesVariable(v.getText()))) {
+ return true;
}
- // Remove the full query pair (including any conjunction/whitespace) from the expression
- updatedExpression = `${updatedExpression.slice(
- 0,
- queryPairStart,
- )}${updatedExpression.slice(queryPairEnd + 1)}`.trim();
}
+ return false;
+ };
+
+ if (valueHasVariable()) {
+ if (removedVariableKeys.has(keyText)) {
+ return src(ctx);
+ }
+ removedVariableKeys.add(keyText);
+ return null;
}
- });
+ return src(ctx);
+ }
- // Clean up any remaining trailing AND/OR operators and extra whitespace
- updatedExpression = updatedExpression
- .replace(/\s+(AND|OR)\s*$/i, '') // Remove trailing AND/OR
- .replace(/^(AND|OR)\s+/i, '') // Remove leading AND/OR
- .trim();
+ return null;
}
- return updatedExpression;
+ const result = visitOrExpression(tree.expression().orExpression());
+ return result?.text ?? '';
};
/**
diff --git a/frontend/src/container/DashboardContainer/DashboardVariablesSelection/useDashboardVariableUpdate.ts b/frontend/src/container/DashboardContainer/DashboardVariablesSelection/useDashboardVariableUpdate.ts
index 5ee6601e2a..96ecd75b5d 100644
--- a/frontend/src/container/DashboardContainer/DashboardVariablesSelection/useDashboardVariableUpdate.ts
+++ b/frontend/src/container/DashboardContainer/DashboardVariablesSelection/useDashboardVariableUpdate.ts
@@ -1,5 +1,7 @@
/* eslint-disable sonarjs/cognitive-complexity */
import { useCallback } from 'react';
+import { useTranslation } from 'react-i18next';
+import { toast } from '@signozhq/ui/sonner';
import { useAddDynamicVariableToPanels } from 'hooks/dashboard/useAddDynamicVariableToPanels';
import { updateLocalStorageDashboardVariable } from 'hooks/dashboard/useDashboardFromLocalStorage';
import { useUpdateDashboard } from 'hooks/dashboard/useUpdateDashboard';
@@ -48,6 +50,7 @@ export const useDashboardVariableUpdate =
);
const addDynamicVariableToPanels = useAddDynamicVariableToPanels();
const updateMutation = useUpdateDashboard();
+ const { t } = useTranslation('dashboard');
const onValueUpdate = useCallback(
(
@@ -177,6 +180,14 @@ export const useDashboardVariableUpdate =
// Get current dashboard variables
const currentVariables = dashboardData.data.variables || {};
+ const nameExists = Object.values(currentVariables).some(
+ (v) => v.name === name,
+ );
+ if (nameExists) {
+ toast.error(t('variable_name_already_exists', { name, ns: 'dashboard' }));
+ return;
+ }
+
// Create tableRowData like Dashboard Settings does
const tableRowData = [];
const variableOrderArr = [];
@@ -202,21 +213,20 @@ export const useDashboardVariableUpdate =
// Create new variable
const nextOrder =
variableOrderArr.length > 0 ? Math.max(...variableOrderArr) + 1 : 0;
- const newVariable: any = {
+ const newVariable: IDashboardVariable = {
id: uuidv4(),
name,
- type: 'DYNAMIC' as const,
+ type: 'DYNAMIC',
description,
order: nextOrder,
selectedValue: value,
allSelected: false,
haveCustomValuesSelected: false,
- sort: 'ASC' as const,
+ sort: 'ASC',
multiSelect: true,
showALLOption: true,
dynamicVariablesAttribute: name,
dynamicVariablesSource: source,
- dynamicVariablesWidgetIds: [],
queryValue: '',
};