From 775779968dd9d2732c5d58c6147fc81705d31af1 Mon Sep 17 00:00:00 2001 From: Durgesh1988 Date: Fri, 10 Feb 2017 12:26:01 +0530 Subject: [PATCH] Migration Script for BOTs --- server/app/db-script/auditTrail-update.js | 49 +++++++++++++++++++++++ server/app/services/botsService.js | 7 ++-- 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 server/app/db-script/auditTrail-update.js diff --git a/server/app/db-script/auditTrail-update.js b/server/app/db-script/auditTrail-update.js new file mode 100644 index 000000000..7a2713ca1 --- /dev/null +++ b/server/app/db-script/auditTrail-update.js @@ -0,0 +1,49 @@ + + +var logger = require('_pr/logger')(module); +var mongodbConnect = require('_pr/lib/mongodb'); +var appConfig = require('_pr/config'); +var bots = require('_pr/model/bots/1.0/bots.js'); +var botService = require('_pr/services/botsService.js'); + +var dbOptions = { + host: appConfig.db.host, + port: appConfig.db.port, + dbName: appConfig.db.dbName +}; +mongodbConnect(dbOptions, function(err) { + if (err) { + logger.error("Unable to connect to mongo db >>" + err); + process.exit(); + } else { + logger.debug('connected to mongodb - host = %s, port = %s, database = %s', dbOptions.host, dbOptions.port, dbOptions.dbName); + } +}); + + +bots.getAllBots({isDeleted:false}, function(err, bots) { + if (err) { + logger.error("Failed to fetch Bots: ", err); + process.exit(); + } + if (bots && bots.length) { + var count = 0; + for (var i = 0; i < bots.length; i++) { + (function(bot) { + botService.updateSavedTimePerBots(bot.botId,function(err,data){ + if(err){ + logger.error("Error in updating BOTs Saved Time. ",err); + } + count++; + if(count === bots.length){ + logger.debug("All BOTs updated."); + process.exit(); + } + }) + })(bots[i]); + } + } else { + logger.debug("No BOTs to update."); + process.exit(); + } +}); diff --git a/server/app/services/botsService.js b/server/app/services/botsService.js index 671d9ad22..60c7a58e9 100644 --- a/server/app/services/botsService.js +++ b/server/app/services/botsService.js @@ -306,7 +306,6 @@ botsService.getBotsHistory = function getBotsHistory(botId,botsQuery,callback){ botsService.updateSavedTimePerBots = function updateSavedTimePerBots(botId,callback){ var query = { auditType: 'BOTs', - actionStatus: 'success', isDeleted: false, auditId: botId }; @@ -319,7 +318,9 @@ botsService.updateSavedTimePerBots = function updateSavedTimePerBots(botId,callb 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) { + && botAuditTrail[m].auditTrailConfig.manualExecutionTime + && botAuditTrail[m].auditTrailConfig.manualExecutionTime !== null + && botAuditTrail[m].actionStatus ==='success' ) { var executionTime = getExecutionTime(botAuditTrail[m].endedOn, botAuditTrail[m].startedOn); totalTimeInSeconds = totalTimeInSeconds + ((botAuditTrail[m].auditTrailConfig.manualExecutionTime * 60) - executionTime); } @@ -329,7 +330,7 @@ botsService.updateSavedTimePerBots = function updateSavedTimePerBots(botId,callb hours: Math.floor(totalTimeInMinutes / 60), minutes: totalTimeInMinutes % 60 } - bots.updateBotsDetail(botId, {savedTime: result}, function (err, data) { + bots.updateBotsDetail(botId, {savedTime: result,executionCount:botAuditTrail.length}, function (err, data) { if (err) { logger.error(err); callback(err, null);