queryCtrl.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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, uiSegmentSrv, templateSrv, $q) {
  10. $scope.init = function() {
  11. var target = $scope.target;
  12. if (!target) { return; }
  13. target.timeField = target.timeField || '@timestamp';
  14. target.metrics = target.metrics || [{ agg: 'count' }];
  15. target.bucketAggs = target.bucketAggs || [];
  16. target.bucketAggs = [
  17. {
  18. type: 'terms',
  19. field: '@hostname'
  20. },
  21. {
  22. type: 'date_histogram',
  23. field: '@timestamp'
  24. },
  25. ];
  26. $scope.timeSegment = uiSegmentSrv.newSegment(target.timeField);
  27. $scope.initSelectSegments();
  28. $scope.removeSelectSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove select --'});
  29. $scope.resetSelectSegment = uiSegmentSrv.newSegment({fake: true, value: '-- reset --'});
  30. $scope.removeGroupBySegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove group by --'});
  31. $scope.queryBuilder = new ElasticQueryBuilder(target);
  32. $scope.rawQueryOld = angular.toJson($scope.queryBuilder.build($scope.target), true);
  33. };
  34. $scope.initSelectSegments = function() {
  35. $scope.selectSegments = [];
  36. _.each($scope.target.metrics, function(select) {
  37. if ($scope.selectSegments.length > 0) {
  38. $scope.selectSegments.push(uiSegmentSrv.newCondition(" and "));
  39. }
  40. if (select.agg === 'count') {
  41. $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.agg, type: 'agg'}));
  42. } else {
  43. $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.agg, type: 'agg'}));
  44. $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.field, type: 'field' }));
  45. }
  46. });
  47. $scope.selectSegments.push(uiSegmentSrv.newPlusButton());
  48. };
  49. $scope.getSelectSegments = function(segment, index) {
  50. if (segment.type === 'agg' || segment.type === 'plus-button') {
  51. var options = [
  52. uiSegmentSrv.newSegment({value: 'count', type: 'agg'}),
  53. uiSegmentSrv.newSegment({value: 'avg', type: 'agg', reqField: true}),
  54. uiSegmentSrv.newSegment({value: 'sum', type: 'agg', reqField: true}),
  55. uiSegmentSrv.newSegment({value: 'min', type: 'agg', reqField: true}),
  56. uiSegmentSrv.newSegment({value: 'max', type: 'agg', reqField: true}),
  57. ];
  58. // if we have other selects and this is not a plus button add remove option
  59. if (segment.type !== 'plus-button' && $scope.selectSegments.length > 3) {
  60. options.splice(0, 0, angular.copy($scope.removeSelectSegment));
  61. }
  62. // revert option is to reset the selectSegments if they become fucked
  63. if (index === 0 && $scope.selectSegments.length > 2) {
  64. options.splice(0, 0, angular.copy($scope.resetSelectSegment));
  65. }
  66. return $q.when(options);
  67. }
  68. return $scope.datasource.metricFindQuery('fields()')
  69. .then($scope.transformToSegments(false))
  70. .then(null, $scope.handleQueryError);
  71. };
  72. $scope.selectChanged = function(segment, index) {
  73. // reset
  74. if (segment.value === $scope.resetSelectSegment.value) {
  75. $scope.target.metrics = [{ agg: 'count' }];
  76. $scope.initSelectSegments();
  77. $scope.queryUpdated();
  78. return;
  79. }
  80. var nextSegment, removeCount;
  81. // remove this select field
  82. if (segment.value === $scope.removeSelectSegment.value) {
  83. nextSegment = $scope.selectSegments[index + 1];
  84. removeCount = 2;
  85. if (nextSegment && nextSegment.type === 'field') {
  86. removeCount += 1;
  87. }
  88. $scope.selectSegments.splice(Math.max(index-1, 0), removeCount);
  89. $scope.rebuildTargetSelects();
  90. $scope.queryUpdated();
  91. return;
  92. }
  93. // add new
  94. if (segment.type === 'plus-button' && index > 0) {
  95. $scope.selectSegments.splice(index, 0, uiSegmentSrv.newCondition(' And '));
  96. segment.type = 'agg';
  97. index += 1;
  98. }
  99. if (segment.type === 'agg') {
  100. nextSegment = $scope.selectSegments[index + 1];
  101. if (segment.value === 'count' && nextSegment && nextSegment.type === 'field') {
  102. $scope.selectSegments.splice(index + 1, 1);
  103. } else if (!nextSegment || nextSegment.type !== 'field') {
  104. $scope.selectSegments.splice(index + 1, 0, uiSegmentSrv.newSegment({value: 'select field', fake: true, type: 'field'}));
  105. }
  106. }
  107. if ((index+1) === $scope.selectSegments.length) {
  108. $scope.selectSegments.push(uiSegmentSrv.newPlusButton());
  109. }
  110. $scope.rebuildTargetSelects();
  111. $scope.queryUpdated();
  112. };
  113. $scope.rebuildTargetSelects = function() {
  114. $scope.target.metrics = [];
  115. for (var i = 0; i < $scope.selectSegments.length; i++) {
  116. var segment = $scope.selectSegments[i];
  117. var select = {agg: segment.value };
  118. if (segment.type === 'agg' && segment.value !== 'count') {
  119. select.field = $scope.selectSegments[i+1].value;
  120. i += 2;
  121. } else {
  122. i += 1;
  123. }
  124. if (select.field === 'select field') { continue; }
  125. $scope.target.metrics.push(select);
  126. }
  127. };
  128. $scope.getGroupByFields = function(segment) {
  129. return $scope.datasource.metricFindQuery('fields()')
  130. .then($scope.transformToSegments(false))
  131. .then(function(results) {
  132. if (segment.type !== 'plus-button') {
  133. results.splice(0, 0, angular.copy($scope.removeGroupBySegment));
  134. }
  135. return results;
  136. })
  137. .then(null, $scope.handleQueryError);
  138. };
  139. $scope.getFields = function() {
  140. return $scope.datasource.metricFindQuery('fields()')
  141. .then($scope.transformToSegments(false))
  142. .then(null, $scope.handleQueryError);
  143. };
  144. $scope.timeFieldChanged = function() {
  145. $scope.target.timeField = $scope.timeSegment.value;
  146. $scope.queryUpdated();
  147. };
  148. $scope.addBucketAgg = function() {
  149. // if last is date histogram add it before
  150. var lastBucket = $scope.target.bucketAggs[$scope.target.bucketAggs.length - 1];
  151. var addIndex = $scope.target.bucketAggs.length - 1;
  152. if (lastBucket && lastBucket.type === 'date_histogram') {
  153. addIndex - 1;
  154. }
  155. $scope.target.bucketAggs.splice(addIndex, 0, {type: "terms", field: "select field" });
  156. };
  157. $scope.removeBucketAgg = function(index) {
  158. $scope.target.bucketAggs.splice(index, 1);
  159. $scope.queryUpdated();
  160. };
  161. $scope.queryUpdated = function() {
  162. var newJson = angular.toJson($scope.queryBuilder.build($scope.target), true);
  163. if (newJson !== $scope.oldQueryRaw) {
  164. $scope.rawQueryOld = newJson;
  165. $scope.get_data();
  166. }
  167. };
  168. $scope.transformToSegments = function(addTemplateVars) {
  169. return function(results) {
  170. var segments = _.map(results, function(segment) {
  171. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  172. });
  173. if (addTemplateVars) {
  174. _.each(templateSrv.variables, function(variable) {
  175. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  176. });
  177. }
  178. return segments;
  179. };
  180. };
  181. $scope.handleQueryError = function(err) {
  182. $scope.parserError = err.message || 'Failed to issue metric query';
  183. return [];
  184. };
  185. $scope.toggleQueryMode = function () {
  186. if ($scope.target.rawQuery) {
  187. delete $scope.target.rawQuery;
  188. } else {
  189. $scope.target.rawQuery = $scope.rawQueryOld;
  190. }
  191. };
  192. $scope.init();
  193. });
  194. });