queryCtrl.js 9.2 KB

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