queryCtrl.js 9.3 KB

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