From f0c211e7718d0fb1dfc14e72515ceb9c1f944765 Mon Sep 17 00:00:00 2001 From: pajowu Date: Sun, 19 Nov 2023 23:58:53 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Reduce=20number=20of=20sql?= =?UTF-8?q?=20queries=20in=20task=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/openapi-schema.yml | 9 +++------ backend/transcribee_backend/auth.py | 5 ++++- .../transcribee_backend/routers/document.py | 6 +++++- backend/transcribee_backend/routers/task.py | 19 ++++++++++--------- backend/transcribee_backend/routers/user.py | 9 +++------ frontend/src/openapi-schema.ts | 6 +++--- 6 files changed, 28 insertions(+), 26 deletions(-) diff --git a/backend/openapi-schema.yml b/backend/openapi-schema.yml index d3456c8c..ba1d443b 100644 --- a/backend/openapi-schema.yml +++ b/backend/openapi-schema.yml @@ -1251,8 +1251,7 @@ paths: '200': content: application/json: - schema: - $ref: '#/components/schemas/AssignedTaskResponse' + schema: {} description: Successful Response '422': content: @@ -1289,8 +1288,7 @@ paths: '200': content: application/json: - schema: - $ref: '#/components/schemas/AssignedTaskResponse' + schema: {} description: Successful Response '422': content: @@ -1327,8 +1325,7 @@ paths: '200': content: application/json: - schema: - $ref: '#/components/schemas/AssignedTaskResponse' + schema: {} description: Successful Response '422': content: diff --git a/backend/transcribee_backend/auth.py b/backend/transcribee_backend/auth.py index d352acd3..10d86b4f 100644 --- a/backend/transcribee_backend/auth.py +++ b/backend/transcribee_backend/auth.py @@ -8,6 +8,7 @@ from typing import Optional, Tuple from fastapi import Depends, Header, HTTPException +from sqlalchemy.orm import joinedload from sqlmodel import Session, col, or_, select from transcribee_backend.db import get_session @@ -157,7 +158,9 @@ def get_authorized_task( session: Session = Depends(get_session), authorized_worker: Worker = Depends(get_authorized_worker), ): - statement = select(Task).where(Task.id == task_id) + statement = ( + select(Task).where(Task.id == task_id).options(joinedload(Task.current_attempt)) + ) task = session.exec(statement).one_or_none() if task is None: raise HTTPException(status_code=404) diff --git a/backend/transcribee_backend/routers/document.py b/backend/transcribee_backend/routers/document.py index 6211ad55..3cccd898 100644 --- a/backend/transcribee_backend/routers/document.py +++ b/backend/transcribee_backend/routers/document.py @@ -462,7 +462,11 @@ def get_document_tasks( auth: AuthInfo = Depends(get_doc_min_readonly_auth), session: Session = Depends(get_session), ) -> List[TaskResponse]: - statement = select(Task).where(Task.document_id == auth.document.id) + statement = ( + select(Task) + .where(Task.document_id == auth.document.id) + .options(selectinload(Task.dependency_links)) + ) return [TaskResponse.from_orm(x) for x in session.exec(statement)] diff --git a/backend/transcribee_backend/routers/task.py b/backend/transcribee_backend/routers/task.py index f07a62ef..5b7c8619 100644 --- a/backend/transcribee_backend/routers/task.py +++ b/backend/transcribee_backend/routers/task.py @@ -3,7 +3,7 @@ from fastapi import APIRouter, Body, Depends, Query from fastapi.exceptions import HTTPException -from sqlalchemy.orm import aliased +from sqlalchemy.orm import aliased, selectinload from sqlalchemy.sql.operators import is_ from sqlmodel import Session, col, select from transcribee_proto.api import KeepaliveBody @@ -104,7 +104,7 @@ def keepalive( keepalive_data: KeepaliveBody = Body(), session: Session = Depends(get_session), task: Task = Depends(get_authorized_task), -) -> Optional[AssignedTaskResponse]: +): # mostly to please the type checker, get_authorized_task already ensures # that the task has a current attempt if task.current_attempt is None: @@ -115,7 +115,6 @@ def keepalive( session.add(task.current_attempt) session.add(task) session.commit() - return AssignedTaskResponse.from_orm(task) @task_router.post("/{task_id}/mark_completed/") @@ -124,11 +123,10 @@ def mark_completed( session: Session = Depends(get_session), task: Task = Depends(get_authorized_task), now: datetime.datetime = Depends(now_tz_aware), -) -> Optional[AssignedTaskResponse]: +): finish_current_attempt( session=session, task=task, now=now, extra_data=extra_data, successful=True ) - return AssignedTaskResponse.from_orm(task) @task_router.post("/{task_id}/mark_failed/") @@ -137,21 +135,24 @@ def mark_failed( session: Session = Depends(get_session), task: Task = Depends(get_authorized_task), now: datetime.datetime = Depends(now_tz_aware), -) -> Optional[AssignedTaskResponse]: +): now = now_tz_aware() finish_current_attempt( session=session, task=task, now=now, extra_data=extra_data, successful=False ) - return AssignedTaskResponse.from_orm(task) - @task_router.get("/") def list_tasks( session: Session = Depends(get_session), token: UserToken = Depends(get_user_token), ) -> List[TaskResponse]: - statement = select(Task).join(Document).where(Document.user == token.user) + statement = ( + select(Task) + .join(Document) + .where(Document.user == token.user) + .options(selectinload(Task.dependency_links)) + ) results = session.exec(statement) return [TaskResponse.from_orm(x) for x in results] diff --git a/backend/transcribee_backend/routers/user.py b/backend/transcribee_backend/routers/user.py index 75fe90df..4d89d140 100644 --- a/backend/transcribee_backend/routers/user.py +++ b/backend/transcribee_backend/routers/user.py @@ -1,5 +1,5 @@ from fastapi import APIRouter, Depends, HTTPException -from sqlmodel import Session, delete, select +from sqlmodel import Session, delete from transcribee_proto.api import LoginResponse from transcribee_backend.auth import ( @@ -12,7 +12,7 @@ ) from transcribee_backend.db import get_session from transcribee_backend.exceptions import UserAlreadyExists -from transcribee_backend.models import CreateUser, User, UserBase, UserToken +from transcribee_backend.models import CreateUser, UserBase, UserToken from transcribee_backend.models.user import ChangePasswordRequest user_router = APIRouter() @@ -57,11 +57,8 @@ def logout( @user_router.get("/me/") def read_user( token: UserToken = Depends(get_user_token), - session: Session = Depends(get_session), ) -> UserBase: - statement = select(User).where(User.id == token.user_id) - user = session.exec(statement).one() - return UserBase(username=user.username) + return UserBase(username=token.user.username) @user_router.post("/change_password/") diff --git a/frontend/src/openapi-schema.ts b/frontend/src/openapi-schema.ts index 71cdac75..c60662f9 100644 --- a/frontend/src/openapi-schema.ts +++ b/frontend/src/openapi-schema.ts @@ -1014,7 +1014,7 @@ export interface operations { /** @description Successful Response */ 200: { content: { - "application/json": components["schemas"]["AssignedTaskResponse"]; + "application/json": unknown; }; }; /** @description Validation Error */ @@ -1044,7 +1044,7 @@ export interface operations { /** @description Successful Response */ 200: { content: { - "application/json": components["schemas"]["AssignedTaskResponse"]; + "application/json": unknown; }; }; /** @description Validation Error */ @@ -1074,7 +1074,7 @@ export interface operations { /** @description Successful Response */ 200: { content: { - "application/json": components["schemas"]["AssignedTaskResponse"]; + "application/json": unknown; }; }; /** @description Validation Error */