queryCtrl.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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({fake: true, value: "=", cssClass: 'query-segment-operator'}));
  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. if (segment.value === $scope.removeTagFilterSegment.value) {
  147. $scope.tagSegments.splice(index, 3);
  148. if ($scope.tagSegments.length === 0) {
  149. $scope.tagSegments.push(MetricSegment.newPlusButton());
  150. } else if ($scope.tagSegments.length > 2) {
  151. $scope.tagSegments.splice(Math.max(index-1, 0), 1);
  152. if ($scope.tagSegments[$scope.tagSegments.length-1].type !== 'plus-button') {
  153. $scope.tagSegments.push(MetricSegment.newPlusButton());
  154. }
  155. }
  156. }
  157. else {
  158. if (segment.type === 'plus-button') {
  159. if (index > 2) {
  160. $scope.tagSegments.splice(index, 0, MetricSegment.newCondition('AND'));
  161. }
  162. $scope.tagSegments.push(MetricSegment.newFake('=', 'operator', 'query-segment-operator'));
  163. $scope.tagSegments.push(MetricSegment.newFake('select tag value', 'value', 'query-segment-value'));
  164. segment.type = 'key';
  165. segment.cssClass = 'query-segment-key';
  166. }
  167. if ((index+1) === $scope.tagSegments.length) {
  168. $scope.tagSegments.push(MetricSegment.newPlusButton());
  169. }
  170. }
  171. $scope.rebuildTargetTagConditions();
  172. };
  173. $scope.rebuildTargetTagConditions = function() {
  174. var tags = [];
  175. var tagIndex = 0;
  176. _.each($scope.tagSegments, function(segment2) {
  177. if (segment2.type === 'key') {
  178. if (tags.length === 0) {
  179. tags.push({});
  180. }
  181. tags[tagIndex].key = segment2.value;
  182. }
  183. else if (segment2.type === 'value') {
  184. tags[tagIndex].value = segment2.value;
  185. }
  186. else if (segment2.type === 'condition') {
  187. tags.push({ condition: segment2.value });
  188. tagIndex += 1;
  189. }
  190. });
  191. $scope.target.tags = tags;
  192. $scope.$parent.get_data();
  193. };
  194. function MetricSegment(options) {
  195. if (options === '*' || options.value === '*') {
  196. this.value = '*';
  197. this.html = $sce.trustAsHtml('<i class="fa fa-asterisk"><i>');
  198. this.expandable = true;
  199. return;
  200. }
  201. if (_.isString(options)) {
  202. this.value = options;
  203. this.html = $sce.trustAsHtml(this.value);
  204. return;
  205. }
  206. this.cssClass = options.cssClass;
  207. this.type = options.type;
  208. this.fake = options.fake;
  209. this.value = options.value;
  210. this.type = options.type;
  211. this.expandable = options.expandable;
  212. this.html = options.html || $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
  213. }
  214. MetricSegment.newSelectMeasurement = function() {
  215. return new MetricSegment({value: 'select measurement', fake: true});
  216. };
  217. MetricSegment.newFake = function(text, type, cssClass) {
  218. return new MetricSegment({value: text, fake: true, type: type, cssClass: cssClass});
  219. };
  220. MetricSegment.newCondition = function(condition) {
  221. return new MetricSegment({value: condition, type: 'condition', cssClass: 'query-keyword' });
  222. };
  223. MetricSegment.newPlusButton = function() {
  224. return new MetricSegment({fake: true, html: '<i class="fa fa-plus "></i>', type: 'plus-button' });
  225. };
  226. MetricSegment.newSelectTagValue = function() {
  227. return new MetricSegment({value: 'select tag value', fake: true});
  228. };
  229. });
  230. });