queryCtrl.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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, $sce, 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. var target = $scope.target;
  16. target.function = target.function || 'mean';
  17. $scope.queryBuilder = new ElasticQueryBuilder(target);
  18. if (!target.keyField) {
  19. target.keyField = '@timestamp';
  20. }
  21. $scope.keyFieldSegment = new MetricSegment({value: target.keyField});
  22. if (!target.valueField) {
  23. target.valueField = 'metric';
  24. }
  25. $scope.valueFieldSegment = new MetricSegment({value: target.valueField});
  26. if (!target.termKey) {
  27. target.termKey = 'service.raw';
  28. }
  29. $scope.termKeySegment = new MetricSegment({value: target.termKey});
  30. if (!target.termValue) {
  31. target.termValue = 'cpu-average/cpu-user';
  32. }
  33. $scope.termValueSegment = new MetricSegment({value: target.termValue});
  34. if (!target.groupByField) {
  35. target.groupByField = 'host.raw';
  36. }
  37. $scope.groupByFieldSegment = new MetricSegment({value: target.groupByField});
  38. if (!target.measurement) {
  39. $scope.measurementSegment = MetricSegment.newSelectMeasurement();
  40. } else {
  41. $scope.measurementSegment = new MetricSegment(target.measurement);
  42. }
  43. $scope.tagSegments = [];
  44. _.each(target.tags, function(tag) {
  45. if (tag.condition) {
  46. $scope.tagSegments.push(MetricSegment.newCondition(tag.condition));
  47. }
  48. $scope.tagSegments.push(new MetricSegment({value: tag.key, type: 'key', cssClass: 'query-segment-key' }));
  49. $scope.tagSegments.push(new MetricSegment.newOperator("="));
  50. $scope.tagSegments.push(new MetricSegment({value: tag.value, type: 'value', cssClass: 'query-segment-value'}));
  51. });
  52. $scope.fixTagSegments();
  53. $scope.groupBySegments = [];
  54. _.each(target.groupByTags, function(tag) {
  55. $scope.groupBySegments.push(new MetricSegment(tag));
  56. });
  57. $scope.groupBySegments.push(MetricSegment.newPlusButton());
  58. $scope.removeTagFilterSegment = new MetricSegment({fake: true, value: '-- remove tag filter --'});
  59. $scope.removeGroupBySegment = new MetricSegment({fake: true, value: '-- remove group by --'});
  60. };
  61. $scope.valueFieldChanged = function() {
  62. $scope.target.valueField = $scope.valueFieldSegment.value;
  63. $scope.$parent.get_data();
  64. };
  65. $scope.keyFieldChanged = function() {
  66. $scope.target.keyField = $scope.keyFieldSegment.value;
  67. $scope.$parent.get_data();
  68. };
  69. $scope.termValueSegmentChanged = function() {
  70. $scope.target.termValue = $scope.termValueSegment.value;
  71. $scope.$parent.get_data();
  72. };
  73. $scope.termKeySegmentChanged = function() {
  74. $scope.target.termKey = $scope.termKeySegment.value;
  75. $scope.$parent.get_data();
  76. };
  77. $scope.groupByFieldChanged = function() {
  78. $scope.target.groupBy = $scope.groupByFieldSegment.value;
  79. $scope.$parent.get_data();
  80. };
  81. $scope.fixTagSegments = function() {
  82. var count = $scope.tagSegments.length;
  83. var lastSegment = $scope.tagSegments[Math.max(count-1, 0)];
  84. if (!lastSegment || lastSegment.type !== 'plus-button') {
  85. $scope.tagSegments.push(MetricSegment.newPlusButton());
  86. }
  87. };
  88. $scope.groupByTagUpdated = function(segment, index) {
  89. if (segment.value === $scope.removeGroupBySegment.value) {
  90. $scope.target.groupByTags.splice(index, 1);
  91. $scope.groupBySegments.splice(index, 1);
  92. $scope.$parent.get_data();
  93. return;
  94. }
  95. if (index === $scope.groupBySegments.length-1) {
  96. $scope.groupBySegments.push(MetricSegment.newPlusButton());
  97. }
  98. segment.type = 'group-by-key';
  99. segment.fake = false;
  100. $scope.target.groupByTags[index] = segment.value;
  101. $scope.$parent.get_data();
  102. };
  103. $scope.changeFunction = function(func) {
  104. $scope.target.function = func;
  105. $scope.$parent.get_data();
  106. };
  107. $scope.measurementChanged = function() {
  108. $scope.target.measurement = $scope.measurementSegment.value;
  109. $scope.$parent.get_data();
  110. };
  111. $scope.toggleQueryMode = function () {
  112. $scope.target.rawQuery = !$scope.target.rawQuery;
  113. };
  114. $scope.handleQueryError = function(err) {
  115. $scope.parserError = err.message || 'Failed to issue metric query';
  116. return [];
  117. };
  118. $scope.transformToSegments = function(results) {
  119. return _.map(results, function(segment) {
  120. return new MetricSegment({ value: segment.text, expandable: segment.expandable });
  121. });
  122. };
  123. $scope.addTemplateVariableSegments = function(segments) {
  124. _.each(templateSrv.variables, function(variable) {
  125. segments.unshift(new MetricSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  126. });
  127. return segments;
  128. };
  129. $scope.init();
  130. });
  131. });