Просмотр исходного кода

bugfixes and improvements to graphite target editor

Torkel Ödegaard 12 лет назад
Родитель
Сommit
c9260d7e80

+ 26 - 35
src/app/controllers/graphiteTarget.js

@@ -2,10 +2,10 @@ define([
   'angular',
   'angular',
   'underscore',
   'underscore',
   'config',
   'config',
-  '../services/graphite/functions',
+  '../services/graphite/graphiteFuncs',
   '../services/graphite/parser'
   '../services/graphite/parser'
 ],
 ],
-function (angular, _, config, graphiteFunctions, Parser) {
+function (angular, _, config, graphiteFuncs, Parser) {
   'use strict';
   'use strict';
 
 
   var module = angular.module('kibana.controllers');
   var module = angular.module('kibana.controllers');
@@ -13,9 +13,8 @@ function (angular, _, config, graphiteFunctions, Parser) {
   module.controller('GraphiteTargetCtrl', function($scope, $http) {
   module.controller('GraphiteTargetCtrl', function($scope, $http) {
 
 
     $scope.init = function() {
     $scope.init = function() {
-      $scope.funcDefs = graphiteFunctions;
+      $scope.funcDefs = graphiteFuncs.getDefList();
       parseTarget();
       parseTarget();
-      console.log('init:');
     };
     };
 
 
     function parseTarget() {
     function parseTarget() {
@@ -34,7 +33,15 @@ function (angular, _, config, graphiteFunctions, Parser) {
         return;
         return;
       }
       }
 
 
-      parseTargeRecursive(astNode);
+      try {
+        parseTargeRecursive(astNode);
+      }
+      catch (err) {
+        console.log('error parsing target:', err.message);
+        $scope.parserError = err.message;
+        $scope.showTextEditor = true;
+      }
+
       checkOtherSegments($scope.segments.length);
       checkOtherSegments($scope.segments.length);
     }
     }
 
 
@@ -43,24 +50,26 @@ function (angular, _, config, graphiteFunctions, Parser) {
         return null;
         return null;
       }
       }
 
 
-      if (astNode.type === 'function') {
-        var innerFunc = {};
-        innerFunc.def = _.findWhere($scope.funcDefs, { name: astNode.name });
-        innerFunc.params = innerFunc.def.defaultParams.slice(0);
+      switch(astNode.type) {
+      case 'function':
+        var innerFunc = graphiteFuncs.createFuncInstance(astNode.name);
 
 
         _.each(astNode.params, function(param, index) {
         _.each(astNode.params, function(param, index) {
           parseTargeRecursive(param, innerFunc, index);
           parseTargeRecursive(param, innerFunc, index);
         });
         });
 
 
-        innerFunc.text = getFuncText(innerFunc.def, innerFunc.params);
+        innerFunc.updateText();
         $scope.functions.push(innerFunc);
         $scope.functions.push(innerFunc);
-      }
+        break;
 
 
-      if (astNode.type === 'number' || astNode.type === 'string') {
+      case 'string':
+      case 'number':
+        if ((index-1) >= func.def.params.length) {
+          throw { message: 'invalid number of parameters to method ' + func.def.name };
+        }
         func.params[index - 1] = astNode.value;
         func.params[index - 1] = astNode.value;
-      }
-
-      if (astNode.type === 'metric') {
+        break;
+      case 'metric':
         $scope.segments = _.map(astNode.segments, function(segment) {
         $scope.segments = _.map(astNode.segments, function(segment) {
           return {
           return {
             val: segment.value,
             val: segment.value,
@@ -114,20 +123,6 @@ function (angular, _, config, graphiteFunctions, Parser) {
       });
       });
     }
     }
 
 
-    function getFuncText(funcDef, params) {
-      if (params.length === 0) {
-        return funcDef.name + '()';
-      }
-
-      var text = funcDef.name + '(';
-      _.each(funcDef.params, function(param, index) {
-        text += params[index] + ', ';
-      });
-      text = text.substring(0, text.length - 2);
-      text += ')';
-      return text;
-    }
-
     function wrapFunction(target, func) {
     function wrapFunction(target, func) {
       var targetWrapped = func.def.name + '(' + target;
       var targetWrapped = func.def.name + '(' + target;
       _.each(func.params, function(param) {
       _.each(func.params, function(param) {
@@ -200,16 +195,12 @@ function (angular, _, config, graphiteFunctions, Parser) {
     };
     };
 
 
     $scope.functionParamsChanged = function(func) {
     $scope.functionParamsChanged = function(func) {
-      func.text = getFuncText(func.def, func.params);
+      func.updateText();
       $scope.targetChanged();
       $scope.targetChanged();
     };
     };
 
 
     $scope.addFunction = function(funcDef) {
     $scope.addFunction = function(funcDef) {
-      $scope.functions.push({
-        def: funcDef,
-        params: funcDef.defaultParams.slice(0),
-        text: getFuncText(funcDef, funcDef.defaultParams)
-      });
+      $scope.functions.push(graphiteFuncs.createFuncInstance(funcDef));
       $scope.targetChanged();
       $scope.targetChanged();
     };
     };
 
 

+ 0 - 45
src/app/services/graphite/functions.js

@@ -1,45 +0,0 @@
-define([
-],
-function () {
-  'use strict';
-
-  return [
-    {
-      name: "scaleToSeconds",
-      params: [ { name: "seconds", type: "int" } ],
-      defaultParams: [1]
-    },
-    {
-      name: "sumSeries",
-      params: [],
-      defaultParams: []
-    },
-    {
-      name: "groupByNode",
-      params: [
-        {
-          name: "node",
-          type: "node",
-        },
-        {
-          name: "function",
-          type: "function",
-        }
-      ],
-      defaultParams: [3, "sum"]
-    },
-    {
-      name: "alias",
-      params: [
-        { name: "alias", type: 'string' }
-      ],
-      defaultParams: ['alias']
-    },
-    {
-      name: 'aliasByNode',
-      params: [ { name: "node", type: "node", } ],
-      defaultParams: [3]
-    }
-  ];
-
-});

+ 96 - 0
src/app/services/graphite/graphiteFuncs.js

@@ -0,0 +1,96 @@
+define([
+  'underscore'
+],
+function (_) {
+  'use strict';
+
+  var funcDefList = [];
+  var index = [];
+
+  function addFuncDef(funcDef) {
+    funcDefList.push(funcDef);
+    index[funcDef.name] = funcDef;
+    index[funcDef.shortName || funcDef.name] = funcDef;
+  }
+
+  addFuncDef({
+    name: 'scaleToSeconds',
+    params: [ { name: 'seconds', type: 'int' } ],
+    defaultParams: [1],
+  });
+
+  addFuncDef({
+    name: "alias",
+    params: [
+      { name: "alias", type: 'string' }
+    ],
+    defaultParams: ['alias']
+  });
+
+  addFuncDef({
+    name: 'sumSeries',
+    shortName: 'sum',
+    params: [],
+    defaultParams: []
+  });
+
+  addFuncDef({
+    name: "groupByNode",
+    params: [
+      {
+        name: "node",
+        type: "node",
+      },
+      {
+        name: "function",
+        type: "function",
+      }
+    ],
+    defaultParams: [3, "sum"]
+  });
+
+  addFuncDef({
+    name: 'aliasByNode',
+    params: [ { name: "node", type: "node", } ],
+    defaultParams: [3]
+  });
+
+  function FuncInstance(funcDef) {
+    this.def = funcDef;
+    this.params = funcDef.defaultParams.slice(0);
+    this.updateText();
+  }
+
+  FuncInstance.prototype.updateText = function () {
+    if (this.params.length === 0) {
+      this.text = this.def.name + '()';
+      return;
+    }
+
+    var text = this.def.name + '(';
+    _.each(this.def.params, function(param, index) {
+      text += this.params[index] + ', ';
+    }, this);
+    text = text.substring(0, text.length - 2);
+    text += ')';
+    this.text = text;
+  };
+
+  return {
+    createFuncInstance: function(name) {
+      if (_.isString(name)) {
+        var funcDef = index[name];
+        if (!funcDef) {
+          throw { message: 'Method not found ' + name };
+        }
+        name = funcDef;
+      }
+      return new FuncInstance(name);
+    },
+
+    getDefList: function() {
+      return funcDefList.slice(0);
+    }
+  };
+
+});