Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve omni queue metrics query #33159

Merged
merged 8 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading