Skip to content

Commit

Permalink
feat: remove n+1 query knowledge (#3238)
Browse files Browse the repository at this point in the history
# Description

closes #3237
  • Loading branch information
AmineDiro authored Sep 23, 2024
1 parent ad87641 commit 5e48594
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
6 changes: 4 additions & 2 deletions backend/api/quivr_api/modules/knowledge/entity/knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ class KnowledgeDB(AsyncAttrs, SQLModel, table=True):
)

# TODO: nested folder search
async def to_dto(self, get_children: bool = True) -> Knowledge:
async def to_dto(
self, get_children: bool = True, get_parent: bool = True
) -> Knowledge:
assert (
self.updated_at
), "knowledge should be inserted before transforming to dto"
Expand All @@ -126,7 +128,7 @@ async def to_dto(self, get_children: bool = True) -> Knowledge:
children: list[KnowledgeDB] = (
await self.awaitable_attrs.children if get_children else []
)
parent = await self.awaitable_attrs.parent
parent = await self.awaitable_attrs.parent if get_parent else None
parent = await parent.to_dto(get_children=False) if parent else None

return Knowledge(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,15 @@ async def get_knowledge_by_id(
raise KnowledgeNotFoundException("Knowledge not found")
return knowledge

async def get_brain_by_id(self, brain_id: UUID) -> Brain:
async def get_brain_by_id(
self, brain_id: UUID, get_knowledge: bool = False
) -> Brain:
query = select(Brain).where(Brain.brain_id == brain_id)
if get_knowledge:
query = query.options(
joinedload(Brain.knowledges).joinedload(KnowledgeDB.brains)
)

result = await self.session.exec(query)
brain = result.first()
if not brain:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,12 @@ async def insert_knowledge_brain(
return inserted_knowledge

async def get_all_knowledge_in_brain(self, brain_id: UUID) -> List[Knowledge]:
brain = await self.repository.get_brain_by_id(brain_id)

all_knowledges = await brain.awaitable_attrs.knowledges
knowledges = [await knowledge.to_dto() for knowledge in all_knowledges]
brain = await self.repository.get_brain_by_id(brain_id, get_knowledge=True)
all_knowledges: List[KnowledgeDB] = await brain.awaitable_attrs.knowledges
knowledges = [
await knowledge.to_dto(get_children=False, get_parent=False)
for knowledge in all_knowledges
]

return knowledges

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ async def test_knowledge_remove_folder_cascade(
await session.commit()

statement = select(KnowledgeDB)
results = (await session.exec(statement)).all()
results = (await session.exec(statement)).unique().all()
assert results == []


Expand Down

0 comments on commit 5e48594

Please sign in to comment.