queryCtrl.js 9.6 KB

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