query_parameter_ctrl.ts 7.3 KB

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