diff --git a/src/app/controllers/graphiteTarget.js b/src/app/controllers/graphiteTarget.js index 6f9c8b7f50a..a5cc244a998 100644 --- a/src/app/controllers/graphiteTarget.js +++ b/src/app/controllers/graphiteTarget.js @@ -2,10 +2,10 @@ define([ 'angular', 'underscore', 'config', - '../services/graphite/functions', + '../services/graphite/graphiteFuncs', '../services/graphite/parser' ], -function (angular, _, config, graphiteFunctions, Parser) { +function (angular, _, config, graphiteFuncs, Parser) { 'use strict'; var module = angular.module('kibana.controllers'); @@ -13,9 +13,8 @@ function (angular, _, config, graphiteFunctions, Parser) { module.controller('GraphiteTargetCtrl', function($scope, $http) { $scope.init = function() { - $scope.funcDefs = graphiteFunctions; + $scope.funcDefs = graphiteFuncs.getDefList(); parseTarget(); - console.log('init:'); }; function parseTarget() { @@ -34,7 +33,15 @@ function (angular, _, config, graphiteFunctions, Parser) { 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); } @@ -43,24 +50,26 @@ function (angular, _, config, graphiteFunctions, Parser) { 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) { parseTargeRecursive(param, innerFunc, index); }); - innerFunc.text = getFuncText(innerFunc.def, innerFunc.params); + innerFunc.updateText(); $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; - } - - if (astNode.type === 'metric') { + break; + case 'metric': $scope.segments = _.map(astNode.segments, function(segment) { return { 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) { var targetWrapped = func.def.name + '(' + target; _.each(func.params, function(param) { @@ -200,16 +195,12 @@ function (angular, _, config, graphiteFunctions, Parser) { }; $scope.functionParamsChanged = function(func) { - func.text = getFuncText(func.def, func.params); + func.updateText(); $scope.targetChanged(); }; $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(); }; diff --git a/src/app/services/graphite/functions.js b/src/app/services/graphite/functions.js deleted file mode 100644 index 90909e18e8d..00000000000 --- a/src/app/services/graphite/functions.js +++ /dev/null @@ -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] - } - ]; - -}); \ No newline at end of file diff --git a/src/app/services/graphite/graphiteFuncs.js b/src/app/services/graphite/graphiteFuncs.js new file mode 100644 index 00000000000..981b8247ac7 --- /dev/null +++ b/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); + } + }; + +}); \ No newline at end of file