queryCtrl.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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) {
  10. $scope.init = function() {
  11. var target = $scope.target;
  12. target.tags = target.tags || [];
  13. target.groupByTags = target.groupByTags || [];
  14. target.fields = target.fields || [{
  15. name: 'value',
  16. func: target.function || 'mean'
  17. }];
  18. $scope.queryBuilder = new InfluxQueryBuilder(target);
  19. if (!target.measurement) {
  20. $scope.measurementSegment = MetricSegment.newSelectMeasurement();
  21. } else {
  22. $scope.measurementSegment = new MetricSegment(target.measurement);
  23. }
  24. $scope.addFieldSegment = MetricSegment.newPlusButton();
  25. $scope.tagSegments = [];
  26. _.each(target.tags, function(tag) {
  27. if (tag.condition) {
  28. $scope.tagSegments.push(MetricSegment.newCondition(tag.condition));
  29. }
  30. $scope.tagSegments.push(new MetricSegment({value: tag.key, type: 'key', cssClass: 'query-segment-key' }));
  31. if (tag.operator) {
  32. $scope.tagSegments.push(MetricSegment.newOperator(tag.operator));
  33. } else if (/^\/.*\/$/.test(tag.value)) {
  34. $scope.tagSegments.push(MetricSegment.newOperator('=~'));
  35. } else {
  36. $scope.tagSegments.push(MetricSegment.newOperator('='));
  37. }
  38. $scope.tagSegments.push(new MetricSegment({value: tag.value, type: 'value', cssClass: 'query-segment-value'}));
  39. });
  40. $scope.fixTagSegments();
  41. $scope.groupBySegments = [];
  42. _.each(target.groupByTags, function(tag) {
  43. $scope.groupBySegments.push(new MetricSegment(tag));
  44. });
  45. $scope.groupBySegments.push(MetricSegment.newPlusButton());
  46. $scope.removeTagFilterSegment = new MetricSegment({fake: true, value: '-- remove tag filter --'});
  47. $scope.removeGroupBySegment = new MetricSegment({fake: true, value: '-- remove group by --'});
  48. };
  49. $scope.fixTagSegments = function() {
  50. var count = $scope.tagSegments.length;
  51. var lastSegment = $scope.tagSegments[Math.max(count-1, 0)];
  52. if (!lastSegment || lastSegment.type !== 'plus-button') {
  53. $scope.tagSegments.push(MetricSegment.newPlusButton());
  54. }
  55. };
  56. $scope.groupByTagUpdated = function(segment, index) {
  57. if (segment.value === $scope.removeGroupBySegment.value) {
  58. $scope.target.groupByTags.splice(index, 1);
  59. $scope.groupBySegments.splice(index, 1);
  60. $scope.$parent.get_data();
  61. return;
  62. }
  63. if (index === $scope.groupBySegments.length-1) {
  64. $scope.groupBySegments.push(MetricSegment.newPlusButton());
  65. }
  66. segment.type = 'group-by-key';
  67. segment.fake = false;
  68. $scope.target.groupByTags[index] = segment.value;
  69. $scope.$parent.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(function(results) {
  83. var values = _.pluck(results, 'text');
  84. if ($scope.target.fields.length > 1) {
  85. values.splice(0, 0, "-- remove from select --");
  86. }
  87. return values;
  88. });
  89. };
  90. $scope.toggleQueryMode = function () {
  91. $scope.target.rawQuery = !$scope.target.rawQuery;
  92. };
  93. $scope.moveMetricQuery = function(fromIndex, toIndex) {
  94. _.move($scope.panel.targets, fromIndex, toIndex);
  95. };
  96. $scope.duplicate = function() {
  97. var clone = angular.copy($scope.target);
  98. $scope.panel.targets.push(clone);
  99. };
  100. $scope.getMeasurements = function () {
  101. var query = $scope.queryBuilder.buildExploreQuery('MEASUREMENTS');
  102. return $scope.datasource.metricFindQuery(query)
  103. .then($scope.transformToSegments)
  104. .then($scope.addTemplateVariableSegments)
  105. .then(null, $scope.handleQueryError);
  106. };
  107. $scope.handleQueryError = function(err) {
  108. $scope.parserError = err.message || 'Failed to issue metric query';
  109. return [];
  110. };
  111. $scope.transformToSegments = function(results) {
  112. return _.map(results, function(segment) {
  113. return new MetricSegment({ value: segment.text, expandable: segment.expandable });
  114. });
  115. };
  116. $scope.addTemplateVariableSegments = function(segments) {
  117. _.each(templateSrv.variables, function(variable) {
  118. segments.unshift(new MetricSegment({ type: 'template', value: '/$' + variable.name + '/', expandable: true }));
  119. });
  120. return segments;
  121. };
  122. $scope.getTagsOrValues = function(segment, index) {
  123. var query;
  124. if (segment.type === 'key' || segment.type === 'plus-button') {
  125. query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
  126. } else if (segment.type === 'value') {
  127. query = $scope.queryBuilder.buildExploreQuery('TAG_VALUES', $scope.tagSegments[index-2].value);
  128. } else if (segment.type === 'condition') {
  129. return $q.when([new MetricSegment('AND'), new MetricSegment('OR')]);
  130. } else if (segment.type === 'operator') {
  131. return $q.when([MetricSegment.newOperator('='), MetricSegment.newOperator('<>'),
  132. MetricSegment.newOperator('<'), MetricSegment.newOperator('>'),
  133. MetricSegment.newOperator('=~'), MetricSegment.newOperator('!~')]);
  134. }
  135. else {
  136. return $q.when([]);
  137. }
  138. return $scope.datasource.metricFindQuery(query)
  139. .then($scope.transformToSegments)
  140. .then($scope.addTemplateVariableSegments)
  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)
  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, MetricSegment.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.getGroupByTagSegments = function(segment) {
  166. var query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
  167. return $scope.datasource.metricFindQuery(query)
  168. .then($scope.transformToSegments)
  169. .then($scope.addTemplateVariableSegments)
  170. .then(function(results) {
  171. if (segment.type !== 'plus-button') {
  172. results.splice(0, 0, angular.copy($scope.removeGroupBySegment));
  173. }
  174. return results;
  175. })
  176. .then(null, $scope.handleQueryError);
  177. };
  178. $scope.tagSegmentUpdated = function(segment, index) {
  179. $scope.tagSegments[index] = segment;
  180. // handle remove tag condition
  181. if (segment.value === $scope.removeTagFilterSegment.value) {
  182. $scope.tagSegments.splice(index, 3);
  183. if ($scope.tagSegments.length === 0) {
  184. $scope.tagSegments.push(MetricSegment.newPlusButton());
  185. } else if ($scope.tagSegments.length > 2) {
  186. $scope.tagSegments.splice(Math.max(index-1, 0), 1);
  187. if ($scope.tagSegments[$scope.tagSegments.length-1].type !== 'plus-button') {
  188. $scope.tagSegments.push(MetricSegment.newPlusButton());
  189. }
  190. }
  191. }
  192. else {
  193. if (segment.type === 'plus-button') {
  194. if (index > 2) {
  195. $scope.tagSegments.splice(index, 0, MetricSegment.newCondition('AND'));
  196. }
  197. $scope.tagSegments.push(MetricSegment.newOperator('='));
  198. $scope.tagSegments.push(MetricSegment.newFake('select tag value', 'value', 'query-segment-value'));
  199. segment.type = 'key';
  200. segment.cssClass = 'query-segment-key';
  201. }
  202. if ((index+1) === $scope.tagSegments.length) {
  203. $scope.tagSegments.push(MetricSegment.newPlusButton());
  204. }
  205. }
  206. $scope.rebuildTargetTagConditions();
  207. };
  208. $scope.rebuildTargetTagConditions = function() {
  209. var tags = [];
  210. var tagIndex = 0;
  211. var tagOperator = "";
  212. _.each($scope.tagSegments, function(segment2, index) {
  213. if (segment2.type === 'key') {
  214. if (tags.length === 0) {
  215. tags.push({});
  216. }
  217. tags[tagIndex].key = segment2.value;
  218. }
  219. else if (segment2.type === 'value') {
  220. tagOperator = $scope.getTagValueOperator(segment2.value, tags[tagIndex].operator);
  221. if (tagOperator) {
  222. $scope.tagSegments[index-1] = MetricSegment.newOperator(tagOperator);
  223. tags[tagIndex].operator = tagOperator;
  224. }
  225. tags[tagIndex].value = segment2.value;
  226. }
  227. else if (segment2.type === 'condition') {
  228. tags.push({ condition: segment2.value });
  229. tagIndex += 1;
  230. }
  231. else if (segment2.type === 'operator') {
  232. tags[tagIndex].operator = segment2.value;
  233. }
  234. });
  235. $scope.target.tags = tags;
  236. $scope.$parent.get_data();
  237. };
  238. $scope.getTagValueOperator = function(tagValue, tagOperator) {
  239. if (tagOperator !== '=~' && tagOperator !== '!~' && /^\/.*\/$/.test(tagValue)) {
  240. return '=~';
  241. }
  242. else if ((tagOperator === '=~' || tagOperator === '!~') && /^(?!\/.*\/$)/.test(tagValue)) {
  243. return '=';
  244. }
  245. };
  246. function MetricSegment(options) {
  247. if (options === '*' || options.value === '*') {
  248. this.value = '*';
  249. this.html = $sce.trustAsHtml('<i class="fa fa-asterisk"><i>');
  250. this.expandable = true;
  251. return;
  252. }
  253. if (_.isString(options)) {
  254. this.value = options;
  255. this.html = $sce.trustAsHtml(this.value);
  256. return;
  257. }
  258. this.cssClass = options.cssClass;
  259. this.type = options.type;
  260. this.fake = options.fake;
  261. this.value = options.value;
  262. this.type = options.type;
  263. this.expandable = options.expandable;
  264. this.html = options.html || $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
  265. }
  266. MetricSegment.newSelectMeasurement = function() {
  267. return new MetricSegment({value: 'select measurement', fake: true});
  268. };
  269. MetricSegment.newFake = function(text, type, cssClass) {
  270. return new MetricSegment({value: text, fake: true, type: type, cssClass: cssClass});
  271. };
  272. MetricSegment.newCondition = function(condition) {
  273. return new MetricSegment({value: condition, type: 'condition', cssClass: 'query-keyword' });
  274. };
  275. MetricSegment.newOperator = function(op) {
  276. return new MetricSegment({value: op, type: 'operator', cssClass: 'query-segment-operator' });
  277. };
  278. MetricSegment.newPlusButton = function() {
  279. return new MetricSegment({fake: true, html: '<i class="fa fa-plus "></i>', type: 'plus-button' });
  280. };
  281. MetricSegment.newSelectTagValue = function() {
  282. return new MetricSegment({value: 'select tag value', fake: true});
  283. };
  284. });
  285. });