query_parameter_ctrl.ts 7.2 KB

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