Skip to content

Commit af8d121

Browse files
committed
feat(cloudwatch): code refactoring and cleanup, trying to rewrite so the code uses promises instead of ccallbacks, grafana#684
1 parent 205d423 commit af8d121

File tree

4 files changed

+40
-93
lines changed

4 files changed

+40
-93
lines changed

public/app/plugins/datasource/cloudwatch/datasource.js

Lines changed: 25 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ define([
22
'angular',
33
'lodash',
44
'moment',
5-
'./queryCtrl',
5+
'./query_ctrl',
66
'./directives',
77
],
88
function (angular, _) {
@@ -278,37 +278,28 @@ function (angular, _) {
278278
Period: query.period
279279
};
280280

281-
var d = $q.defer();
282-
cloudwatch.getMetricStatistics(params, function(err, data) {
283-
if (err) {
284-
return d.reject(err);
285-
}
286-
return d.resolve(data);
287-
});
288-
289-
return d.promise;
281+
return cloudwatch.getMetricStatistics(params);
290282
};
291283

292-
CloudWatchDatasource.prototype.performSuggestRegion = function() {
293-
return this.supportedRegion;
284+
CloudWatchDatasource.prototype.getRegions = function() {
285+
return $q.when(this.supportedRegion);
294286
};
295287

296-
CloudWatchDatasource.prototype.performSuggestNamespace = function() {
297-
console.log(this.supportMetrics);
298-
return _.keys(this.supportedMetrics);
288+
CloudWatchDatasource.prototype.getNamespaces = function() {
289+
return $q.when(_.keys(this.supportedMetrics));
299290
};
300291

301-
CloudWatchDatasource.prototype.performSuggestMetrics = function(namespace) {
292+
CloudWatchDatasource.prototype.getMetrics = function(namespace) {
302293
namespace = templateSrv.replace(namespace);
303-
return this.supportedMetrics[namespace] || [];
294+
return $q.when(this.supportedMetrics[namespace] || []);
304295
};
305296

306-
CloudWatchDatasource.prototype.performSuggestDimensionKeys = function(namespace) {
297+
CloudWatchDatasource.prototype.getDimensionKeys = function(namespace) {
307298
namespace = templateSrv.replace(namespace);
308-
return this.supportedDimensions[namespace] || [];
299+
return $q.when(this.supportedDimensions[namespace] || []);
309300
};
310301

311-
CloudWatchDatasource.prototype.performSuggestDimensionValues = function(region, namespace, metricName, dimensions) {
302+
CloudWatchDatasource.prototype.getDimensionValues = function(region, namespace, metricName, dimensions) {
312303
region = templateSrv.replace(region);
313304
namespace = templateSrv.replace(namespace);
314305
metricName = templateSrv.replace(metricName);
@@ -323,26 +314,15 @@ function (angular, _) {
323314
params.Dimensions = convertDimensionFormat(dimensions);
324315
}
325316

326-
var d = $q.defer();
327-
328-
cloudwatch.listMetrics(params, function(err, data) {
329-
if (err) {
330-
return d.reject(err);
331-
}
332-
333-
var suggestData = _.chain(data.Metrics)
334-
.map(function(metric) {
317+
return cloudwatch.listMetrics(params).then(function(data) {
318+
var suggestData = _.chain(data.Metrics).map(function(metric) {
335319
return metric.Dimensions;
336-
})
337-
.reject(function(metric) {
320+
}).reject(function(metric) {
338321
return _.isEmpty(metric);
339-
})
340-
.value();
322+
}).value();
341323

342-
return d.resolve(suggestData);
324+
return suggestData;
343325
});
344-
345-
return d.promise;
346326
};
347327

348328
CloudWatchDatasource.prototype.performEC2DescribeInstances = function(region, filters, instanceIds) {
@@ -356,26 +336,7 @@ function (angular, _) {
356336
params.InstanceIds = instanceIds;
357337
}
358338

359-
var d = $q.defer();
360-
361-
ec2.describeInstances(params, function(err, data) {
362-
if (err) {
363-
return d.reject(err);
364-
}
365-
366-
return d.resolve(data);
367-
});
368-
369-
return d.promise;
370-
};
371-
372-
CloudWatchDatasource.prototype.getTemplateVariableNames = function() {
373-
var variables = [];
374-
templateSrv.fillVariableValuesForUrl(variables);
375-
376-
return _.map(_.keys(variables), function(k) {
377-
return k.replace(/var-/, '$');
378-
});
339+
return ec2.describeInstances(params);
379340
};
380341

381342
CloudWatchDatasource.prototype.metricFindQuery = function(query) {
@@ -389,32 +350,26 @@ function (angular, _) {
389350
});
390351
};
391352

392-
var d = $q.defer();
393-
394353
var regionQuery = query.match(/^region\(\)/);
395354
if (regionQuery) {
396-
d.resolve(transformSuggestData(this.performSuggestRegion()));
397-
return d.promise;
355+
return this.getRegions().then(transformSuggestData);
398356
}
399357

400358
var namespaceQuery = query.match(/^namespace\(\)/);
401359
if (namespaceQuery) {
402-
d.resolve(transformSuggestData(this.performSuggestNamespace()));
403-
return d.promise;
360+
return this.getNamespaces().then(transformSuggestData);
404361
}
405362

406363
var metricNameQuery = query.match(/^metrics\(([^\)]+?)\)/);
407364
if (metricNameQuery) {
408365
namespace = templateSrv.replace(metricNameQuery[1]);
409-
d.resolve(transformSuggestData(this.performSuggestMetrics(namespace)));
410-
return d.promise;
366+
return this.getMetrics(namespace).then(transformSuggestData);
411367
}
412368

413369
var dimensionKeysQuery = query.match(/^dimension_keys\(([^\)]+?)\)/);
414370
if (dimensionKeysQuery) {
415371
namespace = templateSrv.replace(dimensionKeysQuery[1]);
416-
d.resolve(transformSuggestData(this.performSuggestDimensionKeys(namespace)));
417-
return d.promise;
372+
return this.getDimensionKeys(namespace).then(transformSuggestData);
418373
}
419374

420375
var dimensionValuesQuery = query.match(/^dimension_values\(([^,]+?),\s?([^,]+?),\s?([^,]+?)(,\s?([^)]*))?\)/);
@@ -435,7 +390,7 @@ function (angular, _) {
435390
});
436391
}
437392

438-
return this.performSuggestDimensionValues(region, namespace, metricName, dimensions)
393+
return this.getDimensionValues(region, namespace, metricName, dimensions)
439394
.then(function(suggestData) {
440395
return _.map(suggestData, function(dimensions) {
441396
var result = _.chain(dimensions)
@@ -460,8 +415,7 @@ function (angular, _) {
460415
instanceId
461416
];
462417

463-
return this.performEC2DescribeInstances(region, [], instanceIds)
464-
.then(function(result) {
418+
return this.performEC2DescribeInstances(region, [], instanceIds).then(function(result) {
465419
var volumeIds = _.map(result.Reservations[0].Instances[0].BlockDeviceMappings, function(mapping) {
466420
return mapping.EBS.VolumeID;
467421
});
@@ -488,7 +442,7 @@ function (angular, _) {
488442
CloudWatchDatasource.prototype.getAwsClient = function(region, service) {
489443
var self = this;
490444
var generateRequestProxy = function(service, action) {
491-
return function(params, callback) {
445+
return function(params) {
492446
var data = {
493447
region: region,
494448
service: service,
@@ -502,11 +456,7 @@ function (angular, _) {
502456
data: data
503457
};
504458

505-
$http(options).then(function(response) {
506-
callback(null, response.data);
507-
}, function(err) {
508-
callback(err, []);
509-
});
459+
return $http(options);
510460
};
511461
};
512462

public/app/plugins/datasource/cloudwatch/queryCtrl.js renamed to public/app/plugins/datasource/cloudwatch/query_ctrl.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,31 +81,30 @@ function (angular, _) {
8181
};
8282

8383
$scope.suggestDimensionKeys = function(query, callback) { // jshint unused:false
84-
return _.union($scope.datasource.performSuggestDimensionKeys($scope.target.namespace), $scope.datasource.getTemplateVariableNames());
84+
$scope.datasource.getDimensionKeys($scope.target.namespace).then(function(result) {
85+
callback(result);
86+
});
8587
};
8688

89+
// TODO: Removed template variables from the suggest
90+
// add this feature back after improving the editor
8791
$scope.suggestDimensionValues = function(query, callback) {
8892
if (!$scope.target.namespace || !$scope.target.metricName) {
8993
return callback([]);
9094
}
9195

92-
$scope.datasource.performSuggestDimensionValues(
96+
return $scope.datasource.getDimensionValues(
9397
$scope.target.region,
9498
$scope.target.namespace,
9599
$scope.target.metricName,
96100
$scope.target.dimensions
97-
)
98-
.then(function(result) {
101+
).then(function(result) {
99102
var suggestData = _.chain(result)
100103
.flatten(true)
101-
.filter(function(dimension) {
102-
return dimension.Name === templateSrv.replace($scope.target.currentDimensionKey);
103-
})
104104
.pluck('Value')
105105
.uniq()
106106
.value();
107107

108-
suggestData = _.union(suggestData, $scope.datasource.getTemplateVariableNames());
109108
callback(suggestData);
110109
}, function() {
111110
callback([]);

public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ describe('CloudWatchDatasource', function() {
5555
beforeEach(function() {
5656
ctx.ds.getAwsClient = function() {
5757
return {
58-
getMetricStatistics: function(params, callback) {
58+
getMetricStatistics: function(params) {
5959
requestParams = params;
60-
callback(null, response);
60+
return ctx.$q.when(response);
6161
}
6262
};
6363
};
@@ -89,7 +89,6 @@ describe('CloudWatchDatasource', function() {
8989

9090
describe('When performing CloudWatch metricFindQuery', function() {
9191
var requestParams;
92-
9392
var response = {
9493
Metrics: [
9594
{
@@ -108,9 +107,9 @@ describe('CloudWatchDatasource', function() {
108107
beforeEach(function() {
109108
ctx.ds.getAwsClient = function() {
110109
return {
111-
listMetrics: function(params, callback) {
110+
listMetrics: function(params) {
112111
requestParams = params;
113-
callback(null, response);
112+
return ctx.$q.when(response);
114113
}
115114
};
116115
};
@@ -119,8 +118,7 @@ describe('CloudWatchDatasource', function() {
119118
it('should return suggest list for region()', function(done) {
120119
var query = 'region()';
121120
ctx.ds.metricFindQuery(query).then(function(result) {
122-
result = result.map(function(v) { return v.text; });
123-
expect(result).to.contain('us-east-1');
121+
expect(result[0].text).to.contain('us-east-1');
124122
done();
125123
});
126124
ctx.$rootScope.$apply();

tasks/options/watch.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ module.exports = function(config) {
1313
copy_to_gen: {
1414
files: ['<%= srcDir %>/**/*'],
1515
tasks: [
16-
'jshint',
17-
'jscs',
18-
'tslint',
1916
'clean:gen',
2017
'copy:public_to_gen',
2118
'css',
2219
'typescript:build',
20+
'jshint',
21+
'jscs',
22+
'tslint',
2323
'karma:test'
2424
],
2525
options: {

0 commit comments

Comments
 (0)