query_parameter_ctrl.ts 7.2 KB

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