Skip to content

Commit

Permalink
Merge pull request #1103 from OneCommunityGlobal/huijie-recalculation…
Browse files Browse the repository at this point in the history
…-tangible-hours-quick-fix-2

Huijie - fix recalculation for tangible hours 2
  • Loading branch information
one-community authored Sep 24, 2024
2 parents 71b5642 + 58ffb1d commit aeeea8d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
69 changes: 52 additions & 17 deletions src/controllers/timeEntryController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const moment = require('moment-timezone');
const mongoose = require('mongoose');
const { v4: uuidv4 } = require('uuid');
const logger = require('../startup/logger');
const UserProfile = require('../models/userProfile');
const Project = require('../models/project');
Expand Down Expand Up @@ -1367,21 +1368,16 @@ const timeEntrycontroller = function (TimeEntry) {
return newTotalIntangibleHrs;
};

const recalculationTaskQueue = [];

/**
* recalculate the hoursByCatefory for all users and update the field
* recalculate the hoursByCategory for all users and update the field
*/
const recalculateHoursByCategoryAllUsers = async function (req, res) {
const recalculateHoursByCategoryAllUsers = async function (taskId) {
const session = await mongoose.startSession();
session.startTransaction();
let keepAliveInterval;

try {
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Transfer-Encoding', 'chunked');
keepAliveInterval = setInterval(() => {
res.write('Processing... keep connection alive\n');
}, 150 * 1000); // interval of 150 seconds

const userprofiles = await UserProfile.find({}, '_id').lean();

const recalculationPromises = userprofiles.map(async (userprofile) => {
Expand All @@ -1392,22 +1388,60 @@ const timeEntrycontroller = function (TimeEntry) {
await Promise.all(recalculationPromises);

await session.commitTransaction();
clearInterval(keepAliveInterval);
res.write('finished the recalculation for hoursByCategory for all users\n');
return res.end();

const recalculationTask = recalculationTaskQueue.find((task) => task.taskId === taskId);
if (recalculationTask) {
recalculationTask.status = 'Completed';
recalculationTask.completionTime = new Date().toISOString();
}
} catch (err) {
await session.abortTransaction();
if (keepAliveInterval) {
clearInterval(keepAliveInterval);
const recalculationTask = recalculationTaskQueue.find((task) => task.taskId === taskId);
if (recalculationTask) {
recalculationTask.status = 'Failed';
recalculationTask.completionTime = new Date().toISOString();
}

logger.logException(err);
res.write(`error: ${err.toString()}\n`);
return res.end();
} finally {
session.endSession();
}
};

const startRecalculation = async function (req, res) {
const taskId = uuidv4();
recalculationTaskQueue.push({
taskId,
status: 'In progress',
startTime: new Date().toISOString(),
completionTime: null,
});
if (recalculationTaskQueue.length > 10) {
recalculationTaskQueue.shift();
}

res.status(200).send({
message: 'The recalculation task started in the background',
taskId,
});

setTimeout(() => recalculateHoursByCategoryAllUsers(taskId), 0);
};

const checkRecalculationStatus = async function (req, res) {
const { taskId } = req.params;
const recalculationTask = recalculationTaskQueue.find((task) => task.taskId === taskId);
if (recalculationTask) {
res.status(200).send({
status: recalculationTask.status,
startTime: recalculationTask.startTime,
completionTime: recalculationTask.completionTime,
});
} else {
res.status(404).send({ message: 'Task not found' });
}
};

/**
* recalculate the totalIntangibleHrs for all users and update the field
*/
Expand Down Expand Up @@ -1452,9 +1486,10 @@ const timeEntrycontroller = function (TimeEntry) {
getLostTimeEntriesForTeamList,
backupHoursByCategoryAllUsers,
backupIntangibleHrsAllUsers,
recalculateHoursByCategoryAllUsers,
recalculateIntangibleHrsAllUsers,
getTimeEntriesForReports,
startRecalculation,
checkRecalculationStatus,
};
};

Expand Down
4 changes: 3 additions & 1 deletion src/routes/timeentryRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ const routes = function (TimeEntry) {
);

TimeEntryRouter.route('/TimeEntry/recalculateHoursAllUsers/tangible').post(
controller.recalculateHoursByCategoryAllUsers,
controller.startRecalculation,
);

TimeEntryRouter.route('/TimeEntry/checkStatus/:taskId').get(controller.checkRecalculationStatus);

TimeEntryRouter.route('/TimeEntry/recalculateHoursAllUsers/intangible').post(
controller.recalculateIntangibleHrsAllUsers,
);
Expand Down

0 comments on commit aeeea8d

Please sign in to comment.