query_parameter_ctrl.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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(_.flatten([
  54. angular.copy($scope.removeStatSegment),
  55. _.map($scope.datasource.standardStatistics, function(s) {
  56. return uiSegmentSrv.getSegmentForValue(s);
  57. }),
  58. uiSegmentSrv.getSegmentForValue('pNN.NN'),
  59. ]));
  60. };
  61. $scope.statSegmentChanged = function(segment, index) {
  62. if (segment.value === $scope.removeStatSegment.value) {
  63. $scope.statSegments.splice(index, 1);
  64. } else {
  65. segment.type = 'value';
  66. }
  67. $scope.target.statistics = _.reduce($scope.statSegments, function(memo, seg) {
  68. if (!seg.fake) { memo.push(seg.value); } return memo;
  69. }, []);
  70. $scope.ensurePlusButton($scope.statSegments);
  71. $scope.onChange();
  72. };
  73. $scope.ensurePlusButton = function(segments) {
  74. var count = segments.length;
  75. var lastSegment = segments[Math.max(count-1, 0)];
  76. if (!lastSegment || lastSegment.type !== 'plus-button') {
  77. segments.push(uiSegmentSrv.newPlusButton());
  78. }
  79. };
  80. $scope.getDimSegments = function(segment, $index) {
  81. if (segment.type === 'operator') { return $q.when([]); }
  82. var target = $scope.target;
  83. var query = $q.when([]);
  84. if (segment.type === 'key' || segment.type === 'plus-button') {
  85. query = $scope.datasource.getDimensionKeys($scope.target.namespace, $scope.target.region);
  86. } else if (segment.type === 'value') {
  87. var dimensionKey = $scope.dimSegments[$index-2].value;
  88. query = $scope.datasource.getDimensionValues(target.region, target.namespace, target.metricName, dimensionKey, target.dimensions);
  89. }
  90. return query.then($scope.transformToSegments(true)).then(function(results) {
  91. if (segment.type === 'key') {
  92. results.splice(0, 0, angular.copy($scope.removeDimSegment));
  93. }
  94. return results;
  95. });
  96. };
  97. $scope.dimSegmentChanged = function(segment, index) {
  98. $scope.dimSegments[index] = segment;
  99. if (segment.value === $scope.removeDimSegment.value) {
  100. $scope.dimSegments.splice(index, 3);
  101. }
  102. else if (segment.type === 'plus-button') {
  103. $scope.dimSegments.push(uiSegmentSrv.newOperator('='));
  104. $scope.dimSegments.push(uiSegmentSrv.newFake('select dimension value', 'value', 'query-segment-value'));
  105. segment.type = 'key';
  106. segment.cssClass = 'query-segment-key';
  107. }
  108. $scope.syncDimSegmentsWithModel();
  109. $scope.ensurePlusButton($scope.dimSegments);
  110. $scope.onChange();
  111. };
  112. $scope.syncDimSegmentsWithModel = function() {
  113. var dims = {};
  114. var length = $scope.dimSegments.length;
  115. for (var i = 0; i < length - 2; i += 3) {
  116. var keySegment = $scope.dimSegments[i];
  117. var valueSegment = $scope.dimSegments[i + 2];
  118. if (!valueSegment.fake) {
  119. dims[keySegment.value] = valueSegment.value;
  120. }
  121. }
  122. $scope.target.dimensions = dims;
  123. };
  124. $scope.getRegions = function() {
  125. return $scope.datasource.metricFindQuery('regions()')
  126. .then($scope.transformToSegments(true));
  127. };
  128. $scope.getNamespaces = function() {
  129. return $scope.datasource.metricFindQuery('namespaces()')
  130. .then($scope.transformToSegments(true));
  131. };
  132. $scope.getMetrics = function() {
  133. return $scope.datasource.metricFindQuery('metrics(' + $scope.target.namespace + ',' + $scope.target.region + ')')
  134. .then($scope.transformToSegments(true));
  135. };
  136. $scope.regionChanged = function() {
  137. $scope.target.region = $scope.regionSegment.value;
  138. $scope.onChange();
  139. };
  140. $scope.namespaceChanged = function() {
  141. $scope.target.namespace = $scope.namespaceSegment.value;
  142. $scope.onChange();
  143. };
  144. $scope.metricChanged = function() {
  145. $scope.target.metricName = $scope.metricSegment.value;
  146. $scope.onChange();
  147. };
  148. $scope.transformToSegments = function(addTemplateVars) {
  149. return function(results) {
  150. var segments = _.map(results, function(segment) {
  151. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  152. });
  153. if (addTemplateVars) {
  154. _.each(templateSrv.variables, function(variable) {
  155. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  156. });
  157. }
  158. return segments;
  159. };
  160. };
  161. $scope.init();
  162. });
  163. });