queryCtrl.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.buildTagKeysQuery = function(target) {
  106. var query = 'SHOW TAG KEYS';
  107. if (target.measurement) {
  108. query += ' FROM "' + target.measurement + '"';
  109. }
  110. return query;
  111. };
  112. $scope.getTagsOrValues = function(segment, index) {
  113. var query, queryType;
  114. if (segment.type === 'key' || segment.type === 'plus-button') {
  115. queryType = 'TAG_KEYS';
  116. query = $scope.buildTagKeysQuery($scope.target, segment);
  117. } else if (segment.type === 'value') {
  118. queryType = 'TAG_VALUES';
  119. query = 'SHOW TAG VALUES FROM "' + $scope.target.measurement + '" WITH KEY = ' + $scope.tagSegments[index-2].value;
  120. } else if (segment.type === 'condition') {
  121. return $q.when([new MetricSegment('AND'), new MetricSegment('OR')]);
  122. }
  123. else {
  124. return $q.when([]);
  125. }
  126. return $scope.datasource.metricFindQuery(query, queryType)
  127. .then($scope.transformToSegments)
  128. .then($scope.addTemplateVariableSegments)
  129. .then(function(results) {
  130. if (queryType === 'TAG_KEYS' && segment.type === 'key') {
  131. results.splice(0, 0, angular.copy($scope.removeTagFilterSegment));
  132. }
  133. return results;
  134. })
  135. .then(null, $scope.handleQueryError);
  136. };
  137. $scope.getGroupByTagSegments = function(segment) {
  138. var query = 'SHOW TAG KEYS FROM "' + $scope.target.measurement + '"';
  139. return $scope.datasource.metricFindQuery(query, 'TAG_KEYS')
  140. .then($scope.transformToSegments)
  141. .then($scope.addTemplateVariableSegments)
  142. .then(function(results) {
  143. if (segment.type !== 'plus-button') {
  144. results.splice(0, 0, angular.copy($scope.removeGroupBySegment));
  145. }
  146. return results;
  147. })
  148. .then(null, $scope.handleQueryError);
  149. };
  150. $scope.tagSegmentUpdated = function(segment, index) {
  151. $scope.tagSegments[index] = segment;
  152. if (segment.value === $scope.removeTagFilterSegment.value) {
  153. $scope.tagSegments.splice(index, 3);
  154. if ($scope.tagSegments.length === 0) {
  155. $scope.tagSegments.push(MetricSegment.newPlusButton());
  156. } else if ($scope.tagSegments.length > 2) {
  157. $scope.tagSegments.splice(Math.max(index-1, 0), 1);
  158. if ($scope.tagSegments[$scope.tagSegments.length-1].type !== 'plus-button') {
  159. $scope.tagSegments.push(MetricSegment.newPlusButton());
  160. }
  161. }
  162. }
  163. else {
  164. if (segment.type === 'plus-button') {
  165. if (index > 2) {
  166. $scope.tagSegments.splice(index, 0, MetricSegment.newCondition('AND'));
  167. }
  168. $scope.tagSegments.push(MetricSegment.newFake('=', 'operator', 'query-segment-operator'));
  169. $scope.tagSegments.push(MetricSegment.newFake('select tag value', 'value', 'query-segment-value'));
  170. segment.type = 'key';
  171. segment.cssClass = 'query-segment-key';
  172. }
  173. if ((index+1) === $scope.tagSegments.length) {
  174. $scope.tagSegments.push(MetricSegment.newPlusButton());
  175. }
  176. }
  177. $scope.rebuildTargetTagConditions();
  178. };
  179. $scope.rebuildTargetTagConditions = function() {
  180. var tags = [];
  181. var tagIndex = 0;
  182. _.each($scope.tagSegments, function(segment2) {
  183. if (segment2.type === 'key') {
  184. if (tags.length === 0) {
  185. tags.push({});
  186. }
  187. tags[tagIndex].key = segment2.value;
  188. }
  189. else if (segment2.type === 'value') {
  190. tags[tagIndex].value = segment2.value;
  191. }
  192. else if (segment2.type === 'condition') {
  193. tags.push({ condition: segment2.value });
  194. tagIndex += 1;
  195. }
  196. });
  197. $scope.target.tags = tags;
  198. $scope.$parent.get_data();
  199. };
  200. function MetricSegment(options) {
  201. if (options === '*' || options.value === '*') {
  202. this.value = '*';
  203. this.html = $sce.trustAsHtml('<i class="fa fa-asterisk"><i>');
  204. this.expandable = true;
  205. return;
  206. }
  207. if (_.isString(options)) {
  208. this.value = options;
  209. this.html = $sce.trustAsHtml(this.value);
  210. return;
  211. }
  212. this.cssClass = options.cssClass;
  213. this.type = options.type;
  214. this.fake = options.fake;
  215. this.value = options.value;
  216. this.type = options.type;
  217. this.expandable = options.expandable;
  218. this.html = options.html || $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
  219. }
  220. MetricSegment.newSelectMeasurement = function() {
  221. return new MetricSegment({value: 'select measurement', fake: true});
  222. };
  223. MetricSegment.newFake = function(text, type, cssClass) {
  224. return new MetricSegment({value: text, fake: true, type: type, cssClass: cssClass});
  225. };
  226. MetricSegment.newCondition = function(condition) {
  227. return new MetricSegment({value: condition, type: 'condition', cssClass: 'query-keyword' });
  228. };
  229. MetricSegment.newPlusButton = function() {
  230. return new MetricSegment({fake: true, html: '<i class="fa fa-plus"></i>', type: 'plus-button' });
  231. };
  232. MetricSegment.newSelectTagValue = function() {
  233. return new MetricSegment({value: 'select tag value', fake: true});
  234. };
  235. });
  236. });