queryCtrl.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'config',
  5. './gfunc',
  6. './parser'
  7. ],
  8. function (angular, _, config, gfunc, Parser) {
  9. 'use strict';
  10. var module = angular.module('grafana.controllers');
  11. var targetLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  12. module.controller('GraphiteQueryCtrl', function($scope, $sce, templateSrv) {
  13. $scope.init = function() {
  14. $scope.target.target = $scope.target.target || '';
  15. $scope.targetLetters = targetLetters;
  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 addFunctionParameter(func, value, index, shiftBack) {
  47. if (shiftBack) {
  48. index = Math.max(index - 1, 0);
  49. }
  50. func.params[index] = value;
  51. }
  52. function parseTargeRecursive(astNode, func, index) {
  53. if (astNode === null) {
  54. return null;
  55. }
  56. switch(astNode.type) {
  57. case 'function':
  58. var innerFunc = gfunc.createFuncInstance(astNode.name, { withDefaultParams: false });
  59. _.each(astNode.params, function(param, index) {
  60. parseTargeRecursive(param, innerFunc, index);
  61. });
  62. innerFunc.updateText();
  63. $scope.functions.push(innerFunc);
  64. break;
  65. case 'series-ref':
  66. addFunctionParameter(func, astNode.value, index, $scope.segments.length > 0);
  67. break;
  68. case 'string':
  69. case 'number':
  70. if ((index-1) >= func.def.params.length) {
  71. throw { message: 'invalid number of parameters to method ' + func.def.name };
  72. }
  73. addFunctionParameter(func, astNode.value, index, true);
  74. break;
  75. case 'metric':
  76. if ($scope.segments.length > 0) {
  77. if (astNode.segments.length !== 1) {
  78. throw { message: 'Multiple metric params not supported, use text editor.' };
  79. }
  80. addFunctionParameter(func, astNode.segments[0].value, index, true);
  81. break;
  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. if ($scope.altSegments.length === 0) {
  139. return;
  140. }
  141. // add template variables
  142. _.each(templateSrv.variables, function(variable) {
  143. $scope.altSegments.unshift(new MetricSegment({
  144. type: 'template',
  145. value: '$' + variable.name,
  146. expandable: true,
  147. }));
  148. });
  149. // add wildcard option
  150. $scope.altSegments.unshift(new MetricSegment('*'));
  151. })
  152. .then(null, function(err) {
  153. $scope.parserError = err.message || 'Failed to issue metric query';
  154. });
  155. };
  156. $scope.segmentValueChanged = function (segment, segmentIndex) {
  157. delete $scope.parserError;
  158. if ($scope.functions.length > 0 && $scope.functions[0].def.fake) {
  159. $scope.functions = [];
  160. }
  161. if (segment.expandable) {
  162. return checkOtherSegments(segmentIndex + 1)
  163. .then(function () {
  164. setSegmentFocus(segmentIndex + 1);
  165. $scope.targetChanged();
  166. });
  167. }
  168. else {
  169. $scope.segments = $scope.segments.splice(0, segmentIndex + 1);
  170. }
  171. setSegmentFocus(segmentIndex + 1);
  172. $scope.targetChanged();
  173. };
  174. $scope.targetTextChanged = function() {
  175. parseTarget();
  176. $scope.get_data();
  177. };
  178. $scope.targetChanged = function() {
  179. if ($scope.parserError) {
  180. return;
  181. }
  182. var oldTarget = $scope.target.target;
  183. var target = getSegmentPathUpTo($scope.segments.length);
  184. $scope.target.target = _.reduce($scope.functions, wrapFunction, target);
  185. if ($scope.target.target !== oldTarget) {
  186. $scope.$parent.get_data();
  187. }
  188. };
  189. $scope.removeFunction = function(func) {
  190. $scope.functions = _.without($scope.functions, func);
  191. $scope.targetChanged();
  192. };
  193. $scope.addFunction = function(funcDef) {
  194. var newFunc = gfunc.createFuncInstance(funcDef, { withDefaultParams: true });
  195. newFunc.added = true;
  196. $scope.functions.push(newFunc);
  197. $scope.moveAliasFuncLast();
  198. $scope.smartlyHandleNewAliasByNode(newFunc);
  199. if ($scope.segments.length === 1 && $scope.segments[0].value === 'select metric') {
  200. $scope.segments = [];
  201. }
  202. if (!newFunc.params.length && newFunc.added) {
  203. $scope.targetChanged();
  204. }
  205. };
  206. $scope.moveAliasFuncLast = function() {
  207. var aliasFunc = _.find($scope.functions, function(func) {
  208. return func.def.name === 'alias' ||
  209. func.def.name === 'aliasByNode' ||
  210. func.def.name === 'aliasByMetric';
  211. });
  212. if (aliasFunc) {
  213. $scope.functions = _.without($scope.functions, aliasFunc);
  214. $scope.functions.push(aliasFunc);
  215. }
  216. };
  217. $scope.smartlyHandleNewAliasByNode = function(func) {
  218. if (func.def.name !== 'aliasByNode') {
  219. return;
  220. }
  221. for(var i = 0; i < $scope.segments.length; i++) {
  222. if ($scope.segments[i].value.indexOf('*') >= 0) {
  223. func.params[0] = i;
  224. func.added = false;
  225. $scope.targetChanged();
  226. return;
  227. }
  228. }
  229. };
  230. $scope.toggleMetricOptions = function() {
  231. $scope.panel.metricOptionsEnabled = !$scope.panel.metricOptionsEnabled;
  232. if (!$scope.panel.metricOptionsEnabled) {
  233. delete $scope.panel.cacheTimeout;
  234. }
  235. };
  236. $scope.moveMetricQuery = function(fromIndex, toIndex) {
  237. _.move($scope.panel.targets, fromIndex, toIndex);
  238. };
  239. $scope.duplicate = function() {
  240. var clone = angular.copy($scope.target);
  241. $scope.panel.targets.push(clone);
  242. };
  243. function MetricSegment(options) {
  244. if (options === '*' || options.value === '*') {
  245. this.value = '*';
  246. this.html = $sce.trustAsHtml('<i class="fa fa-asterisk"><i>');
  247. this.expandable = true;
  248. return;
  249. }
  250. if (_.isString(options)) {
  251. this.value = options;
  252. this.html = $sce.trustAsHtml(this.value);
  253. return;
  254. }
  255. this.value = options.value;
  256. this.type = options.type;
  257. this.expandable = options.expandable;
  258. this.html = $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
  259. }
  260. });
  261. module.directive('focusMe', function($timeout, $parse) {
  262. return {
  263. //scope: true, // optionally create a child scope
  264. link: function(scope, element, attrs) {
  265. var model = $parse(attrs.focusMe);
  266. scope.$watch(model, function(value) {
  267. if(value === true) {
  268. $timeout(function() {
  269. element[0].focus();
  270. });
  271. }
  272. });
  273. }
  274. };
  275. });
  276. });