Skip to content

Commit fa359e8

Browse files
committed
Add unassigned Instances for Cost Aggregation
1 parent 3632a68 commit fa359e8

File tree

2 files changed

+81
-52
lines changed

2 files changed

+81
-52
lines changed

server/app/cronjobs/aws-cost-aggregation/AWSCostAggregation.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ function downloadUpdatedCSVFile(provider, next) {
249249

250250
function instanceIdList(instances,callback){
251251
var instanceIds=[];
252-
var length = instances.managed.length + instances.unmanaged.length;
253-
if(instances.managed.length === 0 && instances.unmanaged.length === 0){
252+
var length = instances.managed.length + instances.unmanaged.length + instances.unassigned.length;
253+
if(instances.managed.length === 0 && instances.unmanaged.length === 0 && instances.unassigned.length === 0){
254254
callback(null,instanceIds);
255255
}else{
256256
if(instances.managed.length > 0){
@@ -259,8 +259,13 @@ function instanceIdList(instances,callback){
259259
}
260260
}
261261
if(instances.unmanaged.length > 0){
262-
for(var i = 0; i < instances.unmanaged.length; i++){
263-
instanceIds.push(instances.unmanaged[i].platformId);
262+
for(var j = 0; j < instances.unmanaged.length; j++){
263+
instanceIds.push(instances.unmanaged[j].platformId);
264+
}
265+
}
266+
if(instances.unassigned.length > 0){
267+
for(var k = 0; k < instances.unassigned.length; k++){
268+
instanceIds.push(instances.unassigned[k].platformId);
264269
}
265270
}
266271
if(instanceIds.length === length){

server/app/services/resourceService.js

Lines changed: 72 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -381,58 +381,82 @@ function getCostForServices(provider,callback) {
381381
/*This the Dimension that is required to passed for different services*/
382382
var ec2Dim = [ { Name: 'ServiceName',Value: 'AmazonEC2'},{ Name: 'Currency', Value: 'USD'} ];
383383
var rdsDim = [ { Name: 'ServiceName',Value: 'AmazonRDS'},{ Name: 'Currency', Value: 'USD'} ];
384-
var ec2Cost = 0, rdsCost = 0;
385384
/*Getting the cost of EC2 & RDS for the current day*/
386-
cw.getTotalCost(startDate,endDate,'Maximum',ec2Dim,function(err,presentCost)
387-
{
388-
if(err){
389-
callback(err,null);
390-
}
391-
cw.getTotalCost(startDateOne,endDate,'Minimum',ec2Dim,function(err,yesterdayCost)
392-
{
393-
if(err){
394-
callback(err,null);
395-
}
396-
ec2Cost = presentCost['Maximum'] - yesterdayCost['Minimum'];
397-
});
398-
cw.getTotalCost(startDate,endDate,'Maximum',rdsDim,function(err,presentRdsCost)
399-
{
400-
if(err){
401-
callback(err,null);
402-
}
403-
cw.getTotalCost(startDateOne,endDate,'Minimum',rdsDim,function(err,yesterdayRdsCost)
404-
{
405-
if(err){
406-
callback(err,null);
385+
async.parallel({
386+
ec2Cost:function(callback){
387+
var ec2Cost = 0;
388+
cw.getTotalCost(startDate,endDate,'Maximum',ec2Dim,function(err,presentEC2Cost) {
389+
if (err) {
390+
callback(err, null);
407391
}
408-
rdsCost = presentRdsCost['Maximum'] - yesterdayRdsCost['Minimum'];
409-
var awsResourceCostObject = {
410-
organisationId: provider.orgId,
411-
providerId: provider._id,
412-
providerType: provider.providerType,
413-
providerName: provider.providerName,
414-
resourceType: "serviceCost",
415-
resourceId: "serviceCost",
416-
aggregateResourceCost:ec2Cost + rdsCost,
417-
costMetrics : {
418-
ec2Cost:ec2Cost,
419-
rdsCost:rdsCost,
420-
currency:'USD',
421-
symbol:"$"
422-
},
423-
updatedTime : Date.parse(endDate),
424-
startTime: Date.parse(endDate),
425-
endTime: Date.parse(startDateOne)
426-
};
427-
resourceCost.saveResourceCost(awsResourceCostObject,function(err,resourceCostData){
428-
if(err){
429-
callback(err,null);
430-
} else{
431-
callback(null,resourceCostData);
392+
cw.getTotalCost(startDateOne, endDate, 'Minimum', ec2Dim, function (err, yesterdayEC2Cost) {
393+
if (err) {
394+
callback(err, null);
395+
}else if (typeof presentEC2Cost === "undefined" && typeof yesterdayEC2Cost === "undefined"){
396+
callback(null,ec2Cost);
397+
}else if(presentEC2Cost.Maximum && yesterdayEC2Cost.Minimum) {
398+
ec2Cost = presentEC2Cost['Maximum'] - yesterdayEC2Cost['Minimum'];
399+
callback(null, ec2Cost);
400+
}else {
401+
callback(null, ec2Cost);
432402
}
433-
})
403+
});
434404
});
435-
});
405+
},
406+
rdsCost:function(callback){
407+
var rdsCost = 0;
408+
cw.getTotalCost(startDate,endDate,'Maximum',rdsDim,function(err,presentRDSCost) {
409+
if (err) {
410+
callback(err, null);
411+
}
412+
cw.getTotalCost(startDateOne, endDate, 'Minimum', rdsDim, function (err, yesterdayRDSCost) {
413+
if (err) {
414+
callback(err, null);
415+
}else if (typeof presentRDSCost === "undefined" && typeof yesterdayRDSCost === "undefined"){
416+
callback(null,rdsCost);
417+
}else if(presentRDSCost.Maximum && yesterdayRDSCost.Minimum) {
418+
rdsCost = presentRDSCost['Maximum'] - yesterdayRDSCost['Minimum'];
419+
callback(null, rdsCost);
420+
}else {
421+
callback(null, rdsCost);
422+
}
423+
});
424+
});
425+
}
426+
427+
},function(err,results){
428+
if(err){
429+
callback(err,null);
430+
return;
431+
}else {
432+
var awsResourceCostObject = {
433+
organisationId: provider.orgId,
434+
providerId: provider._id,
435+
providerType: provider.providerType,
436+
providerName: provider.providerName,
437+
resourceType: "serviceCost",
438+
resourceId: "serviceCost",
439+
aggregateResourceCost: results.ec2Cost + results.rdsCost,
440+
costMetrics: {
441+
ec2Cost: results.ec2Cost,
442+
rdsCost: results.rdsCost,
443+
currency: 'USD',
444+
symbol: "$"
445+
},
446+
updatedTime: Date.parse(endDate),
447+
startTime: Date.parse(endDate),
448+
endTime: Date.parse(startDateOne)
449+
};
450+
resourceCost.saveResourceCost(awsResourceCostObject, function (err, resourceCostData) {
451+
if (err) {
452+
callback(err, null);
453+
return;
454+
} else {
455+
callback(null, resourceCostData);
456+
return;
457+
}
458+
})
459+
}
436460
});
437461
}
438462

0 commit comments

Comments
 (0)