queryCtrl.js 10 KB

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