queryCtrl.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. define([
  2. 'angular',
  3. 'lodash',
  4. '../graphite/gfunc',
  5. './queryBuilder',
  6. ],
  7. function (angular, _, gfunc, InfluxQueryBuilder) {
  8. 'use strict';
  9. var module = angular.module('grafana.controllers');
  10. module.controller('InfluxQueryCtrl', function($scope, $timeout, $sce, templateSrv, $q, uiSegmentSrv) {
  11. $scope.init = function() {
  12. if (!$scope.target) { return; }
  13. var target = $scope.target;
  14. target.tags = target.tags || [];
  15. target.groupBy = target.groupBy || [{type: 'time', interval: 'auto'}];
  16. target.fields = target.fields || [{name: 'value', func: target.function || 'mean'}];
  17. target.fields[0].functions = [
  18. gfunc.createFuncInstance('scaleToSeconds', { withDefaultParams: true }),
  19. gfunc.createFuncInstance('movingAverage', { withDefaultParams: true }),
  20. ];
  21. $scope.queryBuilder = new InfluxQueryBuilder(target);
  22. if (!target.measurement) {
  23. $scope.measurementSegment = uiSegmentSrv.newSelectMeasurement();
  24. } else {
  25. $scope.measurementSegment = uiSegmentSrv.newSegment(target.measurement);
  26. }
  27. $scope.tagSegments = [];
  28. _.each(target.tags, function(tag) {
  29. if (!tag.operator) {
  30. if (/^\/.*\/$/.test(tag.value)) {
  31. tag.operator = "=~";
  32. } else {
  33. tag.operator = '=';
  34. }
  35. }
  36. if (tag.condition) {
  37. $scope.tagSegments.push(uiSegmentSrv.newCondition(tag.condition));
  38. }
  39. $scope.tagSegments.push(uiSegmentSrv.newKey(tag.key));
  40. $scope.tagSegments.push(uiSegmentSrv.newOperator(tag.operator));
  41. $scope.tagSegments.push(uiSegmentSrv.newKeyValue(tag.value));
  42. });
  43. $scope.fixTagSegments();
  44. $scope.removeTagFilterSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove tag filter --'});
  45. };
  46. $scope.fixTagSegments = function() {
  47. var count = $scope.tagSegments.length;
  48. var lastSegment = $scope.tagSegments[Math.max(count-1, 0)];
  49. if (!lastSegment || lastSegment.type !== 'plus-button') {
  50. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  51. }
  52. };
  53. $scope.addGroupBy = function() {
  54. $scope.target.groupBy.push({type: 'tag', key: "select tag"});
  55. };
  56. $scope.removeGroupBy = function(index) {
  57. $scope.target.groupBy.splice(index, 1);
  58. $scope.get_data();
  59. };
  60. $scope.addSelect = function() {
  61. $scope.target.fields.push({name: "select field", func: 'mean'});
  62. };
  63. $scope.removeSelect = function(index) {
  64. $scope.target.fields.splice(index, 1);
  65. $scope.get_data();
  66. };
  67. $scope.changeFunction = function(func) {
  68. $scope.target.function = func;
  69. $scope.$parent.get_data();
  70. };
  71. $scope.measurementChanged = function() {
  72. $scope.target.measurement = $scope.measurementSegment.value;
  73. $scope.$parent.get_data();
  74. };
  75. $scope.getFields = function() {
  76. var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS');
  77. return $scope.datasource.metricFindQuery(fieldsQuery)
  78. .then($scope.transformToSegments(false), $scope.handleQueryError);
  79. };
  80. $scope.toggleQueryMode = function () {
  81. $scope.target.rawQuery = !$scope.target.rawQuery;
  82. };
  83. $scope.getMeasurements = function () {
  84. var query = $scope.queryBuilder.buildExploreQuery('MEASUREMENTS');
  85. return $scope.datasource.metricFindQuery(query)
  86. .then($scope.transformToSegments(true), $scope.handleQueryError);
  87. };
  88. $scope.getFunctions = function () {
  89. var functionList = ['count', 'mean', 'sum', 'min', 'max', 'mode', 'distinct', 'median',
  90. 'stddev', 'first', 'last'
  91. ];
  92. return $q.when(_.map(functionList, function(func) {
  93. return uiSegmentSrv.newSegment(func);
  94. }));
  95. };
  96. $scope.getGroupByTimeIntervals = function () {
  97. var times = ['auto', '1s', '10s', '1m', '2m', '5m', '10m', '30m', '1h', '1d'];
  98. return $q.when(_.map(times, function(func) {
  99. return uiSegmentSrv.newSegment(func);
  100. }));
  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.setFill = function(fill) {
  172. $scope.target.fill = fill;
  173. $scope.get_data();
  174. };
  175. $scope.tagSegmentUpdated = function(segment, index) {
  176. $scope.tagSegments[index] = segment;
  177. // handle remove tag condition
  178. if (segment.value === $scope.removeTagFilterSegment.value) {
  179. $scope.tagSegments.splice(index, 3);
  180. if ($scope.tagSegments.length === 0) {
  181. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  182. } else if ($scope.tagSegments.length > 2) {
  183. $scope.tagSegments.splice(Math.max(index-1, 0), 1);
  184. if ($scope.tagSegments[$scope.tagSegments.length-1].type !== 'plus-button') {
  185. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  186. }
  187. }
  188. }
  189. else {
  190. if (segment.type === 'plus-button') {
  191. if (index > 2) {
  192. $scope.tagSegments.splice(index, 0, uiSegmentSrv.newCondition('AND'));
  193. }
  194. $scope.tagSegments.push(uiSegmentSrv.newOperator('='));
  195. $scope.tagSegments.push(uiSegmentSrv.newFake('select tag value', 'value', 'query-segment-value'));
  196. segment.type = 'key';
  197. segment.cssClass = 'query-segment-key';
  198. }
  199. if ((index+1) === $scope.tagSegments.length) {
  200. $scope.tagSegments.push(uiSegmentSrv.newPlusButton());
  201. }
  202. }
  203. $scope.rebuildTargetTagConditions();
  204. };
  205. $scope.rebuildTargetTagConditions = function() {
  206. var tags = [];
  207. var tagIndex = 0;
  208. var tagOperator = "";
  209. _.each($scope.tagSegments, function(segment2, index) {
  210. if (segment2.type === 'key') {
  211. if (tags.length === 0) {
  212. tags.push({});
  213. }
  214. tags[tagIndex].key = segment2.value;
  215. }
  216. else if (segment2.type === 'value') {
  217. tagOperator = $scope.getTagValueOperator(segment2.value, tags[tagIndex].operator);
  218. if (tagOperator) {
  219. $scope.tagSegments[index-1] = uiSegmentSrv.newOperator(tagOperator);
  220. tags[tagIndex].operator = tagOperator;
  221. }
  222. tags[tagIndex].value = segment2.value;
  223. }
  224. else if (segment2.type === 'condition') {
  225. tags.push({ condition: segment2.value });
  226. tagIndex += 1;
  227. }
  228. else if (segment2.type === 'operator') {
  229. tags[tagIndex].operator = segment2.value;
  230. }
  231. });
  232. $scope.target.tags = tags;
  233. $scope.$parent.get_data();
  234. };
  235. $scope.getTagValueOperator = function(tagValue, tagOperator) {
  236. if (tagOperator !== '=~' && tagOperator !== '!~' && /^\/.*\/$/.test(tagValue)) {
  237. return '=~';
  238. }
  239. else if ((tagOperator === '=~' || tagOperator === '!~') && /^(?!\/.*\/$)/.test(tagValue)) {
  240. return '=';
  241. }
  242. };
  243. $scope.init();
  244. });
  245. });