query_ctrl.js 9.3 KB

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