Skip to content
Snippets Groups Projects
Unverified Commit 43494436 authored by Rodrigo Nascimento's avatar Rodrigo Nascimento Committed by GitHub
Browse files

fix: Performance issue on Engagement Dashboard aggregation (#30022)

parent 856c2355
No related branches found
No related tags found
No related merge requests found
---
'@rocket.chat/meteor': patch
---
Fix performance issue on Engagement Dashboard aggregation
...@@ -223,10 +223,32 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel { ...@@ -223,10 +223,32 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel {
getTotalOfMessagesSentByDate({ start, end, options = {} }: { start: Date; end: Date; options?: PaginatedRequest }): Promise<any[]> { getTotalOfMessagesSentByDate({ start, end, options = {} }: { start: Date; end: Date; options?: PaginatedRequest }): Promise<any[]> {
const params: Exclude<Parameters<Collection<IMessage>['aggregate']>[0], undefined> = [ const params: Exclude<Parameters<Collection<IMessage>['aggregate']>[0], undefined> = [
{ $match: { t: { $exists: false }, ts: { $gte: start, $lte: end } } }, { $match: { t: { $exists: false }, ts: { $gte: start, $lte: end } } },
{
$group: {
_id: {
rid: '$rid',
date: {
$dateToString: { format: '%Y%m%d', date: '$ts' },
},
},
messages: { $sum: 1 },
},
},
{
$group: {
_id: '$_id.rid',
data: {
$push: {
date: '$_id.date',
messages: '$messages',
},
},
},
},
{ {
$lookup: { $lookup: {
from: 'rocketchat_room', from: 'rocketchat_room',
localField: 'rid', localField: '_id',
foreignField: '_id', foreignField: '_id',
as: 'room', as: 'room',
}, },
...@@ -237,8 +259,9 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel { ...@@ -237,8 +259,9 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel {
}, },
}, },
{ {
$group: { $project: {
_id: { data: '$data',
room: {
_id: '$room._id', _id: '$room._id',
name: { name: {
$cond: [{ $ifNull: ['$room.fname', false] }, '$room.fname', '$room.name'], $cond: [{ $ifNull: ['$room.fname', false] }, '$room.fname', '$room.name'],
...@@ -247,25 +270,22 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel { ...@@ -247,25 +270,22 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel {
usernames: { usernames: {
$cond: [{ $ifNull: ['$room.usernames', false] }, '$room.usernames', []], $cond: [{ $ifNull: ['$room.usernames', false] }, '$room.usernames', []],
}, },
date: {
$concat: [{ $substr: ['$ts', 0, 4] }, { $substr: ['$ts', 5, 2] }, { $substr: ['$ts', 8, 2] }],
},
}, },
messages: { $sum: 1 }, type: 'messages',
},
},
{
$unwind: {
path: '$data',
}, },
}, },
{ {
$project: { $project: {
_id: 0, _id: 0,
date: '$_id.date', date: '$data.date',
room: { room: 1,
_id: '$_id._id', type: 1,
name: '$_id.name', messages: '$data.messages',
t: '$_id.t',
usernames: '$_id.usernames',
},
type: 'messages',
messages: 1,
}, },
}, },
]; ];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment