diff --git a/qmi-cloud-cli/jobs/stop5.js b/qmi-cloud-cli/jobs/stop5.js index 52b63fe..d4d6ce9 100644 --- a/qmi-cloud-cli/jobs/stop5.js +++ b/qmi-cloud-cli/jobs/stop5.js @@ -41,6 +41,18 @@ function timeRunningOnSchedule(p) { }; } +function timeRunningOnSchedule2(p) { + + let startTimestamp = p.startDateOnSchedule? new Date(p.startDateOnSchedule).getTime() : 0; + let endTimestamp = p.endDateOnSchedule? new Date(p.endDateOnSchedule).getTime() : 0; + let totalTimeOnschedule = endTimestamp - startTimestamp; + let duration = moment.duration(totalTimeOnschedule); + p.duration = { + hours: Math.floor(duration.asHours()), + complete: Math.floor(duration.asDays()) +"d "+duration.hours()+"h "+duration.minutes() + }; +} + async function asyncForEach(array, callback) { for (let index = 0; index < array.length; index++) { await callback(array[index], index, array); @@ -75,10 +87,10 @@ async function init(type) { timeRunningIs24x7(p); periodDays = RUNNING_PERIOD; } else if ( p.schedule && !p.schedule.is24x7 ) { - timeRunningOnSchedule(p); + timeRunningOnSchedule2(p); typeSchedule = 'OnSchedule'; - let onScheduleRenewed = p.schedule.onScheduleRenewed? p.schedule.onScheduleRenewed : 1; - periodDays = RUNNING_PERIOD_ON_SCHEDULE * onScheduleRenewed; + //let onScheduleRenewed = p.schedule.onScheduleRenewed? p.schedule.onScheduleRenewed : 1; + periodDays = RUNNING_PERIOD_ON_SCHEDULE;// * onScheduleRenewed; } var limit; if ( type === "warning" ) { diff --git a/qmi-cloud-common/azurecli.js b/qmi-cloud-common/azurecli.js index 9d288d7..ad2abd3 100644 --- a/qmi-cloud-common/azurecli.js +++ b/qmi-cloud-common/azurecli.js @@ -47,13 +47,37 @@ async function deallocate(provId, isSendEmailAfter ) { }); let timeRunning = db.utils.getNewTimeRunning(provision); - await db.provision.update(provision._id.toString(), {"statusVms": "Stopped", "timeRunning": timeRunning, "stoppedFrom": new Date(), "pendingNextAction": undefined}); - - if ( isSendEmailAfter && provision._scenarioDoc ) { - //From CLI + const dateNow = new Date(); + let patch = { + "statusVms": "Stopped", + "timeRunning": timeRunning, + "stoppedFrom": dateNow, + "pendingNextAction": undefined + }; + if ( isSendEmailAfter && provision._scenarioDoc ) { //From CLI (auto stop) + + // Actual onschedule reset + if ( provision.schedule && !provision.schedule.is24x7 ) { + patch["startDateOnSchedule"] = dateNow; + patch["endDateOnSchedule"] = dateNow; + } + + await db.provision.update(provision._id.toString(), patch); await sendEmail.sendVMsStopped(provision, provision._scenarioDoc); db.event.add({ user: provision.user._id, provision: provision._id, type: 'vms.stop-auto', message: `New count extend: ${provision.countExtend}. OnScheduleRenew: ${provision.schedule.onScheduleRenewed}. New timeRunning: ${timeRunning} mins` }); - } else { + + } else { //On Demand stop + + if ( provision.schedule && !provision.schedule.is24x7 ) { + patch["endDateOnSchedule"] = dateNow; + + //This is temporary, only to make sure there is some initial value soon + if ( !patch["startDateOnSchedule"] ) { + patch["startDateOnSchedule"] = dateNow; + } + } + await db.provision.update(provision._id.toString(), patch); + db.event.add({ user: provision.user._id, provision: provision._id, type: 'vms.stop-ondemand', message: `New count extend: ${provision.countExtend}. OnScheduleRenew: ${provision.schedule.onScheduleRenewed}. New timeRunning: ${timeRunning} mins` }); } @@ -65,6 +89,7 @@ async function deallocate(provId, isSendEmailAfter ) { } async function start(provId){ + let provision = await db.provision.getById(provId); if ( !provision ) return; @@ -82,8 +107,22 @@ async function start(provId){ await asyncForEach(finalResult, async function(vm) { await computeClient.virtualMachines.start(rgName, vm.name); }); + const dateNow = new Date(); let countExtend = db.utils.getNewCountExtend(provision); - await db.provision.update(provision._id.toString(), {"statusVms": "Running", "runningFrom": new Date(), "countExtend": countExtend, "pendingNextAction": undefined}); + var patch = { + "statusVms": "Running", + "runningFrom": dateNow, + "countExtend": countExtend, + "pendingNextAction": undefined + }; + + // Actual onschedule reset + if ( provision.schedule && !provision.schedule.is24x7 ) { + patch["startDateOnSchedule"] = dateNow; + patch["endDateOnSchedule"] = dateNow; + } + + await db.provision.update(provision._id.toString(), patch); console.log("AzureCLI# All VMs RUNNING for resource group: "+rgName); db.event.add({ user: provision.user._id, provision: provision._id, type: 'vms.start-ondemand', message: `New count extend: ${countExtend}. OnScheduleRenew: ${provision.schedule.onScheduleRenewed}. TimeRunning so far: ${provision.timeRunning} mins` }); diff --git a/qmi-cloud-common/models/Provision.js b/qmi-cloud-common/models/Provision.js index 7a1282a..c4ccfc9 100644 --- a/qmi-cloud-common/models/Provision.js +++ b/qmi-cloud-common/models/Provision.js @@ -67,6 +67,12 @@ const provisionSchema = new mongoose.Schema({ type: Number, default: 0 }, + startDateOnSchedule: { + type: Date + }, + endDateOnSchedule: { + type: Date + }, countExtend: { type: Number, default: 0 diff --git a/qmi-cloud-worker/common-apply.js b/qmi-cloud-worker/common-apply.js index debaf7f..ec967ad 100644 --- a/qmi-cloud-worker/common-apply.js +++ b/qmi-cloud-worker/common-apply.js @@ -39,7 +39,19 @@ module.exports = async function(job) { console.log(`ProcessorApply# Error at Terraform PLAN for provision (${idProv}) `); return Promise.reject({"success": false, "error": "Error at Terraform Plan", provStatus: "error_plan"}); } else { - return await db.provision.update(prov._id,{"status": "provisioning", "statusVms": "Running", "runningFrom": new Date(), "runningTime": 0, "countExtend": 0}); + const dateNow = new Date(); + let patch = { + "status": "provisioning", + "statusVms": "Running", + "runningFrom": dateNow, + "runningTime": 0, + "countExtend": 0 + }; + if ( prov.schedule && !prov.schedule.is24x7 ) { + patch["startDateOnSchedule"] = dateNow; + patch["endDateOnSchedule"] = dateNow; + } + return await db.provision.update(prov._id, patch); } } ).then( function(prov) { // TERRAFORM APPLY diff --git a/server/routes/api-divvy.js b/server/routes/api-divvy.js index d055bb7..ab00551 100644 --- a/server/routes/api-divvy.js +++ b/server/routes/api-divvy.js @@ -23,7 +23,9 @@ const passport = require('../passport'); * description: Notifications */ router.post('/updates', passport.ensureAuthenticatedAndAdmin, async (req, res, next) => { - const now = new Date().toISOString(); + + const dateNow = new Date(); + const now = dateNow.toISOString(); try { let event = req.body; @@ -50,8 +52,25 @@ router.post('/updates', passport.ensureAuthenticatedAndAdmin, async (req, res, n if ( provision.statusVms === 'Stopped' ) { console.log(`DivvyCloud (${now})# provision (${id}) - VMs were already Stopped!`); } else { + let timeRunning = db.utils.getNewTimeRunning(provision); - await db.provision.update(id, {"statusVms": "Stopped", "timeRunning": timeRunning, "stoppedFrom": new Date(), "pendingNextAction": undefined}); + let patch = { + "statusVms": "Stopped", + "timeRunning": timeRunning, + "stoppedFrom": dateNow, + "pendingNextAction": undefined + }; + + if ( provision.schedule && !provision.schedule.is24x7 ) { + patch["endDateOnSchedule"] = dateNow; + + //This is temporary, only to make sure there is value + if ( !patch["startDateOnSchedule"] ) { + patch["startDateOnSchedule"] = dateNow; + } + } + + await db.provision.update(id, patch); console.log(`DivvyCloud (${now})# provision (${id}) - VMs changed to Stopped!`); db.event.add({ user: provision.user._id, provision: provision._id, type: 'vms.stop-schedule', message: `Count extend: ${provision.countExtend}. OnScheduleRenew: ${provision.schedule.onScheduleRenewed}. TimeRunning so far: ${provision.timeRunning} mins` }); } @@ -61,7 +80,21 @@ router.post('/updates', passport.ensureAuthenticatedAndAdmin, async (req, res, n if ( provision.statusVms === 'Running' ) { console.log(`DivvyCloud (${now})# provision (${id}) - VMs were already Running!`); } else { - await db.provision.update(id, {"statusVms": "Running", "runningFrom": new Date(), "pendingNextAction": undefined}); + let patch = { + "statusVms": "Running", + "runningFrom": dateNow, + "pendingNextAction": undefined + }; + + // This is temporary, only to make sure there are values soon + if ( provision.schedule && !provision.schedule.is24x7 ) { + if ( !provision["startDateOnSchedule"] ) { + patch["startDateOnSchedule"] = dateNow; + patch["endDateOnSchedule"] = dateNow; + } + } + + await db.provision.update(id, patch); console.log(`DivvyCloud (${now})# provision (${id}) - VMs changed to Running!`); db.event.add({ user: provision.user._id, provision: provision._id, type: 'vms.start-schedule', message: `Count extend: ${provision.countExtend}. OnScheduleRenew: ${provision.schedule.onScheduleRenewed}. TimeRunning so far: ${provision.timeRunning} mins` });