query_ctrl.js 10 KB

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