query_ctrl.js 9.7 KB

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