queryCtrl.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. define([
  2. 'angular',
  3. 'lodash',
  4. './queryBuilder',
  5. ],
  6. function (angular, _, InfluxQueryBuilder) {
  7. 'use strict';
  8. var module = angular.module('grafana.controllers');
  9. module.controller('InfluxQueryCtrl', function($scope, $timeout, $sce, templateSrv, $q) {
  10. $scope.functionList = [
  11. 'count', 'mean', 'sum', 'min', 'max', 'mode', 'distinct', 'median',
  12. 'derivative', 'stddev', 'first', 'last', 'difference'
  13. ];
  14. $scope.functionMenu = _.map($scope.functionList, function(func) {
  15. return { text: func, click: "changeFunction('" + func + "');" };
  16. });
  17. $scope.init = function() {
  18. var target = $scope.target;
  19. target.function = target.function || 'mean';
  20. target.tags = target.tags || [];
  21. target.groupByTags = target.groupByTags || [];
  22. $scope.queryBuilder = new InfluxQueryBuilder(target);
  23. if (!target.measurement) {
  24. $scope.measurementSegment = MetricSegment.newSelectMeasurement();
  25. } else {
  26. $scope.measurementSegment = new MetricSegment(target.measurement);
  27. }
  28. $scope.tagSegments = [];
  29. _.each(target.tags, function(tag) {
  30. if (tag.condition) {
  31. $scope.tagSegments.push(MetricSegment.newCondition(tag.condition));
  32. }
  33. $scope.tagSegments.push(new MetricSegment({value: tag.key, type: 'key', cssClass: 'query-segment-key' }));
  34. $scope.tagSegments.push(new MetricSegment.newOperator("="));
  35. $scope.tagSegments.push(new MetricSegment({value: tag.value, type: 'value', cssClass: 'query-segment-value'}));
  36. });
  37. $scope.fixTagSegments();
  38. $scope.groupBySegments = [];
  39. _.each(target.groupByTags, function(tag) {
  40. $scope.groupBySegments.push(new MetricSegment(tag));
  41. });
  42. $scope.groupBySegments.push(MetricSegment.newPlusButton());
  43. $scope.removeTagFilterSegment = new MetricSegment({fake: true, value: '-- remove tag filter --'});
  44. $scope.removeGroupBySegment = new MetricSegment({fake: true, value: '-- remove group by --'});
  45. };
  46. $scope.fixTagSegments = function() {
  47. var count = $scope.tagSegments.length;
  48. var lastSegment = $scope.tagSegments[Math.max(count-1, 0)];
  49. if (!lastSegment || lastSegment.type !== 'plus-button') {
  50. $scope.tagSegments.push(MetricSegment.newPlusButton());
  51. }
  52. };
  53. $scope.groupByTagUpdated = function(segment, index) {
  54. if (segment.value === $scope.removeGroupBySegment.value) {
  55. $scope.target.groupByTags.splice(index, 1);
  56. $scope.groupBySegments.splice(index, 1);
  57. $scope.$parent.get_data();
  58. return;
  59. }
  60. if (index === $scope.groupBySegments.length-1) {
  61. $scope.groupBySegments.push(MetricSegment.newPlusButton());
  62. }
  63. segment.type = 'group-by-key';
  64. segment.fake = false;
  65. $scope.target.groupByTags[index] = segment.value;
  66. $scope.$parent.get_data();
  67. };
  68. $scope.changeFunction = function(func) {
  69. $scope.target.function = func;
  70. $scope.$parent.get_data();
  71. };
  72. $scope.measurementChanged = function() {
  73. $scope.target.measurement = $scope.measurementSegment.value;
  74. $scope.$parent.get_data();
  75. };
  76. $scope.toggleQueryMode = function () {
  77. $scope.target.rawQuery = !$scope.target.rawQuery;
  78. };
  79. $scope.moveMetricQuery = function(fromIndex, toIndex) {
  80. _.move($scope.panel.targets, fromIndex, toIndex);
  81. };
  82. $scope.duplicate = function() {
  83. var clone = angular.copy($scope.target);
  84. $scope.panel.targets.push(clone);
  85. };
  86. $scope.getMeasurements = function () {
  87. var query = $scope.queryBuilder.buildExploreQuery('MEASUREMENTS');
  88. return $scope.datasource.metricFindQuery(query)
  89. .then($scope.transformToSegments)
  90. .then($scope.addTemplateVariableSegments)
  91. .then(null, $scope.handleQueryError);
  92. };
  93. $scope.handleQueryError = function(err) {
  94. $scope.parserError = err.message || 'Failed to issue metric query';
  95. return [];
  96. };
  97. $scope.transformToSegments = function(results) {
  98. return _.map(results, function(segment) {
  99. return new MetricSegment({ value: segment.text, expandable: segment.expandable });
  100. });
  101. };
  102. $scope.addTemplateVariableSegments = function(segments) {
  103. _.each(templateSrv.variables, function(variable) {
  104. segments.unshift(new MetricSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  105. });
  106. return segments;
  107. };
  108. $scope.getTagsOrValues = function(segment, index) {
  109. var query;
  110. if (segment.type === 'key' || segment.type === 'plus-button') {
  111. query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
  112. } else if (segment.type === 'value') {
  113. query = $scope.queryBuilder.buildExploreQuery('TAG_VALUES', $scope.tagSegments[index-2].value);
  114. } else if (segment.type === 'condition') {
  115. return $q.when([new MetricSegment('AND'), new MetricSegment('OR')]);
  116. }
  117. else {
  118. return $q.when([]);
  119. }
  120. return $scope.datasource.metricFindQuery(query)
  121. .then($scope.transformToSegments)
  122. .then($scope.addTemplateVariableSegments)
  123. .then(function(results) {
  124. if (segment.type === 'key') {
  125. results.splice(0, 0, angular.copy($scope.removeTagFilterSegment));
  126. }
  127. return results;
  128. })
  129. .then(null, $scope.handleQueryError);
  130. };
  131. $scope.getGroupByTagSegments = function(segment) {
  132. var query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
  133. return $scope.datasource.metricFindQuery(query)
  134. .then($scope.transformToSegments)
  135. .then($scope.addTemplateVariableSegments)
  136. .then(function(results) {
  137. if (segment.type !== 'plus-button') {
  138. results.splice(0, 0, angular.copy($scope.removeGroupBySegment));
  139. }
  140. return results;
  141. })
  142. .then(null, $scope.handleQueryError);
  143. };
  144. $scope.tagSegmentUpdated = function(segment, index) {
  145. $scope.tagSegments[index] = segment;
  146. // handle remove tag condition
  147. if (segment.value === $scope.removeTagFilterSegment.value) {
  148. $scope.tagSegments.splice(index, 3);
  149. if ($scope.tagSegments.length === 0) {
  150. $scope.tagSegments.push(MetricSegment.newPlusButton());
  151. } else if ($scope.tagSegments.length > 2) {
  152. $scope.tagSegments.splice(Math.max(index-1, 0), 1);
  153. if ($scope.tagSegments[$scope.tagSegments.length-1].type !== 'plus-button') {
  154. $scope.tagSegments.push(MetricSegment.newPlusButton());
  155. }
  156. }
  157. }
  158. else {
  159. if (segment.type === 'plus-button') {
  160. if (index > 2) {
  161. $scope.tagSegments.splice(index, 0, MetricSegment.newCondition('AND'));
  162. }
  163. $scope.tagSegments.push(MetricSegment.newOperator('='));
  164. $scope.tagSegments.push(MetricSegment.newFake('select tag value', 'value', 'query-segment-value'));
  165. segment.type = 'key';
  166. segment.cssClass = 'query-segment-key';
  167. }
  168. if ((index+1) === $scope.tagSegments.length) {
  169. $scope.tagSegments.push(MetricSegment.newPlusButton());
  170. }
  171. }
  172. $scope.rebuildTargetTagConditions();
  173. };
  174. $scope.rebuildTargetTagConditions = function() {
  175. var tags = [];
  176. var tagIndex = 0;
  177. _.each($scope.tagSegments, function(segment2, index) {
  178. if (segment2.type === 'key') {
  179. if (tags.length === 0) {
  180. tags.push({});
  181. }
  182. tags[tagIndex].key = segment2.value;
  183. }
  184. else if (segment2.type === 'value') {
  185. tags[tagIndex].value = segment2.value;
  186. $scope.tagSegments[index-1] = $scope.getTagValueOperator(segment2.value);
  187. }
  188. else if (segment2.type === 'condition') {
  189. tags.push({ condition: segment2.value });
  190. tagIndex += 1;
  191. }
  192. });
  193. $scope.target.tags = tags;
  194. $scope.$parent.get_data();
  195. };
  196. $scope.getTagValueOperator = function(tagValue) {
  197. if (tagValue[0] === '/' && tagValue[tagValue.length - 1] === '/') {
  198. return MetricSegment.newOperator('=~');
  199. }
  200. return MetricSegment.newOperator('=');
  201. };
  202. function MetricSegment(options) {
  203. if (options === '*' || options.value === '*') {
  204. this.value = '*';
  205. this.html = $sce.trustAsHtml('<i class="fa fa-asterisk"><i>');
  206. this.expandable = true;
  207. return;
  208. }
  209. if (_.isString(options)) {
  210. this.value = options;
  211. this.html = $sce.trustAsHtml(this.value);
  212. return;
  213. }
  214. this.cssClass = options.cssClass;
  215. this.type = options.type;
  216. this.fake = options.fake;
  217. this.value = options.value;
  218. this.type = options.type;
  219. this.expandable = options.expandable;
  220. this.html = options.html || $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
  221. }
  222. MetricSegment.newSelectMeasurement = function() {
  223. return new MetricSegment({value: 'select measurement', fake: true});
  224. };
  225. MetricSegment.newFake = function(text, type, cssClass) {
  226. return new MetricSegment({value: text, fake: true, type: type, cssClass: cssClass});
  227. };
  228. MetricSegment.newCondition = function(condition) {
  229. return new MetricSegment({value: condition, type: 'condition', cssClass: 'query-keyword' });
  230. };
  231. MetricSegment.newOperator = function(op) {
  232. return new MetricSegment({value: op, type: 'operator', cssClass: 'query-segment-operator' });
  233. };
  234. MetricSegment.newPlusButton = function() {
  235. return new MetricSegment({fake: true, html: '<i class="fa fa-plus "></i>', type: 'plus-button' });
  236. };
  237. MetricSegment.newSelectTagValue = function() {
  238. return new MetricSegment({value: 'select tag value', fake: true});
  239. };
  240. });
  241. });