Commit fd96e30c by Mitsuhiro Tanda

add region field in query editor

parent 6a697eed
...@@ -19,11 +19,11 @@ function (angular, _, kbn) { ...@@ -19,11 +19,11 @@ function (angular, _, kbn) {
this.name = datasource.name; this.name = datasource.name;
this.supportMetrics = true; this.supportMetrics = true;
AWS.config.update({ region: datasource.jsonData.region }); this.defaultRegion = datasource.jsonData.defaultRegion;
this.cloudwatch = new AWS.CloudWatch({ this.credentials = {
accessKeyId: datasource.jsonData.accessKeyId, accessKeyId: datasource.jsonData.accessKeyId,
secretAccessKey: datasource.jsonData.secretAccessKey, secretAccessKey: datasource.jsonData.secretAccessKey
}); };
} }
// Called once per panel (graph) // Called once per panel (graph)
...@@ -38,6 +38,7 @@ function (angular, _, kbn) { ...@@ -38,6 +38,7 @@ function (angular, _, kbn) {
} }
var query = {}; var query = {};
query.region = templateSrv.replace(target.region, options.scopedVars);
query.namespace = templateSrv.replace(target.namespace, options.scopedVars); query.namespace = templateSrv.replace(target.namespace, options.scopedVars);
query.metricName = templateSrv.replace(target.metricName, options.scopedVars); query.metricName = templateSrv.replace(target.metricName, options.scopedVars);
query.dimensions = _.map(_.keys(target.dimensions), function(key) { query.dimensions = _.map(_.keys(target.dimensions), function(key) {
...@@ -85,6 +86,8 @@ function (angular, _, kbn) { ...@@ -85,6 +86,8 @@ function (angular, _, kbn) {
}; };
CloudWatchDatasource.prototype.performTimeSeriesQuery = function(query, start, end) { CloudWatchDatasource.prototype.performTimeSeriesQuery = function(query, start, end) {
var cloudwatch = this.getCloudWatchClient(query.region);
var params = { var params = {
Namespace: query.namespace, Namespace: query.namespace,
MetricName: query.metricName, MetricName: query.metricName,
...@@ -96,7 +99,7 @@ function (angular, _, kbn) { ...@@ -96,7 +99,7 @@ function (angular, _, kbn) {
}; };
var d = $q.defer(); var d = $q.defer();
this.cloudwatch.getMetricStatistics(params, function(err, data) { cloudwatch.getMetricStatistics(params, function(err, data) {
if (err) { if (err) {
return d.reject(err); return d.reject(err);
} }
...@@ -106,10 +109,12 @@ function (angular, _, kbn) { ...@@ -106,10 +109,12 @@ function (angular, _, kbn) {
return d.promise; return d.promise;
}; };
CloudWatchDatasource.prototype.performSuggestQuery = function(params) { CloudWatchDatasource.prototype.performSuggestQuery = function(region, params) {
var cloudwatch = this.getCloudWatchClient(region);
var d = $q.defer(); var d = $q.defer();
this.cloudwatch.listMetrics(params, function(err, data) { cloudwatch.listMetrics(params, function(err, data) {
if (err) { if (err) {
return d.reject(err); return d.reject(err);
} }
...@@ -126,6 +131,18 @@ function (angular, _, kbn) { ...@@ -126,6 +131,18 @@ function (angular, _, kbn) {
}); });
}; };
CloudWatchDatasource.prototype.getCloudWatchClient = function(region) {
return new AWS.CloudWatch({
region: region,
accessKeyId: this.credentials.accessKeyId,
secretAccessKey: this.credentials.secretAccessKey
});
};
CloudWatchDatasource.prototype.getDefaultRegion = function() {
return this.defaultRegion;
};
function transformMetricData(md, options) { function transformMetricData(md, options) {
var result = []; var result = [];
......
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
<div class="tight-form"> <div class="tight-form">
<ul class="tight-form-list"> <ul class="tight-form-list">
<li class="tight-form-item" style="width: 80px"> <li class="tight-form-item" style="width: 80px">
Region Default Region
</li> </li>
<li> <li>
<input type="text" class="tight-form-input input-large" ng-model='current.jsonData.region' placeholder="" required></input> <input type="text" class="tight-form-input input-large" ng-model='current.jsonData.defaultRegion' placeholder="" required></input>
</li> </li>
</ul> </ul>
<div class="clearfix"></div> <div class="clearfix"></div>
......
...@@ -203,6 +203,20 @@ ...@@ -203,6 +203,20 @@
<i class="fa fa-warning"></i> <i class="fa fa-warning"></i>
</a> </a>
</li> </li>
<li class="tight-form-item">
Region
</li>
<li>
<input type="text"
class="input-medium tight-form-input"
ng-model="target.region"
spellcheck='false'
bs-typeahead="suggestRegion"
placeholder="region"
data-min-length=0 data-items=100
ng-change="refreshMetricData()"
>
</li>
</ul> </ul>
<div class="clearfix"></div> <div class="clearfix"></div>
......
...@@ -8,6 +8,17 @@ function (angular, _) { ...@@ -8,6 +8,17 @@ function (angular, _) {
var module = angular.module('grafana.controllers'); var module = angular.module('grafana.controllers');
/* jshint -W101 */ /* jshint -W101 */
var supportedRegion = [
'us-east-1',
'us-west-2',
'us-west-1',
'eu-west-1',
'eu-central-1',
'ap-southeast-1',
'ap-southeast-2',
'ap-northeast-1',
'sa-east-1',
];
var supportedMetrics = { var supportedMetrics = {
"AWS/AutoScaling": [ "AWS/AutoScaling": [
"GroupMinSize", "GroupMaxSize", "GroupDesiredCapacity", "GroupInServiceInstances", "GroupPendingInstances", "GroupStandbyInstances", "GroupTerminatingInstances", "GroupTotalInstances" "GroupMinSize", "GroupMaxSize", "GroupDesiredCapacity", "GroupInServiceInstances", "GroupPendingInstances", "GroupStandbyInstances", "GroupTerminatingInstances", "GroupTotalInstances"
...@@ -157,6 +168,7 @@ function (angular, _) { ...@@ -157,6 +168,7 @@ function (angular, _) {
$scope.target.dimensions = $scope.target.dimensions || {}; $scope.target.dimensions = $scope.target.dimensions || {};
$scope.target.statistics = $scope.target.statistics || {}; $scope.target.statistics = $scope.target.statistics || {};
$scope.target.period = $scope.target.period || 60; $scope.target.period = $scope.target.period || 60;
$scope.target.region = $scope.target.region || $scope.datasource.getDefaultRegion();
$scope.target.errors = validateTarget(); $scope.target.errors = validateTarget();
}; };
...@@ -180,6 +192,10 @@ function (angular, _) { ...@@ -180,6 +192,10 @@ function (angular, _) {
$scope.panel.targets.push(clone); $scope.panel.targets.push(clone);
}; };
$scope.suggestRegion = function(query, callback) { // jshint unused:false
return supportedRegion;
};
$scope.suggestNamespace = function(query, callback) { // jshint unused:false $scope.suggestNamespace = function(query, callback) { // jshint unused:false
return _.keys(supportedMetrics); return _.keys(supportedMetrics);
}; };
...@@ -206,7 +222,7 @@ function (angular, _) { ...@@ -206,7 +222,7 @@ function (angular, _) {
} }
$scope.datasource $scope.datasource
.performSuggestQuery(params) .performSuggestQuery($scope.target.region, params)
.then(function(result) { .then(function(result) {
var suggestData = _.chain(result.Metrics) var suggestData = _.chain(result.Metrics)
.map(function(metric) { .map(function(metric) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment