query_ctrl.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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) {
  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($scope.transformToSegments(true)).then(function(results) {
  74. if (segment.type === 'key') {
  75. results.splice(0, 0, angular.copy($scope.removeDimSegment));
  76. }
  77. return results;
  78. });
  79. };
  80. $scope.dimSegmentChanged = function(segment, index) {
  81. $scope.dimSegments[index] = segment;
  82. if (segment.value === $scope.removeDimSegment.value) {
  83. $scope.dimSegments.splice(index, 3);
  84. }
  85. else if (segment.type === 'plus-button') {
  86. $scope.dimSegments.push(uiSegmentSrv.newOperator('='));
  87. $scope.dimSegments.push(uiSegmentSrv.newFake('select dimension value', 'value', 'query-segment-value'));
  88. segment.type = 'key';
  89. segment.cssClass = 'query-segment-key';
  90. }
  91. $scope.syncDimSegmentsWithModel();
  92. $scope.ensurePlusButton($scope.dimSegments);
  93. $scope.get_data();
  94. };
  95. $scope.syncDimSegmentsWithModel = function() {
  96. var dims = {};
  97. var length = $scope.dimSegments.length;
  98. for (var i = 0; i < length - 2; i += 3) {
  99. var keySegment = $scope.dimSegments[i];
  100. var valueSegment = $scope.dimSegments[i + 2];
  101. if (!valueSegment.fake) {
  102. dims[keySegment.value] = valueSegment.value;
  103. }
  104. }
  105. $scope.target.dimensions = dims;
  106. };
  107. $scope.getRegions = function() {
  108. return $scope.datasource.metricFindQuery('regions()')
  109. .then($scope.transformToSegments(true));
  110. };
  111. $scope.getNamespaces = function() {
  112. return $scope.datasource.metricFindQuery('namespaces()')
  113. .then($scope.transformToSegments(true));
  114. };
  115. $scope.getMetrics = function() {
  116. return $scope.datasource.metricFindQuery('metrics(' + $scope.target.namespace + ')')
  117. .then($scope.transformToSegments(true));
  118. };
  119. $scope.regionChanged = function() {
  120. $scope.target.region = $scope.regionSegment.value;
  121. $scope.get_data();
  122. };
  123. $scope.namespaceChanged = function() {
  124. $scope.target.namespace = $scope.namespaceSegment.value;
  125. $scope.get_data();
  126. };
  127. $scope.metricChanged = function() {
  128. $scope.target.metricName = $scope.metricSegment.value;
  129. $scope.get_data();
  130. };
  131. $scope.transformToSegments = function(addTemplateVars) {
  132. return function(results) {
  133. var segments = _.map(results, function(segment) {
  134. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  135. });
  136. if (addTemplateVars) {
  137. _.each(templateSrv.variables, function(variable) {
  138. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  139. });
  140. }
  141. return segments;
  142. };
  143. };
  144. $scope.refreshMetricData = function() {
  145. if (!_.isEqual($scope.oldTarget, $scope.target)) {
  146. $scope.oldTarget = angular.copy($scope.target);
  147. $scope.get_data();
  148. }
  149. };
  150. $scope.init();
  151. });
  152. });