From 5ff513f7809f81618ca72b7e9f0e5fbb289b049c Mon Sep 17 00:00:00 2001 From: yousufislam191 Date: Tue, 21 May 2024 00:59:14 +0600 Subject: [PATCH] optimized get all user query for server load handaling --- server/controller/user.controllers.js | 72 ++++++++---- server/data.js | 156 +++++++++++++------------- 2 files changed, 126 insertions(+), 102 deletions(-) diff --git a/server/controller/user.controllers.js b/server/controller/user.controllers.js index 9568f35..b1a3a7e 100644 --- a/server/controller/user.controllers.js +++ b/server/controller/user.controllers.js @@ -339,38 +339,65 @@ const getAllUsersWithTaskStatusCounts = async (req, res, next) => { const currentUserId = req.user.id; let message; + // Fetch all users except the current user in one query const users = await User.findAll({ where: { id: { [Op.not]: currentUserId, }, }, - exclude: ["password"], + attributes: { exclude: ["password"] }, }); - if (!users || users.length === 0) message = "No user found..."; + if (!users || users.length === 0) { + message = "No user found..."; + return successResponse(res, { + statusCode: 200, + message: message, + payload: [], + }); + } + + // Fetch tasks and failed tasks for all users in one batch query + const userIds = users.map((user) => user.id); + + const tasks = await Task.findAll({ + where: { + createdToTask: { + [Op.in]: userIds, + }, + }, + attributes: ["createdToTask", "status"], + }); + + const failedTasks = await FailedTask.findAll({ + where: { + createdToTask: { + [Op.in]: userIds, + }, + }, + attributes: ["createdToTask", "status"], + }); + + // Combine tasks and failedTasks into one array + const allTasks = [...tasks, ...failedTasks]; // Initialize an empty array to store the final response const response = []; - // Loop through each user to retrieve their associated tasks and status counts - for (const user of users) { - // Perform a JOIN operation between User and Task tables on 'createdByTask' and 'createdToTask' - const tasks = await Task.findAll({ - where: { createdToTask: user.id }, - attributes: ["status"], - }); - - // Perform a JOIN operation between User and failedTask tables on 'createdByTask' and 'createdToTask' - const failedTasks = await FailedTask.findAll({ - where: { createdToTask: user.id }, - attributes: ["status"], - }); - // Combine tasks and failedTasks into one array - const allTasks = [...tasks, ...failedTasks]; + // Aggregate tasks and failedTasks by user + const tasksByUser = {}; + allTasks.forEach((task) => { + if (!tasksByUser[task.createdToTask]) { + tasksByUser[task.createdToTask] = []; + } + tasksByUser[task.createdToTask].push(task); + }); - // Aggregate status counts - const statusCounts = allTasks.reduce((acc, task) => { + // Prepare response data + users.forEach((user) => { + const userTasks = tasksByUser[user.id] || []; + const statusCounts = userTasks.reduce((acc, task) => { const status = task.status; if (!acc[status]) { acc[status] = 1; @@ -380,7 +407,6 @@ const getAllUsersWithTaskStatusCounts = async (req, res, next) => { return acc; }, {}); - // Prepare user data with status counts const userData = { id: user.id, name: user.name, @@ -394,12 +420,10 @@ const getAllUsersWithTaskStatusCounts = async (req, res, next) => { })), }; - // Add the user data to the response array response.push(userData); - } + }); - if (response && response.length > 0) - message = "Users with task status counts were retrieved successfully"; + message = "Users with task status counts were retrieved successfully"; return successResponse(res, { statusCode: 200, diff --git a/server/data.js b/server/data.js index 95d46eb..7af71c8 100644 --- a/server/data.js +++ b/server/data.js @@ -2,198 +2,198 @@ const data = { users: [ { name: "John Doe", - email: "user28@gmail.com", - password: "2qwer1!@T", + email: "user1@gmail.com", + password: "1qasw2!@Q", }, { name: "Jane Smith", - email: "user28@gmail.com", - password: "3poiu4!@U", + email: "user2@gmail.com", + password: "1qasw2!@Q", }, { name: "Alice Johnson", - email: "user28@gmail.com", - password: "4zxcv5!@V", + email: "user3@gmail.com", + password: "1qasw2!@Q", }, { name: "Bob Williams", - email: "user28@gmail.com", - password: "5asdf6!@W", + email: "user4@gmail.com", + password: "1qasw2!@Q", }, { name: "Emily Davis", - email: "user28@gmail.com", - password: "6hjkl7!@X", + email: "user5@gmail.com", + password: "1qasw2!@Q", }, { name: "Michael Wilson", - email: "user28@gmail.com", - password: "7bnmn8!@Y", + email: "user6@gmail.com", + password: "1qasw2!@Q", }, { name: "Sophia Brown", - email: "user28@gmail.com", - password: "8qwerty9!@Z", + email: "user7@gmail.com", + password: "1qasw2!@Q", }, { name: "William Lee", - email: "user28@gmail.com", - password: "9uiop0!@P", + email: "user8@gmail.com", + password: "1qasw2!@Q", }, { name: "Olivia Martinez", - email: "user280@gmail.com", - password: "0lkjh9!@O", + email: "user9@gmail.com", + password: "1qasw2!@Q", }, { name: "James Taylor", - email: "user281@gmail.com", - password: "1poiuy8!@I", + email: "user10@gmail.com", + password: "1qasw2!@Q", }, { name: "Grace Anderson", - email: "user282@gmail.com", - password: "2mnbvc7!@U", + email: "user11@gmail.com", + password: "1qasw2!@Q", }, { name: "Daniel Hernandez", - email: "user283@gmail.com", - password: "3zxcvb6!@Y", + email: "user12@gmail.com", + password: "1qasw2!@Q", }, { name: "Mia Gonzalez", - email: "user284@gmail.com", - password: "4asdfg5!@T", + email: "user13@gmail.com", + password: "1qasw2!@Q", }, { name: "Liam Perez", - email: "user285@gmail.com", - password: "5qwert4!@R", + email: "user14@gmail.com", + password: "1qasw2!@Q", }, { name: "Ava Turner", - email: "user286@gmail.com", - password: "6hjkl3!@E", + email: "user15@gmail.com", + password: "1qasw2!@Q", }, { name: "Ethan Flores", - email: "user287@gmail.com", - password: "7bnm12!@W", + email: "user16@gmail.com", + password: "1qasw2!@Q", }, { name: "Lily Smith", - email: "user288@gmail.com", - password: "8uiop1!@Q", + email: "user17@gmail.com", + password: "1qasw2!@Q", }, { name: "Noah Davis", - email: "user289@gmail.com", - password: "9lkj0!@A", + email: "user18@gmail.com", + password: "1qasw2!@Q", }, { name: "Emma Johnson", - email: "user280@gmail.com", - password: "0zxcv9!@S", + email: "user19@gmail.com", + password: "1qasw2!@Q", }, { name: "Sophie Turner", - email: "user281@gmail.com", - password: "1rtyu2!@W", + email: "user20@gmail.com", + password: "1qasw2!@Q", }, { name: "Jacob Anderson", - email: "user282@gmail.com", - password: "2bnmf3!@E", + email: "user21@gmail.com", + password: "1qasw2!@Q", }, { name: "Mia Brown", - email: "user283@gmail.com", - password: "3asdf4!@R", + email: "user22@gmail.com", + password: "1qasw2!@Q", }, { name: "Oliver Perez", - email: "user284@gmail.com", - password: "4zxcv5!@T", + email: "user23@gmail.com", + password: "1qasw2!@Q", }, { name: "Ava Wilson", - email: "user285@gmail.com", - password: "5hjkl6!@Y", + email: "user24@gmail.com", + password: "1qasw2!@Q", }, { name: "Noah Davis", - email: "user286@gmail.com", - password: "6poiuy7!@U", + email: "user25@gmail.com", + password: "1qasw2!@Q", }, { name: "Emma Johnson", - email: "user287@gmail.com", - password: "7qwert8!@I", + email: "user26@gmail.com", + password: "1qasw2!@Q", }, { name: "Liam Smith", - email: "user288@gmail.com", - password: "8uiop9!@O", + email: "user27@gmail.com", + password: "1qasw2!@Q", }, { name: "Isabella Martin", - email: "user289@gmail.com", - password: "9lkjh0!@P", + email: "user28@gmail.com", + password: "1qasw2!@Q", }, { name: "James White", - email: "user280@gmail.com", - password: "0mnbv1!@A", + email: "user29@gmail.com", + password: "1qasw2!@Q", }, { name: "Olivia Clark", - email: "user281@gmail.com", - password: "1zxcv2!@S", + email: "user30@gmail.com", + password: "1qasw2!@Q", }, { name: "Ethan Thomas", - email: "user282@gmail.com", - password: "2bnmn3!@D", + email: "user31@gmail.com", + password: "1qasw2!@Q", }, { name: "Charlotte Lewis", - email: "user283@gmail.com", - password: "3yuiop4!@F", + email: "user32@gmail.com", + password: "1qasw2!@Q", }, { name: "William Baker", - email: "user284@gmail.com", - password: "4asdf5!@G", + email: "user33@gmail.com", + password: "1qasw2!@Q", }, { name: "Amelia Adams", - email: "user285@gmail.com", - password: "5hjkl6!@H", + email: "user34@gmail.com", + password: "1qasw2!@Q", }, { name: "Henry Garcia", - email: "user286@gmail.com", - password: "6qwert7!@J", + email: "user35@gmail.com", + password: "1qasw2!@Q", }, { name: "Sophia King", - email: "user287@gmail.com", - password: "7zxcv8!@K", + email: "user36@gmail.com", + password: "1qasw2!@Q", }, { name: "Benjamin Parker", - email: "user288@gmail.com", - password: "8mnbv9!@L", + email: "user37@gmail.com", + password: "1qasw2!@Q", }, { name: "Ava Young", - email: "user289@gmail.com", - password: "9poiuy0!@Z", + email: "user38@gmail.com", + password: "1qasw2!@Q", }, { name: "James Wright", - email: "user280@gmail.com", - password: "0lkjh1!@X", + email: "user39@gmail.com", + password: "1qasw2!@Q", }, ], };