query_ctrl.js 8.8 KB

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