queryCtrl.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.selectSegments = [];
  26. _.each(target.select, function(select) {
  27. if ($scope.selectSegments.length > 0) {
  28. $scope.selectSegments.push(uiSegmentSrv.newCondition(" and "));
  29. }
  30. if (select.agg === 'count') {
  31. $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.agg, type: 'agg'}));
  32. } else {
  33. $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.agg, type: 'agg'}));
  34. $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.field, type: 'field' }));
  35. }
  36. });
  37. $scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
  38. $scope.selectSegments.push(uiSegmentSrv.newPlusButton());
  39. $scope.removeSelectSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove select --'});
  40. $scope.removeGroupBySegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove group by --'});
  41. };
  42. $scope.getSelectSegments = function(segment, index) {
  43. if (segment.type === 'agg' || segment.type === 'plus-button') {
  44. var options = [
  45. uiSegmentSrv.newSegment({value: 'count', type: 'agg'}),
  46. uiSegmentSrv.newSegment({value: 'min', type: 'agg', reqField: true}),
  47. uiSegmentSrv.newSegment({value: 'count', type: 'agg', reqField: true}),
  48. uiSegmentSrv.newSegment({value: 'avg', type: 'agg', reqField: true}),
  49. ];
  50. if (index > 0) {
  51. options.splice(0, 0, angular.copy($scope.removeSelectSegment));
  52. }
  53. return $q.when(options);
  54. }
  55. return $scope.datasource.metricFindQuery('fields()')
  56. .then($scope.transformToSegments(false))
  57. .then(null, $scope.handleQueryError);
  58. };
  59. $scope.selectChanged = function(segment, index) {
  60. if (segment.value === $scope.removeSelectSegment.value) {
  61. var nextSegment = $scope.selectSegments[index + 1];
  62. var remove = 2;
  63. if (nextSegment && nextSegment.type === 'field') {
  64. remove += 1;
  65. }
  66. $scope.selectSegments.splice(index-1, remove);
  67. $scope.rebuildTargetSelects();
  68. return;
  69. }
  70. if (segment.type === 'plus-button' && index > 0) {
  71. $scope.selectSegments.splice(index, 0, uiSegmentSrv.newCondition(' And '));
  72. segment.type = 'agg';
  73. index += 1;
  74. }
  75. if (segment.type === 'agg') {
  76. var nextSegment = $scope.selectSegments[index + 1];
  77. if (!segment.reqField && nextSegment && nextSegment.type === 'field') {
  78. $scope.selectSegments.splice(index + 1, 1);
  79. } else if (!nextSegment || nextSegment.type !== 'field') {
  80. $scope.selectSegments.splice(index + 1, 0, uiSegmentSrv.newSegment({value: 'select field', fake: true, type: 'field'}));
  81. }
  82. }
  83. if ((index+1) === $scope.selectSegments.length) {
  84. $scope.selectSegments.push(uiSegmentSrv.newPlusButton());
  85. }
  86. $scope.rebuildTargetSelects();
  87. };
  88. $scope.rebuildTargetSelects = function() {
  89. $scope.target.select = [];
  90. for (var i = 0; i < $scope.selectSegments.length; i++) {
  91. var segment = $scope.selectSegments[i];
  92. var select = {agg: segment.value };
  93. if (segment.type === 'agg' && segment.reqField) {
  94. select.field = $scope.selectSegments[i+1].value;
  95. i += 2;
  96. } else {
  97. i += 1;
  98. }
  99. $scope.target.select.push(select);
  100. };
  101. };
  102. $scope.getGroupByFields = function(segment) {
  103. return $scope.datasource.metricFindQuery('fields()').then($scope.transformToSegments(false))
  104. .then(function(results) {
  105. if (segment.type !== 'plus-button') {
  106. results.splice(0, 0, angular.copy($scope.removeGroupBySegment));
  107. }
  108. return results;
  109. })
  110. .then(null, $scope.handleQueryError);
  111. };
  112. $scope.groupByChanged = function(segment, index) {
  113. if (segment.value === $scope.removeGroupBySegment.value) {
  114. $scope.target.groupByFields.splice(index, 1);
  115. $scope.groupBySegments.splice(index, 1);
  116. $scope.$parent.get_data();
  117. return;
  118. }
  119. if (index === $scope.groupBySegments.length-1) {
  120. $scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
  121. }
  122. segment.type = 'group-by-key';
  123. segment.fake = false;
  124. $scope.target.groupByFields[index] = {field: segment.value};
  125. $scope.$parent.get_data();
  126. };
  127. $scope.transformToSegments = function(addTemplateVars) {
  128. return function(results) {
  129. var segments = _.map(results, function(segment) {
  130. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  131. });
  132. if (addTemplateVars) {
  133. _.each(templateSrv.variables, function(variable) {
  134. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  135. });
  136. }
  137. return segments;
  138. };
  139. };
  140. $scope.handleQueryError = function(err) {
  141. $scope.parserError = err.message || 'Failed to issue metric query';
  142. return [];
  143. };
  144. $scope.toggleQueryMode = function () {
  145. $scope.target.rawQuery = !$scope.target.rawQuery;
  146. };
  147. $scope.init();
  148. });
  149. });