queryCtrl.js 9.3 KB

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