queryCtrl.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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' && /^(?!\/.*\/$)/.test($scope.tagSegments[index+1].value)) {
  131. return $q.when([MetricSegment.newOperator('='), MetricSegment.newOperator('<>'),
  132. MetricSegment.newOperator('<'), MetricSegment.newOperator('>')]);
  133. } else if (segment.type === 'operator' && /^\/.*\/$/.test($scope.tagSegments[index+1].value)) {
  134. return $q.when([MetricSegment.newOperator('=~'), MetricSegment.newOperator('!~')]);
  135. }
  136. else {
  137. return $q.when([]);
  138. }
  139. return $scope.datasource.metricFindQuery(query)
  140. .then($scope.transformToSegments)
  141. .then($scope.addTemplateVariableSegments)
  142. .then(function(results) {
  143. if (segment.type === 'key') {
  144. results.splice(0, 0, angular.copy($scope.removeTagFilterSegment));
  145. }
  146. return results;
  147. })
  148. .then(null, $scope.handleQueryError);
  149. };
  150. $scope.getFieldSegments = function() {
  151. var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS');
  152. return $scope.datasource.metricFindQuery(fieldsQuery)
  153. .then($scope.transformToSegments)
  154. .then(null, $scope.handleQueryError);
  155. };
  156. $scope.addField = function() {
  157. $scope.target.fields.push({name: $scope.addFieldSegment.value, func: 'mean'});
  158. _.extend($scope.addFieldSegment, MetricSegment.newPlusButton());
  159. };
  160. $scope.fieldChanged = function(field) {
  161. if (field.name === '-- remove from select --') {
  162. $scope.target.fields = _.without($scope.target.fields, field);
  163. }
  164. $scope.get_data();
  165. };
  166. $scope.getGroupByTagSegments = function(segment) {
  167. var query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
  168. return $scope.datasource.metricFindQuery(query)
  169. .then($scope.transformToSegments)
  170. .then($scope.addTemplateVariableSegments)
  171. .then(function(results) {
  172. if (segment.type !== 'plus-button') {
  173. results.splice(0, 0, angular.copy($scope.removeGroupBySegment));
  174. }
  175. return results;
  176. })
  177. .then(null, $scope.handleQueryError);
  178. };
  179. $scope.tagSegmentUpdated = function(segment, index) {
  180. $scope.tagSegments[index] = segment;
  181. // handle remove tag condition
  182. if (segment.value === $scope.removeTagFilterSegment.value) {
  183. $scope.tagSegments.splice(index, 3);
  184. if ($scope.tagSegments.length === 0) {
  185. $scope.tagSegments.push(MetricSegment.newPlusButton());
  186. } else if ($scope.tagSegments.length > 2) {
  187. $scope.tagSegments.splice(Math.max(index-1, 0), 1);
  188. if ($scope.tagSegments[$scope.tagSegments.length-1].type !== 'plus-button') {
  189. $scope.tagSegments.push(MetricSegment.newPlusButton());
  190. }
  191. }
  192. }
  193. else {
  194. if (segment.type === 'plus-button') {
  195. if (index > 2) {
  196. $scope.tagSegments.splice(index, 0, MetricSegment.newCondition('AND'));
  197. }
  198. $scope.tagSegments.push(MetricSegment.newOperator('='));
  199. $scope.tagSegments.push(MetricSegment.newFake('select tag value', 'value', 'query-segment-value'));
  200. segment.type = 'key';
  201. segment.cssClass = 'query-segment-key';
  202. }
  203. if ((index+1) === $scope.tagSegments.length) {
  204. $scope.tagSegments.push(MetricSegment.newPlusButton());
  205. }
  206. }
  207. $scope.rebuildTargetTagConditions();
  208. };
  209. $scope.rebuildTargetTagConditions = function() {
  210. var tags = [];
  211. var tagIndex = 0;
  212. var tagOperator = "";
  213. _.each($scope.tagSegments, function(segment2, index) {
  214. if (segment2.type === 'key') {
  215. if (tags.length === 0) {
  216. tags.push({});
  217. }
  218. tags[tagIndex].key = segment2.value;
  219. }
  220. else if (segment2.type === 'value') {
  221. tagOperator = $scope.getTagValueOperator(segment2.value, tags[tagIndex].operator);
  222. if (tagOperator) {
  223. $scope.tagSegments[index-1] = MetricSegment.newOperator(tagOperator);
  224. tags[tagIndex].operator = tagOperator;
  225. }
  226. tags[tagIndex].value = segment2.value;
  227. }
  228. else if (segment2.type === 'condition') {
  229. tags.push({ condition: segment2.value });
  230. tagIndex += 1;
  231. }
  232. else if (segment2.type === 'operator') {
  233. tags[tagIndex].operator = segment2.value;
  234. }
  235. });
  236. $scope.target.tags = tags;
  237. $scope.$parent.get_data();
  238. };
  239. $scope.getTagValueOperator = function(tagValue, tagOperator) {
  240. if (tagOperator !== '=~' && tagOperator !== '!~' && /^\/.*\/$/.test(tagValue)) {
  241. return '=~';
  242. }
  243. else if ((tagOperator === '=~' || tagOperator === '!~') && /^(?!\/.*\/$)/.test(tagValue)) {
  244. return '=';
  245. }
  246. };
  247. function MetricSegment(options) {
  248. if (options === '*' || options.value === '*') {
  249. this.value = '*';
  250. this.html = $sce.trustAsHtml('<i class="fa fa-asterisk"><i>');
  251. this.expandable = true;
  252. return;
  253. }
  254. if (_.isString(options)) {
  255. this.value = options;
  256. this.html = $sce.trustAsHtml(this.value);
  257. return;
  258. }
  259. this.cssClass = options.cssClass;
  260. this.type = options.type;
  261. this.fake = options.fake;
  262. this.value = options.value;
  263. this.type = options.type;
  264. this.expandable = options.expandable;
  265. this.html = options.html || $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
  266. }
  267. MetricSegment.newSelectMeasurement = function() {
  268. return new MetricSegment({value: 'select measurement', fake: true});
  269. };
  270. MetricSegment.newFake = function(text, type, cssClass) {
  271. return new MetricSegment({value: text, fake: true, type: type, cssClass: cssClass});
  272. };
  273. MetricSegment.newCondition = function(condition) {
  274. return new MetricSegment({value: condition, type: 'condition', cssClass: 'query-keyword' });
  275. };
  276. MetricSegment.newOperator = function(op) {
  277. return new MetricSegment({value: op, type: 'operator', cssClass: 'query-segment-operator' });
  278. };
  279. MetricSegment.newPlusButton = function() {
  280. return new MetricSegment({fake: true, html: '<i class="fa fa-plus "></i>', type: 'plus-button' });
  281. };
  282. MetricSegment.newSelectTagValue = function() {
  283. return new MetricSegment({value: 'select tag value', fake: true});
  284. };
  285. });
  286. });