queryCtrl.js 12 KB

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