queryCtrl.js 12 KB

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