query_ctrl.js 9.8 KB

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