query_ctrl.js 10 KB

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