Prefetch related agent when retrieving the conversation for performance improvements

This commit is contained in:
sabaimran 2024-07-19 14:43:30 +05:30
parent 2ab8fb78b1
commit 7426a4f819

View file

@ -681,18 +681,33 @@ class ConversationAdapters:
user: KhojUser, client_application: ClientApplication = None, conversation_id: int = None, title: str = None user: KhojUser, client_application: ClientApplication = None, conversation_id: int = None, title: str = None
) -> Optional[Conversation]: ) -> Optional[Conversation]:
if conversation_id: if conversation_id:
return await Conversation.objects.filter(user=user, client=client_application, id=conversation_id).afirst() return (
await Conversation.objects.filter(user=user, client=client_application, id=conversation_id)
.prefetch_related("agent")
.afirst()
)
elif title: elif title:
return await Conversation.objects.filter(user=user, client=client_application, title=title).afirst() return (
await Conversation.objects.filter(user=user, client=client_application, title=title)
.prefetch_related("agent")
.afirst()
)
else: else:
conversation = Conversation.objects.filter(user=user, client=client_application).order_by("-updated_at") conversation = (
Conversation.objects.filter(user=user, client=client_application)
.prefetch_related("agent")
.order_by("-updated_at")
)
if await conversation.aexists(): if await conversation.aexists():
return await conversation.prefetch_related("agent").afirst() return await conversation.prefetch_related("agent").afirst()
return await ( return await (
Conversation.objects.filter(user=user, client=client_application).order_by("-updated_at").afirst() Conversation.objects.filter(user=user, client=client_application)
) or await Conversation.objects.acreate(user=user, client=client_application) .prefetch_related("agent")
.order_by("-updated_at")
.afirst()
) or await Conversation.objects.prefetch_related("agent").acreate(user=user, client=client_application)
@staticmethod @staticmethod
async def adelete_conversation_by_user( async def adelete_conversation_by_user(