queryCtrl.js 9.3 KB

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