query_ctrl.js 11 KB

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