query_ctrl.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. } else if (segments[0].expandable) {
  115. if ($scope.segments.length === fromIndex) {
  116. $scope.segments.push(uiSegmentSrv.newSelectMetric());
  117. }
  118. else {
  119. return checkOtherSegments(fromIndex + 1);
  120. }
  121. }
  122. })
  123. .then(null, function(err) {
  124. $scope.parserError = err.message || 'Failed to issue metric query';
  125. });
  126. }
  127. function setSegmentFocus(segmentIndex) {
  128. _.each($scope.segments, function(segment, index) {
  129. segment.focus = segmentIndex === index;
  130. });
  131. }
  132. function wrapFunction(target, func) {
  133. return func.render(target);
  134. }
  135. $scope.getAltSegments = function (index) {
  136. var query = index === 0 ? '*' : getSegmentPathUpTo(index) + '.*';
  137. return $scope.datasource.metricFindQuery(query).then(function(segments) {
  138. var altSegments = _.map(segments, function(segment) {
  139. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  140. });
  141. if (altSegments.length === 0) { return altSegments; }
  142. // add template variables
  143. _.each(templateSrv.variables, function(variable) {
  144. altSegments.unshift(uiSegmentSrv.newSegment({
  145. type: 'template',
  146. value: '$' + variable.name,
  147. expandable: true,
  148. }));
  149. });
  150. // add wildcard option
  151. altSegments.unshift(uiSegmentSrv.newSegment('*'));
  152. return altSegments;
  153. })
  154. .then(null, function(err) {
  155. $scope.parserError = err.message || 'Failed to issue metric query';
  156. return [];
  157. });
  158. };
  159. $scope.segmentValueChanged = function (segment, segmentIndex) {
  160. delete $scope.parserError;
  161. if ($scope.functions.length > 0 && $scope.functions[0].def.fake) {
  162. $scope.functions = [];
  163. }
  164. if (segment.expandable) {
  165. return checkOtherSegments(segmentIndex + 1).then(function() {
  166. setSegmentFocus(segmentIndex + 1);
  167. $scope.targetChanged();
  168. });
  169. }
  170. else {
  171. $scope.segments = $scope.segments.splice(0, segmentIndex + 1);
  172. }
  173. setSegmentFocus(segmentIndex + 1);
  174. $scope.targetChanged();
  175. };
  176. $scope.targetTextChanged = function() {
  177. parseTarget();
  178. $scope.get_data();
  179. };
  180. $scope.targetChanged = function() {
  181. if ($scope.parserError) {
  182. return;
  183. }
  184. var oldTarget = $scope.target.target;
  185. var target = getSegmentPathUpTo($scope.segments.length);
  186. $scope.target.target = _.reduce($scope.functions, wrapFunction, target);
  187. if ($scope.target.target !== oldTarget) {
  188. if ($scope.segments[$scope.segments.length - 1].value !== 'select metric') {
  189. $scope.$parent.get_data();
  190. }
  191. }
  192. };
  193. $scope.removeFunction = function(func) {
  194. $scope.functions = _.without($scope.functions, func);
  195. $scope.targetChanged();
  196. };
  197. $scope.addFunction = function(funcDef) {
  198. var newFunc = gfunc.createFuncInstance(funcDef, { withDefaultParams: true });
  199. newFunc.added = true;
  200. $scope.functions.push(newFunc);
  201. $scope.moveAliasFuncLast();
  202. $scope.smartlyHandleNewAliasByNode(newFunc);
  203. if ($scope.segments.length === 1 && $scope.segments[0].fake) {
  204. $scope.segments = [];
  205. }
  206. if (!newFunc.params.length && newFunc.added) {
  207. $scope.targetChanged();
  208. }
  209. };
  210. $scope.moveAliasFuncLast = function() {
  211. var aliasFunc = _.find($scope.functions, function(func) {
  212. return func.def.name === 'alias' ||
  213. func.def.name === 'aliasByNode' ||
  214. func.def.name === 'aliasByMetric';
  215. });
  216. if (aliasFunc) {
  217. $scope.functions = _.without($scope.functions, aliasFunc);
  218. $scope.functions.push(aliasFunc);
  219. }
  220. };
  221. $scope.smartlyHandleNewAliasByNode = function(func) {
  222. if (func.def.name !== 'aliasByNode') {
  223. return;
  224. }
  225. for(var i = 0; i < $scope.segments.length; i++) {
  226. if ($scope.segments[i].value.indexOf('*') >= 0) {
  227. func.params[0] = i;
  228. func.added = false;
  229. $scope.targetChanged();
  230. return;
  231. }
  232. }
  233. };
  234. $scope.toggleMetricOptions = function() {
  235. $scope.panel.metricOptionsEnabled = !$scope.panel.metricOptionsEnabled;
  236. if (!$scope.panel.metricOptionsEnabled) {
  237. delete $scope.panel.cacheTimeout;
  238. }
  239. };
  240. $scope.init();
  241. });
  242. });