From af361a326b0d48fbc8739389ee10cdbbd68d830b Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Mon, 28 Oct 2024 11:35:03 -0300 Subject: [PATCH] chore!: add indexes for servedBy (#33038) --- .changeset/eleven-chefs-grin.md | 13 +++++++++++++ apps/meteor/server/models/raw/LivechatRooms.ts | 13 ++++++------- apps/meteor/server/models/raw/VoipRoom.ts | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 .changeset/eleven-chefs-grin.md diff --git a/.changeset/eleven-chefs-grin.md b/.changeset/eleven-chefs-grin.md new file mode 100644 index 000000000000..1807fb8be478 --- /dev/null +++ b/.changeset/eleven-chefs-grin.md @@ -0,0 +1,13 @@ +--- +'@rocket.chat/meteor': major +--- + +Remove support of filtering by agent's username on the following endpoint: + +- `/v1/livechat/rooms` + +The performance of the endpoint was improved when filtering by: + +- Agent +- Deparment +- Open chats diff --git a/apps/meteor/server/models/raw/LivechatRooms.ts b/apps/meteor/server/models/raw/LivechatRooms.ts index 2d3c3196baa0..303ee16a8dd3 100644 --- a/apps/meteor/server/models/raw/LivechatRooms.ts +++ b/apps/meteor/server/models/raw/LivechatRooms.ts @@ -54,6 +54,7 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive { key: { servedBy: 1 }, sparse: true }, { key: { 'v.token': 1, 'email.thread': 1 }, sparse: true }, { key: { 'v._id': 1 }, sparse: true }, + { key: { 'servedBy._id': 1, 'departmentId': 1, 't': 1, 'open': 1, 'ts': -1 } }, { key: { t: 1, departmentId: 1, closedAt: 1 }, partialFilterExpression: { closedAt: { $exists: true } } }, { key: { source: 1 }, sparse: true }, { key: { departmentAncestors: 1 }, sparse: true }, @@ -755,7 +756,7 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive const match: Document = { $match: { 't': 'l', - 'servedBy.username': { $exists: true }, + 'servedBy._id': { $exists: true }, 'open': true, '$or': [ { @@ -789,7 +790,7 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive const match: Document = { $match: { 't': 'l', - 'servedBy.username': { $exists: true }, + 'servedBy._id': { $exists: true }, 'open': true, 'onHold': { $exists: true, @@ -815,7 +816,7 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive $match: { 't': 'l', 'open': { $exists: false }, - 'servedBy.username': { $exists: true }, + 'servedBy._id': { $exists: true }, 'ts': { $gte: new Date(start) }, 'closedAt': { $lte: new Date(end) }, }, @@ -1072,7 +1073,7 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive 't': 'l', 'ts': { $gte: new Date(start), $lte: new Date(end) }, 'responseBy.lastMessageTs': { $exists: true }, - 'servedBy.ts': { $exists: true }, + 'servedBy._id': { $exists: true }, }, }; const group = { @@ -1276,9 +1277,7 @@ export class LivechatRoomsRaw extends BaseRaw implements ILive const query: Filter = { t: 'l', ...extraQuery, - ...(agents && { - $or: [{ 'servedBy._id': { $in: agents } }, { 'servedBy.username': { $in: agents } }], - }), + ...(agents && { 'servedBy._id': { $in: agents } }), ...(roomName && { fname: new RegExp(escapeRegExp(roomName), 'i') }), ...(departmentId && departmentId !== 'undefined' && { departmentId }), ...(open !== undefined && { open: { $exists: open }, onHold: { $ne: true } }), diff --git a/apps/meteor/server/models/raw/VoipRoom.ts b/apps/meteor/server/models/raw/VoipRoom.ts index 6ac629d807b7..34b66d4d77b5 100644 --- a/apps/meteor/server/models/raw/VoipRoom.ts +++ b/apps/meteor/server/models/raw/VoipRoom.ts @@ -137,7 +137,7 @@ export class VoipRoomRaw extends BaseRaw implements IVoipRoomModel { const query: Filter = { t: 'v', ...(visitorId && visitorId !== 'undefined' && { 'v._id': visitorId }), - ...(agents && { $or: [{ 'servedBy._id': { $in: agents } }, { 'servedBy.username': { $in: agents } }] }), + ...(agents && { 'servedBy._id': { $in: agents } }), }; if (open !== undefined) {