query_parameter_ctrl.js 6.6 KB

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