queryCtrl.js 10.0 KB

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