queryCtrl.js 9.0 KB

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