Skip to content

Commit

Permalink
chore: improve omni queue metrics query (#33159)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Sep 3, 2024
1 parent 9e7cc89 commit 304bc6d
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-actors-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Improves Omnichannel queue page performance
80 changes: 50 additions & 30 deletions apps/meteor/server/models/raw/LivechatRooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,31 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> implements ILive
options?: { offset?: number; count?: number; sort?: { [k: string]: number } };
}) {
const match: Document = { $match: { t: 'l', open: true, servedBy: { $exists: true } } };
const matchUsers: Document = { $match: {} };

if (departmentId && departmentId !== 'undefined') {
match.$match.departmentId = departmentId;
}
if (agentId) {
matchUsers.$match['user._id'] = agentId;
}
if (!includeOfflineAgents) {
matchUsers.$match['user.status'] = { $ne: 'offline' };
matchUsers.$match['user.statusLivechat'] = { $eq: 'available' };
}

const departmentsLookup = {
$lookup: {
from: 'rocketchat_livechat_department',
localField: 'departmentId',
foreignField: '_id',
let: {
deptId: '$departmentId',
},
pipeline: [
{
$match: {
$expr: {
$eq: ['$_id', '$$deptId'],
},
},
},
{
$project: {
name: 1,
},
},
],
as: 'departments',
},
};
Expand All @@ -116,27 +125,40 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> implements ILive
preserveNullAndEmptyArrays: true,
},
};
const departmentsGroup = {
$group: {
_id: {
departmentId: '$departmentId',
name: '$departments.name',
room: '$$ROOT',
},
},
};

const usersLookup = {
$lookup: {
from: 'users',
localField: '_id.room.servedBy._id',
foreignField: '_id',
let: {
servedById: '$servedBy._id',
},
pipeline: [
{
$match: {
$expr: {
$eq: ['$_id', '$$servedById'],
},
...(!includeOfflineAgents && {
status: { $ne: 'offline' },
statusLivechat: 'available',
}),
...(agentId && { _id: agentId }),
},
},
{
$project: {
_id: 1,
username: 1,
status: 1,
},
},
],
as: 'user',
},
};
const usersUnwind = {
$unwind: {
path: '$user',
preserveNullAndEmptyArrays: true,
},
};
const usersGroup = {
Expand All @@ -145,8 +167,8 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> implements ILive
userId: '$user._id',
username: '$user.username',
status: '$user.status',
departmentId: '$_id.departmentId',
departmentName: '$_id.name',
departmentId: '$departmentId',
departmentName: '$departments.name',
},
chats: { $sum: 1 },
},
Expand All @@ -160,16 +182,13 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> implements ILive
status: '$_id.status',
},
department: {
_id: { $ifNull: ['$_id.departmentId', null] },
name: { $ifNull: ['$_id.departmentName', null] },
_id: '$_id.departmentId',
name: '$_id.departmentName',
},
chats: 1,
},
};
const firstParams = [match, departmentsLookup, departmentsUnwind, departmentsGroup, usersLookup, usersUnwind];
if (Object.keys(matchUsers.$match)) {
firstParams.push(matchUsers);
}
const firstParams = [match, departmentsLookup, departmentsUnwind, usersLookup, usersUnwind];
const sort: Document = { $sort: options.sort || { chats: -1 } };
const pagination = [sort];

Expand All @@ -188,6 +207,7 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> implements ILive
};

const params = [...firstParams, usersGroup, project, facet];

return this.col.aggregate(params, { readPreference: readSecondaryPreferred(), allowDiskUse: true }).toArray();
}

Expand Down
4 changes: 4 additions & 0 deletions apps/meteor/tests/end-to-end/api/livechat/00-rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2468,6 +2468,10 @@ describe('LIVECHAT - rooms', () => {
await updateSetting('Unread_Count_Omni', 'all_messages');
});

after(async () => {
await deleteDepartment(departmentWithAgent.department._id);
});

it('it should prepare the required data for further tests', async () => {
departmentWithAgent = await createDepartmentWithAnOnlineAgent();
visitor = await createVisitor(departmentWithAgent.department._id);
Expand Down
Loading

0 comments on commit 304bc6d

Please sign in to comment.