queryCtrl.js 11 KB

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