query_ctrl.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. var panelCtrl = $scope.panelCtrl = $scope.ctrl;
  13. $scope.init = function() {
  14. if ($scope.target) {
  15. $scope.target.target = $scope.target.target || '';
  16. parseTarget();
  17. }
  18. };
  19. $scope.toggleEditorMode = function() {
  20. $scope.target.textEditor = !$scope.target.textEditor;
  21. parseTarget();
  22. };
  23. // The way parsing and the target editor works needs
  24. // to be rewritten to handle functions that take multiple series
  25. function parseTarget() {
  26. $scope.functions = [];
  27. $scope.segments = [];
  28. delete $scope.parserError;
  29. if ($scope.target.textEditor) {
  30. return;
  31. }
  32. var parser = new Parser($scope.target.target);
  33. var astNode = parser.getAst();
  34. if (astNode === null) {
  35. checkOtherSegments(0);
  36. return;
  37. }
  38. if (astNode.type === 'error') {
  39. $scope.parserError = astNode.message + " at position: " + astNode.pos;
  40. $scope.target.textEditor = true;
  41. return;
  42. }
  43. try {
  44. parseTargeRecursive(astNode);
  45. }
  46. catch (err) {
  47. console.log('error parsing target:', err.message);
  48. $scope.parserError = err.message;
  49. $scope.target.textEditor = true;
  50. }
  51. checkOtherSegments($scope.segments.length - 1);
  52. }
  53. function addFunctionParameter(func, value, index, shiftBack) {
  54. if (shiftBack) {
  55. index = Math.max(index - 1, 0);
  56. }
  57. func.params[index] = value;
  58. }
  59. function parseTargeRecursive(astNode, func, index) {
  60. if (astNode === null) {
  61. return null;
  62. }
  63. switch(astNode.type) {
  64. case 'function':
  65. var innerFunc = gfunc.createFuncInstance(astNode.name, { withDefaultParams: false });
  66. _.each(astNode.params, function(param, index) {
  67. parseTargeRecursive(param, innerFunc, index);
  68. });
  69. innerFunc.updateText();
  70. $scope.functions.push(innerFunc);
  71. break;
  72. case 'series-ref':
  73. addFunctionParameter(func, astNode.value, index, $scope.segments.length > 0);
  74. break;
  75. case 'bool':
  76. case 'string':
  77. case 'number':
  78. if ((index-1) >= func.def.params.length) {
  79. throw { message: 'invalid number of parameters to method ' + func.def.name };
  80. }
  81. addFunctionParameter(func, astNode.value, index, true);
  82. break;
  83. case 'metric':
  84. if ($scope.segments.length > 0) {
  85. if (astNode.segments.length !== 1) {
  86. throw { message: 'Multiple metric params not supported, use text editor.' };
  87. }
  88. addFunctionParameter(func, astNode.segments[0].value, index, true);
  89. break;
  90. }
  91. $scope.segments = _.map(astNode.segments, function(segment) {
  92. return uiSegmentSrv.newSegment(segment);
  93. });
  94. }
  95. }
  96. function getSegmentPathUpTo(index) {
  97. var arr = $scope.segments.slice(0, index);
  98. return _.reduce(arr, function(result, segment) {
  99. return result ? (result + "." + segment.value) : segment.value;
  100. }, "");
  101. }
  102. function checkOtherSegments(fromIndex) {
  103. if (fromIndex === 0) {
  104. $scope.segments.push(uiSegmentSrv.newSelectMetric());
  105. return;
  106. }
  107. var path = getSegmentPathUpTo(fromIndex + 1);
  108. return panelCtrl.datasource.metricFindQuery(path)
  109. .then(function(segments) {
  110. if (segments.length === 0) {
  111. if (path !== '') {
  112. $scope.segments = $scope.segments.splice(0, fromIndex);
  113. $scope.segments.push(uiSegmentSrv.newSelectMetric());
  114. }
  115. } else if (segments[0].expandable) {
  116. if ($scope.segments.length === fromIndex) {
  117. $scope.segments.push(uiSegmentSrv.newSelectMetric());
  118. }
  119. else {
  120. return checkOtherSegments(fromIndex + 1);
  121. }
  122. }
  123. })
  124. .then(null, function(err) {
  125. $scope.parserError = err.message || 'Failed to issue metric query';
  126. });
  127. }
  128. function setSegmentFocus(segmentIndex) {
  129. _.each($scope.segments, function(segment, index) {
  130. segment.focus = segmentIndex === index;
  131. });
  132. }
  133. function wrapFunction(target, func) {
  134. return func.render(target);
  135. }
  136. $scope.getAltSegments = function (index) {
  137. var query = index === 0 ? '*' : getSegmentPathUpTo(index) + '.*';
  138. return panelCtrl.datasource.metricFindQuery(query).then(function(segments) {
  139. var altSegments = _.map(segments, function(segment) {
  140. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  141. });
  142. if (altSegments.length === 0) { return altSegments; }
  143. // add template variables
  144. _.each(templateSrv.variables, function(variable) {
  145. altSegments.unshift(uiSegmentSrv.newSegment({
  146. type: 'template',
  147. value: '$' + variable.name,
  148. expandable: true,
  149. }));
  150. });
  151. // add wildcard option
  152. altSegments.unshift(uiSegmentSrv.newSegment('*'));
  153. return altSegments;
  154. })
  155. .then(null, function(err) {
  156. $scope.parserError = err.message || 'Failed to issue metric query';
  157. return [];
  158. });
  159. };
  160. $scope.segmentValueChanged = function (segment, segmentIndex) {
  161. delete $scope.parserError;
  162. if ($scope.functions.length > 0 && $scope.functions[0].def.fake) {
  163. $scope.functions = [];
  164. }
  165. if (segment.expandable) {
  166. return checkOtherSegments(segmentIndex + 1).then(function() {
  167. setSegmentFocus(segmentIndex + 1);
  168. $scope.targetChanged();
  169. });
  170. }
  171. else {
  172. $scope.segments = $scope.segments.splice(0, segmentIndex + 1);
  173. }
  174. setSegmentFocus(segmentIndex + 1);
  175. $scope.targetChanged();
  176. };
  177. $scope.targetTextChanged = function() {
  178. parseTarget();
  179. panelCtrl.refresh();
  180. };
  181. $scope.targetChanged = function() {
  182. if ($scope.parserError) {
  183. return;
  184. }
  185. var oldTarget = $scope.target.target;
  186. var target = getSegmentPathUpTo($scope.segments.length);
  187. $scope.target.target = _.reduce($scope.functions, wrapFunction, target);
  188. if ($scope.target.target !== oldTarget) {
  189. if ($scope.segments[$scope.segments.length - 1].value !== 'select metric') {
  190. panelCtrl.refresh();
  191. }
  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. });