Iss #182 - fixed template variables inside regex.

This commit is contained in:
Alexander Zobnin
2016-04-11 22:21:19 +03:00
parent 9f2265bd36
commit eb9722aef0
2 changed files with 43 additions and 16 deletions

View File

@@ -47,21 +47,8 @@ export class ZabbixAPIDatasource {
this.templateSrv = templateSrv; this.templateSrv = templateSrv;
this.alertSrv = alertSrv; this.alertSrv = alertSrv;
// If template variables are used in request, replace it using regex format // Use custom format for template variables
// and wrap with '/' for proper multi-value work. Example: this.replaceTemplateVars = _.partial(replaceTemplateVars, this.templateSrv);
// $variable selected as a, b, c
// We use filter $variable
// $variable -> a|b|c -> /a|b|c/
// /$variable/ -> /a|b|c/ -> /a|b|c/
this.multiValueFormat = "regex";
this.replaceTemplateVars = function(target, scopedVars) {
var regexPattern = /^\/.*\/$/;
var replacedTarget = this.templateSrv.replace(target, scopedVars, this.multiValueFormat);
if (target !== replacedTarget && !regexPattern.test(replacedTarget)) {
replacedTarget = '/' + replacedTarget + '/';
}
return replacedTarget;
};
console.log(this.zabbixCache); console.log(this.zabbixCache);
} }
@@ -445,3 +432,37 @@ function formatMetric(metricObj) {
expandable: false expandable: false
}; };
} }
/**
* Custom formatter for template variables.
* Default Grafana "regex" formatter returns
* value1|value2
* This formatter returns
* (value1|value2)
* This format needed for using in complex regex with
* template variables, for example
* /CPU $cpu_item.*time/ where $cpu_item is system,user,iowait
*/
function zabbixTemplateFormat(value, variable) {
if (typeof value === 'string') {
return utils.escapeRegex(value);
}
var escapedValues = _.map(value, utils.escapeRegex);
return '(' + escapedValues.join('|') + ')';
}
/** If template variables are used in request, replace it using regex format
* and wrap with '/' for proper multi-value work. Example:
* $variable selected as a, b, c
* We use filter $variable
* $variable -> a|b|c -> /a|b|c/
* /$variable/ -> /a|b|c/ -> /a|b|c/
*/
function replaceTemplateVars(templateSrv, target, scopedVars) {
var replacedTarget = templateSrv.replace(target, scopedVars, zabbixTemplateFormat);
if (target !== replacedTarget && !utils.regexPattern.test(replacedTarget)) {
replacedTarget = '/' + replacedTarget + '/';
}
return replacedTarget;
}

View File

@@ -23,7 +23,7 @@ export function expandItemName(name, key) {
} }
// Pattern for testing regex // Pattern for testing regex
var regexPattern = /^\/(.*)\/([gmi]*)$/m; export var regexPattern = /^\/(.*)\/([gmi]*)$/m;
export function isRegex(str) { export function isRegex(str) {
return regexPattern.test(str); return regexPattern.test(str);
@@ -36,6 +36,12 @@ export function buildRegex(str) {
return new RegExp(pattern, flags); return new RegExp(pattern, flags);
} }
// Need for template variables replace
// From Grafana's templateSrv.js
export function escapeRegex(value) {
return value.replace(/[\\^$*+?.()|[\]{}\/]/g, '\\$&');
}
export function parseInterval(interval) { export function parseInterval(interval) {
var intervalPattern = /(^[\d]+)(y|M|w|d|h|m|s)/g; var intervalPattern = /(^[\d]+)(y|M|w|d|h|m|s)/g;
var momentInterval = intervalPattern.exec(interval); var momentInterval = intervalPattern.exec(interval);