graphiteTarget.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'config',
  5. '../services/graphite/gfunc',
  6. '../services/graphite/parser'
  7. ],
  8. function (angular, _, config, gfunc, Parser) {
  9. 'use strict';
  10. var module = angular.module('grafana.controllers');
  11. var targetLetters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'];
  12. module.controller('GraphiteTargetCtrl', function($scope, $sce, templateSrv) {
  13. $scope.init = function() {
  14. $scope.target.target = $scope.target.target || '';
  15. $scope.targetLetter = targetLetters[$scope.$index];
  16. parseTarget();
  17. };
  18. // The way parsing and the target editor works needs
  19. // to be rewritten to handle functions that take multiple series
  20. function parseTarget() {
  21. $scope.functions = [];
  22. $scope.segments = [];
  23. $scope.showTextEditor = false;
  24. delete $scope.parserError;
  25. var parser = new Parser($scope.target.target);
  26. var astNode = parser.getAst();
  27. if (astNode === null) {
  28. checkOtherSegments(0);
  29. return;
  30. }
  31. if (astNode.type === 'error') {
  32. $scope.parserError = astNode.message + " at position: " + astNode.pos;
  33. $scope.showTextEditor = true;
  34. return;
  35. }
  36. try {
  37. parseTargeRecursive(astNode);
  38. }
  39. catch (err) {
  40. console.log('error parsing target:', err.message);
  41. $scope.parserError = err.message;
  42. $scope.showTextEditor = true;
  43. }
  44. checkOtherSegments($scope.segments.length - 1);
  45. }
  46. function parseTargeRecursive(astNode, func, index) {
  47. if (astNode === null) {
  48. return null;
  49. }
  50. switch(astNode.type) {
  51. case 'function':
  52. var innerFunc = gfunc.createFuncInstance(astNode.name, { withDefaultParams: false });
  53. _.each(astNode.params, function(param, index) {
  54. parseTargeRecursive(param, innerFunc, index);
  55. });
  56. innerFunc.updateText();
  57. $scope.functions.push(innerFunc);
  58. break;
  59. case 'series-ref':
  60. if ($scope.segments.length === 0) {
  61. func.params[index] = astNode.value;
  62. }
  63. else {
  64. func.params[index - 1] = astNode.value;
  65. }
  66. break;
  67. case 'string':
  68. case 'number':
  69. if ((index-1) >= func.def.params.length) {
  70. throw { message: 'invalid number of parameters to method ' + func.def.name };
  71. }
  72. if (index === 0) {
  73. func.params[index] = astNode.value;
  74. }
  75. else {
  76. func.params[index - 1] = astNode.value;
  77. }
  78. break;
  79. case 'metric':
  80. if ($scope.segments.length > 0) {
  81. throw { message: 'Multiple metric params not supported, use text editor.' };
  82. }
  83. $scope.segments = _.map(astNode.segments, function(segment) {
  84. return new MetricSegment(segment);
  85. });
  86. }
  87. }
  88. function getSegmentPathUpTo(index) {
  89. var arr = $scope.segments.slice(0, index);
  90. return _.reduce(arr, function(result, segment) {
  91. return result ? (result + "." + segment.value) : segment.value;
  92. }, "");
  93. }
  94. function checkOtherSegments(fromIndex) {
  95. if (fromIndex === 0) {
  96. $scope.segments.push(new MetricSegment('select metric'));
  97. return;
  98. }
  99. var path = getSegmentPathUpTo(fromIndex + 1);
  100. return $scope.datasource.metricFindQuery(path)
  101. .then(function(segments) {
  102. if (segments.length === 0) {
  103. if (path !== '') {
  104. $scope.segments = $scope.segments.splice(0, fromIndex);
  105. $scope.segments.push(new MetricSegment('select metric'));
  106. }
  107. return;
  108. }
  109. if (segments[0].expandable) {
  110. if ($scope.segments.length === fromIndex) {
  111. $scope.segments.push(new MetricSegment('select metric'));
  112. }
  113. else {
  114. return checkOtherSegments(fromIndex + 1);
  115. }
  116. }
  117. })
  118. .then(null, function(err) {
  119. $scope.parserError = err.message || 'Failed to issue metric query';
  120. });
  121. }
  122. function setSegmentFocus(segmentIndex) {
  123. _.each($scope.segments, function(segment, index) {
  124. segment.focus = segmentIndex === index;
  125. });
  126. }
  127. function wrapFunction(target, func) {
  128. return func.render(target);
  129. }
  130. $scope.getAltSegments = function (index) {
  131. $scope.altSegments = [];
  132. var query = index === 0 ? '*' : getSegmentPathUpTo(index) + '.*';
  133. return $scope.datasource.metricFindQuery(query)
  134. .then(function(segments) {
  135. $scope.altSegments = _.map(segments, function(segment) {
  136. return new MetricSegment({ value: segment.text, expandable: segment.expandable });
  137. });
  138. _.each(templateSrv.variables, function(variable) {
  139. $scope.altSegments.unshift(new MetricSegment({
  140. type: 'template',
  141. value: '$' + variable.name + ']]',
  142. expandable: true,
  143. }));
  144. });
  145. $scope.altSegments.unshift(new MetricSegment('*'));
  146. })
  147. .then(null, function(err) {
  148. $scope.parserError = err.message || 'Failed to issue metric query';
  149. });
  150. };
  151. $scope.segmentValueChanged = function (segment, segmentIndex) {
  152. delete $scope.parserError;
  153. if ($scope.functions.length > 0 && $scope.functions[0].def.fake) {
  154. $scope.functions = [];
  155. }
  156. if (segment.expandable) {
  157. return checkOtherSegments(segmentIndex + 1)
  158. .then(function () {
  159. setSegmentFocus(segmentIndex + 1);
  160. $scope.targetChanged();
  161. });
  162. }
  163. else {
  164. $scope.segments = $scope.segments.splice(0, segmentIndex + 1);
  165. }
  166. setSegmentFocus(segmentIndex + 1);
  167. $scope.targetChanged();
  168. };
  169. $scope.targetTextChanged = function() {
  170. parseTarget();
  171. $scope.$parent.get_data();
  172. };
  173. $scope.targetChanged = function() {
  174. if ($scope.parserError) {
  175. return;
  176. }
  177. var oldTarget = $scope.target.target;
  178. var target = getSegmentPathUpTo($scope.segments.length);
  179. $scope.target.target = _.reduce($scope.functions, wrapFunction, target);
  180. if ($scope.target.target !== oldTarget) {
  181. $scope.$parent.get_data();
  182. }
  183. };
  184. $scope.removeFunction = function(func) {
  185. $scope.functions = _.without($scope.functions, func);
  186. $scope.targetChanged();
  187. };
  188. $scope.addFunction = function(funcDef) {
  189. var newFunc = gfunc.createFuncInstance(funcDef, { withDefaultParams: true });
  190. newFunc.added = true;
  191. $scope.functions.push(newFunc);
  192. $scope.moveAliasFuncLast();
  193. $scope.smartlyHandleNewAliasByNode(newFunc);
  194. if ($scope.segments.length === 1 && $scope.segments[0].value === 'select metric') {
  195. $scope.segments = [];
  196. }
  197. if (!newFunc.params.length && newFunc.added) {
  198. $scope.targetChanged();
  199. }
  200. };
  201. $scope.moveAliasFuncLast = function() {
  202. var aliasFunc = _.find($scope.functions, function(func) {
  203. return func.def.name === 'alias' ||
  204. func.def.name === 'aliasByNode' ||
  205. func.def.name === 'aliasByMetric';
  206. });
  207. if (aliasFunc) {
  208. $scope.functions = _.without($scope.functions, aliasFunc);
  209. $scope.functions.push(aliasFunc);
  210. }
  211. };
  212. $scope.smartlyHandleNewAliasByNode = function(func) {
  213. if (func.def.name !== 'aliasByNode') {
  214. return;
  215. }
  216. for(var i = 0; i < $scope.segments.length; i++) {
  217. if ($scope.segments[i].value.indexOf('*') >= 0) {
  218. func.params[0] = i;
  219. func.added = false;
  220. $scope.targetChanged();
  221. return;
  222. }
  223. }
  224. };
  225. $scope.toggleMetricOptions = function() {
  226. $scope.panel.metricOptionsEnabled = !$scope.panel.metricOptionsEnabled;
  227. if (!$scope.panel.metricOptionsEnabled) {
  228. delete $scope.panel.cacheTimeout;
  229. }
  230. };
  231. $scope.duplicate = function() {
  232. var clone = angular.copy($scope.target);
  233. $scope.panel.targets.push(clone);
  234. };
  235. function MetricSegment(options) {
  236. if (options === '*' || options.value === '*') {
  237. this.value = '*';
  238. this.html = $sce.trustAsHtml('<i class="icon-asterisk"><i>');
  239. this.expandable = true;
  240. return;
  241. }
  242. if (_.isString(options)) {
  243. this.value = options;
  244. this.html = $sce.trustAsHtml(this.value);
  245. return;
  246. }
  247. this.value = options.value;
  248. this.type = options.type;
  249. this.expandable = options.expandable;
  250. if (options.type === 'template') {
  251. this.html = $sce.trustAsHtml(options.value);
  252. }
  253. else {
  254. this.html = $sce.trustAsHtml(this.value);
  255. }
  256. }
  257. });
  258. module.directive('focusMe', function($timeout, $parse) {
  259. return {
  260. //scope: true, // optionally create a child scope
  261. link: function(scope, element, attrs) {
  262. var model = $parse(attrs.focusMe);
  263. scope.$watch(model, function(value) {
  264. if(value === true) {
  265. $timeout(function() {
  266. element[0].focus();
  267. });
  268. }
  269. });
  270. }
  271. };
  272. });
  273. });