queryCtrl.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.select = target.select || [{ agg: 'count' }];
  15. target.groupByFields = target.groupByFields || [];
  16. $scope.timeSegment = uiSegmentSrv.newSegment(target.timeField);
  17. $scope.groupBySegments = _.map(target.groupByFields, function(group) {
  18. return uiSegmentSrv.newSegment(group.field);
  19. });
  20. $scope.initSelectSegments();
  21. $scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
  22. $scope.removeSelectSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove select --'});
  23. $scope.resetSelectSegment = uiSegmentSrv.newSegment({fake: true, value: '-- reset --'});
  24. $scope.removeGroupBySegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove group by --'});
  25. $scope.queryBuilder = new ElasticQueryBuilder(target);
  26. $scope.rawQueryOld = angular.toJson($scope.queryBuilder.build($scope.target), true);
  27. };
  28. $scope.initSelectSegments = function() {
  29. $scope.selectSegments = [];
  30. _.each($scope.target.select, function(select) {
  31. if ($scope.selectSegments.length > 0) {
  32. $scope.selectSegments.push(uiSegmentSrv.newCondition(" and "));
  33. }
  34. if (select.agg === 'count') {
  35. $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.agg, type: 'agg'}));
  36. } else {
  37. $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.agg, type: 'agg'}));
  38. $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.field, type: 'field' }));
  39. }
  40. });
  41. $scope.selectSegments.push(uiSegmentSrv.newPlusButton());
  42. };
  43. $scope.getSelectSegments = function(segment, index) {
  44. if (segment.type === 'agg' || segment.type === 'plus-button') {
  45. var options = [
  46. uiSegmentSrv.newSegment({value: 'count', type: 'agg'}),
  47. uiSegmentSrv.newSegment({value: 'min', type: 'agg', reqField: true}),
  48. uiSegmentSrv.newSegment({value: 'max', type: 'agg', reqField: true}),
  49. uiSegmentSrv.newSegment({value: 'avg', type: 'agg', reqField: true}),
  50. ];
  51. // if we have other selects and this is not a plus button add remove option
  52. if (segment.type !== 'plus-button' && $scope.selectSegments.length > 3) {
  53. options.splice(0, 0, angular.copy($scope.removeSelectSegment));
  54. }
  55. // revert option is to reset the selectSegments if they become fucked
  56. if (index === 0 && $scope.selectSegments.length > 2) {
  57. options.splice(0, 0, angular.copy($scope.resetSelectSegment));
  58. }
  59. return $q.when(options);
  60. }
  61. return $scope.datasource.metricFindQuery('fields()')
  62. .then($scope.transformToSegments(false))
  63. .then(null, $scope.handleQueryError);
  64. };
  65. $scope.selectChanged = function(segment, index) {
  66. // reset
  67. if (segment.value === $scope.resetSelectSegment.value) {
  68. $scope.target.select = [{ agg: 'count' }];
  69. $scope.initSelectSegments();
  70. $scope.queryUpdated();
  71. return;
  72. }
  73. var nextSegment, removeCount;
  74. // remove this select field
  75. if (segment.value === $scope.removeSelectSegment.value) {
  76. nextSegment = $scope.selectSegments[index + 1];
  77. removeCount = 2;
  78. if (nextSegment && nextSegment.type === 'field') {
  79. removeCount += 1;
  80. }
  81. $scope.selectSegments.splice(Math.max(index-1, 0), removeCount);
  82. $scope.rebuildTargetSelects();
  83. $scope.queryUpdated();
  84. return;
  85. }
  86. // add new
  87. if (segment.type === 'plus-button' && index > 0) {
  88. $scope.selectSegments.splice(index, 0, uiSegmentSrv.newCondition(' And '));
  89. segment.type = 'agg';
  90. index += 1;
  91. }
  92. if (segment.type === 'agg') {
  93. nextSegment = $scope.selectSegments[index + 1];
  94. if (segment.value === 'count' && nextSegment && nextSegment.type === 'field') {
  95. $scope.selectSegments.splice(index + 1, 1);
  96. } else if (!nextSegment || nextSegment.type !== 'field') {
  97. $scope.selectSegments.splice(index + 1, 0, uiSegmentSrv.newSegment({value: 'select field', fake: true, type: 'field'}));
  98. }
  99. }
  100. if ((index+1) === $scope.selectSegments.length) {
  101. $scope.selectSegments.push(uiSegmentSrv.newPlusButton());
  102. }
  103. $scope.rebuildTargetSelects();
  104. $scope.queryUpdated();
  105. };
  106. $scope.rebuildTargetSelects = function() {
  107. $scope.target.select = [];
  108. for (var i = 0; i < $scope.selectSegments.length; i++) {
  109. var segment = $scope.selectSegments[i];
  110. var select = {agg: segment.value };
  111. if (segment.type === 'agg' && segment.value !== 'count') {
  112. select.field = $scope.selectSegments[i+1].value;
  113. i += 2;
  114. } else {
  115. i += 1;
  116. }
  117. if (select.field === 'select field') { continue; }
  118. $scope.target.select.push(select);
  119. }
  120. };
  121. $scope.getGroupByFields = function(segment) {
  122. return $scope.datasource.metricFindQuery('fields()').then($scope.transformToSegments(false))
  123. .then(function(results) {
  124. if (segment.type !== 'plus-button') {
  125. results.splice(0, 0, angular.copy($scope.removeGroupBySegment));
  126. }
  127. return results;
  128. })
  129. .then(null, $scope.handleQueryError);
  130. };
  131. $scope.groupByChanged = function(segment, index) {
  132. if (segment.value === $scope.removeGroupBySegment.value) {
  133. $scope.target.groupByFields.splice(index, 1);
  134. $scope.groupBySegments.splice(index, 1);
  135. $scope.queryUpdated();
  136. return;
  137. }
  138. if (index === $scope.groupBySegments.length-1) {
  139. $scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
  140. }
  141. segment.type = 'group-by-key';
  142. segment.fake = false;
  143. $scope.target.groupByFields[index] = {field: segment.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. });