Forráskód Böngészése

Includes more comparators/operators for influxdb

This patch includes some missing comparators/operators for influxdb:
>  - greater than
<  - less than
<> - not equal
!~ - doesn't match against

It includes the tag.operator fields to facilitate identification of the operators on the segments.
It keeps the logic for the regex syntax; although it changes the way to validate it, to handle the new operators use case.
Denis Doria 10 éve
szülő
commit
9a9902cea9

+ 6 - 3
public/app/plugins/datasource/influxdb/queryBuilder.js

@@ -10,14 +10,16 @@ function (_) {
 
   function renderTagCondition (tag, index) {
     var str = "";
+    var operator = (tag.operator || '=');
     if (index > 0) {
       str = (tag.condition || 'AND') + ' ';
     }
 
-    if (tag.value && tag.value[0] === '/' && tag.value[tag.value.length - 1] === '/') {
-      return str + '"' +tag.key + '"' + ' =~ ' + tag.value;
+    if (tag.value && (operator === '=~' || operator === '!~') && /^\/.*\/$/.test(tag.value)) {
+       return str + '"' + tag.key + '"' + ' ' + operator + ' ' + tag.value;
     }
-    return str + '"' + tag.key + '"' + " = '" + tag.value + "'";
+
+    return str + '"' + tag.key + '" ' + operator + " '" + tag.value + "'";
   }
 
   var p = InfluxQueryBuilder.prototype;
@@ -127,3 +129,4 @@ function (_) {
 
   return InfluxQueryBuilder;
 });
+

+ 21 - 7
public/app/plugins/datasource/influxdb/queryCtrl.js

@@ -35,7 +35,7 @@ function (angular, _, InfluxQueryBuilder) {
           $scope.tagSegments.push(MetricSegment.newCondition(tag.condition));
         }
         $scope.tagSegments.push(new MetricSegment({value: tag.key, type: 'key', cssClass: 'query-segment-key' }));
-        $scope.tagSegments.push(new MetricSegment.newOperator("="));
+        $scope.tagSegments.push(MetricSegment.newOperator(tag.operator));
         $scope.tagSegments.push(new MetricSegment({value: tag.value, type: 'value', cssClass: 'query-segment-value'}));
       });
 
@@ -150,6 +150,10 @@ function (angular, _, InfluxQueryBuilder) {
         query = $scope.queryBuilder.buildExploreQuery('TAG_VALUES', $scope.tagSegments[index-2].value);
       } else if (segment.type === 'condition') {
         return $q.when([new MetricSegment('AND'), new MetricSegment('OR')]);
+      } else if (segment.type === 'operator') {
+        return $q.when([MetricSegment.newOperator('='), MetricSegment.newOperator('<>'),
+                        MetricSegment.newOperator('<'), MetricSegment.newOperator('>'),
+                        MetricSegment.newOperator('=~'), MetricSegment.newOperator('!~')]);
       }
       else  {
         return $q.when([]);
@@ -238,6 +242,7 @@ function (angular, _, InfluxQueryBuilder) {
     $scope.rebuildTargetTagConditions = function() {
       var tags = [];
       var tagIndex = 0;
+      var tagOperator = "";
       _.each($scope.tagSegments, function(segment2, index) {
         if (segment2.type === 'key') {
           if (tags.length === 0) {
@@ -246,25 +251,33 @@ function (angular, _, InfluxQueryBuilder) {
           tags[tagIndex].key = segment2.value;
         }
         else if (segment2.type === 'value') {
+          tagOperator = $scope.getTagValueOperator(segment2.value, tags[tagIndex].operator)
+          if (tagOperator) {
+            $scope.tagSegments[index-1] = MetricSegment.newOperator(tagOperator);
+            tags[tagIndex].operator = tagOperator;
+          }
           tags[tagIndex].value = segment2.value;
-          $scope.tagSegments[index-1] = $scope.getTagValueOperator(segment2.value);
         }
         else if (segment2.type === 'condition') {
           tags.push({ condition: segment2.value });
           tagIndex += 1;
         }
+        else if (segment2.type === 'operator') {
+          tags[tagIndex].operator = segment2.value;
+        }
       });
 
       $scope.target.tags = tags;
       $scope.$parent.get_data();
     };
 
-    $scope.getTagValueOperator = function(tagValue) {
-      if (tagValue[0] === '/' && tagValue[tagValue.length - 1] === '/') {
-        return MetricSegment.newOperator('=~');
+    $scope.getTagValueOperator = function(tagValue, tagOperator) {
+      if (tagOperator !== '=~' && tagOperator !== '!~' && /^\/.*\/$/.test(tagValue)) {
+        return '=~';
+      }
+      else if ((tagOperator === '=~' || tagOperator === '!~') && /^(?!\/.*\/$)/.test(tagValue)) {
+        return '=';
       }
-
-      return MetricSegment.newOperator('=');
     };
 
     function MetricSegment(options) {
@@ -317,3 +330,4 @@ function (angular, _, InfluxQueryBuilder) {
   });
 
 });
+