query_ctrl.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 = 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. };
  22. $scope.getRegions = function() {
  23. return $scope.datasource.metricFindQuery('regions()')
  24. .then($scope.transformToSegments(true));
  25. };
  26. $scope.getNamespaces = function() {
  27. return $scope.datasource.metricFindQuery('namespaces()')
  28. .then($scope.transformToSegments(true));
  29. };
  30. $scope.getMetrics = function() {
  31. return $scope.datasource.metricFindQuery('metrics(' + $scope.target.namespace + ')')
  32. .then($scope.transformToSegments(true));
  33. };
  34. $scope.regionChanged = function() {
  35. $scope.target.region = $scope.regionSegment.value;
  36. $scope.get_data();
  37. };
  38. $scope.namespaceChanged = function() {
  39. $scope.target.namespace = $scope.namespaceSegment.value;
  40. $scope.get_data();
  41. };
  42. $scope.metricChanged = function() {
  43. $scope.target.metricName = $scope.metricSegment.value;
  44. $scope.get_data();
  45. };
  46. $scope.transformToSegments = function(addTemplateVars) {
  47. return function(results) {
  48. var segments = _.map(results, function(segment) {
  49. return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  50. });
  51. if (addTemplateVars) {
  52. _.each(templateSrv.variables, function(variable) {
  53. segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
  54. });
  55. }
  56. return segments;
  57. };
  58. };
  59. $scope.refreshMetricData = function() {
  60. $scope.target.errors = validateTarget($scope.target);
  61. // this does not work so good
  62. if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
  63. $scope.oldTarget = angular.copy($scope.target);
  64. $scope.get_data();
  65. }
  66. };
  67. $scope.suggestDimensionKeys = function(query, callback) { // jshint unused:false
  68. $scope.datasource.getDimensionKeys($scope.target.namespace).then(function(result) {
  69. callback(_.pluck(result, 'text'));
  70. });
  71. };
  72. // TODO: Removed template variables from the suggest
  73. // add this feature back after improving the editor
  74. $scope.suggestDimensionValues = function(query, callback) {
  75. if (!$scope.target.namespace || !$scope.target.metricName) {
  76. return callback([]);
  77. }
  78. return $scope.datasource.getDimensionValues(
  79. $scope.target.region,
  80. $scope.target.namespace,
  81. $scope.target.metricName,
  82. $scope.target.dimensions
  83. ).then(function(result) {
  84. callback(result);
  85. }, function() {
  86. callback([]);
  87. });
  88. };
  89. $scope.addDimension = function() {
  90. if (!$scope.addDimensionMode) {
  91. $scope.addDimensionMode = true;
  92. return;
  93. }
  94. if (!$scope.target.dimensions) {
  95. $scope.target.dimensions = {};
  96. }
  97. $scope.target.dimensions[$scope.target.currentDimensionKey] = $scope.target.currentDimensionValue;
  98. $scope.target.escapedDimensions = this.escapeDimensions($scope.target.dimensions);
  99. $scope.target.currentDimensionKey = '';
  100. $scope.target.currentDimensionValue = '';
  101. $scope.refreshMetricData();
  102. $scope.addDimensionMode = false;
  103. };
  104. $scope.removeDimension = function(key) {
  105. key = key.replace(/\\\$/g, '$');
  106. delete $scope.target.dimensions[key];
  107. $scope.target.escapedDimensions = this.escapeDimensions($scope.target.dimensions);
  108. $scope.refreshMetricData();
  109. };
  110. $scope.escapeDimensions = function(d) {
  111. var result = {};
  112. _.chain(d)
  113. .keys(d)
  114. .each(function(k) {
  115. var v = d[k];
  116. result[k.replace(/\$/g, '\uFF04')] = v.replace(/\$/g, '\$');
  117. });
  118. return result;
  119. };
  120. $scope.statisticsOptionChanged = function() {
  121. $scope.refreshMetricData();
  122. };
  123. // TODO: validate target
  124. function validateTarget() {
  125. var errs = {};
  126. if ($scope.target.period < 60 || ($scope.target.period % 60) !== 0) {
  127. errs.period = 'Period must be at least 60 seconds and must be a multiple of 60';
  128. }
  129. return errs;
  130. }
  131. $scope.init();
  132. });
  133. });