queryCtrl.js 12 KB

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