Skip to content

Commit

Permalink
⚡️ Reduce number of sql queries in task api
Browse files Browse the repository at this point in the history
  • Loading branch information
pajowu committed Nov 20, 2023
1 parent a554f6e commit 9335359
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
9 changes: 3 additions & 6 deletions backend/openapi-schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1251,8 +1251,7 @@ paths:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/AssignedTaskResponse'
schema: {}
description: Successful Response
'422':
content:
Expand Down Expand Up @@ -1289,8 +1288,7 @@ paths:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/AssignedTaskResponse'
schema: {}
description: Successful Response
'422':
content:
Expand Down Expand Up @@ -1327,8 +1325,7 @@ paths:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/AssignedTaskResponse'
schema: {}
description: Successful Response
'422':
content:
Expand Down
5 changes: 4 additions & 1 deletion backend/transcribee_backend/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion backend/transcribee_backend/routers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]


Expand Down
19 changes: 10 additions & 9 deletions backend/transcribee_backend/routers/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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/")
Expand All @@ -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/")
Expand All @@ -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]
9 changes: 3 additions & 6 deletions backend/transcribee_backend/routers/user.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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()
Expand Down Expand Up @@ -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/")
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/openapi-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ export interface operations {
/** @description Successful Response */
200: {
content: {
"application/json": components["schemas"]["AssignedTaskResponse"];
"application/json": unknown;
};
};
/** @description Validation Error */
Expand Down Expand Up @@ -1044,7 +1044,7 @@ export interface operations {
/** @description Successful Response */
200: {
content: {
"application/json": components["schemas"]["AssignedTaskResponse"];
"application/json": unknown;
};
};
/** @description Validation Error */
Expand Down Expand Up @@ -1074,7 +1074,7 @@ export interface operations {
/** @description Successful Response */
200: {
content: {
"application/json": components["schemas"]["AssignedTaskResponse"];
"application/json": unknown;
};
};
/** @description Validation Error */
Expand Down

0 comments on commit 9335359

Please sign in to comment.