From 2e14f69fc1c0dd519b8a8bc7ac9814f7172621d5 Mon Sep 17 00:00:00 2001 From: Wade Womersley <155439365+wadedvsa@users.noreply.github.com> Date: Wed, 6 Mar 2024 09:40:10 +0000 Subject: [PATCH] fix: Slow join on documents to messages. (#95) * fix: Slow join on documents to messages. * chore: Typing --- .../Messaging/Conversations/ByLicence.php | 9 +++------ .../Messaging/Conversations/ByOrganisation.php | 2 +- .../Messaging/Message/ByConversation.php | 2 +- module/Api/src/Domain/Repository/Message.php | 14 +++++++++++--- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/module/Api/src/Domain/QueryHandler/Messaging/Conversations/ByLicence.php b/module/Api/src/Domain/QueryHandler/Messaging/Conversations/ByLicence.php index 58ffd77875..707804f851 100644 --- a/module/Api/src/Domain/QueryHandler/Messaging/Conversations/ByLicence.php +++ b/module/Api/src/Domain/QueryHandler/Messaging/Conversations/ByLicence.php @@ -32,7 +32,7 @@ public function handleQuery(QueryInterface $query) $unreadMessageCount = $this->getUnreadMessageCountForUser($value); $conversations[$key]['userContextUnreadCount'] = $unreadMessageCount; $conversations[$key]['userContextStatus'] = $this->stringifyMessageStatusForUser($value, $unreadMessageCount); - $conversations[$key]['latestMessage'] = $this->getLatestMessageMetadata($value['id']); + $conversations[$key]['latestMessage'] = $this->getLatestMessageMetadata((int)$value['id']); } $conversations = $this->orderResultPrioritisingNewMessages($conversations); @@ -51,17 +51,14 @@ private function getUnreadMessageCountForUser($conversation): int return count($results); } - private function getLatestMessageMetadata($conversationId): array + private function getLatestMessageMetadata(int $conversationId): array { $messageRepository = $this->getRepo('Message'); - assert($messageRepository instanceof MessageRepo); return $messageRepository->getLastMessageByConversationId($conversationId); } private function getRepository(): ConversationRepo { - $conversationRepository = $this->getRepo('Conversation'); - assert($conversationRepository instanceof ConversationRepo); - return $conversationRepository; + return $this->getRepo('Conversation'); } } diff --git a/module/Api/src/Domain/QueryHandler/Messaging/Conversations/ByOrganisation.php b/module/Api/src/Domain/QueryHandler/Messaging/Conversations/ByOrganisation.php index 8cfa33efed..fceff94383 100644 --- a/module/Api/src/Domain/QueryHandler/Messaging/Conversations/ByOrganisation.php +++ b/module/Api/src/Domain/QueryHandler/Messaging/Conversations/ByOrganisation.php @@ -34,7 +34,7 @@ public function handleQuery(QueryInterface $query): array ); $conversations[$key]['userContextUnreadCount'] = $unreadMessageCount; $conversations[$key]['userContextStatus'] = $this->stringifyMessageStatusForUser($value, $unreadMessageCount); - $conversations[$key]['latestMessage'] = $messageRepository->getLastMessageByConversationId($value['id']); + $conversations[$key]['latestMessage'] = $messageRepository->getLastMessageByConversationId((int)$value['id']); } $conversations = $this->orderResultPrioritisingNewMessages($conversations); diff --git a/module/Api/src/Domain/QueryHandler/Messaging/Message/ByConversation.php b/module/Api/src/Domain/QueryHandler/Messaging/Message/ByConversation.php index 1e60d7d590..103d4b2ee9 100644 --- a/module/Api/src/Domain/QueryHandler/Messaging/Message/ByConversation.php +++ b/module/Api/src/Domain/QueryHandler/Messaging/Message/ByConversation.php @@ -35,7 +35,7 @@ public function handleQuery(QueryInterface $query) $messageQueryBuilder = $messageRepository->getBaseMessageListWithContentQuery($query); - $messagesQuery = $messageRepository->filterByConversationId($messageQueryBuilder, $query->getConversation()); + $messagesQuery = $messageRepository->filterByConversationId($messageQueryBuilder, (int)$query->getConversation()); $messages = $messageRepository->fetchPaginatedList($messagesQuery); diff --git a/module/Api/src/Domain/Repository/Message.php b/module/Api/src/Domain/Repository/Message.php index 45304ca205..721ba8b1f2 100644 --- a/module/Api/src/Domain/Repository/Message.php +++ b/module/Api/src/Domain/Repository/Message.php @@ -2,6 +2,7 @@ namespace Dvsa\Olcs\Api\Domain\Repository; +use Doctrine\DBAL\ParameterType; use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; use Dvsa\Olcs\Api\Entity\Messaging\MessagingMessage as Entity; @@ -30,13 +31,20 @@ public function getBaseMessageListWithContentQuery(QueryInterface $query): Query $this->getQueryBuilder()->modifyQuery($qb) ->with('messagingContent') - ->with('documents') ->withCreatedByWithTeam(); + $qb->leftJoin( + $this->alias . '.documents', + 'd', + Query\Expr\Join::WITH, + $this->alias . '.messagingConversation = d.messagingConversation' + ); + $qb->addSelect('d'); + return $qb; } - public function filterByConversationId(QueryBuilder $qb, $conversationId): QueryBuilder + public function filterByConversationId(QueryBuilder $qb, int $conversationId): QueryBuilder { $qb ->andWhere($qb->expr()->eq($this->alias . '.messagingConversation', ':messagingConversation')) @@ -45,7 +53,7 @@ public function filterByConversationId(QueryBuilder $qb, $conversationId): Query return $qb; } - public function getLastMessageByConversationId($conversationId): array + public function getLastMessageByConversationId(int $conversationId): array { $qb = $this->createQueryBuilder(); $qb = $this->filterByConversationId($qb, $conversationId);