diff --git a/server/app/cronjobs/task-sync/taskSync.js b/server/app/cronjobs/task-sync/taskSync.js index c31c2d3fe..56e1f0c50 100644 --- a/server/app/cronjobs/task-sync/taskSync.js +++ b/server/app/cronjobs/task-sync/taskSync.js @@ -27,9 +27,6 @@ function taskSync(){ status:'running' }; executeTaskSyncForTaskHistory(query,callback); - }, - savedTimeSync : function(callback){ - addSavedTimePerBots(callback); } },function(err,results){ @@ -157,74 +154,6 @@ function getMinutesDiff(date1,date2){ return Math.abs(Math.round(diff)); } -function getExecutionTime(endTime, startTime) { - var executionTimeInMS = endTime - startTime; - var totalSeconds = Math.floor(executionTimeInMS / 1000); - return totalSeconds; -} -function addSavedTimePerBots(callback) { - var query = { - isDeleted:false - } - botsDao.getAllBots(query,function(err,botList){ - if(err){ - callback(err,null); - return; - }else if(botList.length > 0){ - var count = 0; - for(var i = 0; i 0) { - var totalTimeInSeconds = 0; - for (var m = 0; m < botAuditTrail.length; m++) { - if (botAuditTrail[m].endedOn && botAuditTrail[m].endedOn !== null - && botAuditTrail[m].auditTrailConfig.manualExecutionTime && botAuditTrail[m].auditTrailConfig.manualExecutionTime !== null) { - var executionTime = getExecutionTime(botAuditTrail[m].endedOn, botAuditTrail[m].startedOn); - totalTimeInSeconds = totalTimeInSeconds + ((botAuditTrail[m].auditTrailConfig.manualExecutionTime * 60) - executionTime); - } - } - var totalTimeInMinutes = Math.round(totalTimeInSeconds/60); - var result = { - hours:Math.floor(totalTimeInMinutes / 60), - minutes:totalTimeInMinutes % 60 - } - botsDao.updateBotsDetail(bots.botId,{savedTime:result},function(err,data){ - if(err){ - logger.error(err); - } - count++; - if(count === botList.length){ - callback(null,botList); - return; - } - }) - } else { - count++; - if(count === botList.length){ - callback(null,botList); - return; - } - } - }); - })(botList[i]); - } - }else{ - logger.debug("There is no BOTs for Saved Time Implementation."); - callback(null,botList); - } - }) -} diff --git a/server/app/model/blueprint/blueprint-types/cloud-formation-blueprint/cloud-formation-blueprint.js b/server/app/model/blueprint/blueprint-types/cloud-formation-blueprint/cloud-formation-blueprint.js index 94d5ba485..6f5eafc01 100755 --- a/server/app/model/blueprint/blueprint-types/cloud-formation-blueprint/cloud-formation-blueprint.js +++ b/server/app/model/blueprint/blueprint-types/cloud-formation-blueprint/cloud-formation-blueprint.js @@ -766,6 +766,12 @@ CloudFormationBlueprintSchema.methods.launch = function (launchParams, callback) if (err) { logger.error("Failed to create or update bots Log: ", err); } + var botService = require('_pr/services/botsService'); + botService.updateSavedTimePerBots(launchParams.blueprintData._id,function(err,data){ + if (err) { + logger.error("Failed to update bots saved Time: ", err); + } + }); }); } diff --git a/server/app/model/blueprint/blueprint-types/instance-blueprint/aws-blueprint/aws-blueprint.js b/server/app/model/blueprint/blueprint-types/instance-blueprint/aws-blueprint/aws-blueprint.js index e13863891..89d069a28 100755 --- a/server/app/model/blueprint/blueprint-types/instance-blueprint/aws-blueprint/aws-blueprint.js +++ b/server/app/model/blueprint/blueprint-types/instance-blueprint/aws-blueprint/aws-blueprint.js @@ -629,6 +629,12 @@ AWSInstanceBlueprintSchema.methods.launch = function (launchParams, callback) { if (err) { logger.error("Failed to create or update bots Log: ", err); } + var botService = require('_pr/services/botsService'); + botService.updateSavedTimePerBots(launchParams.blueprintData._id,function(err,data){ + if (err) { + logger.error("Failed to update bots saved Time: ", err); + } + }); }); } instanceLogModel.createOrUpdate(actionLog._id, instance.id, instanceLog, function (err, logData) { @@ -669,6 +675,12 @@ AWSInstanceBlueprintSchema.methods.launch = function (launchParams, callback) { if (err) { logger.error("Failed to create or update bots Log: ", err); } + var botService = require('_pr/services/botsService'); + botService.updateSavedTimePerBots(launchParams.blueprintData.id,function(err,data){ + if (err) { + logger.error("Failed to update bots saved Time: ", err); + } + }); }); } instancesDao.updateActionLog(instance.id, actionLog._id, true, timestampEnded); diff --git a/server/app/model/blueprint/blueprint-types/instance-blueprint/azure-blueprint/azure-blueprint.js b/server/app/model/blueprint/blueprint-types/instance-blueprint/azure-blueprint/azure-blueprint.js index 24a811da5..b256f1295 100755 --- a/server/app/model/blueprint/blueprint-types/instance-blueprint/azure-blueprint/azure-blueprint.js +++ b/server/app/model/blueprint/blueprint-types/instance-blueprint/azure-blueprint/azure-blueprint.js @@ -576,6 +576,12 @@ azureInstanceBlueprintSchema.methods.launch = function(launchParams, callback) { if (err) { logger.error("Failed to create or update bots Log: ", err); } + var botService = require('_pr/services/botsService'); + botService.updateSavedTimePerBots(launchParams.blueprintData._id,function(err,data){ + if (err) { + logger.error("Failed to update bots saved Time: ", err); + } + }); }); } instanceLogModel.createOrUpdate(actionLog._id, instance.id, instanceLog, function(err, logData) { diff --git a/server/app/model/blueprint/blueprint-types/instance-blueprint/openstack-blueprint/openstack-blueprint.js b/server/app/model/blueprint/blueprint-types/instance-blueprint/openstack-blueprint/openstack-blueprint.js index 04485019f..562ad6568 100755 --- a/server/app/model/blueprint/blueprint-types/instance-blueprint/openstack-blueprint/openstack-blueprint.js +++ b/server/app/model/blueprint/blueprint-types/instance-blueprint/openstack-blueprint/openstack-blueprint.js @@ -526,6 +526,12 @@ openstackInstanceBlueprintSchema.methods.launch = function(launchParams, callbac if (err) { logger.error("Failed to create or update bots Log: ", err); } + var botService = require('_pr/services/botsService'); + botService.updateSavedTimePerBots(launchParams.blueprintData._id,function(err,data){ + if (err) { + logger.error("Failed to update bots saved Time: ", err); + } + }); }); } instancesDao.updateActionLog(instance.id, actionLog._id, true, timestampEnded); diff --git a/server/app/model/blueprint/blueprint-types/instance-blueprint/vmware-blueprint/vmware-blueprint.js b/server/app/model/blueprint/blueprint-types/instance-blueprint/vmware-blueprint/vmware-blueprint.js index 6233ada97..c3ee8271c 100755 --- a/server/app/model/blueprint/blueprint-types/instance-blueprint/vmware-blueprint/vmware-blueprint.js +++ b/server/app/model/blueprint/blueprint-types/instance-blueprint/vmware-blueprint/vmware-blueprint.js @@ -585,6 +585,12 @@ vmwareInstanceBlueprintSchema.methods.launch = function(launchParams, callback) if (err) { logger.error("Failed to create or update bots Log: ", err); } + var botService = require('_pr/services/botsService'); + botService.updateSavedTimePerBots(launchParams.blueprintData._id,function(err,data){ + if (err) { + logger.error("Failed to update bots saved Time: ", err); + } + }); }); } instanceLogModel.createOrUpdate(actionLog._id, instance.id, instanceLog, function(err, logData) { diff --git a/server/app/model/classes/tasks/tasks.js b/server/app/model/classes/tasks/tasks.js index b168536a4..39835bd12 100755 --- a/server/app/model/classes/tasks/tasks.js +++ b/server/app/model/classes/tasks/tasks.js @@ -455,6 +455,14 @@ taskSchema.methods.execute = function(userName, baseUrl, choiceParam, appData, b if (err) { logger.error("Failed to create or update bots Log: ", err); } + if(resultTaskExecution.actionStatus === 'success'){ + var botService = require('_pr/services/botsService'); + botService.updateSavedTimePerBots(taskHistoryData.taskId,function(err,data){ + if (err) { + logger.error("Failed to update bots saved Time: ", err); + } + }); + } }); } self.save(); diff --git a/server/app/services/auditTrailService.js b/server/app/services/auditTrailService.js index aaf4e8968..c30dc7245 100644 --- a/server/app/services/auditTrailService.js +++ b/server/app/services/auditTrailService.js @@ -255,34 +255,26 @@ auditTrailService.getBOTsSummary = function getBOTsSummary(queryParam,BOTSchema, }); }, totalSavedTimeForBots: function(callback){ - var query={ - auditType:BOTSchema, - actionStatus:'success', - isDeleted:false, - auditId:{$in:auditIds} - }; - auditTrail.getAuditTrails(query,function(err,botAuditTrail){ - if(err){ - callback(err,null); - } else if(botAuditTrail.length > 0){ - var totalTimeInSeconds = 0; - for(var j = 0; j < botAuditTrail.length; j++){ - if(botAuditTrail[j].endedOn && botAuditTrail[j].endedOn !== null - && botAuditTrail[j].auditTrailConfig.manualExecutionTime && botAuditTrail[j].auditTrailConfig.manualExecutionTime !== null) { - var executionTime = getExecutionTime(botAuditTrail[j].endedOn, botAuditTrail[j].startedOn); - totalTimeInSeconds = totalTimeInSeconds + ((botAuditTrail[j].auditTrailConfig.manualExecutionTime*60) - executionTime); - } + var hours = 0, minutes = 0; + if(botsList.length > 0) { + for (var k = 0; k < botsList.length; k++) { + if(botsList[k].savedTime.hours) { + hours = hours + botsList[k].savedTime.hours; } - var totalTimeInMinutes = Math.round(totalTimeInSeconds/60); - var result = { - hours:Math.floor(totalTimeInMinutes / 60), - minutes:totalTimeInMinutes % 60 + if(botsList[k].savedTime.minutes){ + minutes = minutes + botsList[k].savedTime.minutes; } - callback(null,result); - } else{ - callback(null,botAuditTrail.length); } - }); + } + if(minutes >= 60){ + hours = hours + Math.floor(minutes / 60); + minutes = minutes % 60; + } + var result = { + hours:hours, + minutes:minutes + } + callback(null,result); }, totalNoOfFailedBots: function(callback){ var query={ diff --git a/server/app/services/botsService.js b/server/app/services/botsService.js index bd90062ef..671d9ad22 100644 --- a/server/app/services/botsService.js +++ b/server/app/services/botsService.js @@ -23,7 +23,6 @@ var ObjectId = require('mongoose').Types.ObjectId; var async = require("async"); var apiUtil = require('_pr/lib/utils/apiUtil.js'); var taskService = require('_pr/services/taskService.js'); -var tasks = require('_pr/model/classes/tasks/tasks.js'); var auditTrailService = require('_pr/services/auditTrailService.js'); var blueprintService = require('_pr/services/blueprintService.js'); var auditTrail = require('_pr/model/audit-trail/audit-trail.js'); @@ -130,6 +129,7 @@ botsService.updateBotsScheduler = function updateBotsScheduler(botId,botObj,call } }); if (botsData[0].botLinkedCategory === 'Task') { + var tasks = require('_pr/model/classes/tasks/tasks.js'); tasks.getTaskById(botId, function (err, task) { if (err) { logger.error("Error in fetching Task details", err); @@ -303,6 +303,48 @@ botsService.getBotsHistory = function getBotsHistory(botId,botsQuery,callback){ }); } +botsService.updateSavedTimePerBots = function updateSavedTimePerBots(botId,callback){ + var query = { + auditType: 'BOTs', + actionStatus: 'success', + isDeleted: false, + auditId: botId + }; + auditTrail.getAuditTrails(query, function (err, botAuditTrail) { + if (err) { + logger.error("Error in Fetching Audit Trail.", err); + callback(err, null); + } + if (botAuditTrail.length > 0) { + var totalTimeInSeconds = 0; + for (var m = 0; m < botAuditTrail.length; m++) { + if (botAuditTrail[m].endedOn && botAuditTrail[m].endedOn !== null + && botAuditTrail[m].auditTrailConfig.manualExecutionTime && botAuditTrail[m].auditTrailConfig.manualExecutionTime !== null) { + var executionTime = getExecutionTime(botAuditTrail[m].endedOn, botAuditTrail[m].startedOn); + totalTimeInSeconds = totalTimeInSeconds + ((botAuditTrail[m].auditTrailConfig.manualExecutionTime * 60) - executionTime); + } + } + var totalTimeInMinutes = Math.round(totalTimeInSeconds / 60); + var result = { + hours: Math.floor(totalTimeInMinutes / 60), + minutes: totalTimeInMinutes % 60 + } + bots.updateBotsDetail(botId, {savedTime: result}, function (err, data) { + if (err) { + logger.error(err); + callback(err, null); + return; + } + callback(null, data); + return; + }) + } else { + callback(null, botAuditTrail); + return; + } + }); +} + botsService.getPerticularBotsHistory = function getPerticularBotsHistory(botId,historyId,callback){ async.waterfall([ function(next){ @@ -366,6 +408,7 @@ botsService.executeBots = function executeBots(botId,reqBody,callback){ var taskObj = { 'taskConfig.scriptDetails':encryptedParamList } + var tasks = require('_pr/model/classes/tasks/tasks.js'); tasks.updateTaskDetail(botId,taskObj,callback); } }, function (err, data) { @@ -547,6 +590,12 @@ function encryptedParam(paramDetails, callback) { return; } +function getExecutionTime(endTime, startTime) { + var executionTimeInMS = endTime - startTime; + var totalSeconds = Math.floor(executionTimeInMS / 1000); + return totalSeconds; +} +