query_ctrl.js 8.2 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. var panelCtrl = $scope.panelCtrl = $scope.ctrl;
  13. var datasource = $scope.datasource;
  14. $scope.init = function() {
  15. if ($scope.target) {
  16. $scope.target.target = $scope.target.target || '';
  17. parseTarget();
  18. }
  19. };
  20. $scope.toggleEditorMode = function() {
  21. $scope.target.textEditor = !$scope.target.textEditor;
  22. parseTarget();
  23. };
  24. // The way parsing and the target editor works needs
  25. // to be rewritten to handle functions that take multiple series
  26. function parseTarget() {
  27. $scope.functions = [];
  28. $scope.segments = [];
  29. delete $scope.parserError;
  30. if ($scope.target.textEditor) {
  31. return;
  32. }
  33. var parser = new Parser($scope.target.target);
  34. var astNode = parser.getAst();
  35. if (astNode === null) {
  36. checkOtherSegments(0);
  37. return;
  38. }
  39. if (astNode.type === 'error') {
  40. $scope.parserError = astNode.message + " at position: " + astNode.pos;
  41. $scope.target.textEditor = true;
  42. return;
  43. }
  44. try {
  45. parseTargeRecursive(astNode);
  46. }
  47. catch (err) {
  48. console.log('error parsing target:', err.message);
  49. $scope.parserError = err.message;
  50. $scope.target.textEditor = true;
  51. }
  52. checkOtherSegments($scope.segments.length - 1);
  53. }
  54. function addFunctionParameter(func, value, index, shiftBack) {
  55. if (shiftBack) {
  56. index = Math.max(index - 1, 0);
  57. }
  58. func.params[index] = value;
  59. }
  60. function parseTargeRecursive(astNode, func, index) {
  61. if (astNode === null) {
  62. return null;
  63. }
  64. switch(astNode.type) {
  65. case 'function':
  66. var innerFunc = gfunc.createFuncInstance(astNode.name, { withDefaultParams: false });
  67. _.each(astNode.params, function(param, index) {
  68. parseTargeRecursive(param, innerFunc, index);
  69. });
  70. innerFunc.updateText();
  71. $scope.functions.push(innerFunc);
  72. break;
  73. case 'series-ref':
  74. addFunctionParameter(func, astNode.value, index, $scope.segments.length > 0);
  75. break;
  76. case 'bool':
  77. case 'string':
  78. case 'number':
  79. if ((index-1) >= func.def.params.length) {
  80. throw { message: 'invalid number of parameters to method ' + func.def.name };
  81. }
  82. addFunctionParameter(func, astNode.value, index, true);
  83. break;
  84. case 'metric':
  85. if ($scope.segments.length > 0) {
  86. if (astNode.segments.length !== 1) {
  87. throw { message: 'Multiple metric params not supported, use text editor.' };
  88. }
  89. addFunctionParameter(func, astNode.segments[0].value, index, true);
  90. break;
  91. }
  92. $scope.segments = _.map(astNode.segments, function(segment) {
  93. return uiSegmentSrv.newSegment(segment);
  94. });
  95. }
  96. }
  97. function getSegmentPathUpTo(index) {
  98. var arr = $scope.segments.slice(0, index);
  99. return _.reduce(arr, function(result, segment) {
  100. return result ? (result + "." + segment.value) : segment.value;
  101. }, "");
  102. }
  103. function checkOtherSegments(fromIndex) {
  104. if (fromIndex === 0) {
  105. $scope.segments.push(uiSegmentSrv.newSelectMetric());
  106. return;
  107. }
  108. var path = getSegmentPathUpTo(fromIndex + 1);
  109. return datasource.metricFindQuery(path)
  110. .then(function(segments) {
  111. if (segments.length === 0) {
  112. if (path !== '') {
  113. $scope.segments = $scope.segments.splice(0, fromIndex);
  114. $scope.segments.push(uiSegmentSrv.newSelectMetric());
  115. }
  116. } else 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 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).then(function() {
  168. setSegmentFocus(segmentIndex + 1);
  169. $scope.targetChanged();
  170. });
  171. }
  172. else {
  173. $scope.segments = $scope.segments.splice(0, segmentIndex + 1);
  174. }
  175. setSegmentFocus(segmentIndex + 1);
  176. $scope.targetChanged();
  177. };
  178. $scope.targetTextChanged = function() {
  179. parseTarget();
  180. panelCtrl.refresh();
  181. };
  182. $scope.targetChanged = function() {
  183. if ($scope.parserError) {
  184. return;
  185. }
  186. var oldTarget = $scope.target.target;
  187. var target = getSegmentPathUpTo($scope.segments.length);
  188. $scope.target.target = _.reduce($scope.functions, wrapFunction, target);
  189. if ($scope.target.target !== oldTarget) {
  190. if ($scope.segments[$scope.segments.length - 1].value !== 'select metric') {
  191. panelCtrl.refresh();
  192. }
  193. }
  194. };
  195. $scope.removeFunction = function(func) {
  196. $scope.functions = _.without($scope.functions, func);
  197. $scope.targetChanged();
  198. };
  199. $scope.addFunction = function(funcDef) {
  200. var newFunc = gfunc.createFuncInstance(funcDef, { withDefaultParams: true });
  201. newFunc.added = true;
  202. $scope.functions.push(newFunc);
  203. $scope.moveAliasFuncLast();
  204. $scope.smartlyHandleNewAliasByNode(newFunc);
  205. if ($scope.segments.length === 1 && $scope.segments[0].fake) {
  206. $scope.segments = [];
  207. }
  208. if (!newFunc.params.length && newFunc.added) {
  209. $scope.targetChanged();
  210. }
  211. };
  212. $scope.moveAliasFuncLast = function() {
  213. var aliasFunc = _.find($scope.functions, function(func) {
  214. return func.def.name === 'alias' ||
  215. func.def.name === 'aliasByNode' ||
  216. func.def.name === 'aliasByMetric';
  217. });
  218. if (aliasFunc) {
  219. $scope.functions = _.without($scope.functions, aliasFunc);
  220. $scope.functions.push(aliasFunc);
  221. }
  222. };
  223. $scope.smartlyHandleNewAliasByNode = function(func) {
  224. if (func.def.name !== 'aliasByNode') {
  225. return;
  226. }
  227. for(var i = 0; i < $scope.segments.length; i++) {
  228. if ($scope.segments[i].value.indexOf('*') >= 0) {
  229. func.params[0] = i;
  230. func.added = false;
  231. $scope.targetChanged();
  232. return;
  233. }
  234. }
  235. };
  236. $scope.toggleMetricOptions = function() {
  237. $scope.panel.metricOptionsEnabled = !$scope.panel.metricOptionsEnabled;
  238. if (!$scope.panel.metricOptionsEnabled) {
  239. delete $scope.panel.cacheTimeout;
  240. }
  241. };
  242. $scope.init();
  243. });
  244. });