Detect regex in metric queries.

This commit is contained in:
Alexander Zobnin
2016-01-17 14:11:11 +03:00
parent 7a73449828
commit 0bbfa3910a
3 changed files with 61 additions and 15 deletions

View File

@@ -89,24 +89,28 @@
<li class="tight-form-item input-small" style="width: 5em">Group</li> <li class="tight-form-item input-small" style="width: 5em">Group</li>
<li> <li>
<input type="text" <input type="text"
ng-model="target.groupFilter" ng-model="target.group.filter"
bs-typeahead="getGroupNames" bs-typeahead="getGroupNames"
ng-blur="render()" ng-change="onTargetPartChange(target.group)"
ng-blur="parseTarget()"
data-min-length=0 data-min-length=0
data-items=100 data-items=100
class="input-medium tight-form-input" > class="input-medium tight-form-input"
ng-style="target.group.style">
</li> </li>
<!-- Select Host --> <!-- Select Host -->
<li class="tight-form-item input-small" style="width: 3em">Host</li> <li class="tight-form-item input-small" style="width: 3em">Host</li>
<li> <li>
<input type="text" <input type="text"
ng-model="target.hostFilter" ng-model="target.host.filter"
bs-typeahead="getHostNames" bs-typeahead="getHostNames"
ng-blur="render()" ng-change="onTargetPartChange(target.host)"
ng-blur="parseTarget()"
data-min-length=0 data-min-length=0
data-items=100 data-items=100
class="input-large tight-form-input" > class="input-large tight-form-input"
ng-style="target.host.style">
</li> </li>
<!-- Downsampling function --> <!-- Downsampling function -->
@@ -139,24 +143,28 @@
<li class="tight-form-item input-small" style="width: 5em">Application</li> <li class="tight-form-item input-small" style="width: 5em">Application</li>
<li> <li>
<input type="text" <input type="text"
ng-model="target.applicationFilter" ng-model="target.application.filter"
bs-typeahead="getApplicationNames" bs-typeahead="getApplicationNames"
ng-blur="render()" ng-change="onTargetPartChange(target.application)"
ng-blur="parseTarget()"
data-min-length=0 data-min-length=0
data-items=100 data-items=100
class="input-medium tight-form-input" > class="input-medium tight-form-input"
ng-style="target.application.style">
</li> </li>
<!-- Select Item --> <!-- Select Item -->
<li class="tight-form-item input-small" style="width: 3em">Item</li> <li class="tight-form-item input-small" style="width: 3em">Item</li>
<li> <li>
<input type="text" <input type="text"
ng-model="target.itemFilter" ng-model="target.item.filter"
bs-typeahead="getItemNames" bs-typeahead="getItemNames"
ng-blur="render()" ng-change="onTargetPartChange(target.item)"
ng-blur="parseTarget()"
data-min-length=0 data-min-length=0
data-items=100 data-items=100
class="input-large tight-form-input" > class="input-large tight-form-input"
ng-style="target.item.style">
</li> </li>
<!-- Scale --> <!-- Scale -->

View File

@@ -1,7 +1,8 @@
define([ define([
'angular', 'angular',
'lodash', 'lodash',
'./helperFunctions' './helperFunctions',
'./utils'
], ],
function (angular, _) { function (angular, _) {
'use strict'; 'use strict';
@@ -9,7 +10,7 @@ define([
var module = angular.module('grafana.controllers'); var module = angular.module('grafana.controllers');
var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv, zabbixHelperSrv) { module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv, zabbixHelperSrv, Utils) {
var zabbixCache = $scope.datasource.zabbixCache; var zabbixCache = $scope.datasource.zabbixCache;
@@ -76,12 +77,34 @@ define([
return _.uniq(_.map(scope.metric[metricList], 'name')); return _.uniq(_.map(scope.metric[metricList], 'name'));
} }
// Map functions // Map functions for bs-typeahead
$scope.getGroupNames = _.partial(getMetricNames, $scope, 'groupList'); $scope.getGroupNames = _.partial(getMetricNames, $scope, 'groupList');
$scope.getHostNames = _.partial(getMetricNames, $scope, 'hostList'); $scope.getHostNames = _.partial(getMetricNames, $scope, 'hostList');
$scope.getApplicationNames = _.partial(getMetricNames, $scope, 'applicationList'); $scope.getApplicationNames = _.partial(getMetricNames, $scope, 'applicationList');
$scope.getItemNames = _.partial(getMetricNames, $scope, 'itemList'); $scope.getItemNames = _.partial(getMetricNames, $scope, 'itemList');
$scope.onTargetPartChange = function (targetPart) {
var regexStyle = {'color': '#CCA300'};
targetPart.isRegex = Utils.isRegex(targetPart.filter);
targetPart.style = targetPart.isRegex ? regexStyle : {};
};
$scope.parseTarget = function() {
var regexStyle = {'color': '#CCA300'};
$scope.target.groupIsRegex = Utils.isRegex($scope.target.groupFilter);
$scope.groupStyle = $scope.target.groupIsRegex ? regexStyle : {};
$scope.target.hostIsRegex = Utils.isRegex($scope.target.hostFilter);
$scope.hostStyle = $scope.target.hostIsRegex ? regexStyle : {};
$scope.target.applicationIsRegex = Utils.isRegex($scope.target.applicationFilter);
$scope.applicationsStyle = $scope.target.applicationIsRegex ? regexStyle : {};
$scope.target.itemIsRegex = Utils.isRegex($scope.target.itemFilter);
$scope.itemStyle = $scope.target.itemIsRegex ? regexStyle : {};
};
/** /**
* Switch query editor to specified mode. * Switch query editor to specified mode.
* Modes: * Modes:

View File

@@ -28,5 +28,20 @@ function (angular, _) {
} }
return name; return name;
}; };
// Pattern for testing regex
var regexPattern = /^\/(.*)\/([gmi]*)$/m;
this.isRegex = function (str) {
return regexPattern.test(str);
};
this.buildRegex = function (str) {
var matches = str.match(regexPattern);
var pattern = matches[1];
var flags = matches[2] !== "" ? matches[2] : undefined;
return new RegExp(pattern, flags);
};
}); });
}); });