mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(cloudwatch): code refactoring and cleanup, trying to rewrite so the code uses promises instead of ccallbacks, #684
This commit is contained in:
parent
205d4232b9
commit
af8d12124a
@ -2,7 +2,7 @@ define([
|
|||||||
'angular',
|
'angular',
|
||||||
'lodash',
|
'lodash',
|
||||||
'moment',
|
'moment',
|
||||||
'./queryCtrl',
|
'./query_ctrl',
|
||||||
'./directives',
|
'./directives',
|
||||||
],
|
],
|
||||||
function (angular, _) {
|
function (angular, _) {
|
||||||
@ -278,37 +278,28 @@ function (angular, _) {
|
|||||||
Period: query.period
|
Period: query.period
|
||||||
};
|
};
|
||||||
|
|
||||||
var d = $q.defer();
|
return cloudwatch.getMetricStatistics(params);
|
||||||
cloudwatch.getMetricStatistics(params, function(err, data) {
|
|
||||||
if (err) {
|
|
||||||
return d.reject(err);
|
|
||||||
}
|
|
||||||
return d.resolve(data);
|
|
||||||
});
|
|
||||||
|
|
||||||
return d.promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.performSuggestRegion = function() {
|
CloudWatchDatasource.prototype.getRegions = function() {
|
||||||
return this.supportedRegion;
|
return $q.when(this.supportedRegion);
|
||||||
};
|
};
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.performSuggestNamespace = function() {
|
CloudWatchDatasource.prototype.getNamespaces = function() {
|
||||||
console.log(this.supportMetrics);
|
return $q.when(_.keys(this.supportedMetrics));
|
||||||
return _.keys(this.supportedMetrics);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.performSuggestMetrics = function(namespace) {
|
CloudWatchDatasource.prototype.getMetrics = function(namespace) {
|
||||||
namespace = templateSrv.replace(namespace);
|
namespace = templateSrv.replace(namespace);
|
||||||
return this.supportedMetrics[namespace] || [];
|
return $q.when(this.supportedMetrics[namespace] || []);
|
||||||
};
|
};
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.performSuggestDimensionKeys = function(namespace) {
|
CloudWatchDatasource.prototype.getDimensionKeys = function(namespace) {
|
||||||
namespace = templateSrv.replace(namespace);
|
namespace = templateSrv.replace(namespace);
|
||||||
return this.supportedDimensions[namespace] || [];
|
return $q.when(this.supportedDimensions[namespace] || []);
|
||||||
};
|
};
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.performSuggestDimensionValues = function(region, namespace, metricName, dimensions) {
|
CloudWatchDatasource.prototype.getDimensionValues = function(region, namespace, metricName, dimensions) {
|
||||||
region = templateSrv.replace(region);
|
region = templateSrv.replace(region);
|
||||||
namespace = templateSrv.replace(namespace);
|
namespace = templateSrv.replace(namespace);
|
||||||
metricName = templateSrv.replace(metricName);
|
metricName = templateSrv.replace(metricName);
|
||||||
@ -323,26 +314,15 @@ function (angular, _) {
|
|||||||
params.Dimensions = convertDimensionFormat(dimensions);
|
params.Dimensions = convertDimensionFormat(dimensions);
|
||||||
}
|
}
|
||||||
|
|
||||||
var d = $q.defer();
|
return cloudwatch.listMetrics(params).then(function(data) {
|
||||||
|
var suggestData = _.chain(data.Metrics).map(function(metric) {
|
||||||
cloudwatch.listMetrics(params, function(err, data) {
|
|
||||||
if (err) {
|
|
||||||
return d.reject(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
var suggestData = _.chain(data.Metrics)
|
|
||||||
.map(function(metric) {
|
|
||||||
return metric.Dimensions;
|
return metric.Dimensions;
|
||||||
})
|
}).reject(function(metric) {
|
||||||
.reject(function(metric) {
|
|
||||||
return _.isEmpty(metric);
|
return _.isEmpty(metric);
|
||||||
})
|
}).value();
|
||||||
.value();
|
|
||||||
|
|
||||||
return d.resolve(suggestData);
|
return suggestData;
|
||||||
});
|
});
|
||||||
|
|
||||||
return d.promise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.performEC2DescribeInstances = function(region, filters, instanceIds) {
|
CloudWatchDatasource.prototype.performEC2DescribeInstances = function(region, filters, instanceIds) {
|
||||||
@ -356,26 +336,7 @@ function (angular, _) {
|
|||||||
params.InstanceIds = instanceIds;
|
params.InstanceIds = instanceIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
var d = $q.defer();
|
return ec2.describeInstances(params);
|
||||||
|
|
||||||
ec2.describeInstances(params, function(err, data) {
|
|
||||||
if (err) {
|
|
||||||
return d.reject(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
return d.resolve(data);
|
|
||||||
});
|
|
||||||
|
|
||||||
return d.promise;
|
|
||||||
};
|
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.getTemplateVariableNames = function() {
|
|
||||||
var variables = [];
|
|
||||||
templateSrv.fillVariableValuesForUrl(variables);
|
|
||||||
|
|
||||||
return _.map(_.keys(variables), function(k) {
|
|
||||||
return k.replace(/var-/, '$');
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.metricFindQuery = function(query) {
|
CloudWatchDatasource.prototype.metricFindQuery = function(query) {
|
||||||
@ -389,32 +350,26 @@ function (angular, _) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var d = $q.defer();
|
|
||||||
|
|
||||||
var regionQuery = query.match(/^region\(\)/);
|
var regionQuery = query.match(/^region\(\)/);
|
||||||
if (regionQuery) {
|
if (regionQuery) {
|
||||||
d.resolve(transformSuggestData(this.performSuggestRegion()));
|
return this.getRegions().then(transformSuggestData);
|
||||||
return d.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var namespaceQuery = query.match(/^namespace\(\)/);
|
var namespaceQuery = query.match(/^namespace\(\)/);
|
||||||
if (namespaceQuery) {
|
if (namespaceQuery) {
|
||||||
d.resolve(transformSuggestData(this.performSuggestNamespace()));
|
return this.getNamespaces().then(transformSuggestData);
|
||||||
return d.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var metricNameQuery = query.match(/^metrics\(([^\)]+?)\)/);
|
var metricNameQuery = query.match(/^metrics\(([^\)]+?)\)/);
|
||||||
if (metricNameQuery) {
|
if (metricNameQuery) {
|
||||||
namespace = templateSrv.replace(metricNameQuery[1]);
|
namespace = templateSrv.replace(metricNameQuery[1]);
|
||||||
d.resolve(transformSuggestData(this.performSuggestMetrics(namespace)));
|
return this.getMetrics(namespace).then(transformSuggestData);
|
||||||
return d.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var dimensionKeysQuery = query.match(/^dimension_keys\(([^\)]+?)\)/);
|
var dimensionKeysQuery = query.match(/^dimension_keys\(([^\)]+?)\)/);
|
||||||
if (dimensionKeysQuery) {
|
if (dimensionKeysQuery) {
|
||||||
namespace = templateSrv.replace(dimensionKeysQuery[1]);
|
namespace = templateSrv.replace(dimensionKeysQuery[1]);
|
||||||
d.resolve(transformSuggestData(this.performSuggestDimensionKeys(namespace)));
|
return this.getDimensionKeys(namespace).then(transformSuggestData);
|
||||||
return d.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var dimensionValuesQuery = query.match(/^dimension_values\(([^,]+?),\s?([^,]+?),\s?([^,]+?)(,\s?([^)]*))?\)/);
|
var dimensionValuesQuery = query.match(/^dimension_values\(([^,]+?),\s?([^,]+?),\s?([^,]+?)(,\s?([^)]*))?\)/);
|
||||||
@ -435,7 +390,7 @@ function (angular, _) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.performSuggestDimensionValues(region, namespace, metricName, dimensions)
|
return this.getDimensionValues(region, namespace, metricName, dimensions)
|
||||||
.then(function(suggestData) {
|
.then(function(suggestData) {
|
||||||
return _.map(suggestData, function(dimensions) {
|
return _.map(suggestData, function(dimensions) {
|
||||||
var result = _.chain(dimensions)
|
var result = _.chain(dimensions)
|
||||||
@ -460,8 +415,7 @@ function (angular, _) {
|
|||||||
instanceId
|
instanceId
|
||||||
];
|
];
|
||||||
|
|
||||||
return this.performEC2DescribeInstances(region, [], instanceIds)
|
return this.performEC2DescribeInstances(region, [], instanceIds).then(function(result) {
|
||||||
.then(function(result) {
|
|
||||||
var volumeIds = _.map(result.Reservations[0].Instances[0].BlockDeviceMappings, function(mapping) {
|
var volumeIds = _.map(result.Reservations[0].Instances[0].BlockDeviceMappings, function(mapping) {
|
||||||
return mapping.EBS.VolumeID;
|
return mapping.EBS.VolumeID;
|
||||||
});
|
});
|
||||||
@ -488,7 +442,7 @@ function (angular, _) {
|
|||||||
CloudWatchDatasource.prototype.getAwsClient = function(region, service) {
|
CloudWatchDatasource.prototype.getAwsClient = function(region, service) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var generateRequestProxy = function(service, action) {
|
var generateRequestProxy = function(service, action) {
|
||||||
return function(params, callback) {
|
return function(params) {
|
||||||
var data = {
|
var data = {
|
||||||
region: region,
|
region: region,
|
||||||
service: service,
|
service: service,
|
||||||
@ -502,11 +456,7 @@ function (angular, _) {
|
|||||||
data: data
|
data: data
|
||||||
};
|
};
|
||||||
|
|
||||||
$http(options).then(function(response) {
|
return $http(options);
|
||||||
callback(null, response.data);
|
|
||||||
}, function(err) {
|
|
||||||
callback(err, []);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -81,31 +81,30 @@ function (angular, _) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
$scope.suggestDimensionKeys = function(query, callback) { // jshint unused:false
|
$scope.suggestDimensionKeys = function(query, callback) { // jshint unused:false
|
||||||
return _.union($scope.datasource.performSuggestDimensionKeys($scope.target.namespace), $scope.datasource.getTemplateVariableNames());
|
$scope.datasource.getDimensionKeys($scope.target.namespace).then(function(result) {
|
||||||
|
callback(result);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO: Removed template variables from the suggest
|
||||||
|
// add this feature back after improving the editor
|
||||||
$scope.suggestDimensionValues = function(query, callback) {
|
$scope.suggestDimensionValues = function(query, callback) {
|
||||||
if (!$scope.target.namespace || !$scope.target.metricName) {
|
if (!$scope.target.namespace || !$scope.target.metricName) {
|
||||||
return callback([]);
|
return callback([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$scope.datasource.performSuggestDimensionValues(
|
return $scope.datasource.getDimensionValues(
|
||||||
$scope.target.region,
|
$scope.target.region,
|
||||||
$scope.target.namespace,
|
$scope.target.namespace,
|
||||||
$scope.target.metricName,
|
$scope.target.metricName,
|
||||||
$scope.target.dimensions
|
$scope.target.dimensions
|
||||||
)
|
).then(function(result) {
|
||||||
.then(function(result) {
|
|
||||||
var suggestData = _.chain(result)
|
var suggestData = _.chain(result)
|
||||||
.flatten(true)
|
.flatten(true)
|
||||||
.filter(function(dimension) {
|
|
||||||
return dimension.Name === templateSrv.replace($scope.target.currentDimensionKey);
|
|
||||||
})
|
|
||||||
.pluck('Value')
|
.pluck('Value')
|
||||||
.uniq()
|
.uniq()
|
||||||
.value();
|
.value();
|
||||||
|
|
||||||
suggestData = _.union(suggestData, $scope.datasource.getTemplateVariableNames());
|
|
||||||
callback(suggestData);
|
callback(suggestData);
|
||||||
}, function() {
|
}, function() {
|
||||||
callback([]);
|
callback([]);
|
@ -55,9 +55,9 @@ describe('CloudWatchDatasource', function() {
|
|||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
ctx.ds.getAwsClient = function() {
|
ctx.ds.getAwsClient = function() {
|
||||||
return {
|
return {
|
||||||
getMetricStatistics: function(params, callback) {
|
getMetricStatistics: function(params) {
|
||||||
requestParams = params;
|
requestParams = params;
|
||||||
callback(null, response);
|
return ctx.$q.when(response);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -89,7 +89,6 @@ describe('CloudWatchDatasource', function() {
|
|||||||
|
|
||||||
describe('When performing CloudWatch metricFindQuery', function() {
|
describe('When performing CloudWatch metricFindQuery', function() {
|
||||||
var requestParams;
|
var requestParams;
|
||||||
|
|
||||||
var response = {
|
var response = {
|
||||||
Metrics: [
|
Metrics: [
|
||||||
{
|
{
|
||||||
@ -108,9 +107,9 @@ describe('CloudWatchDatasource', function() {
|
|||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
ctx.ds.getAwsClient = function() {
|
ctx.ds.getAwsClient = function() {
|
||||||
return {
|
return {
|
||||||
listMetrics: function(params, callback) {
|
listMetrics: function(params) {
|
||||||
requestParams = params;
|
requestParams = params;
|
||||||
callback(null, response);
|
return ctx.$q.when(response);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -119,8 +118,7 @@ describe('CloudWatchDatasource', function() {
|
|||||||
it('should return suggest list for region()', function(done) {
|
it('should return suggest list for region()', function(done) {
|
||||||
var query = 'region()';
|
var query = 'region()';
|
||||||
ctx.ds.metricFindQuery(query).then(function(result) {
|
ctx.ds.metricFindQuery(query).then(function(result) {
|
||||||
result = result.map(function(v) { return v.text; });
|
expect(result[0].text).to.contain('us-east-1');
|
||||||
expect(result).to.contain('us-east-1');
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
ctx.$rootScope.$apply();
|
ctx.$rootScope.$apply();
|
||||||
|
@ -13,13 +13,13 @@ module.exports = function(config) {
|
|||||||
copy_to_gen: {
|
copy_to_gen: {
|
||||||
files: ['<%= srcDir %>/**/*'],
|
files: ['<%= srcDir %>/**/*'],
|
||||||
tasks: [
|
tasks: [
|
||||||
'jshint',
|
|
||||||
'jscs',
|
|
||||||
'tslint',
|
|
||||||
'clean:gen',
|
'clean:gen',
|
||||||
'copy:public_to_gen',
|
'copy:public_to_gen',
|
||||||
'css',
|
'css',
|
||||||
'typescript:build',
|
'typescript:build',
|
||||||
|
'jshint',
|
||||||
|
'jscs',
|
||||||
|
'tslint',
|
||||||
'karma:test'
|
'karma:test'
|
||||||
],
|
],
|
||||||
options: {
|
options: {
|
||||||
|
Loading…
Reference in New Issue
Block a user