query_ctrl.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.name, value: item.name};
  49. });
  50. memo.push(menu);
  51. return memo;
  52. }, []);
  53. };
  54. $scope.selectMenuAction = function(selectParts, cat, subitem) {
  55. selectParts.push(queryPart.create({name: subitem.value }));
  56. };
  57. $scope.fixTagSegments = function() {
  58. var count = $scope.tagSegments.length;
  59. var lastSegment = $scope.tagSegments[Math.max(count-1, 0)];
  60. if (!lastSegment || lastSegment.type !== 'plus-button') {
  61. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  62. }
  63. };
  64. $scope.addGroupBy = function() {
  65. $scope.target.groupBy.push({type: 'tag', key: "select tag"});
  66. };
  67. $scope.removeGroupBy = function(index) {
  68. $scope.target.groupBy.splice(index, 1);
  69. $scope.get_data();
  70. };
  71. $scope.addSelect = function() {
  72. $scope.queryModel.addSelect();
  73. };
  74. $scope.removeSelect = function(index) {
  75. $scope.queryModel.removeSelect(index);
  76. $scope.get_data();
  77. };
  78. $scope.measurementChanged = function() {
  79. $scope.target.measurement = $scope.measurementSegment.value;
  80. $scope.$parent.get_data();
  81. };
  82. $scope.getFields = function() {
  83. var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS');
  84. return $scope.datasource.metricFindQuery(fieldsQuery)
  85. .then($scope.transformToSegments(false), $scope.handleQueryError);
  86. };
  87. $scope.toggleQueryMode = function () {
  88. $scope.target.rawQuery = !$scope.target.rawQuery;
  89. };
  90. $scope.getMeasurements = function () {
  91. var query = $scope.queryBuilder.buildExploreQuery('MEASUREMENTS');
  92. return $scope.datasource.metricFindQuery(query)
  93. .then($scope.transformToSegments(true), $scope.handleQueryError);
  94. };
  95. $scope.handleQueryError = function(err) {
  96. $scope.parserError = err.message || 'Failed to issue metric query';
  97. return [];
  98. };
  99. $scope.transformToSegments = function(addTemplateVars) {
  100. return function(results) {
  101. var segments = _.map(results, function(segment) {
  102. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  103. });
  104. if (addTemplateVars) {
  105. _.each(templateSrv.variables, function(variable) {
  106. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '/$' + variable.name + '$/', expandable: true }));
  107. });
  108. }
  109. return segments;
  110. };
  111. };
  112. $scope.getTagsOrValues = function(segment, index) {
  113. if (segment.type === 'condition') {
  114. return $q.when([uiSegmentSrv.newSegment('AND'), uiSegmentSrv.newSegment('OR')]);
  115. }
  116. if (segment.type === 'operator') {
  117. var nextValue = $scope.tagSegments[index+1].value;
  118. if (/^\/.*\/$/.test(nextValue)) {
  119. return $q.when(uiSegmentSrv.newOperators(['=~', '!~']));
  120. } else {
  121. return $q.when(uiSegmentSrv.newOperators(['=', '<>', '<', '>']));
  122. }
  123. }
  124. var query, addTemplateVars;
  125. if (segment.type === 'key' || segment.type === 'plus-button') {
  126. query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
  127. addTemplateVars = false;
  128. } else if (segment.type === 'value') {
  129. query = $scope.queryBuilder.buildExploreQuery('TAG_VALUES', $scope.tagSegments[index-2].value);
  130. addTemplateVars = true;
  131. }
  132. return $scope.datasource.metricFindQuery(query)
  133. .then($scope.transformToSegments(addTemplateVars))
  134. .then(function(results) {
  135. if (segment.type === 'key') {
  136. results.splice(0, 0, angular.copy($scope.removeTagFilterSegment));
  137. }
  138. return results;
  139. })
  140. .then(null, $scope.handleQueryError);
  141. };
  142. $scope.getFieldSegments = function() {
  143. var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS');
  144. return $scope.datasource.metricFindQuery(fieldsQuery)
  145. .then($scope.transformToSegments(false))
  146. .then(null, $scope.handleQueryError);
  147. };
  148. $scope.getTagOptions = function() {
  149. var query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
  150. return $scope.datasource.metricFindQuery(query)
  151. .then($scope.transformToSegments(false))
  152. .then(null, $scope.handleQueryError);
  153. };
  154. $scope.setFill = function(fill) {
  155. $scope.target.fill = fill;
  156. $scope.get_data();
  157. };
  158. $scope.tagSegmentUpdated = function(segment, index) {
  159. $scope.tagSegments[index] = segment;
  160. // handle remove tag condition
  161. if (segment.value === $scope.removeTagFilterSegment.value) {
  162. $scope.tagSegments.splice(index, 3);
  163. if ($scope.tagSegments.length === 0) {
  164. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  165. } else if ($scope.tagSegments.length > 2) {
  166. $scope.tagSegments.splice(Math.max(index-1, 0), 1);
  167. if ($scope.tagSegments[$scope.tagSegments.length-1].type !== 'plus-button') {
  168. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  169. }
  170. }
  171. }
  172. else {
  173. if (segment.type === 'plus-button') {
  174. if (index > 2) {
  175. $scope.tagSegments.splice(index, 0, uiSegmentSrv.newCondition('AND'));
  176. }
  177. $scope.tagSegments.push(uiSegmentSrv.newOperator('='));
  178. $scope.tagSegments.push(uiSegmentSrv.newFake('select tag value', 'value', 'query-segment-value'));
  179. segment.type = 'key';
  180. segment.cssClass = 'query-segment-key';
  181. }
  182. if ((index+1) === $scope.tagSegments.length) {
  183. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  184. }
  185. }
  186. $scope.rebuildTargetTagConditions();
  187. };
  188. $scope.rebuildTargetTagConditions = function() {
  189. var tags = [];
  190. var tagIndex = 0;
  191. var tagOperator = "";
  192. _.each($scope.tagSegments, function(segment2, index) {
  193. if (segment2.type === 'key') {
  194. if (tags.length === 0) {
  195. tags.push({});
  196. }
  197. tags[tagIndex].key = segment2.value;
  198. }
  199. else if (segment2.type === 'value') {
  200. tagOperator = $scope.getTagValueOperator(segment2.value, tags[tagIndex].operator);
  201. if (tagOperator) {
  202. $scope.tagSegments[index-1] = uiSegmentSrv.newOperator(tagOperator);
  203. tags[tagIndex].operator = tagOperator;
  204. }
  205. tags[tagIndex].value = segment2.value;
  206. }
  207. else if (segment2.type === 'condition') {
  208. tags.push({ condition: segment2.value });
  209. tagIndex += 1;
  210. }
  211. else if (segment2.type === 'operator') {
  212. tags[tagIndex].operator = segment2.value;
  213. }
  214. });
  215. $scope.target.tags = tags;
  216. $scope.$parent.get_data();
  217. };
  218. $scope.getTagValueOperator = function(tagValue, tagOperator) {
  219. if (tagOperator !== '=~' && tagOperator !== '!~' && /^\/.*\/$/.test(tagValue)) {
  220. return '=~';
  221. }
  222. else if ((tagOperator === '=~' || tagOperator === '!~') && /^(?!\/.*\/$)/.test(tagValue)) {
  223. return '=';
  224. }
  225. };
  226. $scope.init();
  227. });
  228. });