queryCtrl.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. define([
  2. 'angular',
  3. 'lodash',
  4. './queryBuilder',
  5. ],
  6. function (angular, _, InfluxQueryBuilder) {
  7. 'use strict';
  8. var module = angular.module('grafana.controllers');
  9. module.controller('InfluxQueryCtrl', function($scope, $timeout, $sce, templateSrv, $q, uiSegmentSrv) {
  10. $scope.init = function() {
  11. if (!$scope.target) { return; }
  12. var target = $scope.target;
  13. target.tags = target.tags || [];
  14. target.groupByTags = target.groupByTags || [];
  15. target.fields = target.fields || [{
  16. name: 'value',
  17. func: target.function || 'mean'
  18. }];
  19. $scope.queryBuilder = new InfluxQueryBuilder(target);
  20. if (!target.measurement) {
  21. $scope.measurementSegment = uiSegmentSrv.newSelectMeasurement();
  22. } else {
  23. $scope.measurementSegment = uiSegmentSrv.newSegment(target.measurement);
  24. }
  25. $scope.addFieldSegment = uiSegmentSrv.newPlusButton();
  26. $scope.tagSegments = [];
  27. _.each(target.tags, function(tag) {
  28. if (!tag.operator) {
  29. if (/^\/.*\/$/.test(tag.value)) {
  30. tag.operator = "=~";
  31. } else {
  32. tag.operator = '=';
  33. }
  34. }
  35. if (tag.condition) {
  36. $scope.tagSegments.push(uiSegmentSrv.newCondition(tag.condition));
  37. }
  38. $scope.tagSegments.push(uiSegmentSrv.newKey(tag.Key));
  39. $scope.tagSegments.push(uiSegmentSrv.newOperator(tag.operator));
  40. $scope.tagSegments.push(uiSegmentSrv.newKeyValue(tag.value));
  41. });
  42. $scope.fixTagSegments();
  43. $scope.groupBySegments = [];
  44. _.each(target.groupByTags, function(tag) {
  45. $scope.groupBySegments.push(uiSegmentSrv.newSegment(tag));
  46. });
  47. $scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
  48. $scope.removeTagFilterSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove tag filter --'});
  49. $scope.removeGroupBySegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove group by --'});
  50. };
  51. $scope.fixTagSegments = function() {
  52. var count = $scope.tagSegments.length;
  53. var lastSegment = $scope.tagSegments[Math.max(count-1, 0)];
  54. if (!lastSegment || lastSegment.type !== 'plus-button') {
  55. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  56. }
  57. };
  58. $scope.groupByTagUpdated = function(segment, index) {
  59. if (segment.value === $scope.removeGroupBySegment.value) {
  60. $scope.target.groupByTags.splice(index, 1);
  61. $scope.groupBySegments.splice(index, 1);
  62. $scope.$parent.get_data();
  63. return;
  64. }
  65. if (index === $scope.groupBySegments.length-1) {
  66. $scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
  67. }
  68. segment.type = 'group-by-key';
  69. segment.fake = false;
  70. $scope.target.groupByTags[index] = segment.value;
  71. $scope.$parent.get_data();
  72. };
  73. $scope.changeFunction = function(func) {
  74. $scope.target.function = func;
  75. $scope.$parent.get_data();
  76. };
  77. $scope.measurementChanged = function() {
  78. $scope.target.measurement = $scope.measurementSegment.value;
  79. $scope.$parent.get_data();
  80. };
  81. $scope.getFields = function() {
  82. var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS');
  83. return $scope.datasource.metricFindQuery(fieldsQuery)
  84. .then(function(results) {
  85. var values = _.pluck(results, 'text');
  86. if ($scope.target.fields.length > 1) {
  87. values.splice(0, 0, "-- remove from select --");
  88. }
  89. return values;
  90. });
  91. };
  92. $scope.toggleQueryMode = function () {
  93. $scope.target.rawQuery = !$scope.target.rawQuery;
  94. };
  95. $scope.getMeasurements = function () {
  96. var query = $scope.queryBuilder.buildExploreQuery('MEASUREMENTS');
  97. return $scope.datasource.metricFindQuery(query)
  98. .then($scope.transformToSegments(true))
  99. .then(null, $scope.handleQueryError);
  100. };
  101. $scope.handleQueryError = function(err) {
  102. $scope.parserError = err.message || 'Failed to issue metric query';
  103. return [];
  104. };
  105. $scope.transformToSegments = function(addTemplateVars) {
  106. return function(results) {
  107. var segments = _.map(results, function(segment) {
  108. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  109. });
  110. if (addTemplateVars) {
  111. _.each(templateSrv.variables, function(variable) {
  112. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '/$' + variable.name + '/', expandable: true }));
  113. });
  114. }
  115. return segments;
  116. };
  117. };
  118. $scope.getTagsOrValues = function(segment, index) {
  119. if (segment.type === 'condition') {
  120. return $q.when([uiSegmentSrv.newSegment('AND'), uiSegmentSrv.newSegment('OR')]);
  121. }
  122. if (segment.type === 'operator') {
  123. var nextValue = $scope.tagSegments[index+1].value;
  124. if (/^\/.*\/$/.test(nextValue)) {
  125. return $q.when(uiSegmentSrv.newOperators(['=~', '!~']));
  126. } else {
  127. return $q.when(uiSegmentSrv.newOperators(['=', '<>', '<', '>']));
  128. }
  129. }
  130. var query, addTemplateVars;
  131. if (segment.type === 'key' || segment.type === 'plus-button') {
  132. query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
  133. addTemplateVars = false;
  134. } else if (segment.type === 'value') {
  135. query = $scope.queryBuilder.buildExploreQuery('TAG_VALUES', $scope.tagSegments[index-2].value);
  136. addTemplateVars = true;
  137. }
  138. return $scope.datasource.metricFindQuery(query)
  139. .then($scope.transformToSegments(addTemplateVars))
  140. .then(function(results) {
  141. if (segment.type === 'key') {
  142. results.splice(0, 0, angular.copy($scope.removeTagFilterSegment));
  143. }
  144. return results;
  145. })
  146. .then(null, $scope.handleQueryError);
  147. };
  148. $scope.getFieldSegments = function() {
  149. var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS');
  150. return $scope.datasource.metricFindQuery(fieldsQuery)
  151. .then($scope.transformToSegments(false))
  152. .then(null, $scope.handleQueryError);
  153. };
  154. $scope.addField = function() {
  155. $scope.target.fields.push({name: $scope.addFieldSegment.value, func: 'mean'});
  156. _.extend($scope.addFieldSegment, uiSegmentSrv.newPlusButton());
  157. };
  158. $scope.fieldChanged = function(field) {
  159. if (field.name === '-- remove from select --') {
  160. $scope.target.fields = _.without($scope.target.fields, field);
  161. }
  162. $scope.get_data();
  163. };
  164. $scope.getGroupByTagSegments = function(segment) {
  165. var query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
  166. return $scope.datasource.metricFindQuery(query)
  167. .then($scope.transformToSegments(false))
  168. .then(function(results) {
  169. if (segment.type !== 'plus-button') {
  170. results.splice(0, 0, angular.copy($scope.removeGroupBySegment));
  171. }
  172. return results;
  173. })
  174. .then(null, $scope.handleQueryError);
  175. };
  176. $scope.tagSegmentUpdated = function(segment, index) {
  177. $scope.tagSegments[index] = segment;
  178. // handle remove tag condition
  179. if (segment.value === $scope.removeTagFilterSegment.value) {
  180. $scope.tagSegments.splice(index, 3);
  181. if ($scope.tagSegments.length === 0) {
  182. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  183. } else if ($scope.tagSegments.length > 2) {
  184. $scope.tagSegments.splice(Math.max(index-1, 0), 1);
  185. if ($scope.tagSegments[$scope.tagSegments.length-1].type !== 'plus-button') {
  186. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  187. }
  188. }
  189. }
  190. else {
  191. if (segment.type === 'plus-button') {
  192. if (index > 2) {
  193. $scope.tagSegments.splice(index, 0, uiSegmentSrv.newCondition('AND'));
  194. }
  195. $scope.tagSegments.push(uiSegmentSrv.newOperator('='));
  196. $scope.tagSegments.push(uiSegmentSrv.newFake('select tag value', 'value', 'query-segment-value'));
  197. segment.type = 'key';
  198. segment.cssClass = 'query-segment-key';
  199. }
  200. if ((index+1) === $scope.tagSegments.length) {
  201. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  202. }
  203. }
  204. $scope.rebuildTargetTagConditions();
  205. };
  206. $scope.rebuildTargetTagConditions = function() {
  207. var tags = [];
  208. var tagIndex = 0;
  209. var tagOperator = "";
  210. _.each($scope.tagSegments, function(segment2, index) {
  211. if (segment2.type === 'key') {
  212. if (tags.length === 0) {
  213. tags.push({});
  214. }
  215. tags[tagIndex].key = segment2.value;
  216. }
  217. else if (segment2.type === 'value') {
  218. tagOperator = $scope.getTagValueOperator(segment2.value, tags[tagIndex].operator);
  219. if (tagOperator) {
  220. $scope.tagSegments[index-1] = uiSegmentSrv.newOperator(tagOperator);
  221. tags[tagIndex].operator = tagOperator;
  222. }
  223. tags[tagIndex].value = segment2.value;
  224. }
  225. else if (segment2.type === 'condition') {
  226. tags.push({ condition: segment2.value });
  227. tagIndex += 1;
  228. }
  229. else if (segment2.type === 'operator') {
  230. tags[tagIndex].operator = segment2.value;
  231. }
  232. });
  233. $scope.target.tags = tags;
  234. $scope.$parent.get_data();
  235. };
  236. $scope.getTagValueOperator = function(tagValue, tagOperator) {
  237. if (tagOperator !== '=~' && tagOperator !== '!~' && /^\/.*\/$/.test(tagValue)) {
  238. return '=~';
  239. }
  240. else if ((tagOperator === '=~' || tagOperator === '!~') && /^(?!\/.*\/$)/.test(tagValue)) {
  241. return '=';
  242. }
  243. };
  244. $scope.init();
  245. });
  246. });