Commit c9260d7e by Torkel Ödegaard

bugfixes and improvements to graphite target editor

parent 7cbd88f3
......@@ -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();
};
......
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
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment