queryCtrl.js 11 KB

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