query_ctrl.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. define([
  2. 'angular',
  3. 'lodash',
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.controllers');
  8. module.controller('CloudWatchQueryCtrl', function($scope, templateSrv, uiSegmentSrv, $q) {
  9. $scope.init = function() {
  10. $scope.target.namespace = $scope.target.namespace || '';
  11. $scope.target.metricName = $scope.target.metricName || '';
  12. $scope.target.statistics = $scope.target.statistics || {Average: true};
  13. $scope.target.dimensions = $scope.target.dimensions || {};
  14. $scope.target.period = $scope.target.period || 60;
  15. $scope.target.region = $scope.target.region || $scope.datasource.getDefaultRegion();
  16. $scope.regionSegment = uiSegmentSrv.getSegmentForValue($scope.target.region, 'select region');
  17. $scope.namespaceSegment = uiSegmentSrv.getSegmentForValue($scope.target.namespace, 'select namespace');
  18. $scope.metricSegment = uiSegmentSrv.getSegmentForValue($scope.target.metricName, 'select metric');
  19. $scope.dimSegments = _.reduce($scope.target.dimensions, function(memo, value, key) {
  20. memo.push(uiSegmentSrv.newKey(key));
  21. memo.push(uiSegmentSrv.newOperator("="));
  22. memo.push(uiSegmentSrv.newKeyValue(value));
  23. return memo;
  24. }, []);
  25. $scope.fixSegments();
  26. $scope.removeDimSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove dimension --'});
  27. };
  28. $scope.fixSegments = function() {
  29. var count = $scope.dimSegments.length;
  30. var lastSegment = $scope.dimSegments[Math.max(count-1, 0)];
  31. if (!lastSegment || lastSegment.type !== 'plus-button') {
  32. $scope.dimSegments.push(uiSegmentSrv.newPlusButton());
  33. }
  34. };
  35. $scope.getDimSegments = function(segment) {
  36. if (segment.type === 'operator') { return $q.when([]); }
  37. var target = $scope.target;
  38. var query = $q.when([]);
  39. if (segment.type === 'key' || segment.type === 'plus-button') {
  40. query = $scope.datasource.getDimensionKeys($scope.target.namespace);
  41. } else if (segment.type === 'value') {
  42. query = $scope.datasource.getDimensionValues(target.region, target.namespace, target.metricName, {});
  43. }
  44. return query.then($scope.transformToSegments(true)).then(function(results) {
  45. if (segment.type === 'key') {
  46. results.splice(0, 0, angular.copy($scope.removeDimSegment));
  47. }
  48. return results;
  49. });
  50. };
  51. $scope.dimSegmentChanged = function(segment, index) {
  52. $scope.dimSegments[index] = segment;
  53. if (segment.value === $scope.removeDimSegment.value) {
  54. $scope.dimSegments.splice(index, 3);
  55. }
  56. else if (segment.type === 'plus-button') {
  57. $scope.dimSegments.push(uiSegmentSrv.newOperator('='));
  58. $scope.dimSegments.push(uiSegmentSrv.newFake('select dimension value', 'value', 'query-segment-value'));
  59. segment.type = 'key';
  60. segment.cssClass = 'query-segment-key';
  61. }
  62. $scope.fixSegments();
  63. $scope.syncDimSegmentsWithModel();
  64. $scope.get_data();
  65. };
  66. $scope.syncDimSegmentsWithModel = function() {
  67. var dims = {};
  68. var length = $scope.dimSegments.length;
  69. for (var i = 0; i < length - 2; i += 3) {
  70. var keySegment = $scope.dimSegments[i];
  71. var valueSegment = $scope.dimSegments[i + 2];
  72. if (!valueSegment.fake) {
  73. dims[keySegment.value] = valueSegment.value;
  74. }
  75. }
  76. $scope.target.dimensions = dims;
  77. };
  78. $scope.getRegions = function() {
  79. return $scope.datasource.metricFindQuery('regions()')
  80. .then($scope.transformToSegments(true));
  81. };
  82. $scope.getNamespaces = function() {
  83. return $scope.datasource.metricFindQuery('namespaces()')
  84. .then($scope.transformToSegments(true));
  85. };
  86. $scope.getMetrics = function() {
  87. return $scope.datasource.metricFindQuery('metrics(' + $scope.target.namespace + ')')
  88. .then($scope.transformToSegments(true));
  89. };
  90. $scope.regionChanged = function() {
  91. $scope.target.region = $scope.regionSegment.value;
  92. $scope.get_data();
  93. };
  94. $scope.namespaceChanged = function() {
  95. $scope.target.namespace = $scope.namespaceSegment.value;
  96. $scope.get_data();
  97. };
  98. $scope.metricChanged = function() {
  99. $scope.target.metricName = $scope.metricSegment.value;
  100. $scope.get_data();
  101. };
  102. $scope.transformToSegments = function(addTemplateVars) {
  103. return function(results) {
  104. var segments = _.map(results, function(segment) {
  105. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  106. });
  107. if (addTemplateVars) {
  108. _.each(templateSrv.variables, function(variable) {
  109. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  110. });
  111. }
  112. return segments;
  113. };
  114. };
  115. $scope.refreshMetricData = function() {
  116. if (!_.isEqual($scope.oldTarget, $scope.target)) {
  117. $scope.oldTarget = angular.copy($scope.target);
  118. $scope.get_data();
  119. }
  120. };
  121. $scope.addDimension = function() {
  122. if (!$scope.addDimensionMode) {
  123. $scope.addDimensionMode = true;
  124. return;
  125. }
  126. if (!$scope.target.dimensions) {
  127. $scope.target.dimensions = {};
  128. }
  129. $scope.target.dimensions[$scope.target.currentDimensionKey] = $scope.target.currentDimensionValue;
  130. $scope.target.escapedDimensions = this.escapeDimensions($scope.target.dimensions);
  131. $scope.target.currentDimensionKey = '';
  132. $scope.target.currentDimensionValue = '';
  133. $scope.refreshMetricData();
  134. $scope.addDimensionMode = false;
  135. };
  136. $scope.removeDimension = function(key) {
  137. key = key.replace(/\\\$/g, '$');
  138. delete $scope.target.dimensions[key];
  139. $scope.target.escapedDimensions = this.escapeDimensions($scope.target.dimensions);
  140. $scope.refreshMetricData();
  141. };
  142. $scope.escapeDimensions = function(d) {
  143. var result = {};
  144. _.chain(d)
  145. .keys(d)
  146. .each(function(k) {
  147. var v = d[k];
  148. result[k.replace(/\$/g, '\uFF04')] = v.replace(/\$/g, '\$');
  149. });
  150. return result;
  151. };
  152. $scope.statisticsOptionChanged = function() {
  153. $scope.refreshMetricData();
  154. };
  155. $scope.init();
  156. });
  157. });