query_ctrl.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.getFields = function() {
  107. var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS');
  108. return $scope.datasource.metricFindQuery(fieldsQuery)
  109. .then($scope.transformToSegments(false), $scope.handleQueryError);
  110. };
  111. $scope.toggleQueryMode = function () {
  112. $scope.target.rawQuery = !$scope.target.rawQuery;
  113. };
  114. $scope.getMeasurements = function () {
  115. var query = $scope.queryBuilder.buildExploreQuery('MEASUREMENTS');
  116. return $scope.datasource.metricFindQuery(query)
  117. .then($scope.transformToSegments(true), $scope.handleQueryError);
  118. };
  119. $scope.handleQueryError = function(err) {
  120. $scope.parserError = err.message || 'Failed to issue metric query';
  121. return [];
  122. };
  123. $scope.transformToSegments = function(addTemplateVars) {
  124. return function(results) {
  125. var segments = _.map(results, function(segment) {
  126. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  127. });
  128. if (addTemplateVars) {
  129. _.each(templateSrv.variables, function(variable) {
  130. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '/$' + variable.name + '$/', expandable: true }));
  131. });
  132. }
  133. return segments;
  134. };
  135. };
  136. $scope.getTagsOrValues = function(segment, index) {
  137. if (segment.type === 'condition') {
  138. return $q.when([uiSegmentSrv.newSegment('AND'), uiSegmentSrv.newSegment('OR')]);
  139. }
  140. if (segment.type === 'operator') {
  141. var nextValue = $scope.tagSegments[index+1].value;
  142. if (/^\/.*\/$/.test(nextValue)) {
  143. return $q.when(uiSegmentSrv.newOperators(['=~', '!~']));
  144. } else {
  145. return $q.when(uiSegmentSrv.newOperators(['=', '<>', '<', '>']));
  146. }
  147. }
  148. var query, addTemplateVars;
  149. if (segment.type === 'key' || segment.type === 'plus-button') {
  150. query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
  151. addTemplateVars = false;
  152. } else if (segment.type === 'value') {
  153. query = $scope.queryBuilder.buildExploreQuery('TAG_VALUES', $scope.tagSegments[index-2].value);
  154. addTemplateVars = true;
  155. }
  156. return $scope.datasource.metricFindQuery(query)
  157. .then($scope.transformToSegments(addTemplateVars))
  158. .then(function(results) {
  159. if (segment.type === 'key') {
  160. results.splice(0, 0, angular.copy($scope.removeTagFilterSegment));
  161. }
  162. return results;
  163. })
  164. .then(null, $scope.handleQueryError);
  165. };
  166. $scope.getFieldSegments = function() {
  167. var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS');
  168. return $scope.datasource.metricFindQuery(fieldsQuery)
  169. .then($scope.transformToSegments(false))
  170. .then(null, $scope.handleQueryError);
  171. };
  172. $scope.getTagOptions = function() {
  173. };
  174. $scope.setFill = function(fill) {
  175. $scope.target.fill = fill;
  176. $scope.get_data();
  177. };
  178. $scope.tagSegmentUpdated = function(segment, index) {
  179. $scope.tagSegments[index] = segment;
  180. // handle remove tag condition
  181. if (segment.value === $scope.removeTagFilterSegment.value) {
  182. $scope.tagSegments.splice(index, 3);
  183. if ($scope.tagSegments.length === 0) {
  184. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  185. } else if ($scope.tagSegments.length > 2) {
  186. $scope.tagSegments.splice(Math.max(index-1, 0), 1);
  187. if ($scope.tagSegments[$scope.tagSegments.length-1].type !== 'plus-button') {
  188. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  189. }
  190. }
  191. }
  192. else {
  193. if (segment.type === 'plus-button') {
  194. if (index > 2) {
  195. $scope.tagSegments.splice(index, 0, uiSegmentSrv.newCondition('AND'));
  196. }
  197. $scope.tagSegments.push(uiSegmentSrv.newOperator('='));
  198. $scope.tagSegments.push(uiSegmentSrv.newFake('select tag value', 'value', 'query-segment-value'));
  199. segment.type = 'key';
  200. segment.cssClass = 'query-segment-key';
  201. }
  202. if ((index+1) === $scope.tagSegments.length) {
  203. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  204. }
  205. }
  206. $scope.rebuildTargetTagConditions();
  207. };
  208. $scope.rebuildTargetTagConditions = function() {
  209. var tags = [];
  210. var tagIndex = 0;
  211. var tagOperator = "";
  212. _.each($scope.tagSegments, function(segment2, index) {
  213. if (segment2.type === 'key') {
  214. if (tags.length === 0) {
  215. tags.push({});
  216. }
  217. tags[tagIndex].key = segment2.value;
  218. }
  219. else if (segment2.type === 'value') {
  220. tagOperator = $scope.getTagValueOperator(segment2.value, tags[tagIndex].operator);
  221. if (tagOperator) {
  222. $scope.tagSegments[index-1] = uiSegmentSrv.newOperator(tagOperator);
  223. tags[tagIndex].operator = tagOperator;
  224. }
  225. tags[tagIndex].value = segment2.value;
  226. }
  227. else if (segment2.type === 'condition') {
  228. tags.push({ condition: segment2.value });
  229. tagIndex += 1;
  230. }
  231. else if (segment2.type === 'operator') {
  232. tags[tagIndex].operator = segment2.value;
  233. }
  234. });
  235. $scope.target.tags = tags;
  236. $scope.$parent.get_data();
  237. };
  238. $scope.getTagValueOperator = function(tagValue, tagOperator) {
  239. if (tagOperator !== '=~' && tagOperator !== '!~' && /^\/.*\/$/.test(tagValue)) {
  240. return '=~';
  241. }
  242. else if ((tagOperator === '=~' || tagOperator === '!~') && /^(?!\/.*\/$)/.test(tagValue)) {
  243. return '=';
  244. }
  245. };
  246. $scope.init();
  247. });
  248. });