query_parameter_ctrl.ts 7.4 KB

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