query_ctrl.js 6.3 KB

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