query_parameter_ctrl.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. delete target.dimensions[dimensionKey];
  100. query = $scope.datasource.getDimensionValues(
  101. target.region,
  102. target.namespace,
  103. target.metricName,
  104. dimensionKey,
  105. target.dimensions
  106. );
  107. }
  108. return query.then($scope.transformToSegments(true)).then(results => {
  109. if (segment.type === 'key') {
  110. results.splice(0, 0, angular.copy($scope.removeDimSegment));
  111. }
  112. return results;
  113. });
  114. };
  115. $scope.dimSegmentChanged = (segment, index) => {
  116. $scope.dimSegments[index] = segment;
  117. if (segment.value === $scope.removeDimSegment.value) {
  118. $scope.dimSegments.splice(index, 3);
  119. } else if (segment.type === 'plus-button') {
  120. $scope.dimSegments.push(uiSegmentSrv.newOperator('='));
  121. $scope.dimSegments.push(uiSegmentSrv.newFake('select dimension value', 'value', 'query-segment-value'));
  122. segment.type = 'key';
  123. segment.cssClass = 'query-segment-key';
  124. }
  125. $scope.syncDimSegmentsWithModel();
  126. $scope.ensurePlusButton($scope.dimSegments);
  127. $scope.onChange();
  128. };
  129. $scope.syncDimSegmentsWithModel = () => {
  130. const dims = {};
  131. const length = $scope.dimSegments.length;
  132. for (let i = 0; i < length - 2; i += 3) {
  133. const keySegment = $scope.dimSegments[i];
  134. const valueSegment = $scope.dimSegments[i + 2];
  135. if (!valueSegment.fake) {
  136. dims[keySegment.value] = valueSegment.value;
  137. }
  138. }
  139. $scope.target.dimensions = dims;
  140. };
  141. $scope.getRegions = () => {
  142. return $scope.datasource
  143. .metricFindQuery('regions()')
  144. .then(results => {
  145. results.unshift({ text: 'default' });
  146. return results;
  147. })
  148. .then($scope.transformToSegments(true));
  149. };
  150. $scope.getNamespaces = () => {
  151. return $scope.datasource.metricFindQuery('namespaces()').then($scope.transformToSegments(true));
  152. };
  153. $scope.getMetrics = () => {
  154. return $scope.datasource
  155. .metricFindQuery('metrics(' + $scope.target.namespace + ',' + $scope.target.region + ')')
  156. .then($scope.transformToSegments(true));
  157. };
  158. $scope.regionChanged = () => {
  159. $scope.target.region = $scope.regionSegment.value;
  160. $scope.onChange();
  161. };
  162. $scope.namespaceChanged = () => {
  163. $scope.target.namespace = $scope.namespaceSegment.value;
  164. $scope.onChange();
  165. };
  166. $scope.metricChanged = () => {
  167. $scope.target.metricName = $scope.metricSegment.value;
  168. $scope.onChange();
  169. };
  170. $scope.transformToSegments = addTemplateVars => {
  171. return results => {
  172. const segments = _.map(results, segment => {
  173. return uiSegmentSrv.newSegment({
  174. value: segment.text,
  175. expandable: segment.expandable,
  176. });
  177. });
  178. if (addTemplateVars) {
  179. _.each(templateSrv.variables, variable => {
  180. segments.unshift(
  181. uiSegmentSrv.newSegment({
  182. type: 'template',
  183. value: '$' + variable.name,
  184. expandable: true,
  185. })
  186. );
  187. });
  188. }
  189. return segments;
  190. };
  191. };
  192. $scope.init();
  193. }
  194. }
  195. export function cloudWatchQueryParameter() {
  196. return {
  197. templateUrl: 'public/app/plugins/datasource/cloudwatch/partials/query.parameter.html',
  198. controller: CloudWatchQueryParameterCtrl,
  199. restrict: 'E',
  200. scope: {
  201. target: '=',
  202. datasource: '=',
  203. onChange: '&',
  204. },
  205. };
  206. }
  207. coreModule.directive('cloudwatchQueryParameter', cloudWatchQueryParameter);