query_parameter_ctrl.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. define([
  2. 'angular',
  3. 'lodash',
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.controllers');
  8. module.controller('CloudWatchQueryParameterCtrl', function($scope, templateSrv, uiSegmentSrv, datasourceSrv, $q) {
  9. $scope.init = function() {
  10. var target = $scope.target;
  11. target.namespace = target.namespace || '';
  12. target.metricName = target.metricName || '';
  13. target.statistics = target.statistics || ['Average'];
  14. target.dimensions = target.dimensions || {};
  15. target.period = target.period || '';
  16. target.region = target.region || '';
  17. $scope.regionSegment = uiSegmentSrv.getSegmentForValue($scope.target.region, 'select region');
  18. $scope.namespaceSegment = uiSegmentSrv.getSegmentForValue($scope.target.namespace, 'select namespace');
  19. $scope.metricSegment = uiSegmentSrv.getSegmentForValue($scope.target.metricName, 'select metric');
  20. $scope.dimSegments = _.reduce($scope.target.dimensions, function(memo, value, key) {
  21. memo.push(uiSegmentSrv.newKey(key));
  22. memo.push(uiSegmentSrv.newOperator("="));
  23. memo.push(uiSegmentSrv.newKeyValue(value));
  24. return memo;
  25. }, []);
  26. $scope.statSegments = _.map($scope.target.statistics, function(stat) {
  27. return uiSegmentSrv.getSegmentForValue(stat);
  28. });
  29. $scope.ensurePlusButton($scope.statSegments);
  30. $scope.ensurePlusButton($scope.dimSegments);
  31. $scope.removeDimSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove dimension --'});
  32. $scope.removeStatSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove stat --'});
  33. datasourceSrv.get($scope.datasourceName).then(function(datasource) {
  34. $scope.datasource = datasource;
  35. if (_.isEmpty($scope.target.region)) {
  36. $scope.target.region = $scope.datasource.getDefaultRegion();
  37. }
  38. });
  39. if (!$scope.onChange) {
  40. $scope.onChange = function() {};
  41. }
  42. };
  43. $scope.getStatSegments = function() {
  44. return $q.when([
  45. angular.copy($scope.removeStatSegment),
  46. uiSegmentSrv.getSegmentForValue('Average'),
  47. uiSegmentSrv.getSegmentForValue('Maximum'),
  48. uiSegmentSrv.getSegmentForValue('Minimum'),
  49. uiSegmentSrv.getSegmentForValue('Sum'),
  50. uiSegmentSrv.getSegmentForValue('SampleCount'),
  51. ]);
  52. };
  53. $scope.statSegmentChanged = function(segment, index) {
  54. if (segment.value === $scope.removeStatSegment.value) {
  55. $scope.statSegments.splice(index, 1);
  56. } else {
  57. segment.type = 'value';
  58. }
  59. $scope.target.statistics = _.reduce($scope.statSegments, function(memo, seg) {
  60. if (!seg.fake) { memo.push(seg.value); } return memo;
  61. }, []);
  62. $scope.ensurePlusButton($scope.statSegments);
  63. $scope.onChange();
  64. };
  65. $scope.ensurePlusButton = function(segments) {
  66. var count = segments.length;
  67. var lastSegment = segments[Math.max(count-1, 0)];
  68. if (!lastSegment || lastSegment.type !== 'plus-button') {
  69. segments.push(uiSegmentSrv.newPlusButton());
  70. }
  71. };
  72. $scope.getDimSegments = function(segment, $index) {
  73. if (segment.type === 'operator') { return $q.when([]); }
  74. var target = $scope.target;
  75. var query = $q.when([]);
  76. if (segment.type === 'key' || segment.type === 'plus-button') {
  77. query = $scope.datasource.getDimensionKeys($scope.target.namespace);
  78. } else if (segment.type === 'value') {
  79. var dimensionKey = $scope.dimSegments[$index-2].value;
  80. query = $scope.datasource.getDimensionValues(target.region, target.namespace, target.metricName, dimensionKey, {});
  81. }
  82. return query.then($scope.transformToSegments(true)).then(function(results) {
  83. if (segment.type === 'key') {
  84. results.splice(0, 0, angular.copy($scope.removeDimSegment));
  85. }
  86. return results;
  87. });
  88. };
  89. $scope.dimSegmentChanged = function(segment, index) {
  90. $scope.dimSegments[index] = segment;
  91. if (segment.value === $scope.removeDimSegment.value) {
  92. $scope.dimSegments.splice(index, 3);
  93. }
  94. else if (segment.type === 'plus-button') {
  95. $scope.dimSegments.push(uiSegmentSrv.newOperator('='));
  96. $scope.dimSegments.push(uiSegmentSrv.newFake('select dimension value', 'value', 'query-segment-value'));
  97. segment.type = 'key';
  98. segment.cssClass = 'query-segment-key';
  99. }
  100. $scope.syncDimSegmentsWithModel();
  101. $scope.ensurePlusButton($scope.dimSegments);
  102. $scope.onChange();
  103. };
  104. $scope.syncDimSegmentsWithModel = function() {
  105. var dims = {};
  106. var length = $scope.dimSegments.length;
  107. for (var i = 0; i < length - 2; i += 3) {
  108. var keySegment = $scope.dimSegments[i];
  109. var valueSegment = $scope.dimSegments[i + 2];
  110. if (!valueSegment.fake) {
  111. dims[keySegment.value] = valueSegment.value;
  112. }
  113. }
  114. $scope.target.dimensions = dims;
  115. };
  116. $scope.getRegions = function() {
  117. return $scope.datasource.metricFindQuery('regions()')
  118. .then($scope.transformToSegments(true));
  119. };
  120. $scope.getNamespaces = function() {
  121. return $scope.datasource.metricFindQuery('namespaces()')
  122. .then($scope.transformToSegments(true));
  123. };
  124. $scope.getMetrics = function() {
  125. return $scope.datasource.metricFindQuery('metrics(' + $scope.target.namespace + ')')
  126. .then($scope.transformToSegments(true));
  127. };
  128. $scope.regionChanged = function() {
  129. $scope.target.region = $scope.regionSegment.value;
  130. $scope.onChange();
  131. };
  132. $scope.namespaceChanged = function() {
  133. $scope.target.namespace = $scope.namespaceSegment.value;
  134. $scope.onChange();
  135. };
  136. $scope.metricChanged = function() {
  137. $scope.target.metricName = $scope.metricSegment.value;
  138. $scope.onChange();
  139. };
  140. $scope.transformToSegments = function(addTemplateVars) {
  141. return function(results) {
  142. var segments = _.map(results, function(segment) {
  143. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  144. });
  145. if (addTemplateVars) {
  146. _.each(templateSrv.variables, function(variable) {
  147. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  148. });
  149. }
  150. return segments;
  151. };
  152. };
  153. $scope.init();
  154. });
  155. });