queryCtrl.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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) {
  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.statistics = $scope.target.statistics || {};
  14. $scope.target.period = $scope.target.period || 60;
  15. $scope.target.region = $scope.target.region || $scope.datasource.getDefaultRegion();
  16. $scope.target.errors = validateTarget();
  17. };
  18. $scope.refreshMetricData = function() {
  19. $scope.target.errors = validateTarget($scope.target);
  20. // this does not work so good
  21. if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
  22. $scope.oldTarget = angular.copy($scope.target);
  23. $scope.get_data();
  24. }
  25. };
  26. $scope.moveMetricQuery = function(fromIndex, toIndex) {
  27. _.move($scope.panel.targets, fromIndex, toIndex);
  28. };
  29. $scope.duplicate = function() {
  30. var clone = angular.copy($scope.target);
  31. $scope.panel.targets.push(clone);
  32. };
  33. $scope.suggestRegion = function(query, callback) { // jshint unused:false
  34. return $scope.datasource.performSuggestRegion();
  35. };
  36. $scope.suggestNamespace = function(query, callback) { // jshint unused:false
  37. return $scope.datasource.performSuggestNamespace();
  38. };
  39. $scope.suggestMetrics = function(query, callback) { // jshint unused:false
  40. return $scope.datasource.performSuggestMetrics($scope.target.namespace);
  41. };
  42. $scope.suggestDimensionKeys = function(query, callback) { // jshint unused:false
  43. return $scope.datasource.performSuggestDimensionKeys($scope.target.namespace);
  44. };
  45. $scope.suggestDimensionValues = function(query, callback) {
  46. if (!$scope.target.namespace || !$scope.target.metricName) {
  47. return callback([]);
  48. }
  49. $scope.datasource.performSuggestDimensionValues(
  50. $scope.target.region,
  51. $scope.target.namespace,
  52. $scope.target.metricName,
  53. $scope.target.dimensions
  54. )
  55. .then(function(result) {
  56. var suggestData = _.chain(result)
  57. .flatten(true)
  58. .filter(function(dimension) {
  59. return dimension.Name === $scope.target.currentDimensionKey;
  60. })
  61. .pluck('Value')
  62. .uniq()
  63. .value();
  64. callback(suggestData);
  65. }, function() {
  66. callback([]);
  67. });
  68. };
  69. $scope.addDimension = function() {
  70. if (!$scope.addDimensionMode) {
  71. $scope.addDimensionMode = true;
  72. return;
  73. }
  74. if (!$scope.target.dimensions) {
  75. $scope.target.dimensions = {};
  76. }
  77. $scope.target.dimensions[$scope.target.currentDimensionKey] = $scope.target.currentDimensionValue;
  78. $scope.target.currentDimensionKey = '';
  79. $scope.target.currentDimensionValue = '';
  80. $scope.refreshMetricData();
  81. $scope.addDimensionMode = false;
  82. };
  83. $scope.removeDimension = function(key) {
  84. delete $scope.target.dimensions[key];
  85. $scope.refreshMetricData();
  86. };
  87. $scope.statisticsOptionChanged = function() {
  88. $scope.refreshMetricData();
  89. };
  90. // TODO: validate target
  91. function validateTarget() {
  92. var errs = {};
  93. if ($scope.target.period < 60 || ($scope.target.period % 60) !== 0) {
  94. errs.period = 'Period must be at least 60 seconds and must be a multiple of 60';
  95. }
  96. return errs;
  97. }
  98. });
  99. });