queryCtrl.js 7.0 KB

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