queryCtrl.js 9.2 KB

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