query_parameter_ctrl.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. export class CloudWatchQueryParameter {
  4. constructor() {
  5. return {
  6. templateUrl: 'public/app/plugins/datasource/cloudwatch/partials/query.parameter.html',
  7. controller: 'CloudWatchQueryParameterCtrl',
  8. restrict: 'E',
  9. scope: {
  10. target: "=",
  11. datasource: "=",
  12. onChange: "&",
  13. }
  14. };
  15. }
  16. }
  17. export class CloudWatchQueryParameterCtrl {
  18. constructor($scope, templateSrv, uiSegmentSrv, datasourceSrv, $q) {
  19. $scope.init = function() {
  20. var target = $scope.target;
  21. target.namespace = target.namespace || '';
  22. target.metricName = target.metricName || '';
  23. target.statistics = target.statistics || ['Average'];
  24. target.dimensions = target.dimensions || {};
  25. target.period = target.period || '';
  26. target.region = target.region || '';
  27. $scope.regionSegment = uiSegmentSrv.getSegmentForValue($scope.target.region, 'select region');
  28. $scope.namespaceSegment = uiSegmentSrv.getSegmentForValue($scope.target.namespace, 'select namespace');
  29. $scope.metricSegment = uiSegmentSrv.getSegmentForValue($scope.target.metricName, 'select metric');
  30. $scope.dimSegments = _.reduce($scope.target.dimensions, function(memo, value, key) {
  31. memo.push(uiSegmentSrv.newKey(key));
  32. memo.push(uiSegmentSrv.newOperator("="));
  33. memo.push(uiSegmentSrv.newKeyValue(value));
  34. return memo;
  35. }, []);
  36. $scope.statSegments = _.map($scope.target.statistics, function(stat) {
  37. return uiSegmentSrv.getSegmentForValue(stat);
  38. });
  39. $scope.ensurePlusButton($scope.statSegments);
  40. $scope.ensurePlusButton($scope.dimSegments);
  41. $scope.removeDimSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove dimension --'});
  42. $scope.removeStatSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove stat --'});
  43. if (_.isEmpty($scope.target.region)) {
  44. $scope.target.region = $scope.datasource.getDefaultRegion();
  45. }
  46. if (!$scope.onChange) {
  47. $scope.onChange = function() {};
  48. }
  49. };
  50. $scope.getStatSegments = function() {
  51. return $q.when(_.flatten([
  52. angular.copy($scope.removeStatSegment),
  53. _.map($scope.datasource.standardStatistics, function(s) {
  54. return uiSegmentSrv.getSegmentForValue(s);
  55. }),
  56. uiSegmentSrv.getSegmentForValue('pNN.NN'),
  57. ]));
  58. };
  59. $scope.statSegmentChanged = function(segment, index) {
  60. if (segment.value === $scope.removeStatSegment.value) {
  61. $scope.statSegments.splice(index, 1);
  62. } else {
  63. segment.type = 'value';
  64. }
  65. $scope.target.statistics = _.reduce($scope.statSegments, function(memo, seg) {
  66. if (!seg.fake) { memo.push(seg.value); } return memo;
  67. }, []);
  68. $scope.ensurePlusButton($scope.statSegments);
  69. $scope.onChange();
  70. };
  71. $scope.ensurePlusButton = function(segments) {
  72. var count = segments.length;
  73. var lastSegment = segments[Math.max(count-1, 0)];
  74. if (!lastSegment || lastSegment.type !== 'plus-button') {
  75. segments.push(uiSegmentSrv.newPlusButton());
  76. }
  77. };
  78. $scope.getDimSegments = function(segment, $index) {
  79. if (segment.type === 'operator') { return $q.when([]); }
  80. var target = $scope.target;
  81. var query = $q.when([]);
  82. if (segment.type === 'key' || segment.type === 'plus-button') {
  83. query = $scope.datasource.getDimensionKeys($scope.target.namespace, $scope.target.region);
  84. } else if (segment.type === 'value') {
  85. var dimensionKey = $scope.dimSegments[$index-2].value;
  86. query = $scope.datasource.getDimensionValues(target.region, target.namespace, target.metricName, dimensionKey, target.dimensions);
  87. }
  88. return query.then($scope.transformToSegments(true)).then(function(results) {
  89. if (segment.type === 'key') {
  90. results.splice(0, 0, angular.copy($scope.removeDimSegment));
  91. }
  92. return results;
  93. });
  94. };
  95. $scope.dimSegmentChanged = function(segment, index) {
  96. $scope.dimSegments[index] = segment;
  97. if (segment.value === $scope.removeDimSegment.value) {
  98. $scope.dimSegments.splice(index, 3);
  99. } else if (segment.type === 'plus-button') {
  100. $scope.dimSegments.push(uiSegmentSrv.newOperator('='));
  101. $scope.dimSegments.push(uiSegmentSrv.newFake('select dimension value', 'value', 'query-segment-value'));
  102. segment.type = 'key';
  103. segment.cssClass = 'query-segment-key';
  104. }
  105. $scope.syncDimSegmentsWithModel();
  106. $scope.ensurePlusButton($scope.dimSegments);
  107. $scope.onChange();
  108. };
  109. $scope.syncDimSegmentsWithModel = function() {
  110. var dims = {};
  111. var length = $scope.dimSegments.length;
  112. for (var i = 0; i < length - 2; i += 3) {
  113. var keySegment = $scope.dimSegments[i];
  114. var valueSegment = $scope.dimSegments[i + 2];
  115. if (!valueSegment.fake) {
  116. dims[keySegment.value] = valueSegment.value;
  117. }
  118. }
  119. $scope.target.dimensions = dims;
  120. };
  121. $scope.getRegions = function() {
  122. return $scope.datasource.metricFindQuery('regions()')
  123. .then($scope.transformToSegments(true));
  124. };
  125. $scope.getNamespaces = function() {
  126. return $scope.datasource.metricFindQuery('namespaces()')
  127. .then($scope.transformToSegments(true));
  128. };
  129. $scope.getMetrics = function() {
  130. return $scope.datasource.metricFindQuery('metrics(' + $scope.target.namespace + ',' + $scope.target.region + ')')
  131. .then($scope.transformToSegments(true));
  132. };
  133. $scope.regionChanged = function() {
  134. $scope.target.region = $scope.regionSegment.value;
  135. $scope.onChange();
  136. };
  137. $scope.namespaceChanged = function() {
  138. $scope.target.namespace = $scope.namespaceSegment.value;
  139. $scope.onChange();
  140. };
  141. $scope.metricChanged = function() {
  142. $scope.target.metricName = $scope.metricSegment.value;
  143. $scope.onChange();
  144. };
  145. $scope.transformToSegments = function(addTemplateVars) {
  146. return function(results) {
  147. var segments = _.map(results, function(segment) {
  148. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  149. });
  150. if (addTemplateVars) {
  151. _.each(templateSrv.variables, function(variable) {
  152. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  153. });
  154. }
  155. return segments;
  156. };
  157. };
  158. $scope.init();
  159. }
  160. }
  161. angular.module('grafana.controllers').directive('cloudwatchQueryParameter', CloudWatchQueryParameter);
  162. angular.module('grafana.controllers').controller('CloudWatchQueryParameterCtrl', CloudWatchQueryParameterCtrl);