Skip to content

Commit 0dccb98

Browse files
authored
Merge pull request #498 from uhh-lt/improve-tags-codes
Query Performance Improvements
2 parents 5f7ab40 + 2690661 commit 0dccb98

File tree

229 files changed

+4113
-6884
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

229 files changed

+4113
-6884
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ repos:
2222
- id: pyright
2323
name: "Pyright"
2424
types: [python]
25-
entry: bash -c 'ENV_NAME=dats source backend/_activate_current_env.sh && pyright $@'
26-
language: system
25+
entry: ./bin/run-pyright.sh
26+
language: script
2727
- repo: https://github.com/pre-commit/mirrors-eslint
2828
rev: v9.11.0
2929
hooks:

backend/src/api/dependencies.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ async def skip_limit_params(
4242
return result
4343

4444

45-
async def resolve_code_param(
46-
resolve: bool = Query(
47-
title="Resolve Code",
48-
description="If true, the code_id of the"
49-
" SpanAnnotation gets resolved and replaced"
50-
" by the respective Code entity",
51-
default=True,
52-
),
53-
) -> bool:
54-
return resolve
55-
56-
5745
async def get_db_session() -> AsyncGenerator[Session, None]:
5846
session = SQLService().session_maker()
5947
try:

backend/src/api/endpoints/bbox_annotation.py

Lines changed: 12 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1-
from typing import List, Union
1+
from typing import List
22

33
from fastapi import APIRouter, Depends
44
from sqlalchemy.orm import Session
55

66
from api.dependencies import (
77
get_current_user,
88
get_db_session,
9-
resolve_code_param,
109
)
11-
from api.util import get_object_memo_for_user, get_object_memos
1210
from app.core.authorization.authz_user import AuthzUser
1311
from app.core.data.crud import Crud
1412
from app.core.data.crud.bbox_annotation import crud_bbox_anno
1513
from app.core.data.dto.bbox_annotation import (
1614
BBoxAnnotationCreate,
1715
BBoxAnnotationRead,
18-
BBoxAnnotationReadResolved,
1916
BBoxAnnotationUpdate,
2017
)
2118
from app.core.data.dto.code import CodeRead
22-
from app.core.data.dto.memo import (
23-
MemoRead,
24-
)
2519

2620
router = APIRouter(
2721
prefix="/bbox", dependencies=[Depends(get_current_user)], tags=["bboxAnnotation"]
@@ -30,81 +24,69 @@
3024

3125
@router.put(
3226
"",
33-
response_model=Union[BBoxAnnotationRead, BBoxAnnotationReadResolved],
27+
response_model=BBoxAnnotationRead,
3428
summary="Creates a BBoxAnnotation",
3529
)
3630
def add_bbox_annotation(
3731
*,
3832
db: Session = Depends(get_db_session),
3933
bbox: BBoxAnnotationCreate,
40-
resolve_code: bool = Depends(resolve_code_param),
4134
authz_user: AuthzUser = Depends(),
42-
) -> Union[BBoxAnnotationRead, BBoxAnnotationReadResolved]:
35+
) -> BBoxAnnotationRead:
4336
authz_user.assert_in_same_project_as(Crud.SOURCE_DOCUMENT, bbox.sdoc_id)
4437
authz_user.assert_in_same_project_as(Crud.CODE, bbox.code_id)
4538

4639
db_obj = crud_bbox_anno.create(db=db, user_id=authz_user.user.id, create_dto=bbox)
47-
if resolve_code:
48-
return BBoxAnnotationReadResolved.model_validate(db_obj)
49-
else:
50-
return BBoxAnnotationRead.model_validate(db_obj)
40+
return BBoxAnnotationRead.model_validate(db_obj)
5141

5242

5343
@router.get(
5444
"/{bbox_id}",
55-
response_model=Union[BBoxAnnotationRead, BBoxAnnotationReadResolved],
45+
response_model=BBoxAnnotationRead,
5646
summary="Returns the BBoxAnnotation with the given ID.",
5747
)
5848
def get_by_id(
5949
*,
6050
db: Session = Depends(get_db_session),
6151
bbox_id: int,
62-
resolve_code: bool = Depends(resolve_code_param),
6352
authz_user: AuthzUser = Depends(),
64-
) -> Union[BBoxAnnotationRead, BBoxAnnotationReadResolved]:
53+
) -> BBoxAnnotationRead:
6554
authz_user.assert_in_same_project_as(Crud.BBOX_ANNOTATION, bbox_id)
6655

6756
db_obj = crud_bbox_anno.read(db=db, id=bbox_id)
68-
if resolve_code:
69-
return BBoxAnnotationReadResolved.model_validate(db_obj)
70-
else:
71-
return BBoxAnnotationRead.model_validate(db_obj)
57+
return BBoxAnnotationRead.model_validate(db_obj)
7258

7359

7460
@router.patch(
7561
"/{bbox_id}",
76-
response_model=Union[BBoxAnnotationRead, BBoxAnnotationReadResolved],
62+
response_model=BBoxAnnotationRead,
7763
summary="Updates the BBoxAnnotation with the given ID.",
7864
)
7965
def update_by_id(
8066
*,
8167
db: Session = Depends(get_db_session),
8268
bbox_id: int,
8369
bbox_anno: BBoxAnnotationUpdate,
84-
resolve_code: bool = Depends(resolve_code_param),
8570
authz_user: AuthzUser = Depends(),
86-
) -> Union[BBoxAnnotationRead, BBoxAnnotationReadResolved]:
71+
) -> BBoxAnnotationRead:
8772
authz_user.assert_in_same_project_as(Crud.BBOX_ANNOTATION, bbox_id)
8873
authz_user.assert_in_same_project_as(Crud.CODE, bbox_anno.code_id)
8974

9075
db_obj = crud_bbox_anno.update(db=db, id=bbox_id, update_dto=bbox_anno)
91-
if resolve_code:
92-
return BBoxAnnotationReadResolved.model_validate(db_obj)
93-
else:
94-
return BBoxAnnotationRead.model_validate(db_obj)
76+
return BBoxAnnotationRead.model_validate(db_obj)
9577

9678

9779
@router.delete(
9880
"/{bbox_id}",
99-
response_model=Union[BBoxAnnotationRead, BBoxAnnotationReadResolved],
81+
response_model=BBoxAnnotationRead,
10082
summary="Deletes the BBoxAnnotation with the given ID.",
10183
)
10284
def delete_by_id(
10385
*,
10486
db: Session = Depends(get_db_session),
10587
bbox_id: int,
10688
authz_user: AuthzUser = Depends(),
107-
) -> Union[BBoxAnnotationRead, BBoxAnnotationReadResolved]:
89+
) -> BBoxAnnotationRead:
10890
authz_user.assert_in_same_project_as(Crud.BBOX_ANNOTATION, bbox_id)
10991

11092
db_obj = crud_bbox_anno.remove(db=db, id=bbox_id)
@@ -128,43 +110,6 @@ def get_code(
128110
return CodeRead.model_validate(bbox_db_obj.code)
129111

130112

131-
@router.get(
132-
"/{bbox_id}/memo",
133-
response_model=List[MemoRead],
134-
summary="Returns the Memos attached to the BBoxAnnotation with the given ID if it exists.",
135-
)
136-
def get_memos(
137-
*,
138-
db: Session = Depends(get_db_session),
139-
bbox_id: int,
140-
authz_user: AuthzUser = Depends(),
141-
) -> List[MemoRead]:
142-
authz_user.assert_in_same_project_as(Crud.BBOX_ANNOTATION, bbox_id)
143-
144-
db_obj = crud_bbox_anno.read(db=db, id=bbox_id)
145-
# TODO how to authorize memo access here?
146-
return get_object_memos(db_obj=db_obj)
147-
148-
149-
@router.get(
150-
"/{bbox_id}/memo/user",
151-
response_model=MemoRead,
152-
summary=(
153-
"Returns the Memo attached to the BBoxAnnotation with the given ID of the logged-in User if it exists."
154-
),
155-
)
156-
def get_user_memo(
157-
*,
158-
db: Session = Depends(get_db_session),
159-
bbox_id: int,
160-
authz_user: AuthzUser = Depends(),
161-
) -> MemoRead:
162-
authz_user.assert_in_same_project_as(Crud.BBOX_ANNOTATION, bbox_id)
163-
164-
db_obj = crud_bbox_anno.read(db=db, id=bbox_id)
165-
return get_object_memo_for_user(db_obj=db_obj, user_id=authz_user.user.id)
166-
167-
168113
@router.get(
169114
"/code/{code_id}/user",
170115
response_model=List[BBoxAnnotationRead],

backend/src/api/endpoints/code.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
from typing import List
2-
31
from fastapi import APIRouter, Depends
42
from sqlalchemy.orm import Session
53

64
from api.dependencies import get_current_user, get_db_session
7-
from api.util import get_object_memo_for_user, get_object_memos
85
from app.core.authorization.authz_user import AuthzUser
96
from app.core.data.crud import Crud
107
from app.core.data.crud.code import crud_code
118
from app.core.data.dto.code import CodeCreate, CodeRead, CodeUpdate
12-
from app.core.data.dto.memo import (
13-
MemoRead,
14-
)
159

1610
router = APIRouter(
1711
prefix="/code", dependencies=[Depends(get_current_user)], tags=["code"]
@@ -87,39 +81,3 @@ def delete_by_id(
8781

8882
db_obj = crud_code.remove(db=db, id=code_id)
8983
return CodeRead.model_validate(db_obj)
90-
91-
92-
@router.get(
93-
"/{code_id}/memo",
94-
response_model=List[MemoRead],
95-
summary="Returns the Memo attached to the Code with the given ID if it exists.",
96-
)
97-
def get_memos(
98-
*,
99-
db: Session = Depends(get_db_session),
100-
code_id: int,
101-
authz_user: AuthzUser = Depends(),
102-
) -> List[MemoRead]:
103-
authz_user.assert_in_same_project_as(Crud.CODE, code_id)
104-
105-
db_obj = crud_code.read(db=db, id=code_id)
106-
return get_object_memos(db_obj=db_obj)
107-
108-
109-
@router.get(
110-
"/{code_id}/memo/user",
111-
response_model=MemoRead,
112-
summary=(
113-
"Returns the Memo attached to the Code with the given ID of the logged-in User if it exists."
114-
),
115-
)
116-
def get_user_memo(
117-
*,
118-
db: Session = Depends(get_db_session),
119-
code_id: int,
120-
authz_user: AuthzUser = Depends(),
121-
) -> MemoRead:
122-
authz_user.assert_in_same_project_as(Crud.CODE, code_id)
123-
124-
db_obj = crud_code.read(db=db, id=code_id)
125-
return get_object_memo_for_user(db_obj=db_obj, user_id=authz_user.user.id)

backend/src/api/endpoints/document_tag.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from sqlalchemy.orm import Session
55

66
from api.dependencies import get_current_user, get_db_session
7-
from api.util import get_object_memo_for_user, get_object_memos
87
from api.validation import Validate
98
from app.core.authorization.authz_user import AuthzUser
109
from app.core.data.crud import Crud
@@ -16,9 +15,6 @@
1615
SourceDocumentDocumentTagLinks,
1716
SourceDocumentDocumentTagMultiLink,
1817
)
19-
from app.core.data.dto.memo import (
20-
MemoRead,
21-
)
2218

2319
router = APIRouter(
2420
prefix="/doctag", dependencies=[Depends(get_current_user)], tags=["documentTag"]
@@ -228,42 +224,6 @@ def delete_by_id(
228224
return DocumentTagRead.model_validate(db_obj)
229225

230226

231-
@router.get(
232-
"/{tag_id}/memo",
233-
response_model=List[MemoRead],
234-
summary="Returns the Memos attached to the DocumentTag with the given ID if it exists.",
235-
)
236-
def get_memos(
237-
*,
238-
db: Session = Depends(get_db_session),
239-
tag_id: int,
240-
authz_user: AuthzUser = Depends(),
241-
) -> List[MemoRead]:
242-
authz_user.assert_in_same_project_as(Crud.DOCUMENT_TAG, tag_id)
243-
244-
db_obj = crud_document_tag.read(db=db, id=tag_id)
245-
return get_object_memos(db_obj=db_obj)
246-
247-
248-
@router.get(
249-
"/{tag_id}/memo/user",
250-
response_model=MemoRead,
251-
summary=(
252-
"Returns the Memo attached to the document tag with the given ID of the logged-in User if it exists."
253-
),
254-
)
255-
def get_user_memo(
256-
*,
257-
db: Session = Depends(get_db_session),
258-
tag_id: int,
259-
authz_user: AuthzUser = Depends(),
260-
) -> MemoRead:
261-
authz_user.assert_in_same_project_as(Crud.DOCUMENT_TAG, tag_id)
262-
263-
db_obj = crud_document_tag.read(db=db, id=tag_id)
264-
return get_object_memo_for_user(db_obj=db_obj, user_id=authz_user.user.id)
265-
266-
267227
@router.get(
268228
"/{tag_id}/sdocs",
269229
response_model=List[int],

0 commit comments

Comments
 (0)