-
Notifications
You must be signed in to change notification settings - Fork 22
238 fix interpolation of multi variables #412
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
Merged
arturminchukov
merged 7 commits into
main
from
238-incorrect-interpolation-of-multi-value-variable-in-regex
Oct 29, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6b03e73
fix interpolation of multi-value query variables by expanding to the …
arturminchukov e66a2c9
update CHANGELOG with bugfix details on multi-value variable interpol…
arturminchukov 1b6e039
fix comment displaying of starting preview build
arturminchukov 9d9cbe6
fix tests for interpolateQueryExpr
arturminchukov 2af851b
add tests for regExpOperator and ds.interpolateString; refactor code
arturminchukov 629bafe
update changelog
arturminchukov fdafddb
remove unnecessary console.log
arturminchukov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { getQueryExprVariableRegExp } from './regExpOperator'; | ||
|
|
||
| describe('getQueryExprVariableRegExp', () => { | ||
| it('should fing fieldName:~$var', () => { | ||
| const result = getQueryExprVariableRegExp(' fieldName:~$var '); | ||
| expect(result?.[0]).toEqual(' fieldName:~$var'); | ||
| }); | ||
|
|
||
| it('should find fieldName:~$var without spaces', () => { | ||
| const result = getQueryExprVariableRegExp('fieldName:~$var'); | ||
| expect(result?.[0]).toEqual('fieldName:~$var'); | ||
| }); | ||
|
|
||
| it('should find fieldName:~$var at the start of string', () => { | ||
| const result = getQueryExprVariableRegExp('fieldName:~$var and more'); | ||
| expect(result?.[0]).toEqual('fieldName:~$var'); | ||
| }); | ||
|
|
||
| it('should find fieldName:~$var at the end of string', () => { | ||
| const result = getQueryExprVariableRegExp('some query fieldName:~$var'); | ||
| expect(result?.[0]).toEqual(' fieldName:~$var'); | ||
| }); | ||
|
|
||
| it('should find variable with underscores', () => { | ||
| const result = getQueryExprVariableRegExp('fieldName:~$my_var_name'); | ||
| expect(result?.[0]).toEqual('fieldName:~$my_var_name'); | ||
| }); | ||
|
|
||
| it('should find variable with numbers', () => { | ||
| const result = getQueryExprVariableRegExp('fieldName:~$var123'); | ||
| expect(result?.[0]).toEqual('fieldName:~$var123'); | ||
| }); | ||
|
|
||
| it('should find complex field name with variable', () => { | ||
| const result = getQueryExprVariableRegExp('complex_field_name:~$variable'); | ||
| expect(result?.[0]).toEqual('complex_field_name:~$variable'); | ||
| }); | ||
|
|
||
| it('should return null for query without variables', () => { | ||
| const result = getQueryExprVariableRegExp('fieldName:value'); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should return null for empty string', () => { | ||
| const result = getQueryExprVariableRegExp(''); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should return null for string with only spaces', () => { | ||
| const result = getQueryExprVariableRegExp(' '); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should find first variable when multiple variables exist', () => { | ||
| const result = getQueryExprVariableRegExp('field1:~$var1 field2:~$var2'); | ||
| expect(result?.[0]).toEqual('field1:~$var1'); | ||
| }); | ||
|
|
||
| it('should find variable with special characters in field name', () => { | ||
| const result = getQueryExprVariableRegExp('field-name:~$var'); | ||
| expect(result?.[0]).toEqual('field-name:~$var'); | ||
| }); | ||
|
|
||
| it('should not handle variable without field name prefix', () => { | ||
| const result = getQueryExprVariableRegExp('~$var'); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should handle multiple spaces around variable', () => { | ||
| const result = getQueryExprVariableRegExp(' fieldName:~$var '); | ||
| expect(result?.[0]).toEqual(' fieldName:~$var'); | ||
| }); | ||
|
|
||
| it('should find variable with mixed case field name', () => { | ||
| const result = getQueryExprVariableRegExp('fieldName:~$MyVariable'); | ||
| expect(result?.[0]).toEqual('fieldName:~$MyVariable'); | ||
| }); | ||
|
|
||
| it('should find variable with dot separated field name', () => { | ||
| const result = getQueryExprVariableRegExp('someField: Some value | field.name.with.some.dot.separated:~$MyVariable'); | ||
| expect(result?.[0]).toEqual(' field.name.with.some.dot.separated:~$MyVariable'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /** | ||
| * Regular expression for matching variables in a query expression -> filterName:~"$VariableName" | ||
| * */ | ||
| const variableRegExp = /(^|\||\s)([^\s:~]+)\s*:\s*~\s*(?:"\$([A-Za-z0-9_.-]+)"|\$([A-Za-z0-9_.-]+)|"([^"]+)"|([^\s|]+))(?=\s|\||$)/; | ||
|
|
||
| export function getQueryExprVariableRegExp(queryExpr: string) { | ||
| return variableRegExp.exec(queryExpr); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { useMemo } from "react"; | ||
| import * as React from "react"; | ||
|
|
||
| import { Icon, Tooltip } from "@grafana/ui"; | ||
|
|
||
| export const QueryEditorHelp = () => { | ||
| const helpTooltipContent = useMemo(() => { | ||
| const helpText = `Variable Interpolation Rules: | ||
|
|
||
| 1. Field Value Variables: | ||
| • field:$var -> field:in("v1", ..., "vN") | ||
| • field:=$var -> field:in("v1", ..., "vN") | ||
| • Values are quoted and escaped | ||
| • Empty values or "All" expand to in(*) | ||
|
|
||
| 2. Function Contexts: | ||
| • in($var) and contains_any($var) expand to quoted lists | ||
| • Values maintain proper quoting within function calls | ||
|
|
||
| 3. Nequality Operators in stream filters: | ||
| • field:!$var -> !field in("v1", ..., "vN") | ||
| • field:!=$var -> !field in("v1", ..., "vN") | ||
|
|
||
| 4. Stream Filters: | ||
| • {tag=$var} -> {tag in(...)} | ||
| • {field!=$var} -> {tag not_in(...)} | ||
|
|
||
| 5. Restrictions: | ||
| • Interpolation is NOT allowed in regexp (e.g., field:~$var) | ||
| • Invalid usage will show an error message`; | ||
| return <pre>{helpText}</pre> | ||
| }, []); | ||
|
|
||
| return ( | ||
| <Tooltip placement="top" content={helpTooltipContent} theme="info"> | ||
| <Icon name="info-circle" size="sm" width={16} height={16} /> | ||
| </Tooltip> | ||
| ) | ||
| } |
44 changes: 44 additions & 0 deletions
44
src/components/QueryEditor/QueryEditorVariableRegexpError.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { css } from "@emotion/css"; | ||
| import React from 'react'; | ||
|
|
||
| import { GrafanaTheme2 } from "@grafana/data"; | ||
| import { Badge, useTheme2 } from "@grafana/ui"; | ||
|
|
||
|
|
||
| interface Props { | ||
| regExp: string; | ||
| } | ||
|
|
||
| const QueryEditorVariableRegexpError = ({ regExp }: Props) => { | ||
| const theme = useTheme2(); | ||
| const styles = getStyles(theme); | ||
|
|
||
| const text = ( | ||
| <div> | ||
| Regexp operator (~) cannot be used with variables in {regExp}. Use exact match operator (:) or use non variable in regexp instead. | ||
| </div> | ||
| ) | ||
|
|
||
| return ( | ||
| <div className={styles.root}> | ||
| <Badge | ||
| icon={"exclamation-triangle"} | ||
| color={"red"} | ||
| text={text} | ||
| /> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default QueryEditorVariableRegexpError; | ||
|
|
||
| const getStyles = (theme: GrafanaTheme2) => { | ||
| return { | ||
| root: css({ | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| gap: theme.spacing(1), | ||
| flexGrow: 1, | ||
| }), | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const VARIABLE_ALL_VALUE = "$__all"; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.