queryCtrl.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.functionList = ['count', 'min', 'max', 'total', 'mean'];
  11. $scope.functionMenu = _.map($scope.functionList, function(func) {
  12. return { text: func, click: "changeFunction('" + func + "');" };
  13. });
  14. $scope.init = function() {
  15. $scope.queryBuilder = new ElasticQueryBuilder(target);
  16. var target = $scope.target;
  17. target.function = target.function || 'mean';
  18. target.timeField = target.timeField || '@timestamp';
  19. target.select = target.select || [{ agg: 'count' }];
  20. target.groupByFields = target.groupByFields || [];
  21. $scope.timeSegment = uiSegmentSrv.newSegment(target.timeField);
  22. $scope.groupBySegments = _.map(target.groupByFields, function(group) {
  23. return uiSegmentSrv.newSegment(group.field);
  24. });
  25. $scope.initSelectSegments();
  26. $scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
  27. $scope.removeSelectSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove select --'});
  28. $scope.resetSelectSegment = uiSegmentSrv.newSegment({fake: true, value: '-- reset --'});
  29. $scope.removeGroupBySegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove group by --'});
  30. };
  31. $scope.initSelectSegments = function() {
  32. $scope.selectSegments = [];
  33. _.each($scope.target.select, function(select) {
  34. if ($scope.selectSegments.length > 0) {
  35. $scope.selectSegments.push(uiSegmentSrv.newCondition(" and "));
  36. }
  37. if (select.agg === 'count') {
  38. $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.agg, type: 'agg'}));
  39. } else {
  40. $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.agg, type: 'agg'}));
  41. $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.field, type: 'field' }));
  42. }
  43. });
  44. $scope.selectSegments.push(uiSegmentSrv.newPlusButton());
  45. };
  46. $scope.getSelectSegments = function(segment, index) {
  47. if (segment.type === 'agg' || segment.type === 'plus-button') {
  48. var options = [
  49. uiSegmentSrv.newSegment({value: 'count', type: 'agg'}),
  50. uiSegmentSrv.newSegment({value: 'min', type: 'agg', reqField: true}),
  51. uiSegmentSrv.newSegment({value: 'max', type: 'agg', reqField: true}),
  52. uiSegmentSrv.newSegment({value: 'avg', type: 'agg', reqField: true}),
  53. ];
  54. if (segment.type !== 'plus-button' && $scope.selectSegments.length > 3) {
  55. options.splice(0, 0, angular.copy($scope.removeSelectSegment));
  56. }
  57. if (index === 0 && $scope.selectSegments.length > 2) {
  58. options.splice(0, 0, angular.copy($scope.resetSelectSegment));
  59. }
  60. return $q.when(options);
  61. }
  62. return $scope.datasource.metricFindQuery('fields()')
  63. .then($scope.transformToSegments(false))
  64. .then(null, $scope.handleQueryError);
  65. };
  66. $scope.selectChanged = function(segment, index) {
  67. // reset
  68. if (segment.value === $scope.resetSelectSegment.value) {
  69. $scope.target.select = [{ agg: 'count' }];
  70. $scope.initSelectSegments();
  71. $scope.get_data();
  72. return;
  73. }
  74. // remove this select field
  75. if (segment.value === $scope.removeSelectSegment.value) {
  76. var nextSegment = $scope.selectSegments[index + 1];
  77. var remove = 2;
  78. if (nextSegment && nextSegment.type === 'field') {
  79. remove += 1;
  80. }
  81. $scope.selectSegments.splice(Math.max(index-1, 0), remove);
  82. $scope.rebuildTargetSelects();
  83. $scope.get_data();
  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. var nextSegment = $scope.selectSegments[index + 1];
  94. if (!segment.reqField && 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.get_data();
  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.$parent.get_data();
  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.$parent.get_data();
  145. };
  146. $scope.transformToSegments = function(addTemplateVars) {
  147. return function(results) {
  148. var segments = _.map(results, function(segment) {
  149. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  150. });
  151. if (addTemplateVars) {
  152. _.each(templateSrv.variables, function(variable) {
  153. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  154. });
  155. }
  156. return segments;
  157. };
  158. };
  159. $scope.handleQueryError = function(err) {
  160. $scope.parserError = err.message || 'Failed to issue metric query';
  161. return [];
  162. };
  163. $scope.toggleQueryMode = function () {
  164. $scope.target.rawQuery = !$scope.target.rawQuery;
  165. };
  166. $scope.init();
  167. });
  168. });