query_ctrl.js 11 KB

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