queryCtrl.js 12 KB

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