From 042e2a208dee4180ce4f0d73bafabf8b63cd38ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 24 Dec 2013 12:38:47 +0100 Subject: [PATCH] integrated parser into graphite target editor --- src/app/controllers/graphiteTarget.js | 54 ++++++++++++++++++++----- src/app/panels/graphite/funcEditor.html | 2 +- src/app/services/graphite/functions.js | 2 +- src/app/services/graphite/parser.js | 2 +- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/app/controllers/graphiteTarget.js b/src/app/controllers/graphiteTarget.js index 4314b834bf5..eea1100384d 100644 --- a/src/app/controllers/graphiteTarget.js +++ b/src/app/controllers/graphiteTarget.js @@ -2,9 +2,10 @@ define([ 'angular', 'underscore', 'config', - '/app/services/graphite/functions.js' + '../services/graphite/functions', + '../services/graphite/parser' ], -function (angular, _, config, graphiteFunctions) { +function (angular, _, config, graphiteFunctions, Parser) { 'use strict'; var module = angular.module('kibana.controllers'); @@ -12,17 +13,50 @@ function (angular, _, config, graphiteFunctions) { module.controller('GraphiteTargetCtrl', function($scope, $http) { $scope.init = function() { - $scope.segments = _.map($scope.target.target.split('.'), function (segmentStr) { - return { - val: segmentStr, - html: segmentStr === '*' ? '' : segmentStr - }; - }); - - $scope.funcDefs = graphiteFunctions; $scope.functions = []; + $scope.segments = []; + $scope.funcDefs = graphiteFunctions; + + var parser = new Parser($scope.target.target); + var astNode = parser.getAst(); + console.log('GraphiteTargetCtrl:init -> target', $scope.target.target); + console.log('GraphiteTargetCtrl:init -> ast', astNode); + parseTargetExpression(astNode); + }; + function parseTargetExpression(astNode, func, index) { + if (astNode === null) { + return null; + } + + if (astNode.type === 'function') { + var innerFunc = {}; + innerFunc.def = _.findWhere($scope.funcDefs, { name: astNode.name }) + innerFunc.params = innerFunc.def.defaultParams; + + _.each(astNode.params, function(param, index) { + parseTargetExpression(param, innerFunc, index); + }); + + innerFunc.text = getFuncText(innerFunc.def, innerFunc.params); + $scope.functions.push(innerFunc); + } + + if (astNode.type === 'number' || astNode.type === 'string') { + func.params[index - 1] = astNode.value; + } + + if (astNode.type === 'metric') { + $scope.segments = _.map(astNode.segments, function(segment) { + return { + val: segment.value, + html: segment.value === '*' ? '' : segment.value + }; + }); + } + } + function getSegmentPathUpTo(index) { var arr = $scope.segments.slice(0, index); diff --git a/src/app/panels/graphite/funcEditor.html b/src/app/panels/graphite/funcEditor.html index 63a098de7aa..65244ed3d84 100644 --- a/src/app/panels/graphite/funcEditor.html +++ b/src/app/panels/graphite/funcEditor.html @@ -49,7 +49,7 @@ style="width: 110px" ng-change="functionParamsChanged(func)" ng-model="func.params[$index]" - ng-options="f for f in ['sumSeries', 'avgSeries']"> + ng-options="f for f in ['sum', 'avg']"> diff --git a/src/app/services/graphite/functions.js b/src/app/services/graphite/functions.js index 38c064a4ce4..90909e18e8d 100644 --- a/src/app/services/graphite/functions.js +++ b/src/app/services/graphite/functions.js @@ -26,7 +26,7 @@ function () { type: "function", } ], - defaultParams: [3, "sumSeries"] + defaultParams: [3, "sum"] }, { name: "alias", diff --git a/src/app/services/graphite/parser.js b/src/app/services/graphite/parser.js index 675de8ed8ac..08755657449 100644 --- a/src/app/services/graphite/parser.js +++ b/src/app/services/graphite/parser.js @@ -117,7 +117,7 @@ define([ return { type: 'number', - value: this.tokens[this.index-1].value + value: parseInt(this.tokens[this.index-1].value) }; },