query_ctrl.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. define([
  2. 'angular',
  3. 'lodash',
  4. './query_builder',
  5. './influx_query',
  6. './query_part_editor',
  7. ],
  8. function (angular, _, InfluxQueryBuilder, InfluxQuery) {
  9. 'use strict';
  10. var module = angular.module('grafana.controllers');
  11. module.controller('InfluxQueryCtrl', function($scope, templateSrv, $q, uiSegmentSrv) {
  12. $scope.init = function() {
  13. if (!$scope.target) { return; }
  14. $scope.target = $scope.target;
  15. $scope.queryModel = new InfluxQuery($scope.target);
  16. $scope.queryBuilder = new InfluxQueryBuilder($scope.target);
  17. if (!$scope.target.measurement) {
  18. $scope.measurementSegment = uiSegmentSrv.newSelectMeasurement();
  19. } else {
  20. $scope.measurementSegment = uiSegmentSrv.newSegment($scope.target.measurement);
  21. }
  22. $scope.tagSegments = [];
  23. _.each($scope.target.tags, function(tag) {
  24. if (!tag.operator) {
  25. if (/^\/.*\/$/.test(tag.value)) {
  26. tag.operator = "=~";
  27. } else {
  28. tag.operator = '=';
  29. }
  30. }
  31. if (tag.condition) {
  32. $scope.tagSegments.push(uiSegmentSrv.newCondition(tag.condition));
  33. }
  34. $scope.tagSegments.push(uiSegmentSrv.newKey(tag.key));
  35. $scope.tagSegments.push(uiSegmentSrv.newOperator(tag.operator));
  36. $scope.tagSegments.push(uiSegmentSrv.newKeyValue(tag.value));
  37. });
  38. $scope.fixTagSegments();
  39. $scope.removeTagFilterSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove tag filter --'});
  40. };
  41. $scope.fixTagSegments = function() {
  42. var count = $scope.tagSegments.length;
  43. var lastSegment = $scope.tagSegments[Math.max(count-1, 0)];
  44. if (!lastSegment || lastSegment.type !== 'plus-button') {
  45. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  46. }
  47. };
  48. $scope.addGroupBy = function() {
  49. $scope.target.groupBy.push({type: 'tag', key: "select tag"});
  50. };
  51. $scope.removeGroupBy = function(index) {
  52. $scope.target.groupBy.splice(index, 1);
  53. $scope.get_data();
  54. };
  55. $scope.addSelect = function() {
  56. $scope.queryModel.addSelect();
  57. };
  58. $scope.removeSelect = function(index) {
  59. $scope.queryModel.removeSelect(index);
  60. $scope.get_data();
  61. };
  62. $scope.measurementChanged = function() {
  63. $scope.target.measurement = $scope.measurementSegment.value;
  64. $scope.$parent.get_data();
  65. };
  66. $scope.getFields = function() {
  67. var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS');
  68. return $scope.datasource.metricFindQuery(fieldsQuery)
  69. .then($scope.transformToSegments(false), $scope.handleQueryError);
  70. };
  71. $scope.toggleQueryMode = function () {
  72. $scope.target.rawQuery = !$scope.target.rawQuery;
  73. };
  74. $scope.getMeasurements = function () {
  75. var query = $scope.queryBuilder.buildExploreQuery('MEASUREMENTS');
  76. return $scope.datasource.metricFindQuery(query)
  77. .then($scope.transformToSegments(true), $scope.handleQueryError);
  78. };
  79. $scope.handleQueryError = function(err) {
  80. $scope.parserError = err.message || 'Failed to issue metric query';
  81. return [];
  82. };
  83. $scope.transformToSegments = function(addTemplateVars) {
  84. return function(results) {
  85. var segments = _.map(results, function(segment) {
  86. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  87. });
  88. if (addTemplateVars) {
  89. _.each(templateSrv.variables, function(variable) {
  90. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '/$' + variable.name + '$/', expandable: true }));
  91. });
  92. }
  93. return segments;
  94. };
  95. };
  96. $scope.getTagsOrValues = function(segment, index) {
  97. if (segment.type === 'condition') {
  98. return $q.when([uiSegmentSrv.newSegment('AND'), uiSegmentSrv.newSegment('OR')]);
  99. }
  100. if (segment.type === 'operator') {
  101. var nextValue = $scope.tagSegments[index+1].value;
  102. if (/^\/.*\/$/.test(nextValue)) {
  103. return $q.when(uiSegmentSrv.newOperators(['=~', '!~']));
  104. } else {
  105. return $q.when(uiSegmentSrv.newOperators(['=', '<>', '<', '>']));
  106. }
  107. }
  108. var query, addTemplateVars;
  109. if (segment.type === 'key' || segment.type === 'plus-button') {
  110. query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
  111. addTemplateVars = false;
  112. } else if (segment.type === 'value') {
  113. query = $scope.queryBuilder.buildExploreQuery('TAG_VALUES', $scope.tagSegments[index-2].value);
  114. addTemplateVars = true;
  115. }
  116. return $scope.datasource.metricFindQuery(query)
  117. .then($scope.transformToSegments(addTemplateVars))
  118. .then(function(results) {
  119. if (segment.type === 'key') {
  120. results.splice(0, 0, angular.copy($scope.removeTagFilterSegment));
  121. }
  122. return results;
  123. })
  124. .then(null, $scope.handleQueryError);
  125. };
  126. $scope.getFieldSegments = function() {
  127. var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS');
  128. return $scope.datasource.metricFindQuery(fieldsQuery)
  129. .then($scope.transformToSegments(false))
  130. .then(null, $scope.handleQueryError);
  131. };
  132. $scope.getTagOptions = function() {
  133. var query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
  134. return $scope.datasource.metricFindQuery(query)
  135. .then($scope.transformToSegments(false))
  136. .then(null, $scope.handleQueryError);
  137. };
  138. $scope.setFill = function(fill) {
  139. $scope.target.fill = fill;
  140. $scope.get_data();
  141. };
  142. $scope.tagSegmentUpdated = function(segment, index) {
  143. $scope.tagSegments[index] = segment;
  144. // handle remove tag condition
  145. if (segment.value === $scope.removeTagFilterSegment.value) {
  146. $scope.tagSegments.splice(index, 3);
  147. if ($scope.tagSegments.length === 0) {
  148. $scope.tagSegments.push(uiSegmentSrv.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(uiSegmentSrv.newPlusButton());
  153. }
  154. }
  155. }
  156. else {
  157. if (segment.type === 'plus-button') {
  158. if (index > 2) {
  159. $scope.tagSegments.splice(index, 0, uiSegmentSrv.newCondition('AND'));
  160. }
  161. $scope.tagSegments.push(uiSegmentSrv.newOperator('='));
  162. $scope.tagSegments.push(uiSegmentSrv.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(uiSegmentSrv.newPlusButton());
  168. }
  169. }
  170. $scope.rebuildTargetTagConditions();
  171. };
  172. $scope.rebuildTargetTagConditions = function() {
  173. var tags = [];
  174. var tagIndex = 0;
  175. var tagOperator = "";
  176. _.each($scope.tagSegments, function(segment2, index) {
  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. tagOperator = $scope.getTagValueOperator(segment2.value, tags[tagIndex].operator);
  185. if (tagOperator) {
  186. $scope.tagSegments[index-1] = uiSegmentSrv.newOperator(tagOperator);
  187. tags[tagIndex].operator = tagOperator;
  188. }
  189. tags[tagIndex].value = segment2.value;
  190. }
  191. else if (segment2.type === 'condition') {
  192. tags.push({ condition: segment2.value });
  193. tagIndex += 1;
  194. }
  195. else if (segment2.type === 'operator') {
  196. tags[tagIndex].operator = segment2.value;
  197. }
  198. });
  199. $scope.target.tags = tags;
  200. $scope.$parent.get_data();
  201. };
  202. $scope.getTagValueOperator = function(tagValue, tagOperator) {
  203. if (tagOperator !== '=~' && tagOperator !== '!~' && /^\/.*\/$/.test(tagValue)) {
  204. return '=~';
  205. }
  206. else if ((tagOperator === '=~' || tagOperator === '!~') && /^(?!\/.*\/$)/.test(tagValue)) {
  207. return '=';
  208. }
  209. };
  210. $scope.init();
  211. });
  212. });