queryCtrl.js 11 KB

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