Skip to content

Commit

Permalink
chore: improve omni queue metrics query
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Aug 26, 2024
1 parent 5c18a1f commit 053d325
Showing 1 changed file with 50 additions and 29 deletions.
79 changes: 50 additions & 29 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,20 +125,34 @@ 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',
},
};
Expand All @@ -145,8 +168,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 +183,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 +208,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

0 comments on commit 053d325

Please sign in to comment.