queryCtrl.js 11 KB

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