From 6b9bcfc9b546ef52a10bbad08fda95949e33da40 Mon Sep 17 00:00:00 2001 From: Karthik K N Date: Thu, 1 Dec 2016 14:28:06 +0530 Subject: [PATCH 01/14] Fix for CAT-2445 Incorrect Total Cost is showing in Analytics tab --- .../model/resource-costs/resource-costs.js | 43 +++-- server/app/services/resourceService.js | 161 ++++++++++-------- 2 files changed, 124 insertions(+), 80 deletions(-) diff --git a/server/app/model/resource-costs/resource-costs.js b/server/app/model/resource-costs/resource-costs.js index 012c774f6..28a47e042 100644 --- a/server/app/model/resource-costs/resource-costs.js +++ b/server/app/model/resource-costs/resource-costs.js @@ -70,6 +70,11 @@ var ResourceCostsSchema = new Schema({ required: false, trim: true }, + billIntervalId: { + type: String, + required: true, + trim: true + }, billLineItemId: { type: Number, required: true, @@ -132,38 +137,56 @@ var ResourceCostsSchema = new Schema({ }) ResourceCostsSchema.index({'platformDetails.serviceId' : 1}) -ResourceCostsSchema.index({'organizationId': 1, 'providerId': 1, 'billLineItemId': 1, - 'startTime': 1, 'interval': 1}, {'unique': true}) +ResourceCostsSchema.index({'organizationId': 1, 'providerId': 1, 'startTime': 1, + 'billIntervalId': 1, 'billLineItemId': 1, 'interval': 1}, {'unique': true}) -ResourceCostsSchema.statics.saveResourceCost = function saveResourceCost(resourceCostData, callback) { +ResourceCostsSchema.statics.save = function save(resourceCostData, callback) { var resourceCosts = new ResourceCosts(resourceCostData) - resourceCosts.save(function(err, data) { + resourceCosts.save(function(err, result) { + if (err) { + callback(err) + } else { + callback(null, result) + } + }) +} + +ResourceCostsSchema.statics.remove + = function remove(organizationId, providerId, billIntervalId, callback) { + var query = { + organizationId: organizationId, + providerId: providerId, + billIntervalId: billIntervalId + } + + this.find(query).remove(function(err, result) { if (err) { callback(err) } else { - callback(null) + callback(null, result) } }) } -ResourceCostsSchema.statics.upsertResourceCost = function upsertResourceCost(resourceCostData, callback) { +ResourceCostsSchema.statics.upsert = function upsert(resourceCostData, callback) { var query = { organizationId: resourceCostData.organizationId, providerId: resourceCostData.providerId, - billLineItemId: resourceCostData.billLineItemId, startTime: resourceCostData.startTime, + billIntervalId: resourceCostData.billIntervalId, + billLineItemId: resourceCostData.billLineItemId, interval: resourceCostData.interval } - this.findOneAndUpdate(query, resourceCostData, {upsert:true}, + this.findOneAndUpdate(query, resourceCostData, {upsert: true}, function(err, result){ if (err) { callback(null) } else { callback(null, result) } - }); + }) } var ResourceCosts = mongoose.model('ResourceCosts', ResourceCostsSchema) -module.exports = ResourceCosts +module.exports = ResourceCosts \ No newline at end of file diff --git a/server/app/services/resourceService.js b/server/app/services/resourceService.js index f56a7f95c..deb4f322d 100644 --- a/server/app/services/resourceService.js +++ b/server/app/services/resourceService.js @@ -91,91 +91,112 @@ function updateAWSResourceCostsFromCSV(provider, resources, downlaodedCSVPath, u var awsZones = appConfig.aws.zones var lineNumber = 0 - var stream = fs.createReadStream(downlaodedCSVPath) - csv.fromStream(stream, {headers: false}).on('data', function(data) { - if(data[awsBillIndexes.totalCost] == 'LineItem') { - var resourceCostEntry = {platformDetails: {}} - - resourceCostEntry.organizationId = provider.orgId - resourceCostEntry.providerId = provider._id - resourceCostEntry.providerType = provider.providerType - resourceCostEntry.cost = data[awsBillIndexes.cost] - resourceCostEntry.startTime = Date.parse(data[awsBillIndexes.startDate]) - resourceCostEntry.endTime = Date.parse(data[awsBillIndexes.endDate]) - resourceCostEntry.lastUpdateTime = Date.parse(updateTime) - resourceCostEntry.interval = 3600 - resourceCostEntry.platformDetails.serviceName = data[awsBillIndexes.prod] - resourceCostEntry.billLineItemId = ++lineNumber - resourceCostEntry.platformDetails.billRecordId = data[awsBillIndexes.recordId] - - resourceCostEntry.platformDetails.serviceId - = (data[awsBillIndexes.prod] in awsServices)?awsServices[data[awsBillIndexes.prod]] - :resourceCostEntry.platformDetails.serviceId = 'Other' - - resourceCostEntry.platformDetails.zone = (data[awsBillIndexes.zone] == null) - ? 'Global' : data[awsBillIndexes.zone] - - resourceCostEntry.platformDetails.region = (data[awsBillIndexes.zone] in awsZones) - ? awsZones[data[awsBillIndexes.zone]] : 'Global' - - if (data[awsBillIndexes.instanceId] != null) { - resourceCostEntry.platformDetails.instanceId = data[awsBillIndexes.instanceId] - } + var date = new Date() + var billIntervalId = date.getFullYear() + '-' + date.getMonth() - if(data[awsBillIndexes.usageType] != null) { - resourceCostEntry.platformDetails.usageType = data[awsBillIndexes.usageType] - } + async.waterfall([ + function(next) { + resourceCost.remove(provider.orgId, provider._id, billIntervalId, next) + }, + function(count, next) { + /*var lineNumber = (count == 0)?0:count + var startingLineNumber = (count == 0)?1:(count+2)*/ - if (data[awsBillIndexes.instanceId] in resources) { - var resource = resources[data[awsBillIndexes.instanceId]] + var stream = fs.createReadStream(downlaodedCSVPath) + csv.fromStream(stream).on('data', function(data) { + if(data[awsBillIndexes.totalCost] == 'LineItem') { + var resourceCostEntry = {platformDetails: {}} - resourceCostEntry.resourceId = resource._id + resourceCostEntry.organizationId = provider.orgId + resourceCostEntry.providerId = provider._id + resourceCostEntry.providerType = provider.providerType + resourceCostEntry.cost = data[awsBillIndexes.cost] + resourceCostEntry.startTime = Date.parse(data[awsBillIndexes.startDate]) + resourceCostEntry.endTime = Date.parse(data[awsBillIndexes.endDate]) + resourceCostEntry.lastUpdateTime = Date.parse(updateTime) + resourceCostEntry.interval = 3600 + resourceCostEntry.platformDetails.serviceName = data[awsBillIndexes.prod] + resourceCostEntry.billIntervalId = billIntervalId + resourceCostEntry.billLineItemId = ++lineNumber + resourceCostEntry.platformDetails.billRecordId = data[awsBillIndexes.recordId] - if (('bgId' in resource) && (resource.bgId != null)) { - resourceCostEntry.businessGroupId = resource['bgId'] - } + resourceCostEntry.platformDetails.serviceId + = (data[awsBillIndexes.prod] in awsServices)?awsServices[data[awsBillIndexes.prod]] + :resourceCostEntry.platformDetails.serviceId = 'Other' - if (('projectId' in resource) && (resource.projectId != null)) { - resourceCostEntry.projectId = resource['projectId'] - } + resourceCostEntry.platformDetails.zone = (data[awsBillIndexes.zone] == null) + ? 'Global' : data[awsBillIndexes.zone] - if (('environmentId' in resource) && (resource.environmentId != null)) { - resourceCostEntry.environmentId = resource['environmentId'] - } + resourceCostEntry.platformDetails.region = (data[awsBillIndexes.zone] in awsZones) + ? awsZones[data[awsBillIndexes.zone]] : 'Global' - if (('masterDetails.bgId' in resource) && (resource.masterDetails.bgId != null)) { - console.log("BG: " + resource['bgId']) - resourceCostEntry.businessGroupId = resource['bgId'] - } + if (data[awsBillIndexes.instanceId] != null) { + resourceCostEntry.platformDetails.instanceId = data[awsBillIndexes.instanceId] + } - if (('masterDetails.projectId' in resource) - && (resource.masterDetails.projectId != null)) { - resourceCostEntry.projectId = resource['projectId'] - } + if(data[awsBillIndexes.usageType] != null) { + resourceCostEntry.platformDetails.usageType = data[awsBillIndexes.usageType] + } - if (('masterDetails.environmentId' in resource) - && (resource.masterDetails.environmentId != null)) { - resourceCostEntry.environmentId = resource['environmentId'] - } - } + if (data[awsBillIndexes.instanceId] in resources) { + var resource = resources[data[awsBillIndexes.instanceId]] - resourceCostEntry.businessGroupId - = ('businessGroupId' in resourceCostEntry)?resourceCostEntry.businessGroupId:'Unassigned' - resourceCostEntry.projectId - = ('projectId' in resourceCostEntry)?resourceCostEntry.projectId:'Unassigned' - resourceCostEntry.environmentId - = ('environmentId' in resourceCostEntry)?resourceCostEntry.environmentId:'Unassigned' + resourceCostEntry.resourceId = resource._id - resourceCost.upsertResourceCost(resourceCostEntry, function (err, costEntry) { - if (err) { - logger.error(err) - return callback(new Error('Database Error')) + if (('bgId' in resource) && (resource.bgId != null)) { + resourceCostEntry.businessGroupId = resource['bgId'] + } + + if (('projectId' in resource) && (resource.projectId != null)) { + resourceCostEntry.projectId = resource['projectId'] + } + + if (('environmentId' in resource) && (resource.environmentId != null)) { + resourceCostEntry.environmentId = resource['environmentId'] + } + + if (('masterDetails.bgId' in resource) && (resource.masterDetails.bgId != null)) { + console.log("BG: " + resource['bgId']) + resourceCostEntry.businessGroupId = resource['bgId'] + } + + if (('masterDetails.projectId' in resource) + && (resource.masterDetails.projectId != null)) { + resourceCostEntry.projectId = resource['projectId'] + } + + if (('masterDetails.environmentId' in resource) + && (resource.masterDetails.environmentId != null)) { + resourceCostEntry.environmentId = resource['environmentId'] + } + } + + resourceCostEntry.businessGroupId + = ('businessGroupId' in resourceCostEntry)?resourceCostEntry.businessGroupId:'Unassigned' + resourceCostEntry.projectId + = ('projectId' in resourceCostEntry)?resourceCostEntry.projectId:'Unassigned' + resourceCostEntry.environmentId + = ('environmentId' in resourceCostEntry)?resourceCostEntry.environmentId:'Unassigned' + + resourceCost.save(resourceCostEntry, function (err, costEntry) { + if (err) { + logger.error(err) + return next(new Error('Database Error')) + } + }) } + }).on('end', function() { + next() }) } - }).on('end', function() { - callback(null) + ], function(err) { + if(err) { + callback(err) + } else { + callback() + } }) + } function getCostForServices_deprecated(provider,callback) { From 1b2fcfc0845b75d35b3608fe869f701328ff499a Mon Sep 17 00:00:00 2001 From: Karthik K N Date: Thu, 1 Dec 2016 14:28:06 +0530 Subject: [PATCH 02/14] Fix for CAT-2445 Incorrect Total Cost is showing in Analytics tab --- .../model/resource-costs/resource-costs.js | 43 +++-- server/app/services/resourceService.js | 161 ++++++++++-------- 2 files changed, 124 insertions(+), 80 deletions(-) diff --git a/server/app/model/resource-costs/resource-costs.js b/server/app/model/resource-costs/resource-costs.js index 012c774f6..28a47e042 100644 --- a/server/app/model/resource-costs/resource-costs.js +++ b/server/app/model/resource-costs/resource-costs.js @@ -70,6 +70,11 @@ var ResourceCostsSchema = new Schema({ required: false, trim: true }, + billIntervalId: { + type: String, + required: true, + trim: true + }, billLineItemId: { type: Number, required: true, @@ -132,38 +137,56 @@ var ResourceCostsSchema = new Schema({ }) ResourceCostsSchema.index({'platformDetails.serviceId' : 1}) -ResourceCostsSchema.index({'organizationId': 1, 'providerId': 1, 'billLineItemId': 1, - 'startTime': 1, 'interval': 1}, {'unique': true}) +ResourceCostsSchema.index({'organizationId': 1, 'providerId': 1, 'startTime': 1, + 'billIntervalId': 1, 'billLineItemId': 1, 'interval': 1}, {'unique': true}) -ResourceCostsSchema.statics.saveResourceCost = function saveResourceCost(resourceCostData, callback) { +ResourceCostsSchema.statics.save = function save(resourceCostData, callback) { var resourceCosts = new ResourceCosts(resourceCostData) - resourceCosts.save(function(err, data) { + resourceCosts.save(function(err, result) { + if (err) { + callback(err) + } else { + callback(null, result) + } + }) +} + +ResourceCostsSchema.statics.remove + = function remove(organizationId, providerId, billIntervalId, callback) { + var query = { + organizationId: organizationId, + providerId: providerId, + billIntervalId: billIntervalId + } + + this.find(query).remove(function(err, result) { if (err) { callback(err) } else { - callback(null) + callback(null, result) } }) } -ResourceCostsSchema.statics.upsertResourceCost = function upsertResourceCost(resourceCostData, callback) { +ResourceCostsSchema.statics.upsert = function upsert(resourceCostData, callback) { var query = { organizationId: resourceCostData.organizationId, providerId: resourceCostData.providerId, - billLineItemId: resourceCostData.billLineItemId, startTime: resourceCostData.startTime, + billIntervalId: resourceCostData.billIntervalId, + billLineItemId: resourceCostData.billLineItemId, interval: resourceCostData.interval } - this.findOneAndUpdate(query, resourceCostData, {upsert:true}, + this.findOneAndUpdate(query, resourceCostData, {upsert: true}, function(err, result){ if (err) { callback(null) } else { callback(null, result) } - }); + }) } var ResourceCosts = mongoose.model('ResourceCosts', ResourceCostsSchema) -module.exports = ResourceCosts +module.exports = ResourceCosts \ No newline at end of file diff --git a/server/app/services/resourceService.js b/server/app/services/resourceService.js index f56a7f95c..deb4f322d 100644 --- a/server/app/services/resourceService.js +++ b/server/app/services/resourceService.js @@ -91,91 +91,112 @@ function updateAWSResourceCostsFromCSV(provider, resources, downlaodedCSVPath, u var awsZones = appConfig.aws.zones var lineNumber = 0 - var stream = fs.createReadStream(downlaodedCSVPath) - csv.fromStream(stream, {headers: false}).on('data', function(data) { - if(data[awsBillIndexes.totalCost] == 'LineItem') { - var resourceCostEntry = {platformDetails: {}} - - resourceCostEntry.organizationId = provider.orgId - resourceCostEntry.providerId = provider._id - resourceCostEntry.providerType = provider.providerType - resourceCostEntry.cost = data[awsBillIndexes.cost] - resourceCostEntry.startTime = Date.parse(data[awsBillIndexes.startDate]) - resourceCostEntry.endTime = Date.parse(data[awsBillIndexes.endDate]) - resourceCostEntry.lastUpdateTime = Date.parse(updateTime) - resourceCostEntry.interval = 3600 - resourceCostEntry.platformDetails.serviceName = data[awsBillIndexes.prod] - resourceCostEntry.billLineItemId = ++lineNumber - resourceCostEntry.platformDetails.billRecordId = data[awsBillIndexes.recordId] - - resourceCostEntry.platformDetails.serviceId - = (data[awsBillIndexes.prod] in awsServices)?awsServices[data[awsBillIndexes.prod]] - :resourceCostEntry.platformDetails.serviceId = 'Other' - - resourceCostEntry.platformDetails.zone = (data[awsBillIndexes.zone] == null) - ? 'Global' : data[awsBillIndexes.zone] - - resourceCostEntry.platformDetails.region = (data[awsBillIndexes.zone] in awsZones) - ? awsZones[data[awsBillIndexes.zone]] : 'Global' - - if (data[awsBillIndexes.instanceId] != null) { - resourceCostEntry.platformDetails.instanceId = data[awsBillIndexes.instanceId] - } + var date = new Date() + var billIntervalId = date.getFullYear() + '-' + date.getMonth() - if(data[awsBillIndexes.usageType] != null) { - resourceCostEntry.platformDetails.usageType = data[awsBillIndexes.usageType] - } + async.waterfall([ + function(next) { + resourceCost.remove(provider.orgId, provider._id, billIntervalId, next) + }, + function(count, next) { + /*var lineNumber = (count == 0)?0:count + var startingLineNumber = (count == 0)?1:(count+2)*/ - if (data[awsBillIndexes.instanceId] in resources) { - var resource = resources[data[awsBillIndexes.instanceId]] + var stream = fs.createReadStream(downlaodedCSVPath) + csv.fromStream(stream).on('data', function(data) { + if(data[awsBillIndexes.totalCost] == 'LineItem') { + var resourceCostEntry = {platformDetails: {}} - resourceCostEntry.resourceId = resource._id + resourceCostEntry.organizationId = provider.orgId + resourceCostEntry.providerId = provider._id + resourceCostEntry.providerType = provider.providerType + resourceCostEntry.cost = data[awsBillIndexes.cost] + resourceCostEntry.startTime = Date.parse(data[awsBillIndexes.startDate]) + resourceCostEntry.endTime = Date.parse(data[awsBillIndexes.endDate]) + resourceCostEntry.lastUpdateTime = Date.parse(updateTime) + resourceCostEntry.interval = 3600 + resourceCostEntry.platformDetails.serviceName = data[awsBillIndexes.prod] + resourceCostEntry.billIntervalId = billIntervalId + resourceCostEntry.billLineItemId = ++lineNumber + resourceCostEntry.platformDetails.billRecordId = data[awsBillIndexes.recordId] - if (('bgId' in resource) && (resource.bgId != null)) { - resourceCostEntry.businessGroupId = resource['bgId'] - } + resourceCostEntry.platformDetails.serviceId + = (data[awsBillIndexes.prod] in awsServices)?awsServices[data[awsBillIndexes.prod]] + :resourceCostEntry.platformDetails.serviceId = 'Other' - if (('projectId' in resource) && (resource.projectId != null)) { - resourceCostEntry.projectId = resource['projectId'] - } + resourceCostEntry.platformDetails.zone = (data[awsBillIndexes.zone] == null) + ? 'Global' : data[awsBillIndexes.zone] - if (('environmentId' in resource) && (resource.environmentId != null)) { - resourceCostEntry.environmentId = resource['environmentId'] - } + resourceCostEntry.platformDetails.region = (data[awsBillIndexes.zone] in awsZones) + ? awsZones[data[awsBillIndexes.zone]] : 'Global' - if (('masterDetails.bgId' in resource) && (resource.masterDetails.bgId != null)) { - console.log("BG: " + resource['bgId']) - resourceCostEntry.businessGroupId = resource['bgId'] - } + if (data[awsBillIndexes.instanceId] != null) { + resourceCostEntry.platformDetails.instanceId = data[awsBillIndexes.instanceId] + } - if (('masterDetails.projectId' in resource) - && (resource.masterDetails.projectId != null)) { - resourceCostEntry.projectId = resource['projectId'] - } + if(data[awsBillIndexes.usageType] != null) { + resourceCostEntry.platformDetails.usageType = data[awsBillIndexes.usageType] + } - if (('masterDetails.environmentId' in resource) - && (resource.masterDetails.environmentId != null)) { - resourceCostEntry.environmentId = resource['environmentId'] - } - } + if (data[awsBillIndexes.instanceId] in resources) { + var resource = resources[data[awsBillIndexes.instanceId]] - resourceCostEntry.businessGroupId - = ('businessGroupId' in resourceCostEntry)?resourceCostEntry.businessGroupId:'Unassigned' - resourceCostEntry.projectId - = ('projectId' in resourceCostEntry)?resourceCostEntry.projectId:'Unassigned' - resourceCostEntry.environmentId - = ('environmentId' in resourceCostEntry)?resourceCostEntry.environmentId:'Unassigned' + resourceCostEntry.resourceId = resource._id - resourceCost.upsertResourceCost(resourceCostEntry, function (err, costEntry) { - if (err) { - logger.error(err) - return callback(new Error('Database Error')) + if (('bgId' in resource) && (resource.bgId != null)) { + resourceCostEntry.businessGroupId = resource['bgId'] + } + + if (('projectId' in resource) && (resource.projectId != null)) { + resourceCostEntry.projectId = resource['projectId'] + } + + if (('environmentId' in resource) && (resource.environmentId != null)) { + resourceCostEntry.environmentId = resource['environmentId'] + } + + if (('masterDetails.bgId' in resource) && (resource.masterDetails.bgId != null)) { + console.log("BG: " + resource['bgId']) + resourceCostEntry.businessGroupId = resource['bgId'] + } + + if (('masterDetails.projectId' in resource) + && (resource.masterDetails.projectId != null)) { + resourceCostEntry.projectId = resource['projectId'] + } + + if (('masterDetails.environmentId' in resource) + && (resource.masterDetails.environmentId != null)) { + resourceCostEntry.environmentId = resource['environmentId'] + } + } + + resourceCostEntry.businessGroupId + = ('businessGroupId' in resourceCostEntry)?resourceCostEntry.businessGroupId:'Unassigned' + resourceCostEntry.projectId + = ('projectId' in resourceCostEntry)?resourceCostEntry.projectId:'Unassigned' + resourceCostEntry.environmentId + = ('environmentId' in resourceCostEntry)?resourceCostEntry.environmentId:'Unassigned' + + resourceCost.save(resourceCostEntry, function (err, costEntry) { + if (err) { + logger.error(err) + return next(new Error('Database Error')) + } + }) } + }).on('end', function() { + next() }) } - }).on('end', function() { - callback(null) + ], function(err) { + if(err) { + callback(err) + } else { + callback() + } }) + } function getCostForServices_deprecated(provider,callback) { From 0bb74d01a7cb9efa1d2fdd0b66b83663ae674ea0 Mon Sep 17 00:00:00 2001 From: srikanthv02 Date: Thu, 1 Dec 2016 17:42:47 +0530 Subject: [PATCH 03/14] Check for undefined and null --- .../workzone/orchestration/popups/addChefJobEventCtrl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/addChefJobEventCtrl.js b/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/addChefJobEventCtrl.js index 189e99aa7..187ec039b 100644 --- a/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/addChefJobEventCtrl.js +++ b/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/addChefJobEventCtrl.js @@ -16,7 +16,7 @@ $scope.schedulerEndOn=moment(new Date()).format('MM/DD/YYYY'); }; if(items.type !== 'new'){ - if(items.chefJenkScriptTaskObj !==undefined){ + if(items.chefJenkScriptTaskObj !=null){ if(items.chefJenkScriptTaskObj.cronStartOn && items.chefJenkScriptTaskObj.cronEndOn) { var newStartOn = parseInt(items.chefJenkScriptTaskObj.cronStartOn); var newDate = new Date(newStartOn).toLocaleDateString(); From c6d67c730ae811cf3eebde7bad8e41c783ed47ca Mon Sep 17 00:00:00 2001 From: srikanthv02 Date: Thu, 1 Dec 2016 17:49:43 +0530 Subject: [PATCH 04/14] Review comments taken care --- .../workzone/orchestration/popups/addChefJobEventCtrl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/addChefJobEventCtrl.js b/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/addChefJobEventCtrl.js index 187ec039b..9a3a24b2c 100644 --- a/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/addChefJobEventCtrl.js +++ b/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/addChefJobEventCtrl.js @@ -16,7 +16,7 @@ $scope.schedulerEndOn=moment(new Date()).format('MM/DD/YYYY'); }; if(items.type !== 'new'){ - if(items.chefJenkScriptTaskObj !=null){ + if(items.chefJenkScriptTaskObj){ if(items.chefJenkScriptTaskObj.cronStartOn && items.chefJenkScriptTaskObj.cronEndOn) { var newStartOn = parseInt(items.chefJenkScriptTaskObj.cronStartOn); var newDate = new Date(newStartOn).toLocaleDateString(); From 5ab8fa933a079e393e86a6af113e42f7b5f28c02 Mon Sep 17 00:00:00 2001 From: Karthik K N Date: Thu, 1 Dec 2016 14:28:06 +0530 Subject: [PATCH 05/14] Fix for CAT-2445 Incorrect Total Cost is showing in Analytics tab --- .../model/resource-costs/resource-costs.js | 43 +++-- server/app/services/resourceService.js | 161 ++++++++++-------- 2 files changed, 124 insertions(+), 80 deletions(-) diff --git a/server/app/model/resource-costs/resource-costs.js b/server/app/model/resource-costs/resource-costs.js index 012c774f6..28a47e042 100644 --- a/server/app/model/resource-costs/resource-costs.js +++ b/server/app/model/resource-costs/resource-costs.js @@ -70,6 +70,11 @@ var ResourceCostsSchema = new Schema({ required: false, trim: true }, + billIntervalId: { + type: String, + required: true, + trim: true + }, billLineItemId: { type: Number, required: true, @@ -132,38 +137,56 @@ var ResourceCostsSchema = new Schema({ }) ResourceCostsSchema.index({'platformDetails.serviceId' : 1}) -ResourceCostsSchema.index({'organizationId': 1, 'providerId': 1, 'billLineItemId': 1, - 'startTime': 1, 'interval': 1}, {'unique': true}) +ResourceCostsSchema.index({'organizationId': 1, 'providerId': 1, 'startTime': 1, + 'billIntervalId': 1, 'billLineItemId': 1, 'interval': 1}, {'unique': true}) -ResourceCostsSchema.statics.saveResourceCost = function saveResourceCost(resourceCostData, callback) { +ResourceCostsSchema.statics.save = function save(resourceCostData, callback) { var resourceCosts = new ResourceCosts(resourceCostData) - resourceCosts.save(function(err, data) { + resourceCosts.save(function(err, result) { + if (err) { + callback(err) + } else { + callback(null, result) + } + }) +} + +ResourceCostsSchema.statics.remove + = function remove(organizationId, providerId, billIntervalId, callback) { + var query = { + organizationId: organizationId, + providerId: providerId, + billIntervalId: billIntervalId + } + + this.find(query).remove(function(err, result) { if (err) { callback(err) } else { - callback(null) + callback(null, result) } }) } -ResourceCostsSchema.statics.upsertResourceCost = function upsertResourceCost(resourceCostData, callback) { +ResourceCostsSchema.statics.upsert = function upsert(resourceCostData, callback) { var query = { organizationId: resourceCostData.organizationId, providerId: resourceCostData.providerId, - billLineItemId: resourceCostData.billLineItemId, startTime: resourceCostData.startTime, + billIntervalId: resourceCostData.billIntervalId, + billLineItemId: resourceCostData.billLineItemId, interval: resourceCostData.interval } - this.findOneAndUpdate(query, resourceCostData, {upsert:true}, + this.findOneAndUpdate(query, resourceCostData, {upsert: true}, function(err, result){ if (err) { callback(null) } else { callback(null, result) } - }); + }) } var ResourceCosts = mongoose.model('ResourceCosts', ResourceCostsSchema) -module.exports = ResourceCosts +module.exports = ResourceCosts \ No newline at end of file diff --git a/server/app/services/resourceService.js b/server/app/services/resourceService.js index f56a7f95c..deb4f322d 100644 --- a/server/app/services/resourceService.js +++ b/server/app/services/resourceService.js @@ -91,91 +91,112 @@ function updateAWSResourceCostsFromCSV(provider, resources, downlaodedCSVPath, u var awsZones = appConfig.aws.zones var lineNumber = 0 - var stream = fs.createReadStream(downlaodedCSVPath) - csv.fromStream(stream, {headers: false}).on('data', function(data) { - if(data[awsBillIndexes.totalCost] == 'LineItem') { - var resourceCostEntry = {platformDetails: {}} - - resourceCostEntry.organizationId = provider.orgId - resourceCostEntry.providerId = provider._id - resourceCostEntry.providerType = provider.providerType - resourceCostEntry.cost = data[awsBillIndexes.cost] - resourceCostEntry.startTime = Date.parse(data[awsBillIndexes.startDate]) - resourceCostEntry.endTime = Date.parse(data[awsBillIndexes.endDate]) - resourceCostEntry.lastUpdateTime = Date.parse(updateTime) - resourceCostEntry.interval = 3600 - resourceCostEntry.platformDetails.serviceName = data[awsBillIndexes.prod] - resourceCostEntry.billLineItemId = ++lineNumber - resourceCostEntry.platformDetails.billRecordId = data[awsBillIndexes.recordId] - - resourceCostEntry.platformDetails.serviceId - = (data[awsBillIndexes.prod] in awsServices)?awsServices[data[awsBillIndexes.prod]] - :resourceCostEntry.platformDetails.serviceId = 'Other' - - resourceCostEntry.platformDetails.zone = (data[awsBillIndexes.zone] == null) - ? 'Global' : data[awsBillIndexes.zone] - - resourceCostEntry.platformDetails.region = (data[awsBillIndexes.zone] in awsZones) - ? awsZones[data[awsBillIndexes.zone]] : 'Global' - - if (data[awsBillIndexes.instanceId] != null) { - resourceCostEntry.platformDetails.instanceId = data[awsBillIndexes.instanceId] - } + var date = new Date() + var billIntervalId = date.getFullYear() + '-' + date.getMonth() - if(data[awsBillIndexes.usageType] != null) { - resourceCostEntry.platformDetails.usageType = data[awsBillIndexes.usageType] - } + async.waterfall([ + function(next) { + resourceCost.remove(provider.orgId, provider._id, billIntervalId, next) + }, + function(count, next) { + /*var lineNumber = (count == 0)?0:count + var startingLineNumber = (count == 0)?1:(count+2)*/ - if (data[awsBillIndexes.instanceId] in resources) { - var resource = resources[data[awsBillIndexes.instanceId]] + var stream = fs.createReadStream(downlaodedCSVPath) + csv.fromStream(stream).on('data', function(data) { + if(data[awsBillIndexes.totalCost] == 'LineItem') { + var resourceCostEntry = {platformDetails: {}} - resourceCostEntry.resourceId = resource._id + resourceCostEntry.organizationId = provider.orgId + resourceCostEntry.providerId = provider._id + resourceCostEntry.providerType = provider.providerType + resourceCostEntry.cost = data[awsBillIndexes.cost] + resourceCostEntry.startTime = Date.parse(data[awsBillIndexes.startDate]) + resourceCostEntry.endTime = Date.parse(data[awsBillIndexes.endDate]) + resourceCostEntry.lastUpdateTime = Date.parse(updateTime) + resourceCostEntry.interval = 3600 + resourceCostEntry.platformDetails.serviceName = data[awsBillIndexes.prod] + resourceCostEntry.billIntervalId = billIntervalId + resourceCostEntry.billLineItemId = ++lineNumber + resourceCostEntry.platformDetails.billRecordId = data[awsBillIndexes.recordId] - if (('bgId' in resource) && (resource.bgId != null)) { - resourceCostEntry.businessGroupId = resource['bgId'] - } + resourceCostEntry.platformDetails.serviceId + = (data[awsBillIndexes.prod] in awsServices)?awsServices[data[awsBillIndexes.prod]] + :resourceCostEntry.platformDetails.serviceId = 'Other' - if (('projectId' in resource) && (resource.projectId != null)) { - resourceCostEntry.projectId = resource['projectId'] - } + resourceCostEntry.platformDetails.zone = (data[awsBillIndexes.zone] == null) + ? 'Global' : data[awsBillIndexes.zone] - if (('environmentId' in resource) && (resource.environmentId != null)) { - resourceCostEntry.environmentId = resource['environmentId'] - } + resourceCostEntry.platformDetails.region = (data[awsBillIndexes.zone] in awsZones) + ? awsZones[data[awsBillIndexes.zone]] : 'Global' - if (('masterDetails.bgId' in resource) && (resource.masterDetails.bgId != null)) { - console.log("BG: " + resource['bgId']) - resourceCostEntry.businessGroupId = resource['bgId'] - } + if (data[awsBillIndexes.instanceId] != null) { + resourceCostEntry.platformDetails.instanceId = data[awsBillIndexes.instanceId] + } - if (('masterDetails.projectId' in resource) - && (resource.masterDetails.projectId != null)) { - resourceCostEntry.projectId = resource['projectId'] - } + if(data[awsBillIndexes.usageType] != null) { + resourceCostEntry.platformDetails.usageType = data[awsBillIndexes.usageType] + } - if (('masterDetails.environmentId' in resource) - && (resource.masterDetails.environmentId != null)) { - resourceCostEntry.environmentId = resource['environmentId'] - } - } + if (data[awsBillIndexes.instanceId] in resources) { + var resource = resources[data[awsBillIndexes.instanceId]] - resourceCostEntry.businessGroupId - = ('businessGroupId' in resourceCostEntry)?resourceCostEntry.businessGroupId:'Unassigned' - resourceCostEntry.projectId - = ('projectId' in resourceCostEntry)?resourceCostEntry.projectId:'Unassigned' - resourceCostEntry.environmentId - = ('environmentId' in resourceCostEntry)?resourceCostEntry.environmentId:'Unassigned' + resourceCostEntry.resourceId = resource._id - resourceCost.upsertResourceCost(resourceCostEntry, function (err, costEntry) { - if (err) { - logger.error(err) - return callback(new Error('Database Error')) + if (('bgId' in resource) && (resource.bgId != null)) { + resourceCostEntry.businessGroupId = resource['bgId'] + } + + if (('projectId' in resource) && (resource.projectId != null)) { + resourceCostEntry.projectId = resource['projectId'] + } + + if (('environmentId' in resource) && (resource.environmentId != null)) { + resourceCostEntry.environmentId = resource['environmentId'] + } + + if (('masterDetails.bgId' in resource) && (resource.masterDetails.bgId != null)) { + console.log("BG: " + resource['bgId']) + resourceCostEntry.businessGroupId = resource['bgId'] + } + + if (('masterDetails.projectId' in resource) + && (resource.masterDetails.projectId != null)) { + resourceCostEntry.projectId = resource['projectId'] + } + + if (('masterDetails.environmentId' in resource) + && (resource.masterDetails.environmentId != null)) { + resourceCostEntry.environmentId = resource['environmentId'] + } + } + + resourceCostEntry.businessGroupId + = ('businessGroupId' in resourceCostEntry)?resourceCostEntry.businessGroupId:'Unassigned' + resourceCostEntry.projectId + = ('projectId' in resourceCostEntry)?resourceCostEntry.projectId:'Unassigned' + resourceCostEntry.environmentId + = ('environmentId' in resourceCostEntry)?resourceCostEntry.environmentId:'Unassigned' + + resourceCost.save(resourceCostEntry, function (err, costEntry) { + if (err) { + logger.error(err) + return next(new Error('Database Error')) + } + }) } + }).on('end', function() { + next() }) } - }).on('end', function() { - callback(null) + ], function(err) { + if(err) { + callback(err) + } else { + callback() + } }) + } function getCostForServices_deprecated(provider,callback) { From d93e2963dd9eb79feade2f256fc9d88ad8202975 Mon Sep 17 00:00:00 2001 From: Durgesh1988 Date: Fri, 2 Dec 2016 17:12:51 +0530 Subject: [PATCH 06/14] Script Executor for Python and bots library Issue --- .../orchestration/popups/addChefJobEvent.html | 9 ++- .../popups/addChefJobEventCtrl.js | 26 ++++-- .../orchestration/popups/addScriptParams.html | 10 ++- .../popups/addScriptParamsCtrl.js | 11 ++- .../orchestration/popups/newTask.html | 11 ++- .../orchestration/popups/newTaskCtrl.js | 16 +++- .../private/ajax/Settings/scriptList.html | 37 ++++++++- client/htmls/private/js/scriptList.js | 31 ++++++- .../catalyst-scheduler/catalystScheduler.js | 2 - server/app/lib/utils/apiUtil.js | 65 +++++++-------- server/app/model/blueprint/blueprint.js | 27 +++++++ .../app/model/classes/tasks/taskTypeScript.js | 81 +++++++++++-------- server/app/model/classes/tasks/tasks.js | 57 ++++++++++--- server/app/model/scripts/scripts.js | 14 +++- server/app/routes/v1.0/routes_blueprints.js | 11 +++ .../app/routes/v1.0/routes_organizations.js | 26 +++--- server/app/routes/v1.0/routes_tasks.js | 21 ++--- server/app/services/instanceService.js | 22 +++-- server/app/services/schedulerService.js | 3 +- server/app/services/scriptService.js | 34 ++++---- server/app/services/taskService.js | 11 +++ 21 files changed, 369 insertions(+), 156 deletions(-) diff --git a/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/addChefJobEvent.html b/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/addChefJobEvent.html index 6cdbf04b0..d454fa2d5 100644 --- a/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/addChefJobEvent.html +++ b/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/addChefJobEvent.html @@ -23,12 +23,13 @@
Script Details
@@ -561,13 +562,15 @@
Script Details
Parameter + Description Action - {{param}} - ..... + {{param.paramVal}} + ..... + {{param.paramDesc}} diff --git a/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/newTaskCtrl.js b/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/newTaskCtrl.js index 0c6e70413..b370ef5d6 100644 --- a/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/newTaskCtrl.js +++ b/client/cat3/src/partials/sections/dashboard/workzone/orchestration/popups/newTaskCtrl.js @@ -152,7 +152,12 @@ templateUrl: 'src/partials/sections/dashboard/workzone/orchestration/popups/addScriptParams.html', controller: 'addScriptParamsCtrl', backdrop: 'static', - keyboard: false + keyboard: false, + resolve: { + items: function () { + return scriptObject; + } + } }).result.then(function (addScriptParams) { $scope.scriptParamsObj[scriptObject._id] = $scope.scriptParamsObj[scriptObject._id].concat(addScriptParams); }, function () { @@ -271,9 +276,12 @@ cronEndOn : $scope.chefJenkScriptTaskObj.cronEnd, cronRepeatEvery : $scope.chefJenkScriptTaskObj.repeatBy, cronFrequency: $scope.chefJenkScriptTaskObj.repeats, - cronTime: typeof startTimeHour !=='undefined'? startTimeHour : new Date().getHours() + ':' + typeof startTimeMinute !=='undefined'? startTimeMinute:new Date().getMinutes(), - cronDays: $scope.chefJenkScriptTaskObj.dayOfWeek, - cronMonth: $scope.chefJenkScriptTaskObj.monthOfYear + cronMinute:startTimeMinute, + cronHour: startTimeHour, + cronWeekDay:dayOfWeek, + cronDate: selectedDayOfTheMonth, + cronMonth: selectedMonth, + cronYear: $scope.chefJenkScriptTaskObj.monthOfYear } }, function () { console.log('Dismiss time is ' + new Date()); diff --git a/client/htmls/private/ajax/Settings/scriptList.html b/client/htmls/private/ajax/Settings/scriptList.html index 426cc4e7d..c00fece50 100644 --- a/client/htmls/private/ajax/Settings/scriptList.html +++ b/client/htmls/private/ajax/Settings/scriptList.html @@ -77,6 +77,7 @@