query_ctrl.js 6.7 KB

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