query_ctrl.js 10.0 KB

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