From 0bbfa3910a99f8fa672d3d3eeb9e504dce30bff7 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 17 Jan 2016 14:11:11 +0300 Subject: [PATCH] Detect regex in metric queries. --- .../partials/query.editor.html | 32 ++++++++++++------- plugins/datasource-zabbix/queryCtrl.js | 29 +++++++++++++++-- plugins/datasource-zabbix/utils.js | 15 +++++++++ 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/plugins/datasource-zabbix/partials/query.editor.html b/plugins/datasource-zabbix/partials/query.editor.html index 7682311..fd65f88 100644 --- a/plugins/datasource-zabbix/partials/query.editor.html +++ b/plugins/datasource-zabbix/partials/query.editor.html @@ -89,24 +89,28 @@
  • Group
  • + class="input-medium tight-form-input" + ng-style="target.group.style">
  • Host
  • + class="input-large tight-form-input" + ng-style="target.host.style">
  • @@ -139,24 +143,28 @@
  • Application
  • + class="input-medium tight-form-input" + ng-style="target.application.style">
  • Item
  • + class="input-large tight-form-input" + ng-style="target.item.style">
  • diff --git a/plugins/datasource-zabbix/queryCtrl.js b/plugins/datasource-zabbix/queryCtrl.js index f9cc3dd..ec83953 100644 --- a/plugins/datasource-zabbix/queryCtrl.js +++ b/plugins/datasource-zabbix/queryCtrl.js @@ -1,7 +1,8 @@ define([ 'angular', 'lodash', - './helperFunctions' + './helperFunctions', + './utils' ], function (angular, _) { 'use strict'; @@ -9,7 +10,7 @@ define([ var module = angular.module('grafana.controllers'); 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; @@ -76,12 +77,34 @@ define([ return _.uniq(_.map(scope.metric[metricList], 'name')); } - // Map functions + // Map functions for bs-typeahead $scope.getGroupNames = _.partial(getMetricNames, $scope, 'groupList'); $scope.getHostNames = _.partial(getMetricNames, $scope, 'hostList'); $scope.getApplicationNames = _.partial(getMetricNames, $scope, 'applicationList'); $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. * Modes: diff --git a/plugins/datasource-zabbix/utils.js b/plugins/datasource-zabbix/utils.js index 8a73482..900bba7 100644 --- a/plugins/datasource-zabbix/utils.js +++ b/plugins/datasource-zabbix/utils.js @@ -28,5 +28,20 @@ function (angular, _) { } 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); + }; + }); }); \ No newline at end of file