query_ctrl.js 8.5 KB

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