query_parameter_ctrl.ts 7.1 KB

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