-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# Changelog | ||
|
||
## tip | ||
|
||
* FEATURE: add support for variables in the query. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/5). |
This file contains 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 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,33 @@ | ||
const varTypeFunc = [ | ||
(v: string) => `\$${v}`, | ||
(v: string, f?: string) => `[[${v}${f ? `:${f}` : ''}]]`, | ||
(v: string, f?: string) => `\$\{${v}${f ? `:${f}` : ''}\}`, | ||
]; | ||
|
||
export const variableRegex = /\$(\w+)|\[\[([\s\S]+?)(?::(\w+))?]]|\${(\w+)(?:\.([^:^}]+))?(?::([^}]+))?}/g; | ||
|
||
export function returnVariables(expr: string) { | ||
const replacer = (match: string, type: any, v: any, f: any) => varTypeFunc[parseInt(type, 10)](v, f) | ||
return expr.replace(/__V_(\d)__(.+?)__V__(?:__F__(\w+)__F__)?/g, replacer); | ||
} | ||
|
||
|
||
export function replaceVariables(expr: string) { | ||
return expr.replace(variableRegex, (match, var1, var2, fmt2, var3, fieldPath, fmt3) => { | ||
const fmt = fmt2 || fmt3; | ||
let variable = var1; | ||
let varType = '0'; | ||
|
||
if (var2) { | ||
variable = var2; | ||
varType = '1'; | ||
} | ||
|
||
if (var3) { | ||
variable = var3; | ||
varType = '2'; | ||
} | ||
|
||
return `__V_${varType}__` + variable + '__V__' + (fmt ? '__F__' + fmt + '__F__' : ''); | ||
}); | ||
} |