queryCtrl.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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) {
  9. $scope.init = function() {
  10. $scope.target.namespace = $scope.target.namespace || '';
  11. $scope.target.metricName = $scope.target.metricName || '';
  12. $scope.target.dimensions = $scope.target.dimensions || {};
  13. $scope.target.escapedDimensions = this.escapeDimensions($scope.target.dimensions);
  14. $scope.target.statistics = $scope.target.statistics || {};
  15. $scope.target.period = $scope.target.period || 60;
  16. $scope.target.region = $scope.target.region || $scope.datasource.getDefaultRegion();
  17. $scope.target.errors = validateTarget();
  18. $scope.regionSegment = $scope.getSegmentForValue($scope.target.region, 'select region');
  19. $scope.namespaceSegment = $scope.getSegmentForValue($scope.target.namespace, 'select namespace');
  20. $scope.metricSegment = $scope.getSegmentForValue($scope.target.metricName, 'select metric');
  21. };
  22. $scope.getSegmentForValue = function(value, fallbackText) {
  23. if (value) {
  24. return uiSegmentSrv.newSegment(value);
  25. } else {
  26. return uiSegmentSrv.newSegment({value: fallbackText, fake: true});
  27. }
  28. };
  29. $scope.getRegions = function() {
  30. return $scope.datasource.metricFindQuery('region()')
  31. .then($scope.transformToSegments(true));
  32. };
  33. $scope.getNamespaces = function() {
  34. return $scope.datasource.metricFindQuery('namespace()')
  35. .then($scope.transformToSegments(true));
  36. };
  37. $scope.getMetrics = function() {
  38. return $scope.datasource.metricFindQuery('metrics(' + $scope.target.namespace + ')')
  39. .then($scope.transformToSegments(true));
  40. };
  41. $scope.regionChanged = function() {
  42. $scope.target.region = $scope.regionSegment.value;
  43. $scope.get_data();
  44. };
  45. $scope.namespaceChanged = function() {
  46. $scope.target.namespace = $scope.namespaceSegment.value;
  47. $scope.get_data();
  48. };
  49. $scope.metricChanged = function() {
  50. $scope.target.metricName = $scope.metricSegment.value;
  51. $scope.get_data();
  52. };
  53. $scope.transformToSegments = function(addTemplateVars) {
  54. return function(results) {
  55. var segments = _.map(results, function(segment) {
  56. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  57. });
  58. if (addTemplateVars) {
  59. _.each(templateSrv.variables, function(variable) {
  60. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  61. });
  62. }
  63. return segments;
  64. };
  65. };
  66. $scope.refreshMetricData = function() {
  67. $scope.target.errors = validateTarget($scope.target);
  68. // this does not work so good
  69. if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
  70. $scope.oldTarget = angular.copy($scope.target);
  71. $scope.get_data();
  72. }
  73. };
  74. $scope.suggestDimensionKeys = function(query, callback) { // jshint unused:false
  75. return _.union($scope.datasource.performSuggestDimensionKeys($scope.target.namespace), $scope.datasource.getTemplateVariableNames());
  76. };
  77. $scope.suggestDimensionValues = function(query, callback) {
  78. if (!$scope.target.namespace || !$scope.target.metricName) {
  79. return callback([]);
  80. }
  81. $scope.datasource.performSuggestDimensionValues(
  82. $scope.target.region,
  83. $scope.target.namespace,
  84. $scope.target.metricName,
  85. $scope.target.dimensions
  86. )
  87. .then(function(result) {
  88. var suggestData = _.chain(result)
  89. .flatten(true)
  90. .filter(function(dimension) {
  91. return dimension.Name === templateSrv.replace($scope.target.currentDimensionKey);
  92. })
  93. .pluck('Value')
  94. .uniq()
  95. .value();
  96. suggestData = _.union(suggestData, $scope.datasource.getTemplateVariableNames());
  97. callback(suggestData);
  98. }, function() {
  99. callback([]);
  100. });
  101. };
  102. $scope.addDimension = function() {
  103. if (!$scope.addDimensionMode) {
  104. $scope.addDimensionMode = true;
  105. return;
  106. }
  107. if (!$scope.target.dimensions) {
  108. $scope.target.dimensions = {};
  109. }
  110. $scope.target.dimensions[$scope.target.currentDimensionKey] = $scope.target.currentDimensionValue;
  111. $scope.target.escapedDimensions = this.escapeDimensions($scope.target.dimensions);
  112. $scope.target.currentDimensionKey = '';
  113. $scope.target.currentDimensionValue = '';
  114. $scope.refreshMetricData();
  115. $scope.addDimensionMode = false;
  116. };
  117. $scope.removeDimension = function(key) {
  118. key = key.replace(/\\\$/g, '$');
  119. delete $scope.target.dimensions[key];
  120. $scope.target.escapedDimensions = this.escapeDimensions($scope.target.dimensions);
  121. $scope.refreshMetricData();
  122. };
  123. $scope.escapeDimensions = function(d) {
  124. var result = {};
  125. _.chain(d)
  126. .keys(d)
  127. .each(function(k) {
  128. var v = d[k];
  129. result[k.replace(/\$/g, '\uFF04')] = v.replace(/\$/g, '\$');
  130. });
  131. return result;
  132. };
  133. $scope.statisticsOptionChanged = function() {
  134. $scope.refreshMetricData();
  135. };
  136. // TODO: validate target
  137. function validateTarget() {
  138. var errs = {};
  139. if ($scope.target.period < 60 || ($scope.target.period % 60) !== 0) {
  140. errs.period = 'Period must be at least 60 seconds and must be a multiple of 60';
  141. }
  142. return errs;
  143. }
  144. $scope.init();
  145. });
  146. });