queryCtrl.js 9.3 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. return $scope.datasource.metricFindQuery('SHOW MEASUREMENTS', 'MEASUREMENTS')
  88. .then($scope.transformToSegments)
  89. .then($scope.addTemplateVariableSegments)
  90. .then(null, $scope.handleQueryError);
  91. };
  92. $scope.handleQueryError = function(err) {
  93. $scope.parserError = err.message || 'Failed to issue metric query';
  94. return [];
  95. };
  96. $scope.transformToSegments = function(results) {
  97. return _.map(results, function(segment) {
  98. return new MetricSegment({ value: segment.text, expandable: segment.expandable });
  99. });
  100. };
  101. $scope.addTemplateVariableSegments = function(segments) {
  102. _.each(templateSrv.variables, function(variable) {
  103. segments.unshift(new MetricSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  104. });
  105. return segments;
  106. };
  107. $scope.getTagsOrValues = function(segment, index) {
  108. var query, queryType;
  109. if (segment.type === 'key' || segment.type === 'plus-button') {
  110. queryType = 'TAG_KEYS';
  111. query = $scope.queryBuilder.showTagsQuery();
  112. } else if (segment.type === 'value') {
  113. queryType = 'TAG_VALUES';
  114. query = 'SHOW TAG VALUES FROM "' + $scope.target.measurement + '" WITH KEY = ' + $scope.tagSegments[index-2].value;
  115. } else if (segment.type === 'condition') {
  116. return $q.when([new MetricSegment('AND'), new MetricSegment('OR')]);
  117. }
  118. else {
  119. return $q.when([]);
  120. }
  121. return $scope.datasource.metricFindQuery(query, queryType)
  122. .then($scope.transformToSegments)
  123. .then($scope.addTemplateVariableSegments)
  124. .then(function(results) {
  125. if (queryType === 'TAG_KEYS' && segment.type === 'key') {
  126. results.splice(0, 0, angular.copy($scope.removeTagFilterSegment));
  127. }
  128. return results;
  129. })
  130. .then(null, $scope.handleQueryError);
  131. };
  132. $scope.getGroupByTagSegments = function(segment) {
  133. var query = 'SHOW TAG KEYS FROM "' + $scope.target.measurement + '"';
  134. return $scope.datasource.metricFindQuery(query, 'TAG_KEYS')
  135. .then($scope.transformToSegments)
  136. .then($scope.addTemplateVariableSegments)
  137. .then(function(results) {
  138. if (segment.type !== 'plus-button') {
  139. results.splice(0, 0, angular.copy($scope.removeGroupBySegment));
  140. }
  141. return results;
  142. })
  143. .then(null, $scope.handleQueryError);
  144. };
  145. $scope.tagSegmentUpdated = function(segment, index) {
  146. $scope.tagSegments[index] = segment;
  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.newFake('=', 'operator', 'query-segment-operator'));
  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) {
  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. }
  187. else if (segment2.type === 'condition') {
  188. tags.push({ condition: segment2.value });
  189. tagIndex += 1;
  190. }
  191. });
  192. $scope.target.tags = tags;
  193. $scope.$parent.get_data();
  194. };
  195. function MetricSegment(options) {
  196. if (options === '*' || options.value === '*') {
  197. this.value = '*';
  198. this.html = $sce.trustAsHtml('<i class="fa fa-asterisk"><i>');
  199. this.expandable = true;
  200. return;
  201. }
  202. if (_.isString(options)) {
  203. this.value = options;
  204. this.html = $sce.trustAsHtml(this.value);
  205. return;
  206. }
  207. this.cssClass = options.cssClass;
  208. this.type = options.type;
  209. this.fake = options.fake;
  210. this.value = options.value;
  211. this.type = options.type;
  212. this.expandable = options.expandable;
  213. this.html = options.html || $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
  214. }
  215. MetricSegment.newSelectMeasurement = function() {
  216. return new MetricSegment({value: 'select measurement', fake: true});
  217. };
  218. MetricSegment.newFake = function(text, type, cssClass) {
  219. return new MetricSegment({value: text, fake: true, type: type, cssClass: cssClass});
  220. };
  221. MetricSegment.newCondition = function(condition) {
  222. return new MetricSegment({value: condition, type: 'condition', cssClass: 'query-keyword' });
  223. };
  224. MetricSegment.newPlusButton = function() {
  225. return new MetricSegment({fake: true, html: '<i class="fa fa-plus"></i>', type: 'plus-button' });
  226. };
  227. MetricSegment.newSelectTagValue = function() {
  228. return new MetricSegment({value: 'select tag value', fake: true});
  229. };
  230. });
  231. });