query_parameter_ctrl.ts 6.8 KB

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