queryCtrl.js 7.1 KB

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