queryCtrl.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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' }));
  32. $scope.tagSegments.push(new MetricSegment({fake: true, value: "="}));
  33. $scope.tagSegments.push(new MetricSegment({value: tag.value, type: 'value'}));
  34. });
  35. if ($scope.tagSegments.length % 3 === 0) {
  36. $scope.tagSegments.push(MetricSegment.newPlusButton());
  37. }
  38. $scope.groupBySegments = [];
  39. _.each(target.groupByTags, function(tag) {
  40. $scope.groupBySegments.push(new MetricSegment(tag));
  41. });
  42. $scope.groupBySegments.push(MetricSegment.newPlusButton());
  43. };
  44. $scope.groupByTagUpdated = function(segment, index) {
  45. if (index === $scope.groupBySegments.length-1) {
  46. $scope.groupBySegments.push(MetricSegment.newPlusButton());
  47. }
  48. };
  49. $scope.changeFunction = function(func) {
  50. $scope.target.function = func;
  51. $scope.$parent.get_data();
  52. };
  53. $scope.measurementChanged = function() {
  54. $scope.target.measurement = $scope.measurementSegment.value;
  55. console.log('measurement updated', $scope.target.measurement);
  56. $scope.$parent.get_data();
  57. };
  58. $scope.toggleQueryMode = function () {
  59. $scope.target.rawQuery = !$scope.target.rawQuery;
  60. };
  61. $scope.moveMetricQuery = function(fromIndex, toIndex) {
  62. _.move($scope.panel.targets, fromIndex, toIndex);
  63. };
  64. $scope.duplicate = function() {
  65. var clone = angular.copy($scope.target);
  66. $scope.panel.targets.push(clone);
  67. };
  68. $scope.getMeasurements = function () {
  69. return $scope.datasource.metricFindQuery('SHOW MEASUREMENTS', 'MEASUREMENTS')
  70. .then($scope.transformToSegments)
  71. .then($scope.addTemplateVariableSegments)
  72. .then(null, $scope.handleQueryError);
  73. };
  74. $scope.handleQueryError = function(err) {
  75. $scope.parserError = err.message || 'Failed to issue metric query';
  76. return [];
  77. };
  78. $scope.transformToSegments = function(results) {
  79. return _.map(results, function(segment) {
  80. return new MetricSegment({ value: segment.text, expandable: segment.expandable });
  81. });
  82. };
  83. $scope.addTemplateVariableSegments = function(segments) {
  84. _.each(templateSrv.variables, function(variable) {
  85. segments.unshift(new MetricSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  86. });
  87. return segments;
  88. };
  89. $scope.getTagsOrValues = function(segment, index) {
  90. var query, queryType;
  91. if (segment.type === 'key' || segment.type === 'plus-button') {
  92. queryType = 'TAG_KEYS';
  93. query = 'SHOW TAG KEYS FROM "' + $scope.target.measurement + '"';
  94. } else if (segment.type === 'value') {
  95. queryType = 'TAG_VALUES';
  96. query = 'SHOW TAG VALUES FROM "' + $scope.target.measurement + '" WITH KEY = ' + $scope.tagSegments[index-2].value;
  97. } else if (segment.type === 'condition') {
  98. return $q.when([new MetricSegment('AND'), new MetricSegment('OR')]);
  99. }
  100. else {
  101. return $q.when([]);
  102. }
  103. return $scope.datasource.metricFindQuery(query, queryType)
  104. .then($scope.transformToSegments)
  105. .then($scope.addTemplateVariableSegments)
  106. .then(function(results) {
  107. if (queryType === 'TAG_KEYS' && segment.type !== 'plus-button') {
  108. results.push(new MetricSegment({fake: true, value: 'remove tag filter'}));
  109. }
  110. return results;
  111. })
  112. .then(null, $scope.handleQueryError);
  113. };
  114. $scope.tagSegmentUpdated = function(segment, index) {
  115. $scope.tagSegments[index] = segment;
  116. if (segment.value === 'remove tag filter') {
  117. $scope.tagSegments.splice(index, 3);
  118. if ($scope.tagSegments.length === 0) {
  119. $scope.tagSegments.push(MetricSegment.newPlusButton());
  120. } else {
  121. $scope.tagSegments.splice(index-1, 1);
  122. $scope.tagSegments.push(MetricSegment.newPlusButton());
  123. }
  124. }
  125. else {
  126. if (segment.type === 'plus-button') {
  127. if (index > 2) {
  128. $scope.tagSegments.splice(index, 0, MetricSegment.newCondition('AND'));
  129. }
  130. $scope.tagSegments.push(new MetricSegment({fake: true, value: '=', type: 'operator'}));
  131. $scope.tagSegments.push(new MetricSegment({fake: true, value: 'select tag value', type: 'value' }));
  132. segment.type = 'key';
  133. }
  134. if ((index+1) === $scope.tagSegments.length) {
  135. $scope.tagSegments.push(MetricSegment.newPlusButton());
  136. }
  137. }
  138. $scope.rebuildTargetTagConditions();
  139. };
  140. $scope.rebuildTargetTagConditions = function() {
  141. var tags = [{}];
  142. var tagIndex = 0;
  143. _.each($scope.tagSegments, function(segment2) {
  144. if (segment2.type === 'key') {
  145. tags[tagIndex].key = segment2.value;
  146. }
  147. else if (segment2.type === 'value') {
  148. tags[tagIndex].value = segment2.value;
  149. }
  150. else if (segment2.type === 'condition') {
  151. tags.push({ condition: segment2.value });
  152. tagIndex += 1;
  153. }
  154. });
  155. $scope.target.tags = tags;
  156. $scope.$parent.get_data();
  157. };
  158. function MetricSegment(options) {
  159. if (options === '*' || options.value === '*') {
  160. this.value = '*';
  161. this.html = $sce.trustAsHtml('<i class="fa fa-asterisk"><i>');
  162. this.expandable = true;
  163. return;
  164. }
  165. if (_.isString(options)) {
  166. this.value = options;
  167. this.html = $sce.trustAsHtml(this.value);
  168. return;
  169. }
  170. this.cssClass = options.cssClass;
  171. this.type = options.type;
  172. this.fake = options.fake;
  173. this.value = options.value;
  174. this.type = options.type;
  175. this.expandable = options.expandable;
  176. this.html = options.html || $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
  177. }
  178. MetricSegment.newSelectMeasurement = function() {
  179. return new MetricSegment({value: 'select measurement', fake: true});
  180. };
  181. MetricSegment.newCondition = function(condition) {
  182. return new MetricSegment({value: condition, type: 'condition', cssClass: 'query-keyword' });
  183. };
  184. MetricSegment.newPlusButton = function() {
  185. return new MetricSegment({fake: true, html: '<i class="fa fa-plus"></i>', type: 'plus-button' });
  186. };
  187. MetricSegment.newSelectTagValue = function() {
  188. return new MetricSegment({value: 'select tag value', fake: true});
  189. };
  190. });
  191. });