queryCtrl.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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()')
  123. .then($scope.transformToSegments(false))
  124. .then(function(results) {
  125. if (segment.type !== 'plus-button') {
  126. results.splice(0, 0, angular.copy($scope.removeGroupBySegment));
  127. }
  128. return results;
  129. })
  130. .then(null, $scope.handleQueryError);
  131. };
  132. $scope.getTimeFields = function() {
  133. return $scope.datasource.metricFindQuery('fields()')
  134. .then($scope.transformToSegments(false))
  135. .then(null, $scope.handleQueryError);
  136. };
  137. $scope.timeFieldChanged = function() {
  138. $scope.target.timeField = $scope.timeSegment.value;
  139. $scope.queryUpdated();
  140. };
  141. $scope.groupByChanged = function(segment, index) {
  142. if (segment.value === $scope.removeGroupBySegment.value) {
  143. $scope.target.groupByFields.splice(index, 1);
  144. $scope.groupBySegments.splice(index, 1);
  145. $scope.queryUpdated();
  146. return;
  147. }
  148. if (index === $scope.groupBySegments.length-1) {
  149. $scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
  150. }
  151. segment.type = 'group-by-key';
  152. segment.fake = false;
  153. $scope.target.groupByFields[index] = {field: segment.value};
  154. $scope.queryUpdated();
  155. };
  156. $scope.queryUpdated = function() {
  157. var newJson = angular.toJson($scope.queryBuilder.build($scope.target), true);
  158. if (newJson !== $scope.oldQueryRaw) {
  159. $scope.rawQueryOld = newJson;
  160. $scope.get_data();
  161. }
  162. };
  163. $scope.transformToSegments = function(addTemplateVars) {
  164. return function(results) {
  165. var segments = _.map(results, function(segment) {
  166. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  167. });
  168. if (addTemplateVars) {
  169. _.each(templateSrv.variables, function(variable) {
  170. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  171. });
  172. }
  173. return segments;
  174. };
  175. };
  176. $scope.handleQueryError = function(err) {
  177. $scope.parserError = err.message || 'Failed to issue metric query';
  178. return [];
  179. };
  180. $scope.toggleQueryMode = function () {
  181. if ($scope.target.rawQuery) {
  182. delete $scope.target.rawQuery;
  183. } else {
  184. $scope.target.rawQuery = $scope.rawQueryOld;
  185. }
  186. };
  187. $scope.init();
  188. });
  189. });