query_ctrl.js 8.1 KB

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