From 0e30b8af9bed4d5d56e643c6959098c946a021ac Mon Sep 17 00:00:00 2001 From: Tim Fischer Date: Thu, 26 Sep 2024 15:14:45 +0000 Subject: [PATCH 1/4] remove CurrentCode everywhere --- backend/src/api/dependencies.py | 2 +- backend/src/api/endpoints/bbox_annotation.py | 15 ++-- backend/src/api/endpoints/code.py | 18 ----- backend/src/api/endpoints/span_annotation.py | 17 ++-- .../src/app/core/analysis/analysis_service.py | 78 ++++++------------- .../src/app/core/analysis/annotated_images.py | 7 +- .../app/core/analysis/annotated_segments.py | 7 +- backend/src/app/core/analysis/timeline.py | 5 +- .../src/app/core/analysis/word_frequency.py | 5 +- backend/src/app/core/data/crud/__init__.py | 2 - .../src/app/core/data/crud/bbox_annotation.py | 47 ++--------- backend/src/app/core/data/crud/code.py | 8 -- .../src/app/core/data/crud/current_code.py | 10 --- .../src/app/core/data/crud/object_handle.py | 6 +- .../src/app/core/data/crud/span_annotation.py | 63 ++------------- .../src/app/core/data/dto/bbox_annotation.py | 15 +--- backend/src/app/core/data/dto/current_code.py | 26 ------- .../src/app/core/data/dto/object_handle.py | 1 - .../src/app/core/data/dto/span_annotation.py | 23 +----- .../llm/prompts/annotation_prompt_builder.py | 2 +- .../src/app/core/data/orm/bbox_annotation.py | 14 +--- backend/src/app/core/data/orm/code.py | 41 +++------- .../src/app/core/data/orm/object_handle.py | 12 +-- .../src/app/core/data/orm/span_annotation.py | 14 +--- backend/src/app/core/data/orm/util.py | 4 +- backend/src/app/core/search/search_service.py | 11 +-- .../steps/image/write_ppid_to_database.py | 4 +- .../steps/text/write_pptd_to_database.py | 4 +- 28 files changed, 94 insertions(+), 367 deletions(-) delete mode 100644 backend/src/app/core/data/crud/current_code.py delete mode 100644 backend/src/app/core/data/dto/current_code.py diff --git a/backend/src/api/dependencies.py b/backend/src/api/dependencies.py index 2961209a9..c39be93dc 100644 --- a/backend/src/api/dependencies.py +++ b/backend/src/api/dependencies.py @@ -45,7 +45,7 @@ async def skip_limit_params( async def resolve_code_param( resolve: bool = Query( title="Resolve Code", - description="If true, the current_code_id of the" + description="If true, the code_id of the" " SpanAnnotation gets resolved and replaced" " by the respective Code entity", default=True, diff --git a/backend/src/api/endpoints/bbox_annotation.py b/backend/src/api/endpoints/bbox_annotation.py index 6599c83e4..69ad0a8b7 100644 --- a/backend/src/api/endpoints/bbox_annotation.py +++ b/backend/src/api/endpoints/bbox_annotation.py @@ -15,10 +15,10 @@ from app.core.data.crud.bbox_annotation import crud_bbox_anno from app.core.data.crud.memo import crud_memo from app.core.data.dto.bbox_annotation import ( - BBoxAnnotationCreateWithCodeId, + BBoxAnnotationCreate, BBoxAnnotationRead, BBoxAnnotationReadResolved, - BBoxAnnotationUpdateWithCodeId, + BBoxAnnotationUpdate, ) from app.core.data.dto.code import CodeRead from app.core.data.dto.memo import AttachedObjectType, MemoCreate, MemoInDB, MemoRead @@ -36,7 +36,7 @@ def add_bbox_annotation( *, db: Session = Depends(get_db_session), - bbox: BBoxAnnotationCreateWithCodeId, + bbox: BBoxAnnotationCreate, resolve_code: bool = Depends(resolve_code_param), authz_user: AuthzUser = Depends(), ) -> Union[BBoxAnnotationRead, BBoxAnnotationReadResolved]: @@ -44,7 +44,7 @@ def add_bbox_annotation( authz_user.assert_in_same_project_as(Crud.SOURCE_DOCUMENT, bbox.sdoc_id) authz_user.assert_in_same_project_as(Crud.CODE, bbox.code_id) - db_obj = crud_bbox_anno.create_with_code_id(db=db, create_dto=bbox) + db_obj = crud_bbox_anno.create(db=db, create_dto=bbox) if resolve_code: return BBoxAnnotationReadResolved.model_validate(db_obj) else: @@ -81,14 +81,14 @@ def update_by_id( *, db: Session = Depends(get_db_session), bbox_id: int, - bbox_anno: BBoxAnnotationUpdateWithCodeId, + bbox_anno: BBoxAnnotationUpdate, resolve_code: bool = Depends(resolve_code_param), authz_user: AuthzUser = Depends(), ) -> Union[BBoxAnnotationRead, BBoxAnnotationReadResolved]: authz_user.assert_in_same_project_as(Crud.BBOX_ANNOTATION, bbox_id) authz_user.assert_in_same_project_as(Crud.CODE, bbox_anno.code_id) - db_obj = crud_bbox_anno.update_with_code_id(db=db, id=bbox_id, update_dto=bbox_anno) + db_obj = crud_bbox_anno.update(db=db, id=bbox_id, update_dto=bbox_anno) if resolve_code: return BBoxAnnotationReadResolved.model_validate(db_obj) else: @@ -126,8 +126,7 @@ def get_code( authz_user.assert_in_same_project_as(Crud.BBOX_ANNOTATION, bbox_id) bbox_db_obj = crud_bbox_anno.read(db=db, id=bbox_id) - - return CodeRead.model_validate(bbox_db_obj.current_code.code) + return CodeRead.model_validate(bbox_db_obj.code) @router.put( diff --git a/backend/src/api/endpoints/code.py b/backend/src/api/endpoints/code.py index c44d4f2ac..076c93d54 100644 --- a/backend/src/api/endpoints/code.py +++ b/backend/src/api/endpoints/code.py @@ -9,7 +9,6 @@ from app.core.authorization.authz_user import AuthzUser from app.core.data.crud import Crud from app.core.data.crud.code import crud_code -from app.core.data.crud.current_code import crud_current_code from app.core.data.crud.memo import crud_memo from app.core.data.dto.code import CodeCreate, CodeRead, CodeUpdate from app.core.data.dto.memo import AttachedObjectType, MemoCreate, MemoInDB, MemoRead @@ -39,23 +38,6 @@ def create_new_code( return CodeRead.model_validate(db_code) -@router.get( - "/current/{current_code_id}", - response_model=CodeRead, - summary="Returns the Code linked by the CurrentCode with the given ID.", -) -def get_code_by_current_code_id( - *, - db: Session = Depends(get_db_session), - current_code_id: int, - authz_user: AuthzUser = Depends(), -) -> CodeRead: - authz_user.assert_in_same_project_as(Crud.CURRENT_CODE, current_code_id) - - cc_db_obj = crud_current_code.read(db=db, id=current_code_id) - return CodeRead.model_validate(cc_db_obj.code) - - @router.get( "/{code_id}", response_model=CodeRead, diff --git a/backend/src/api/endpoints/span_annotation.py b/backend/src/api/endpoints/span_annotation.py index 7bed7b4e3..675c7c5cc 100644 --- a/backend/src/api/endpoints/span_annotation.py +++ b/backend/src/api/endpoints/span_annotation.py @@ -13,11 +13,10 @@ from app.core.data.dto.code import CodeRead from app.core.data.dto.memo import AttachedObjectType, MemoCreate, MemoInDB, MemoRead from app.core.data.dto.span_annotation import ( - SpanAnnotationCreateBulkWithCodeId, - SpanAnnotationCreateWithCodeId, + SpanAnnotationCreate, SpanAnnotationRead, SpanAnnotationReadResolved, - SpanAnnotationUpdateWithCodeId, + SpanAnnotationUpdate, ) from app.core.data.dto.span_group import SpanGroupRead @@ -34,7 +33,7 @@ def add_span_annotation( *, db: Session = Depends(get_db_session), - span: SpanAnnotationCreateWithCodeId, + span: SpanAnnotationCreate, resolve_code: bool = Depends(resolve_code_param), authz_user: AuthzUser = Depends(), validate: Validate = Depends(), @@ -49,7 +48,7 @@ def add_span_annotation( ] ) - db_obj = crud_span_anno.create_with_code_id(db=db, create_dto=span) + db_obj = crud_span_anno.create(db=db, create_dto=span) if resolve_code: return SpanAnnotationReadResolved.model_validate(db_obj) else: @@ -64,7 +63,7 @@ def add_span_annotation( def add_span_annotations_bulk( *, db: Session = Depends(get_db_session), - spans: List[SpanAnnotationCreateBulkWithCodeId], + spans: List[SpanAnnotationCreate], resolve_code: bool = Depends(resolve_code_param), authz_user: AuthzUser = Depends(), validate: Validate = Depends(), @@ -116,7 +115,7 @@ def update_by_id( *, db: Session = Depends(get_db_session), span_id: int, - span_anno: SpanAnnotationUpdateWithCodeId, + span_anno: SpanAnnotationUpdate, resolve_code: bool = Depends(resolve_code_param), authz_user: AuthzUser = Depends(), validate: Validate = Depends(), @@ -127,7 +126,7 @@ def update_by_id( [(Crud.SPAN_ANNOTATION, span_id), (Crud.CODE, span_anno.code_id)] ) - db_obj = crud_span_anno.update_with_code_id(db=db, id=span_id, update_dto=span_anno) + db_obj = crud_span_anno.update(db=db, id=span_id, update_dto=span_anno) if resolve_code: return SpanAnnotationReadResolved.model_validate(db_obj) else: @@ -165,7 +164,7 @@ def get_code( authz_user.assert_in_same_project_as(Crud.SPAN_ANNOTATION, span_id) span_db_obj = crud_span_anno.read(db=db, id=span_id) - return CodeRead.model_validate(span_db_obj.current_code.code) + return CodeRead.model_validate(span_db_obj.code) @router.get( diff --git a/backend/src/app/core/analysis/analysis_service.py b/backend/src/app/core/analysis/analysis_service.py index e3c88b8c9..f0a8719b5 100644 --- a/backend/src/app/core/analysis/analysis_service.py +++ b/backend/src/app/core/analysis/analysis_service.py @@ -18,7 +18,7 @@ from app.core.data.dto.span_annotation import SpanAnnotationRead from app.core.data.orm.annotation_document import AnnotationDocumentORM from app.core.data.orm.bbox_annotation import BBoxAnnotationORM -from app.core.data.orm.code import CodeORM, CurrentCodeORM +from app.core.data.orm.code import CodeORM from app.core.data.orm.document_tag import DocumentTagORM from app.core.data.orm.source_document import SourceDocumentORM from app.core.data.orm.source_document_metadata import SourceDocumentMetadataORM @@ -71,48 +71,32 @@ def compute_code_frequency( # 2. query all span annotation occurrences of the codes of interest codes_of_interest = [code_id for group in result for code_id in group] - query = ( - db.query( - CurrentCodeORM.code_id, - SpanAnnotationORM.id, - ) - .join( - SpanAnnotationORM, - SpanAnnotationORM.current_code_id == CurrentCodeORM.id, - ) - .join( - AnnotationDocumentORM, - AnnotationDocumentORM.id - == SpanAnnotationORM.annotation_document_id, - ) + query = db.query( + SpanAnnotationORM.code_id, + SpanAnnotationORM.id, + ).join( + AnnotationDocumentORM, + AnnotationDocumentORM.id == SpanAnnotationORM.annotation_document_id, ) # noinspection PyUnresolvedReferences query = query.filter( AnnotationDocumentORM.user_id.in_(user_ids), - CurrentCodeORM.code_id.in_(codes_of_interest), + SpanAnnotationORM.code_id.in_(codes_of_interest), ) span_res = query.all() # 3. query all bbox annotation occurrences of the codes of interest - query = ( - db.query( - CurrentCodeORM.code_id, - BBoxAnnotationORM.id, - ) - .join( - BBoxAnnotationORM, - BBoxAnnotationORM.current_code_id == CurrentCodeORM.id, - ) - .join( - AnnotationDocumentORM, - AnnotationDocumentORM.id - == BBoxAnnotationORM.annotation_document_id, - ) + query = db.query( + BBoxAnnotationORM.code_id, + BBoxAnnotationORM.id, + ).join( + AnnotationDocumentORM, + AnnotationDocumentORM.id == BBoxAnnotationORM.annotation_document_id, ) # noinspection PyUnresolvedReferences query = query.filter( AnnotationDocumentORM.user_id.in_(user_ids), - CurrentCodeORM.code_id.in_(codes_of_interest), + BBoxAnnotationORM.code_id.in_(codes_of_interest), ) bbox_res = query.all() @@ -147,11 +131,7 @@ def find_code_occurrences( SpanAnnotationORM.annotation_document_id == AnnotationDocumentORM.id, ) - .join( - CurrentCodeORM, - CurrentCodeORM.id == SpanAnnotationORM.current_code_id, - ) - .join(CodeORM, CodeORM.id == CurrentCodeORM.code_id) + .join(CodeORM, CodeORM.id == SpanAnnotationORM.code_id) .join(SpanTextORM, SpanTextORM.id == SpanAnnotationORM.span_text_id) ) # noinspection PyUnresolvedReferences @@ -159,7 +139,7 @@ def find_code_occurrences( and_( SourceDocumentORM.project_id == project_id, AnnotationDocumentORM.user_id.in_(user_ids), - CurrentCodeORM.code_id == code_id, + CodeORM.id == code_id, ) ) query = query.group_by(SourceDocumentORM, CodeORM, SpanTextORM.text) @@ -191,18 +171,14 @@ def find_code_occurrences( BBoxAnnotationORM.annotation_document_id == AnnotationDocumentORM.id, ) - .join( - CurrentCodeORM, - CurrentCodeORM.id == BBoxAnnotationORM.current_code_id, - ) - .join(CodeORM, CodeORM.id == CurrentCodeORM.code_id) + .join(CodeORM, CodeORM.id == BBoxAnnotationORM.code_id) ) # noinspection PyUnresolvedReferences query = query.filter( and_( SourceDocumentORM.project_id == project_id, AnnotationDocumentORM.user_id.in_(user_ids), - CurrentCodeORM.code_id == code_id, + CodeORM.id == code_id, ) ) query = query.group_by( @@ -244,11 +220,7 @@ def find_annotation_occurrences( SpanAnnotationORM.annotation_document_id == AnnotationDocumentORM.id, ) - .join( - CurrentCodeORM, - CurrentCodeORM.id == SpanAnnotationORM.current_code_id, - ) - .join(CodeORM, CodeORM.id == CurrentCodeORM.code_id) + .join(CodeORM, CodeORM.id == SpanAnnotationORM.code_id) .join(SpanTextORM, SpanTextORM.id == SpanAnnotationORM.span_text_id) ) # noinspection PyUnresolvedReferences @@ -256,7 +228,7 @@ def find_annotation_occurrences( and_( SourceDocumentORM.project_id == project_id, AnnotationDocumentORM.user_id.in_(user_ids), - CurrentCodeORM.code_id == code_id, + CodeORM.id == code_id, ) ) res = query.all() @@ -286,18 +258,14 @@ def find_annotation_occurrences( BBoxAnnotationORM.annotation_document_id == AnnotationDocumentORM.id, ) - .join( - CurrentCodeORM, - CurrentCodeORM.id == BBoxAnnotationORM.current_code_id, - ) - .join(CodeORM, CodeORM.id == CurrentCodeORM.code_id) + .join(CodeORM, CodeORM.id == BBoxAnnotationORM.code_id) ) # noinspection PyUnresolvedReferences query = query.filter( and_( SourceDocumentORM.project_id == project_id, AnnotationDocumentORM.user_id.in_(user_ids), - CurrentCodeORM.code_id == code_id, + CodeORM.id == code_id, ) ) res = query.all() diff --git a/backend/src/app/core/analysis/annotated_images.py b/backend/src/app/core/analysis/annotated_images.py index d32d56427..738101993 100644 --- a/backend/src/app/core/analysis/annotated_images.py +++ b/backend/src/app/core/analysis/annotated_images.py @@ -15,7 +15,7 @@ from app.core.data.dto.source_document import SourceDocumentRead from app.core.data.orm.annotation_document import AnnotationDocumentORM from app.core.data.orm.bbox_annotation import BBoxAnnotationORM -from app.core.data.orm.code import CodeORM, CurrentCodeORM +from app.core.data.orm.code import CodeORM from app.core.data.orm.document_tag import DocumentTagORM from app.core.data.orm.memo import MemoORM from app.core.data.orm.object_handle import ObjectHandleORM @@ -168,8 +168,7 @@ def find_annotated_images( # join Source Document with Document Tag .join(SourceDocumentORM.document_tags, isouter=True) # join BBox Annotation with Code - .join(BBoxAnnotationORM.current_code) - .join(CurrentCodeORM.code) + .join(BBoxAnnotationORM.code) # join BBox Annotation with my Memo .join( memo_subquery, @@ -227,7 +226,7 @@ def find_annotated_images( webp=True, thumbnail=False, ), - code=CodeRead.model_validate(row[0].current_code.code), + code=CodeRead.model_validate(row[0].code), user_id=row[0].annotation_document.user_id, sdoc=SourceDocumentRead.model_validate( row[0].annotation_document.source_document diff --git a/backend/src/app/core/analysis/annotated_segments.py b/backend/src/app/core/analysis/annotated_segments.py index 3f7af9de6..0c8cb8af4 100644 --- a/backend/src/app/core/analysis/annotated_segments.py +++ b/backend/src/app/core/analysis/annotated_segments.py @@ -14,7 +14,7 @@ from app.core.data.dto.document_tag import DocumentTagRead from app.core.data.dto.source_document import SourceDocumentRead from app.core.data.orm.annotation_document import AnnotationDocumentORM -from app.core.data.orm.code import CodeORM, CurrentCodeORM +from app.core.data.orm.code import CodeORM from app.core.data.orm.document_tag import DocumentTagORM from app.core.data.orm.memo import MemoORM from app.core.data.orm.object_handle import ObjectHandleORM @@ -178,8 +178,7 @@ def find_annotated_segments( # join Source Document with Document Tag .join(SourceDocumentORM.document_tags, isouter=True) # join Span Annotation with Code - .join(SpanAnnotationORM.current_code) - .join(CurrentCodeORM.code) + .join(SpanAnnotationORM.code) # join Span Annotation with Text .join(SpanAnnotationORM.span_text) # join Span Annotation with my Memo @@ -229,7 +228,7 @@ def find_annotated_segments( AnnotationTableRow( id=row[0].id, span_text=row[0].span_text.text, - code=CodeRead.model_validate(row[0].current_code.code), + code=CodeRead.model_validate(row[0].code), user_id=row[0].annotation_document.user_id, sdoc=SourceDocumentRead.model_validate( row[0].annotation_document.source_document diff --git a/backend/src/app/core/analysis/timeline.py b/backend/src/app/core/analysis/timeline.py index 9a76d56f3..ede9d1a67 100644 --- a/backend/src/app/core/analysis/timeline.py +++ b/backend/src/app/core/analysis/timeline.py @@ -9,7 +9,7 @@ from app.core.data.doc_type import DocType from app.core.data.dto.analysis import DateGroupBy, TimelineAnalysisResult from app.core.data.orm.annotation_document import AnnotationDocumentORM -from app.core.data.orm.code import CodeORM, CurrentCodeORM +from app.core.data.orm.code import CodeORM from app.core.data.orm.document_tag import DocumentTagORM from app.core.data.orm.source_document import SourceDocumentORM from app.core.data.orm.source_document_metadata import SourceDocumentMetadataORM @@ -149,8 +149,7 @@ def timeline_analysis( .join(AnnotationDocumentORM.user) .join(AnnotationDocumentORM.span_annotations) .join(SpanAnnotationORM.span_text) - .join(SpanAnnotationORM.current_code) - .join(CurrentCodeORM.code) + .join(SpanAnnotationORM.code) .join(SourceDocumentORM.metadata_) .filter( SourceDocumentORM.project_id == project_id, diff --git a/backend/src/app/core/analysis/word_frequency.py b/backend/src/app/core/analysis/word_frequency.py index dad9c7001..712d9f26f 100644 --- a/backend/src/app/core/analysis/word_frequency.py +++ b/backend/src/app/core/analysis/word_frequency.py @@ -7,7 +7,7 @@ from app.core.data.doc_type import DocType from app.core.data.dto.analysis import WordFrequencyResult, WordFrequencyStat from app.core.data.orm.annotation_document import AnnotationDocumentORM -from app.core.data.orm.code import CodeORM, CurrentCodeORM +from app.core.data.orm.code import CodeORM from app.core.data.orm.document_tag import DocumentTagORM from app.core.data.orm.source_document import SourceDocumentORM from app.core.data.orm.span_annotation import SpanAnnotationORM @@ -205,8 +205,7 @@ def word_frequency( .join(AnnotationDocumentORM.user) .join(AnnotationDocumentORM.span_annotations) .join(SpanAnnotationORM.span_text) - .join(SpanAnnotationORM.current_code) - .join(CurrentCodeORM.code) + .join(SpanAnnotationORM.code) .join(SourceDocumentORM.metadata_) .filter( SourceDocumentORM.project_id == project_id, diff --git a/backend/src/app/core/data/crud/__init__.py b/backend/src/app/core/data/crud/__init__.py index 81c175185..35048ae5b 100644 --- a/backend/src/app/core/data/crud/__init__.py +++ b/backend/src/app/core/data/crud/__init__.py @@ -6,7 +6,6 @@ from app.core.data.crud.bbox_annotation import crud_bbox_anno from app.core.data.crud.code import crud_code from app.core.data.crud.concept_over_time_analysis import crud_cota -from app.core.data.crud.current_code import crud_current_code from app.core.data.crud.document_tag import crud_document_tag from app.core.data.crud.memo import crud_memo from app.core.data.crud.object_handle import crud_object_handle @@ -32,7 +31,6 @@ class Crud(Enum): ANNOTATION_DOCUMENT = crud_adoc BBOX_ANNOTATION = crud_bbox_anno CODE = crud_code - CURRENT_CODE = crud_current_code DOCUMENT_TAG = crud_document_tag MEMO = crud_memo OBJECT_HANDLE = crud_object_handle diff --git a/backend/src/app/core/data/crud/bbox_annotation.py b/backend/src/app/core/data/crud/bbox_annotation.py index 5340c3aea..f63713909 100644 --- a/backend/src/app/core/data/crud/bbox_annotation.py +++ b/backend/src/app/core/data/crud/bbox_annotation.py @@ -9,14 +9,11 @@ from app.core.data.dto.bbox_annotation import ( BBoxAnnotationCreate, BBoxAnnotationCreateIntern, - BBoxAnnotationCreateWithCodeId, BBoxAnnotationReadResolved, BBoxAnnotationUpdate, - BBoxAnnotationUpdateWithCodeId, ) from app.core.data.orm.annotation_document import AnnotationDocumentORM from app.core.data.orm.bbox_annotation import BBoxAnnotationORM -from app.core.data.orm.code import CodeORM, CurrentCodeORM class CRUDBBoxAnnotation( @@ -38,7 +35,7 @@ def create( x_max=create_dto.x_max, y_min=create_dto.y_min, y_max=create_dto.y_max, - current_code_id=create_dto.current_code_id, + code_id=create_dto.code_id, annotation_document_id=adoc.id, ), ) @@ -48,26 +45,6 @@ def create( return db_obj - def create_with_code_id( - self, db: Session, *, create_dto: BBoxAnnotationCreateWithCodeId - ) -> BBoxAnnotationORM: - from app.core.data.crud.code import crud_code - - db_code = crud_code.read(db=db, id=create_dto.code_id) - ccid = db_code.current_code.id - - create_dto_with_ccid = BBoxAnnotationCreate( - x_min=create_dto.x_min, - x_max=create_dto.x_max, - y_min=create_dto.y_min, - y_max=create_dto.y_max, - current_code_id=ccid, - user_id=create_dto.user_id, - sdoc_id=create_dto.sdoc_id, - ) - - return self.create(db=db, create_dto=create_dto_with_ccid) - def read_by_user_and_sdoc( self, db: Session, @@ -109,10 +86,10 @@ def read_by_code_and_user( ) -> List[BBoxAnnotationORM]: query = ( db.query(self.model) - .join(AnnotationDocumentORM) - .join(CurrentCodeORM) - .join(CodeORM) - .filter(CodeORM.id == code_id, AnnotationDocumentORM.user_id == user_id) + .join(self.model.annotation_document) + .filter( + self.model.code_id == code_id, AnnotationDocumentORM.user_id == user_id + ) ) return query.all() @@ -126,20 +103,6 @@ def update( return bbox_anno - def update_with_code_id( - self, db: Session, *, id: int, update_dto: BBoxAnnotationUpdateWithCodeId - ) -> BBoxAnnotationORM: - from app.core.data.crud.code import crud_code - - db_code = crud_code.read(db=db, id=update_dto.code_id) - ccid = db_code.current_code.id - - update_dto_with_ccid = BBoxAnnotationUpdate( - current_code_id=ccid, - ) - - return self.update(db=db, id=id, update_dto=update_dto_with_ccid) - def remove(self, db: Session, *, id: int) -> Optional[BBoxAnnotationORM]: bbox_anno = super().remove(db, id=id) # update the annotation document's timestamp diff --git a/backend/src/app/core/data/crud/code.py b/backend/src/app/core/data/crud/code.py index 560ada2b0..d686f7786 100644 --- a/backend/src/app/core/data/crud/code.py +++ b/backend/src/app/core/data/crud/code.py @@ -5,11 +5,9 @@ from sqlalchemy.orm import Session from app.core.data.crud.crud_base import CRUDBase -from app.core.data.crud.current_code import crud_current_code from app.core.data.crud.user import SYSTEM_USER_ID from app.core.data.dto.action import ActionType from app.core.data.dto.code import CodeCreate, CodeRead, CodeUpdate -from app.core.data.dto.current_code import CurrentCodeCreate from app.core.data.orm.code import CodeORM from app.util.color import get_next_color from config import conf @@ -27,12 +25,6 @@ def create(self, db: Session, *, create_dto: CodeCreate) -> CodeORM: db.add(db_obj) db.commit() - # second create a CurrentCode that links to the code - ccc = CurrentCodeCreate(code_id=db_obj.id) - crud_current_code.create(db=db, create_dto=ccc) - - db.refresh(db_obj) - # create the action manually since we are not using the crud base create after_state = self._get_action_state_from_orm(db_obj=db_obj) self._create_action( diff --git a/backend/src/app/core/data/crud/current_code.py b/backend/src/app/core/data/crud/current_code.py deleted file mode 100644 index 6270066e5..000000000 --- a/backend/src/app/core/data/crud/current_code.py +++ /dev/null @@ -1,10 +0,0 @@ -from app.core.data.crud.crud_base import CRUDBase -from app.core.data.dto.current_code import CurrentCodeCreate, CurrentCodeUpdate -from app.core.data.orm.code import CurrentCodeORM - - -class CRUDCurrentCode(CRUDBase[CurrentCodeORM, CurrentCodeCreate, CurrentCodeUpdate]): - pass - - -crud_current_code = CRUDCurrentCode(CurrentCodeORM) diff --git a/backend/src/app/core/data/crud/object_handle.py b/backend/src/app/core/data/crud/object_handle.py index 64077a0f2..7efc6c874 100644 --- a/backend/src/app/core/data/crud/object_handle.py +++ b/backend/src/app/core/data/crud/object_handle.py @@ -8,7 +8,6 @@ from app.core.data.crud.bbox_annotation import crud_bbox_anno from app.core.data.crud.code import crud_code from app.core.data.crud.crud_base import CRUDBase, NoSuchElementError -from app.core.data.crud.current_code import crud_current_code from app.core.data.crud.document_tag import crud_document_tag from app.core.data.crud.memo import crud_memo from app.core.data.crud.project import crud_project @@ -19,7 +18,7 @@ from app.core.data.dto.object_handle import ObjectHandleCreate from app.core.data.orm.action import ActionORM from app.core.data.orm.bbox_annotation import BBoxAnnotationORM -from app.core.data.orm.code import CodeORM, CurrentCodeORM +from app.core.data.orm.code import CodeORM from app.core.data.orm.document_tag import DocumentTagORM from app.core.data.orm.memo import MemoORM from app.core.data.orm.object_handle import ObjectHandleORM @@ -34,7 +33,6 @@ class CRUDObjectHandle(CRUDBase[ObjectHandleORM, ObjectHandleCreate, None]): __obj_id_crud_map = { "code_id": crud_code, - "current_code_id": crud_current_code, "document_tag_id": crud_document_tag, "project_id": crud_project, "source_document_id": crud_sdoc, @@ -48,7 +46,6 @@ class CRUDObjectHandle(CRUDBase[ObjectHandleORM, ObjectHandleCreate, None]): __obj_id_orm_type_map = { "code_id": CodeORM, - "current_code_id": CurrentCodeORM, "document_tag_id": DocumentTagORM, "project_id": ProjectORM, "source_document_id": SourceDocumentORM, @@ -98,7 +95,6 @@ def resolve_handled_object( self, db: Session, handle: ObjectHandleORM ) -> Union[ CodeORM, - CurrentCodeORM, DocumentTagORM, ProjectORM, SourceDocumentORM, diff --git a/backend/src/app/core/data/crud/span_annotation.py b/backend/src/app/core/data/crud/span_annotation.py index bfc225f11..a43134dd7 100644 --- a/backend/src/app/core/data/crud/span_annotation.py +++ b/backend/src/app/core/data/crud/span_annotation.py @@ -1,27 +1,22 @@ -from typing import Dict, List, Optional +from typing import List, Optional import srsly from fastapi.encoders import jsonable_encoder from sqlalchemy.orm import Session from app.core.data.crud.annotation_document import crud_adoc -from app.core.data.crud.code import crud_code from app.core.data.crud.crud_base import CRUDBase from app.core.data.crud.span_group import crud_span_group from app.core.data.crud.span_text import crud_span_text from app.core.data.dto.action import ActionType from app.core.data.dto.span_annotation import ( SpanAnnotationCreate, - SpanAnnotationCreateBulkWithCodeId, SpanAnnotationCreateIntern, - SpanAnnotationCreateWithCodeId, SpanAnnotationReadResolved, SpanAnnotationUpdate, - SpanAnnotationUpdateWithCodeId, ) from app.core.data.dto.span_text import SpanTextCreate from app.core.data.orm.annotation_document import AnnotationDocumentORM -from app.core.data.orm.code import CodeORM, CurrentCodeORM from app.core.data.orm.span_annotation import SpanAnnotationORM @@ -49,7 +44,7 @@ def create( end=create_dto.end, begin_token=create_dto.begin_token, end_token=create_dto.end_token, - current_code_id=create_dto.current_code_id, + code_id=create_dto.code_id, span_text=create_dto.span_text, ).model_dump(exclude={"span_text"}) ) @@ -75,27 +70,6 @@ def create( return db_obj - def create_with_code_id( - self, db: Session, *, create_dto: SpanAnnotationCreateWithCodeId - ) -> SpanAnnotationORM: - from app.core.data.crud.code import crud_code - - db_code = crud_code.read(db=db, id=create_dto.code_id) - ccid = db_code.current_code.id - - create_dto_with_ccid = SpanAnnotationCreate( - begin=create_dto.begin, - end=create_dto.end, - span_text=create_dto.span_text, - begin_token=create_dto.begin_token, - end_token=create_dto.end_token, - current_code_id=ccid, - user_id=create_dto.user_id, - sdoc_id=create_dto.sdoc_id, - ) - - return self.create(db=db, create_dto=create_dto_with_ccid) - def create_multi( self, db: Session, *, create_dtos: List[SpanAnnotationCreateIntern] ) -> List[SpanAnnotationORM]: @@ -131,7 +105,7 @@ def create_multi( return db_objs def create_bulk( - self, db: Session, *, create_dtos: List[SpanAnnotationCreateBulkWithCodeId] + self, db: Session, *, create_dtos: List[SpanAnnotationCreate] ) -> List[SpanAnnotationORM]: # group by user and sdoc_id # identify codes @@ -150,13 +124,6 @@ def create_bulk( db=db, user_id=user_id, sdoc_id=sdoc_id ).id - # find all codes - code_ids = list(set([create_dto.code_id for create_dto in create_dtos])) - db_codes = crud_code.read_by_ids(db=db, ids=code_ids) - cid2ccid: Dict[int, int] = {} - for db_code in db_codes: - cid2ccid[db_code.id] = db_code.current_code.id - # create the annotations return self.create_multi( db=db, @@ -167,7 +134,7 @@ def create_bulk( span_text=create_dto.span_text, begin_token=create_dto.begin_token, end_token=create_dto.end_token, - current_code_id=cid2ccid[create_dto.code_id], + code_id=create_dto.code_id, annotation_document_id=adoc_id_by_user_sdoc[ (create_dto.user_id, create_dto.sdoc_id) ], @@ -217,10 +184,10 @@ def read_by_code_and_user( ) -> List[SpanAnnotationORM]: query = ( db.query(self.model) - .join(AnnotationDocumentORM) - .join(CurrentCodeORM) - .join(CodeORM) - .filter(CodeORM.id == code_id, AnnotationDocumentORM.user_id == user_id) + .join(self.model.annotation_document) + .filter( + self.model.code_id == code_id, AnnotationDocumentORM.user_id == user_id + ) ) return query.all() @@ -235,20 +202,6 @@ def update( return span_anno - def update_with_code_id( - self, db: Session, *, id: int, update_dto: SpanAnnotationUpdateWithCodeId - ) -> SpanAnnotationORM: - from app.core.data.crud.code import crud_code - - db_code = crud_code.read(db=db, id=update_dto.code_id) - ccid = db_code.current_code.id - - update_dto_with_ccid = SpanAnnotationUpdate( - current_code_id=ccid, - ) - - return self.update(db=db, id=id, update_dto=update_dto_with_ccid) - def remove(self, db: Session, *, id: int) -> SpanAnnotationORM: span_anno = super().remove(db, id=id) diff --git a/backend/src/app/core/data/dto/bbox_annotation.py b/backend/src/app/core/data/dto/bbox_annotation.py index 92021c964..c51e9cc5e 100644 --- a/backend/src/app/core/data/dto/bbox_annotation.py +++ b/backend/src/app/core/data/dto/bbox_annotation.py @@ -16,19 +16,13 @@ class BBoxAnnotationBaseDTO(BaseModel): # Properties for creation class BBoxAnnotationCreateIntern(BBoxAnnotationBaseDTO): - current_code_id: int = Field(description="CurrentCode the BBoxAnnotation refers to") + code_id: int = Field(description="Code the BBoxAnnotation refers to") annotation_document_id: int = Field( description="AnnotationDocument the BBoxAnnotation refers to" ) class BBoxAnnotationCreate(BBoxAnnotationBaseDTO): - current_code_id: int = Field(description="CurrentCode the BBoxAnnotation refers to") - user_id: int = Field(description="User that created the BBoxAnnotation") - sdoc_id: int = Field(description="SourceDocument the BBoxAnnotation refers to") - - -class BBoxAnnotationCreateWithCodeId(BBoxAnnotationBaseDTO): code_id: int = Field(description="Code the BBoxAnnotation refers to") user_id: int = Field(description="User that created the BBoxAnnotation") sdoc_id: int = Field(description="SourceDocument the BBoxAnnotation refers to") @@ -36,18 +30,13 @@ class BBoxAnnotationCreateWithCodeId(BBoxAnnotationBaseDTO): # Properties for updating class BBoxAnnotationUpdate(BaseModel, UpdateDTOBase): - current_code_id: int = Field(description="CurrentCode the BBoxAnnotation refers to") - - -# Properties for updating -class BBoxAnnotationUpdateWithCodeId(BaseModel, UpdateDTOBase): code_id: int = Field(description="Code the BBoxAnnotation refers to") # Properties for reading (as in ORM) class BBoxAnnotationRead(BBoxAnnotationBaseDTO): id: int = Field(description="ID of the BBoxAnnotation") - current_code_id: int = Field(description="CurrentCode the BBoxAnnotation refers to") + code_id: int = Field(description="Code the BBoxAnnotation refers to") user_id: int = Field(description="User that created the BBoxAnnotation") sdoc_id: int = Field(description="SourceDocument the BBoxAnnotation refers to") created: datetime = Field(description="Created timestamp of the BBoxAnnotation") diff --git a/backend/src/app/core/data/dto/current_code.py b/backend/src/app/core/data/dto/current_code.py deleted file mode 100644 index 64823a22a..000000000 --- a/backend/src/app/core/data/dto/current_code.py +++ /dev/null @@ -1,26 +0,0 @@ -from typing import Optional - -from pydantic import BaseModel, ConfigDict, Field - -from .dto_base import UpdateDTOBase - - -# Properties shared across all DTOs -class CurrentCodeBaseDTO(BaseModel): - code_id: Optional[int] = Field(description="Code of the CurrentCode", default=None) - - -# Properties for creation -class CurrentCodeCreate(BaseModel): - code_id: int = Field(description="Code of the CurrentCode", default=None) - - -# Properties for updating -class CurrentCodeUpdate(CurrentCodeBaseDTO, UpdateDTOBase): - pass - - -# Properties for reading (as in ORM) -class CurrentCodeRead(CurrentCodeBaseDTO): - id: int = Field(description="ID of the CurrentCode") - model_config = ConfigDict(from_attributes=True) diff --git a/backend/src/app/core/data/dto/object_handle.py b/backend/src/app/core/data/dto/object_handle.py index 68dfcff32..29729e3bb 100644 --- a/backend/src/app/core/data/dto/object_handle.py +++ b/backend/src/app/core/data/dto/object_handle.py @@ -8,7 +8,6 @@ class ObjectHandleBaseDTO(BaseModel): user_id: Optional[int] = None project_id: Optional[int] = None code_id: Optional[int] = None - current_code_id: Optional[int] = None source_document_id: Optional[int] = None span_annotation_id: Optional[int] = None span_group_id: Optional[int] = None diff --git a/backend/src/app/core/data/dto/span_annotation.py b/backend/src/app/core/data/dto/span_annotation.py index d3b381d4d..86c26ba74 100644 --- a/backend/src/app/core/data/dto/span_annotation.py +++ b/backend/src/app/core/data/dto/span_annotation.py @@ -17,40 +17,21 @@ class SpanAnnotationBaseDTO(BaseModel): # Properties for creation class SpanAnnotationCreateIntern(SpanAnnotationBaseDTO): span_text: str = Field(description="The SpanText the SpanAnnotation spans.") - current_code_id: int = Field(description="CurrentCode the SpanAnnotation refers to") + code_id: int = Field(description="Code the SpanAnnotation refers to") annotation_document_id: int = Field( description="AnnotationDocument the SpanAnnotation refers to" ) class SpanAnnotationCreate(SpanAnnotationBaseDTO): - span_text: str = Field(description="The SpanText the SpanAnnotation spans.") - current_code_id: int = Field(description="CurrentCode the SpanAnnotation refers to") - user_id: int = Field(description="User that created the SpanAnnotation") - sdoc_id: int = Field(description="SourceDocument the SpanAnnotation refers to") - - -class SpanAnnotationCreateWithCodeId(SpanAnnotationBaseDTO): span_text: str = Field(description="The SpanText the SpanAnnotation spans.") code_id: int = Field(description="Code the SpanAnnotation refers to") user_id: int = Field(description="User that created the SpanAnnotation") sdoc_id: int = Field(description="SourceDocument the SpanAnnotation refers to") -class SpanAnnotationCreateBulkWithCodeId(SpanAnnotationBaseDTO): - span_text: str = Field(description="The SpanText the SpanAnnotation spans.") - code_id: int = Field(description="Code the SpanAnnotation refers to") - sdoc_id: int = Field(description="SourceDocument the SpanAnnotation refers to") - user_id: int = Field(description="User the SpanAnnotation belongs to") - - # Properties for updating class SpanAnnotationUpdate(BaseModel, UpdateDTOBase): - current_code_id: int = Field(description="CurrentCode the SpanAnnotation refers to") - - -# Properties for updating -class SpanAnnotationUpdateWithCodeId(BaseModel, UpdateDTOBase): code_id: int = Field(description="Code the SpanAnnotation refers to") @@ -58,7 +39,7 @@ class SpanAnnotationUpdateWithCodeId(BaseModel, UpdateDTOBase): class SpanAnnotationRead(SpanAnnotationBaseDTO): id: int = Field(description="ID of the SpanAnnotation") span_text_id: int = Field(description="The SpanText the SpanAnnotation spans.") - current_code_id: int = Field(description="CurrentCode the SpanAnnotation refers to") + code_id: int = Field(description="Code the SpanAnnotation refers to") user_id: int = Field(description="User the SpanAnnotation belongs to") sdoc_id: int = Field(description="SourceDocument the SpanAnnotation refers to") created: datetime = Field(description="Created timestamp of the SpanAnnotation") diff --git a/backend/src/app/core/data/llm/prompts/annotation_prompt_builder.py b/backend/src/app/core/data/llm/prompts/annotation_prompt_builder.py index 6d6454d69..59b4abb63 100644 --- a/backend/src/app/core/data/llm/prompts/annotation_prompt_builder.py +++ b/backend/src/app/core/data/llm/prompts/annotation_prompt_builder.py @@ -68,7 +68,7 @@ def __init__(self, db: Session, project_id: int): examples: Dict[int, str] = {} for code in project.codes: # get all annotations for the code - annotations = code.current_code.span_annotations + annotations = code.span_annotations if len(annotations) == 0: continue random_annotation = random.choice(annotations) diff --git a/backend/src/app/core/data/orm/bbox_annotation.py b/backend/src/app/core/data/orm/bbox_annotation.py index d89490b8b..4794d9ec6 100644 --- a/backend/src/app/core/data/orm/bbox_annotation.py +++ b/backend/src/app/core/data/orm/bbox_annotation.py @@ -4,11 +4,11 @@ from sqlalchemy import DateTime, ForeignKey, Integer, func from sqlalchemy.orm import Mapped, mapped_column, relationship +from app.core.data.orm.code import CodeORM from app.core.data.orm.orm_base import ORMBase if TYPE_CHECKING: from app.core.data.orm.annotation_document import AnnotationDocumentORM - from app.core.data.orm.code import CurrentCodeORM from app.core.data.orm.object_handle import ObjectHandleORM @@ -34,15 +34,13 @@ class BBoxAnnotationORM(ORMBase): ) # many to one - current_code_id: Mapped[int] = mapped_column( + code_id: Mapped[int] = mapped_column( Integer, - ForeignKey("currentcode.id", ondelete="CASCADE"), + ForeignKey("code.id", ondelete="CASCADE"), nullable=False, index=True, ) - current_code: Mapped["CurrentCodeORM"] = relationship( - "CurrentCodeORM", back_populates="bbox_annotations" - ) + code: Mapped["CodeORM"] = relationship("CodeORM", back_populates="bbox_annotations") annotation_document_id: Mapped[int] = mapped_column( Integer, @@ -54,10 +52,6 @@ class BBoxAnnotationORM(ORMBase): "AnnotationDocumentORM", back_populates="bbox_annotations" ) - @property - def code(self): - return self.current_code.code - @property def user_id(self): return self.annotation_document.user_id diff --git a/backend/src/app/core/data/orm/code.py b/backend/src/app/core/data/orm/code.py index 3ca352a5a..ac4a3418d 100644 --- a/backend/src/app/core/data/orm/code.py +++ b/backend/src/app/core/data/orm/code.py @@ -27,10 +27,6 @@ class CodeORM(ORMBase): ) # one to one - current_code: Mapped["CurrentCodeORM"] = relationship( - "CurrentCodeORM", uselist=False, back_populates="code", passive_deletes=True - ) - object_handle: Mapped["ObjectHandleORM"] = relationship( "ObjectHandleORM", uselist=False, back_populates="code", passive_deletes=True ) @@ -49,6 +45,16 @@ class CodeORM(ORMBase): ) project: Mapped["ProjectORM"] = relationship("ProjectORM", back_populates="codes") + # one to many + span_annotations: Mapped[List["SpanAnnotationORM"]] = relationship( + "SpanAnnotationORM", back_populates="code", passive_deletes=True + ) + + # one to many + bbox_annotations: Mapped[List["BBoxAnnotationORM"]] = relationship( + "BBoxAnnotationORM", back_populates="code", passive_deletes=True + ) + # hierarchy reference parent_id: Mapped[Optional[int]] = mapped_column( Integer, ForeignKey("code.id", ondelete="CASCADE") @@ -64,30 +70,3 @@ class CodeORM(ORMBase): name="UC_name_unique_per_user_parent_and_project", ), ) - - -class CurrentCodeORM(ORMBase): - id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) - - # one to one - object_handle: Mapped["ObjectHandleORM"] = relationship( - "ObjectHandleORM", - uselist=False, - back_populates="current_code", - passive_deletes=True, - ) - - code_id: Mapped[int] = mapped_column( - Integer, ForeignKey("code.id", ondelete="CASCADE"), nullable=False, index=True - ) - code: Mapped["CodeORM"] = relationship("CodeORM", back_populates="current_code") - - # one to many - span_annotations: Mapped[List["SpanAnnotationORM"]] = relationship( - "SpanAnnotationORM", back_populates="current_code", passive_deletes=True - ) - - # one to many - bbox_annotations: Mapped[List["BBoxAnnotationORM"]] = relationship( - "BBoxAnnotationORM", back_populates="current_code", passive_deletes=True - ) diff --git a/backend/src/app/core/data/orm/object_handle.py b/backend/src/app/core/data/orm/object_handle.py index cd137d74c..26882d1c5 100644 --- a/backend/src/app/core/data/orm/object_handle.py +++ b/backend/src/app/core/data/orm/object_handle.py @@ -9,7 +9,7 @@ if TYPE_CHECKING: from app.core.data.orm.action import ActionORM from app.core.data.orm.bbox_annotation import BBoxAnnotationORM - from app.core.data.orm.code import CodeORM, CurrentCodeORM + from app.core.data.orm.code import CodeORM from app.core.data.orm.document_tag import DocumentTagORM from app.core.data.orm.memo import MemoORM from app.core.data.orm.project import ProjectORM @@ -55,13 +55,6 @@ class ObjectHandleORM(ORMBase): ) code: Mapped["CodeORM"] = relationship("CodeORM", back_populates="object_handle") - current_code_id: Mapped[Optional[int]] = mapped_column( - Integer, ForeignKey("currentcode.id", ondelete="CASCADE"), index=True - ) - current_code: Mapped["CurrentCodeORM"] = relationship( - "CurrentCodeORM", back_populates="object_handle" - ) - source_document_id: Mapped[Optional[int]] = mapped_column( Integer, ForeignKey("sourcedocument.id", ondelete="CASCADE"), index=True ) @@ -112,7 +105,6 @@ class ObjectHandleORM(ORMBase): coalesce(user_id, 0), coalesce(project_id, 0), coalesce(code_id, 0), - coalesce(current_code_id, 0), coalesce(source_document_id, 0), coalesce(span_annotation_id, 0), coalesce(bbox_annotation_id, 0), @@ -131,7 +123,6 @@ class ObjectHandleORM(ORMBase): + CASE WHEN project_id IS NULL THEN 0 ELSE 1 END + CASE WHEN code_id IS NULL THEN 0 ELSE 1 END + CASE WHEN memo_id IS NULL THEN 0 ELSE 1 END - + CASE WHEN current_code_id IS NULL THEN 0 ELSE 1 END + CASE WHEN source_document_id IS NULL THEN 0 ELSE 1 END + CASE WHEN span_annotation_id IS NULL THEN 0 ELSE 1 END + CASE WHEN bbox_annotation_id IS NULL THEN 0 ELSE 1 END @@ -146,7 +137,6 @@ class ObjectHandleORM(ORMBase): "user_id", "project_id", "code_id", - "current_code_id", "source_document_id", "span_annotation_id", "span_group_id", diff --git a/backend/src/app/core/data/orm/span_annotation.py b/backend/src/app/core/data/orm/span_annotation.py index 856bc9ccc..cfe9b070c 100644 --- a/backend/src/app/core/data/orm/span_annotation.py +++ b/backend/src/app/core/data/orm/span_annotation.py @@ -8,7 +8,7 @@ if TYPE_CHECKING: from app.core.data.orm.annotation_document import AnnotationDocumentORM - from app.core.data.orm.code import CurrentCodeORM + from app.core.data.orm.code import CodeORM from app.core.data.orm.object_handle import ObjectHandleORM from app.core.data.orm.span_group import SpanGroupORM from app.core.data.orm.span_text import SpanTextORM @@ -36,15 +36,13 @@ class SpanAnnotationORM(ORMBase): ) # many to one - current_code_id: Mapped[int] = mapped_column( + code_id: Mapped[int] = mapped_column( Integer, - ForeignKey("currentcode.id", ondelete="CASCADE"), + ForeignKey("code.id", ondelete="CASCADE"), nullable=False, index=True, ) - current_code: Mapped["CurrentCodeORM"] = relationship( - "CurrentCodeORM", back_populates="span_annotations" - ) + code: Mapped["CodeORM"] = relationship("CodeORM", back_populates="span_annotations") annotation_document_id: Mapped[int] = mapped_column( Integer, @@ -77,10 +75,6 @@ class SpanAnnotationORM(ORMBase): def text(self) -> str: return self.span_text.text - @property - def code(self): - return self.current_code.code - @property def user_id(self): return self.annotation_document.user_id diff --git a/backend/src/app/core/data/orm/util.py b/backend/src/app/core/data/orm/util.py index e4429a9aa..306d708af 100644 --- a/backend/src/app/core/data/orm/util.py +++ b/backend/src/app/core/data/orm/util.py @@ -6,7 +6,7 @@ from app.core.data.dto.action import ActionTargetObjectType from app.core.data.orm.annotation_document import AnnotationDocumentORM from app.core.data.orm.bbox_annotation import BBoxAnnotationORM -from app.core.data.orm.code import CodeORM, CurrentCodeORM +from app.core.data.orm.code import CodeORM from app.core.data.orm.document_tag import DocumentTagORM from app.core.data.orm.memo import MemoORM from app.core.data.orm.object_handle import ObjectHandleORM @@ -52,8 +52,6 @@ def get_parent_project_id(orm: ORMBase) -> Optional[int]: orm, SourceDocumentMetadataORM ): return orm.source_document.project_id - elif isinstance(orm, CurrentCodeORM): - return orm.code.project_id elif ( isinstance(orm, SpanAnnotationORM) or isinstance(orm, BBoxAnnotationORM) diff --git a/backend/src/app/core/search/search_service.py b/backend/src/app/core/search/search_service.py index bc3456bc1..bba51cb7e 100644 --- a/backend/src/app/core/search/search_service.py +++ b/backend/src/app/core/search/search_service.py @@ -17,7 +17,7 @@ ) from app.core.data.dto.search_stats import KeywordStat, SpanEntityStat, TagStat from app.core.data.orm.annotation_document import AnnotationDocumentORM -from app.core.data.orm.code import CodeORM, CurrentCodeORM +from app.core.data.orm.code import CodeORM from app.core.data.orm.document_tag import ( DocumentTagORM, SourceDocumentDocumentTagLinkTable, @@ -153,8 +153,7 @@ def _get_filtered_sdoc_ids( # isouter=True is important, otherwise we will only get sdocs with annotations .join(AnnotationDocumentORM.span_annotations, isouter=True) .join(SpanAnnotationORM.span_text, isouter=True) - .join(SpanAnnotationORM.current_code, isouter=True) - .join(CurrentCodeORM.code, isouter=True) + .join(SpanAnnotationORM.code, isouter=True) .join(SourceDocumentORM.metadata_) .join(SourceDocumentMetadataORM.project_metadata) .group_by(SourceDocumentORM.id) @@ -316,8 +315,7 @@ def compute_code_statistics( ) .join(SpanTextORM.span_annotations) .join(SpanAnnotationORM.annotation_document) - .join(SpanAnnotationORM.current_code) - .join(CurrentCodeORM.code) + .join(SpanAnnotationORM.code) .group_by(SpanTextORM.id) .filter( CodeORM.id == code_id, @@ -342,8 +340,7 @@ def compute_code_statistics( ) .join(SpanTextORM.span_annotations) .join(SpanAnnotationORM.annotation_document) - .join(SpanAnnotationORM.current_code) - .join(CurrentCodeORM.code) + .join(SpanAnnotationORM.code) .group_by(SpanTextORM.id) .filter( CodeORM.id == code_id, diff --git a/backend/src/app/preprocessing/pipeline/steps/image/write_ppid_to_database.py b/backend/src/app/preprocessing/pipeline/steps/image/write_ppid_to_database.py index a69e2d6be..515288bce 100644 --- a/backend/src/app/preprocessing/pipeline/steps/image/write_ppid_to_database.py +++ b/backend/src/app/preprocessing/pipeline/steps/image/write_ppid_to_database.py @@ -69,14 +69,12 @@ def _persist_bbox__annotations( ) db_code = crud_code.create(db, create_dto=create_dto) - ccid = db_code.current_code.id - create_dto = BBoxAnnotationCreateIntern( x_min=bbox.x_min, x_max=bbox.x_max, y_min=bbox.y_min, y_max=bbox.y_max, - current_code_id=ccid, + code_id=db_code.id, annotation_document_id=adoc_db_obj.id, ) diff --git a/backend/src/app/preprocessing/pipeline/steps/text/write_pptd_to_database.py b/backend/src/app/preprocessing/pipeline/steps/text/write_pptd_to_database.py index 8b435a5fc..36e5fc812 100644 --- a/backend/src/app/preprocessing/pipeline/steps/text/write_pptd_to_database.py +++ b/backend/src/app/preprocessing/pipeline/steps/text/write_pptd_to_database.py @@ -151,13 +151,11 @@ def _persist_span_annotations( ) db_code = crud_code.create(db, create_dto=create_dto) - ccid = db_code.current_code.id - create_dtos = [ SpanAnnotationCreateIntern( begin=aspan.start, end=aspan.end, - current_code_id=ccid, + code_id=db_code.id, annotation_document_id=adoc_db_obj.id, span_text=aspan.text, begin_token=aspan.start_token, From 3e09011d45f28147b640658940d6f452f3a7319b Mon Sep 17 00:00:00 2001 From: Tim Fischer Date: Thu, 26 Sep 2024 15:15:00 +0000 Subject: [PATCH 2/4] add alembic migration --- .../85d69d90d3e4_remove_current_code.py | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 backend/src/alembic/versions/85d69d90d3e4_remove_current_code.py diff --git a/backend/src/alembic/versions/85d69d90d3e4_remove_current_code.py b/backend/src/alembic/versions/85d69d90d3e4_remove_current_code.py new file mode 100644 index 000000000..22cc75e38 --- /dev/null +++ b/backend/src/alembic/versions/85d69d90d3e4_remove_current_code.py @@ -0,0 +1,121 @@ +"""remove current code + +Revision ID: 85d69d90d3e4 +Revises: 714fa3c0323d +Create Date: 2024-09-26 14:59:52.379282 + +""" + +from typing import Sequence, Union + +import sqlalchemy as sa + +from alembic import op + +# revision identifiers, used by Alembic. +revision: str = "85d69d90d3e4" +down_revision: Union[str, None] = "714fa3c0323d" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # add code id columns to bbox and span annotation + op.add_column("bboxannotation", sa.Column("code_id", sa.Integer(), nullable=True)) + op.create_foreign_key( + None, "bboxannotation", "code", ["code_id"], ["id"], ondelete="CASCADE" + ) + op.create_index( + op.f("ix_bboxannotation_code_id"), "bboxannotation", ["code_id"], unique=False + ) + op.add_column("spanannotation", sa.Column("code_id", sa.Integer(), nullable=True)) + op.create_index( + op.f("ix_spanannotation_code_id"), "spanannotation", ["code_id"], unique=False + ) + op.create_foreign_key( + None, "spanannotation", "code", ["code_id"], ["id"], ondelete="CASCADE" + ) + + # now, populate the code_id columns + # basically, to get the code_id for span and bbox annotations, we need to get the code_id from the current code + op.execute( + """ + UPDATE spanannotation + SET code_id = currentcode.code_id + FROM currentcode + WHERE spanannotation.current_code_id = currentcode.id + """ + ) + + op.execute( + """ + UPDATE bboxannotation + SET code_id = currentcode.code_id + FROM currentcode + WHERE bboxannotation.current_code_id = currentcode.id + """ + ) + + # now, make the columns not nullable + op.alter_column("bboxannotation", "code_id", nullable=False) + op.alter_column("spanannotation", "code_id", nullable=False) + + # remove everything related to current code + op.drop_index("ix_currentcode_code_id", table_name="currentcode") + op.drop_index("ix_currentcode_id", table_name="currentcode") + op.drop_index("ix_bboxannotation_current_code_id", table_name="bboxannotation") + op.drop_constraint( + "bboxannotation_current_code_id_fkey", "bboxannotation", type_="foreignkey" + ) + op.drop_column("bboxannotation", "current_code_id") + op.drop_index("ix_objecthandle_current_code_id", table_name="objecthandle") + op.drop_constraint( + "UC_only_one_object_handle_per_instance", "objecthandle", type_="unique" + ) + op.create_unique_constraint( + "UC_only_one_object_handle_per_instance", + "objecthandle", + [ + "user_id", + "project_id", + "code_id", + "source_document_id", + "span_annotation_id", + "span_group_id", + "document_tag_id", + "action_id", + "memo_id", + ], + ) + op.drop_index("idx_for_uc_work_with_null", table_name="objecthandle") + op.create_index( + "idx_for_uc_work_with_null", + "objecthandle", + [ + sa.text("coalesce(user_id, 0)"), + sa.text("coalesce(project_id, 0)"), + sa.text("coalesce(code_id, 0)"), + sa.text("coalesce(source_document_id, 0)"), + sa.text("coalesce(span_annotation_id, 0)"), + sa.text("coalesce(bbox_annotation_id, 0)"), + sa.text("coalesce(span_group_id, 0)"), + sa.text("coalesce(document_tag_id, 0)"), + sa.text("coalesce(action_id, 0)"), + sa.text("coalesce(memo_id, 0)"), + ], + unique=True, + ) + op.drop_constraint( + "objecthandle_current_code_id_fkey", "objecthandle", type_="foreignkey" + ) + op.drop_index("ix_spanannotation_current_code_id", table_name="spanannotation") + op.drop_constraint( + "spanannotation_current_code_id_fkey", "spanannotation", type_="foreignkey" + ) + op.drop_column("objecthandle", "current_code_id") + op.drop_column("spanannotation", "current_code_id") + op.drop_table("currentcode") + + +def downgrade() -> None: + raise RuntimeError("Downgrade not supported") From d7b2963e30464c7c79765c7aac69ba3738561cb9 Mon Sep 17 00:00:00 2001 From: Tim Fischer Date: Thu, 26 Sep 2024 15:25:32 +0000 Subject: [PATCH 3/4] updated api --- frontend/src/api/SpanAnnotationHooks.ts | 4 +- ...eWithCodeId.ts => BBoxAnnotationCreate.ts} | 2 +- .../api/openapi/models/BBoxAnnotationRead.ts | 4 +- ...eWithCodeId.ts => BBoxAnnotationUpdate.ts} | 2 +- ...eWithCodeId.ts => SpanAnnotationCreate.ts} | 2 +- .../SpanAnnotationCreateBulkWithCodeId.ts | 38 ---- .../api/openapi/models/SpanAnnotationRead.ts | 4 +- ...eWithCodeId.ts => SpanAnnotationUpdate.ts} | 2 +- .../openapi/services/BboxAnnotationService.ts | 14 +- .../src/api/openapi/services/CodeService.ts | 17 -- .../openapi/services/SourceDocumentService.ts | 8 +- .../openapi/services/SpanAnnotationService.ts | 19 +- .../api/openapi/services/SpanGroupService.ts | 2 +- frontend/src/openapi.json | 166 +++++------------- 14 files changed, 72 insertions(+), 212 deletions(-) rename frontend/src/api/openapi/models/{BBoxAnnotationCreateWithCodeId.ts => BBoxAnnotationCreate.ts} (93%) rename frontend/src/api/openapi/models/{BBoxAnnotationUpdateWithCodeId.ts => BBoxAnnotationUpdate.ts} (81%) rename frontend/src/api/openapi/models/{SpanAnnotationCreateWithCodeId.ts => SpanAnnotationCreate.ts} (93%) delete mode 100644 frontend/src/api/openapi/models/SpanAnnotationCreateBulkWithCodeId.ts rename frontend/src/api/openapi/models/{SpanAnnotationUpdateWithCodeId.ts => SpanAnnotationUpdate.ts} (81%) diff --git a/frontend/src/api/SpanAnnotationHooks.ts b/frontend/src/api/SpanAnnotationHooks.ts index 352a97030..ed253bad6 100644 --- a/frontend/src/api/SpanAnnotationHooks.ts +++ b/frontend/src/api/SpanAnnotationHooks.ts @@ -3,7 +3,7 @@ import queryClient from "../plugins/ReactQueryClient.ts"; import { QueryKey } from "./QueryKey.ts"; import { MemoRead } from "./openapi/models/MemoRead.ts"; import { SpanAnnotationReadResolved } from "./openapi/models/SpanAnnotationReadResolved.ts"; -import { SpanAnnotationUpdateWithCodeId } from "./openapi/models/SpanAnnotationUpdateWithCodeId.ts"; +import { SpanAnnotationUpdate } from "./openapi/models/SpanAnnotationUpdate.ts"; import { SpanAnnotationService } from "./openapi/services/SpanAnnotationService.ts"; export const FAKE_ANNOTATION_ID = -1; @@ -39,7 +39,7 @@ const useUpdateSpan = () => useMutation({ mutationFn: (variables: { spanAnnotationId: number; - requestBody: SpanAnnotationUpdateWithCodeId; + requestBody: SpanAnnotationUpdate; resolve?: boolean | undefined; }) => SpanAnnotationService.updateById({ diff --git a/frontend/src/api/openapi/models/BBoxAnnotationCreateWithCodeId.ts b/frontend/src/api/openapi/models/BBoxAnnotationCreate.ts similarity index 93% rename from frontend/src/api/openapi/models/BBoxAnnotationCreateWithCodeId.ts rename to frontend/src/api/openapi/models/BBoxAnnotationCreate.ts index 72e31a91e..a971e3e5d 100644 --- a/frontend/src/api/openapi/models/BBoxAnnotationCreateWithCodeId.ts +++ b/frontend/src/api/openapi/models/BBoxAnnotationCreate.ts @@ -2,7 +2,7 @@ /* istanbul ignore file */ /* tslint:disable */ /* eslint-disable */ -export type BBoxAnnotationCreateWithCodeId = { +export type BBoxAnnotationCreate = { /** * Absolute x_min coordinate of the BBoxAnnotation */ diff --git a/frontend/src/api/openapi/models/BBoxAnnotationRead.ts b/frontend/src/api/openapi/models/BBoxAnnotationRead.ts index 37049483f..81f6d832c 100644 --- a/frontend/src/api/openapi/models/BBoxAnnotationRead.ts +++ b/frontend/src/api/openapi/models/BBoxAnnotationRead.ts @@ -24,9 +24,9 @@ export type BBoxAnnotationRead = { */ id: number; /** - * CurrentCode the BBoxAnnotation refers to + * Code the BBoxAnnotation refers to */ - current_code_id: number; + code_id: number; /** * User that created the BBoxAnnotation */ diff --git a/frontend/src/api/openapi/models/BBoxAnnotationUpdateWithCodeId.ts b/frontend/src/api/openapi/models/BBoxAnnotationUpdate.ts similarity index 81% rename from frontend/src/api/openapi/models/BBoxAnnotationUpdateWithCodeId.ts rename to frontend/src/api/openapi/models/BBoxAnnotationUpdate.ts index dcebce714..484189181 100644 --- a/frontend/src/api/openapi/models/BBoxAnnotationUpdateWithCodeId.ts +++ b/frontend/src/api/openapi/models/BBoxAnnotationUpdate.ts @@ -2,7 +2,7 @@ /* istanbul ignore file */ /* tslint:disable */ /* eslint-disable */ -export type BBoxAnnotationUpdateWithCodeId = { +export type BBoxAnnotationUpdate = { /** * Code the BBoxAnnotation refers to */ diff --git a/frontend/src/api/openapi/models/SpanAnnotationCreateWithCodeId.ts b/frontend/src/api/openapi/models/SpanAnnotationCreate.ts similarity index 93% rename from frontend/src/api/openapi/models/SpanAnnotationCreateWithCodeId.ts rename to frontend/src/api/openapi/models/SpanAnnotationCreate.ts index 91b618122..c0f4db436 100644 --- a/frontend/src/api/openapi/models/SpanAnnotationCreateWithCodeId.ts +++ b/frontend/src/api/openapi/models/SpanAnnotationCreate.ts @@ -2,7 +2,7 @@ /* istanbul ignore file */ /* tslint:disable */ /* eslint-disable */ -export type SpanAnnotationCreateWithCodeId = { +export type SpanAnnotationCreate = { /** * Begin of the SpanAnnotation in characters */ diff --git a/frontend/src/api/openapi/models/SpanAnnotationCreateBulkWithCodeId.ts b/frontend/src/api/openapi/models/SpanAnnotationCreateBulkWithCodeId.ts deleted file mode 100644 index 647bd1abb..000000000 --- a/frontend/src/api/openapi/models/SpanAnnotationCreateBulkWithCodeId.ts +++ /dev/null @@ -1,38 +0,0 @@ -/* generated using openapi-typescript-codegen -- do no edit */ -/* istanbul ignore file */ -/* tslint:disable */ -/* eslint-disable */ -export type SpanAnnotationCreateBulkWithCodeId = { - /** - * Begin of the SpanAnnotation in characters - */ - begin: number; - /** - * End of the SpanAnnotation in characters - */ - end: number; - /** - * Begin of the SpanAnnotation in tokens - */ - begin_token: number; - /** - * End of the SpanAnnotation in tokens - */ - end_token: number; - /** - * The SpanText the SpanAnnotation spans. - */ - span_text: string; - /** - * Code the SpanAnnotation refers to - */ - code_id: number; - /** - * SourceDocument the SpanAnnotation refers to - */ - sdoc_id: number; - /** - * User the SpanAnnotation belongs to - */ - user_id: number; -}; diff --git a/frontend/src/api/openapi/models/SpanAnnotationRead.ts b/frontend/src/api/openapi/models/SpanAnnotationRead.ts index 8386b6ac6..371652816 100644 --- a/frontend/src/api/openapi/models/SpanAnnotationRead.ts +++ b/frontend/src/api/openapi/models/SpanAnnotationRead.ts @@ -28,9 +28,9 @@ export type SpanAnnotationRead = { */ span_text_id: number; /** - * CurrentCode the SpanAnnotation refers to + * Code the SpanAnnotation refers to */ - current_code_id: number; + code_id: number; /** * User the SpanAnnotation belongs to */ diff --git a/frontend/src/api/openapi/models/SpanAnnotationUpdateWithCodeId.ts b/frontend/src/api/openapi/models/SpanAnnotationUpdate.ts similarity index 81% rename from frontend/src/api/openapi/models/SpanAnnotationUpdateWithCodeId.ts rename to frontend/src/api/openapi/models/SpanAnnotationUpdate.ts index 514c39c9d..a78ee4a03 100644 --- a/frontend/src/api/openapi/models/SpanAnnotationUpdateWithCodeId.ts +++ b/frontend/src/api/openapi/models/SpanAnnotationUpdate.ts @@ -2,7 +2,7 @@ /* istanbul ignore file */ /* tslint:disable */ /* eslint-disable */ -export type SpanAnnotationUpdateWithCodeId = { +export type SpanAnnotationUpdate = { /** * Code the SpanAnnotation refers to */ diff --git a/frontend/src/api/openapi/services/BboxAnnotationService.ts b/frontend/src/api/openapi/services/BboxAnnotationService.ts index 8b21b54a1..3d555833b 100644 --- a/frontend/src/api/openapi/services/BboxAnnotationService.ts +++ b/frontend/src/api/openapi/services/BboxAnnotationService.ts @@ -2,10 +2,10 @@ /* istanbul ignore file */ /* tslint:disable */ /* eslint-disable */ -import type { BBoxAnnotationCreateWithCodeId } from "../models/BBoxAnnotationCreateWithCodeId"; +import type { BBoxAnnotationCreate } from "../models/BBoxAnnotationCreate"; import type { BBoxAnnotationRead } from "../models/BBoxAnnotationRead"; import type { BBoxAnnotationReadResolved } from "../models/BBoxAnnotationReadResolved"; -import type { BBoxAnnotationUpdateWithCodeId } from "../models/BBoxAnnotationUpdateWithCodeId"; +import type { BBoxAnnotationUpdate } from "../models/BBoxAnnotationUpdate"; import type { CodeRead } from "../models/CodeRead"; import type { MemoCreate } from "../models/MemoCreate"; import type { MemoRead } from "../models/MemoRead"; @@ -22,9 +22,9 @@ export class BboxAnnotationService { requestBody, resolve = true, }: { - requestBody: BBoxAnnotationCreateWithCodeId; + requestBody: BBoxAnnotationCreate; /** - * If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity + * If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity */ resolve?: boolean; }): CancelablePromise { @@ -52,7 +52,7 @@ export class BboxAnnotationService { }: { bboxId: number; /** - * If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity + * If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity */ resolve?: boolean; }): CancelablePromise { @@ -81,9 +81,9 @@ export class BboxAnnotationService { resolve = true, }: { bboxId: number; - requestBody: BBoxAnnotationUpdateWithCodeId; + requestBody: BBoxAnnotationUpdate; /** - * If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity + * If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity */ resolve?: boolean; }): CancelablePromise { diff --git a/frontend/src/api/openapi/services/CodeService.ts b/frontend/src/api/openapi/services/CodeService.ts index 22a507d93..ee502f36a 100644 --- a/frontend/src/api/openapi/services/CodeService.ts +++ b/frontend/src/api/openapi/services/CodeService.ts @@ -27,23 +27,6 @@ export class CodeService { }, }); } - /** - * Returns the Code linked by the CurrentCode with the given ID. - * @returns CodeRead Successful Response - * @throws ApiError - */ - public static getCodeByCurrentCodeId({ currentCodeId }: { currentCodeId: number }): CancelablePromise { - return __request(OpenAPI, { - method: "GET", - url: "/code/current/{current_code_id}", - path: { - current_code_id: currentCodeId, - }, - errors: { - 422: `Validation Error`, - }, - }); - } /** * Returns the Code with the given ID. * @returns CodeRead Successful Response diff --git a/frontend/src/api/openapi/services/SourceDocumentService.ts b/frontend/src/api/openapi/services/SourceDocumentService.ts index 27d4c8c71..90e1a998e 100644 --- a/frontend/src/api/openapi/services/SourceDocumentService.ts +++ b/frontend/src/api/openapi/services/SourceDocumentService.ts @@ -329,7 +329,7 @@ export class SourceDocumentService { sdocId: number; userId: number; /** - * If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity + * If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity */ resolve?: boolean; }): CancelablePromise | Array> { @@ -361,7 +361,7 @@ export class SourceDocumentService { sdocId: number; userId: Array; /** - * If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity + * If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity */ resolve?: boolean; }): CancelablePromise | Array> { @@ -403,7 +403,7 @@ export class SourceDocumentService { */ limit?: number | null; /** - * If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity + * If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity */ resolve?: boolean; }): CancelablePromise | Array> { @@ -447,7 +447,7 @@ export class SourceDocumentService { */ limit?: number | null; /** - * If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity + * If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity */ resolve?: boolean; }): CancelablePromise | Array> { diff --git a/frontend/src/api/openapi/services/SpanAnnotationService.ts b/frontend/src/api/openapi/services/SpanAnnotationService.ts index 4ab8fe8fc..4f4173a5e 100644 --- a/frontend/src/api/openapi/services/SpanAnnotationService.ts +++ b/frontend/src/api/openapi/services/SpanAnnotationService.ts @@ -5,11 +5,10 @@ import type { CodeRead } from "../models/CodeRead"; import type { MemoCreate } from "../models/MemoCreate"; import type { MemoRead } from "../models/MemoRead"; -import type { SpanAnnotationCreateBulkWithCodeId } from "../models/SpanAnnotationCreateBulkWithCodeId"; -import type { SpanAnnotationCreateWithCodeId } from "../models/SpanAnnotationCreateWithCodeId"; +import type { SpanAnnotationCreate } from "../models/SpanAnnotationCreate"; import type { SpanAnnotationRead } from "../models/SpanAnnotationRead"; import type { SpanAnnotationReadResolved } from "../models/SpanAnnotationReadResolved"; -import type { SpanAnnotationUpdateWithCodeId } from "../models/SpanAnnotationUpdateWithCodeId"; +import type { SpanAnnotationUpdate } from "../models/SpanAnnotationUpdate"; import type { SpanGroupRead } from "../models/SpanGroupRead"; import type { CancelablePromise } from "../core/CancelablePromise"; import { OpenAPI } from "../core/OpenAPI"; @@ -24,9 +23,9 @@ export class SpanAnnotationService { requestBody, resolve = true, }: { - requestBody: SpanAnnotationCreateWithCodeId; + requestBody: SpanAnnotationCreate; /** - * If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity + * If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity */ resolve?: boolean; }): CancelablePromise { @@ -52,9 +51,9 @@ export class SpanAnnotationService { requestBody, resolve = true, }: { - requestBody: Array; + requestBody: Array; /** - * If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity + * If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity */ resolve?: boolean; }): CancelablePromise | Array> { @@ -82,7 +81,7 @@ export class SpanAnnotationService { }: { spanId: number; /** - * If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity + * If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity */ resolve?: boolean; }): CancelablePromise { @@ -111,9 +110,9 @@ export class SpanAnnotationService { resolve = true, }: { spanId: number; - requestBody: SpanAnnotationUpdateWithCodeId; + requestBody: SpanAnnotationUpdate; /** - * If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity + * If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity */ resolve?: boolean; }): CancelablePromise { diff --git a/frontend/src/api/openapi/services/SpanGroupService.ts b/frontend/src/api/openapi/services/SpanGroupService.ts index 4765bb4c3..dd30a7c0d 100644 --- a/frontend/src/api/openapi/services/SpanGroupService.ts +++ b/frontend/src/api/openapi/services/SpanGroupService.ts @@ -101,7 +101,7 @@ export class SpanGroupService { }: { spanGroupId: number; /** - * If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity + * If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity */ resolve?: boolean; }): CancelablePromise> { diff --git a/frontend/src/openapi.json b/frontend/src/openapi.json index a09b83fc3..83b30528a 100644 --- a/frontend/src/openapi.json +++ b/frontend/src/openapi.json @@ -1375,10 +1375,10 @@ "schema": { "type": "boolean", "title": "Resolve Code", - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", "default": true }, - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" } ], "responses": { @@ -1424,10 +1424,10 @@ "schema": { "type": "boolean", "title": "Resolve Code", - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", "default": true }, - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" } ], "responses": { @@ -1490,10 +1490,10 @@ "schema": { "type": "boolean", "title": "Resolve Code", - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", "default": true }, - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" } ], "responses": { @@ -1561,10 +1561,10 @@ "schema": { "type": "boolean", "title": "Resolve Code", - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", "default": true }, - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" } ], "responses": { @@ -1965,17 +1965,15 @@ "schema": { "type": "boolean", "title": "Resolve Code", - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", "default": true }, - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" } ], "requestBody": { "required": true, - "content": { - "application/json": { "schema": { "$ref": "#/components/schemas/SpanAnnotationCreateWithCodeId" } } - } + "content": { "application/json": { "schema": { "$ref": "#/components/schemas/SpanAnnotationCreate" } } } }, "responses": { "200": { @@ -2013,10 +2011,10 @@ "schema": { "type": "boolean", "title": "Resolve Code", - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", "default": true }, - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" } ], "requestBody": { @@ -2025,7 +2023,7 @@ "application/json": { "schema": { "type": "array", - "items": { "$ref": "#/components/schemas/SpanAnnotationCreateBulkWithCodeId" }, + "items": { "$ref": "#/components/schemas/SpanAnnotationCreate" }, "title": "Spans" } } @@ -2068,10 +2066,10 @@ "schema": { "type": "boolean", "title": "Resolve Code", - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", "default": true }, - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" } ], "responses": { @@ -2109,17 +2107,15 @@ "schema": { "type": "boolean", "title": "Resolve Code", - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", "default": true }, - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" } ], "requestBody": { "required": true, - "content": { - "application/json": { "schema": { "$ref": "#/components/schemas/SpanAnnotationUpdateWithCodeId" } } - } + "content": { "application/json": { "schema": { "$ref": "#/components/schemas/SpanAnnotationUpdate" } } } }, "responses": { "200": { @@ -2535,10 +2531,10 @@ "schema": { "type": "boolean", "title": "Resolve Code", - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", "default": true }, - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" } ], "responses": { @@ -2580,17 +2576,15 @@ "schema": { "type": "boolean", "title": "Resolve Code", - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", "default": true }, - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" } ], "requestBody": { "required": true, - "content": { - "application/json": { "schema": { "$ref": "#/components/schemas/BBoxAnnotationCreateWithCodeId" } } - } + "content": { "application/json": { "schema": { "$ref": "#/components/schemas/BBoxAnnotationCreate" } } } }, "responses": { "200": { @@ -2629,10 +2623,10 @@ "schema": { "type": "boolean", "title": "Resolve Code", - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", "default": true }, - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" } ], "responses": { @@ -2670,17 +2664,15 @@ "schema": { "type": "boolean", "title": "Resolve Code", - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity", "default": true }, - "description": "If true, the current_code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" + "description": "If true, the code_id of the SpanAnnotation gets resolved and replaced by the respective Code entity" } ], "requestBody": { "required": true, - "content": { - "application/json": { "schema": { "$ref": "#/components/schemas/BBoxAnnotationUpdateWithCodeId" } } - } + "content": { "application/json": { "schema": { "$ref": "#/components/schemas/BBoxAnnotationUpdate" } } } }, "responses": { "200": { @@ -2880,32 +2872,6 @@ "security": [{ "OAuth2PasswordBearer": [] }] } }, - "/code/current/{current_code_id}": { - "get": { - "tags": ["code"], - "summary": "Returns the Code linked by the CurrentCode with the given ID.", - "operationId": "get_code_by_current_code_id", - "security": [{ "OAuth2PasswordBearer": [] }], - "parameters": [ - { - "name": "current_code_id", - "in": "path", - "required": true, - "schema": { "type": "integer", "title": "Current Code Id" } - } - ], - "responses": { - "200": { - "description": "Successful Response", - "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CodeRead" } } } - }, - "422": { - "description": "Validation Error", - "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } } - } - } - } - }, "/code/{code_id}": { "get": { "tags": ["code"], @@ -5994,7 +5960,7 @@ ], "title": "AttachedObjectType" }, - "BBoxAnnotationCreateWithCodeId": { + "BBoxAnnotationCreate": { "properties": { "x_min": { "type": "integer", @@ -6026,7 +5992,7 @@ }, "type": "object", "required": ["x_min", "x_max", "y_min", "y_max", "code_id", "user_id", "sdoc_id"], - "title": "BBoxAnnotationCreateWithCodeId" + "title": "BBoxAnnotationCreate" }, "BBoxAnnotationRead": { "properties": { @@ -6051,11 +6017,7 @@ "description": "Absolute y_max coordinate of the BBoxAnnotation" }, "id": { "type": "integer", "title": "Id", "description": "ID of the BBoxAnnotation" }, - "current_code_id": { - "type": "integer", - "title": "Current Code Id", - "description": "CurrentCode the BBoxAnnotation refers to" - }, + "code_id": { "type": "integer", "title": "Code Id", "description": "Code the BBoxAnnotation refers to" }, "user_id": { "type": "integer", "title": "User Id", "description": "User that created the BBoxAnnotation" }, "sdoc_id": { "type": "integer", @@ -6076,18 +6038,7 @@ } }, "type": "object", - "required": [ - "x_min", - "x_max", - "y_min", - "y_max", - "id", - "current_code_id", - "user_id", - "sdoc_id", - "created", - "updated" - ], + "required": ["x_min", "x_max", "y_min", "y_max", "id", "code_id", "user_id", "sdoc_id", "created", "updated"], "title": "BBoxAnnotationRead" }, "BBoxAnnotationReadResolved": { @@ -6172,13 +6123,13 @@ "required": ["id", "x", "y", "width", "height", "url", "code", "user_id", "sdoc", "tags", "memo"], "title": "BBoxAnnotationTableRow" }, - "BBoxAnnotationUpdateWithCodeId": { + "BBoxAnnotationUpdate": { "properties": { "code_id": { "type": "integer", "title": "Code Id", "description": "Code the BBoxAnnotation refers to" } }, "type": "object", "required": ["code_id"], - "title": "BBoxAnnotationUpdateWithCodeId" + "title": "BBoxAnnotationUpdate" }, "BackgroundJobStatus": { "type": "string", @@ -8838,38 +8789,7 @@ ], "title": "SourceDocumentWithDataRead" }, - "SpanAnnotationCreateBulkWithCodeId": { - "properties": { - "begin": { "type": "integer", "title": "Begin", "description": "Begin of the SpanAnnotation in characters" }, - "end": { "type": "integer", "title": "End", "description": "End of the SpanAnnotation in characters" }, - "begin_token": { - "type": "integer", - "title": "Begin Token", - "description": "Begin of the SpanAnnotation in tokens" - }, - "end_token": { - "type": "integer", - "title": "End Token", - "description": "End of the SpanAnnotation in tokens" - }, - "span_text": { - "type": "string", - "title": "Span Text", - "description": "The SpanText the SpanAnnotation spans." - }, - "code_id": { "type": "integer", "title": "Code Id", "description": "Code the SpanAnnotation refers to" }, - "sdoc_id": { - "type": "integer", - "title": "Sdoc Id", - "description": "SourceDocument the SpanAnnotation refers to" - }, - "user_id": { "type": "integer", "title": "User Id", "description": "User the SpanAnnotation belongs to" } - }, - "type": "object", - "required": ["begin", "end", "begin_token", "end_token", "span_text", "code_id", "sdoc_id", "user_id"], - "title": "SpanAnnotationCreateBulkWithCodeId" - }, - "SpanAnnotationCreateWithCodeId": { + "SpanAnnotationCreate": { "properties": { "begin": { "type": "integer", "title": "Begin", "description": "Begin of the SpanAnnotation in characters" }, "end": { "type": "integer", "title": "End", "description": "End of the SpanAnnotation in characters" }, @@ -8898,7 +8818,7 @@ }, "type": "object", "required": ["begin", "end", "begin_token", "end_token", "span_text", "code_id", "user_id", "sdoc_id"], - "title": "SpanAnnotationCreateWithCodeId" + "title": "SpanAnnotationCreate" }, "SpanAnnotationRead": { "properties": { @@ -8920,11 +8840,7 @@ "title": "Span Text Id", "description": "The SpanText the SpanAnnotation spans." }, - "current_code_id": { - "type": "integer", - "title": "Current Code Id", - "description": "CurrentCode the SpanAnnotation refers to" - }, + "code_id": { "type": "integer", "title": "Code Id", "description": "Code the SpanAnnotation refers to" }, "user_id": { "type": "integer", "title": "User Id", "description": "User the SpanAnnotation belongs to" }, "sdoc_id": { "type": "integer", @@ -8952,7 +8868,7 @@ "end_token", "id", "span_text_id", - "current_code_id", + "code_id", "user_id", "sdoc_id", "created", @@ -9015,13 +8931,13 @@ ], "title": "SpanAnnotationReadResolved" }, - "SpanAnnotationUpdateWithCodeId": { + "SpanAnnotationUpdate": { "properties": { "code_id": { "type": "integer", "title": "Code Id", "description": "Code the SpanAnnotation refers to" } }, "type": "object", "required": ["code_id"], - "title": "SpanAnnotationUpdateWithCodeId" + "title": "SpanAnnotationUpdate" }, "SpanEntityStat": { "properties": { From c54ceb29f5f88a9d9e55f0be5d576421bbedf7fd Mon Sep 17 00:00:00 2001 From: Tim Fischer Date: Thu, 26 Sep 2024 15:27:50 +0000 Subject: [PATCH 4/4] updated components where necessary --- .../steps/AnnotationResultStep/AnnotationResultStep.tsx | 4 ++-- .../views/annotation/ImageAnnotator/imageAnnotationHooks.ts | 4 ++-- .../src/views/annotation/TextAnnotator/TextAnnotator.tsx | 6 +++--- .../views/annotation/TextAnnotator/textAnnotationHooks.ts | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/frontend/src/components/LLMDialog/steps/AnnotationResultStep/AnnotationResultStep.tsx b/frontend/src/components/LLMDialog/steps/AnnotationResultStep/AnnotationResultStep.tsx index a2dac9987..096a16cf5 100644 --- a/frontend/src/components/LLMDialog/steps/AnnotationResultStep/AnnotationResultStep.tsx +++ b/frontend/src/components/LLMDialog/steps/AnnotationResultStep/AnnotationResultStep.tsx @@ -5,7 +5,7 @@ import { useEffect, useMemo, useState } from "react"; import LLMHooks from "../../../../api/LLMHooks.ts"; import { AnnotationLLMJobResult } from "../../../../api/openapi/models/AnnotationLLMJobResult.ts"; import { CodeRead } from "../../../../api/openapi/models/CodeRead.ts"; -import { SpanAnnotationCreateBulkWithCodeId } from "../../../../api/openapi/models/SpanAnnotationCreateBulkWithCodeId.ts"; +import { SpanAnnotationCreate } from "../../../../api/openapi/models/SpanAnnotationCreate.ts"; import { SpanAnnotationReadResolved } from "../../../../api/openapi/models/SpanAnnotationReadResolved.ts"; import SpanAnnotationHooks from "../../../../api/SpanAnnotationHooks.ts"; import { useAuth } from "../../../../auth/useAuth.ts"; @@ -99,7 +99,7 @@ function AnnotationResultStep() { }); } return acc; - }, [] as SpanAnnotationCreateBulkWithCodeId[]), + }, [] as SpanAnnotationCreate[]), }, { onSuccess: () => { diff --git a/frontend/src/views/annotation/ImageAnnotator/imageAnnotationHooks.ts b/frontend/src/views/annotation/ImageAnnotator/imageAnnotationHooks.ts index 09d7124c4..efc989034 100644 --- a/frontend/src/views/annotation/ImageAnnotator/imageAnnotationHooks.ts +++ b/frontend/src/views/annotation/ImageAnnotator/imageAnnotationHooks.ts @@ -2,7 +2,7 @@ import { useMutation } from "@tanstack/react-query"; import { FAKE_BBOX_ID } from "../../../api/BboxAnnotationHooks.ts"; import { BBoxAnnotationRead } from "../../../api/openapi/models/BBoxAnnotationRead.ts"; import { BBoxAnnotationReadResolved } from "../../../api/openapi/models/BBoxAnnotationReadResolved.ts"; -import { BBoxAnnotationUpdateWithCodeId } from "../../../api/openapi/models/BBoxAnnotationUpdateWithCodeId.ts"; +import { BBoxAnnotationUpdate } from "../../../api/openapi/models/BBoxAnnotationUpdate.ts"; import { BboxAnnotationService } from "../../../api/openapi/services/BboxAnnotationService.ts"; import { QueryKey } from "../../../api/QueryKey.ts"; import queryClient from "../../../plugins/ReactQueryClient.ts"; @@ -66,7 +66,7 @@ export const useUpdateBBoxAnnotation = (visibleUserIds: number[]) => useMutation({ mutationFn: (variables: { bboxToUpdate: BBoxAnnotationRead | BBoxAnnotationReadResolved; - requestBody: BBoxAnnotationUpdateWithCodeId; + requestBody: BBoxAnnotationUpdate; resolve?: boolean | undefined; }) => BboxAnnotationService.updateById({ diff --git a/frontend/src/views/annotation/TextAnnotator/TextAnnotator.tsx b/frontend/src/views/annotation/TextAnnotator/TextAnnotator.tsx index 179c3efae..23bb66222 100644 --- a/frontend/src/views/annotation/TextAnnotator/TextAnnotator.tsx +++ b/frontend/src/views/annotation/TextAnnotator/TextAnnotator.tsx @@ -6,7 +6,7 @@ import { FAKE_ANNOTATION_ID } from "../../../api/SpanAnnotationHooks.ts"; import { BBoxAnnotationReadResolved } from "../../../api/openapi/models/BBoxAnnotationReadResolved.ts"; import { CodeRead } from "../../../api/openapi/models/CodeRead.ts"; import { SourceDocumentWithDataRead } from "../../../api/openapi/models/SourceDocumentWithDataRead.ts"; -import { SpanAnnotationCreateWithCodeId } from "../../../api/openapi/models/SpanAnnotationCreateWithCodeId.ts"; +import { SpanAnnotationCreate } from "../../../api/openapi/models/SpanAnnotationCreate.ts"; import { SpanAnnotationReadResolved } from "../../../api/openapi/models/SpanAnnotationReadResolved.ts"; import { useAuth } from "../../../auth/useAuth.ts"; import ConfirmationAPI from "../../../components/ConfirmationDialog/ConfirmationAPI.ts"; @@ -32,7 +32,7 @@ function TextAnnotator({ sdoc }: AnnotatorRemasteredProps) { // local state const spanMenuRef = useRef(null); - const [fakeAnnotation, setFakeAnnotation] = useState(undefined); + const [fakeAnnotation, setFakeAnnotation] = useState(undefined); // global client state (redux) const visibleUserIds = useAppSelector((state) => state.annotations.visibleUserIds); @@ -139,7 +139,7 @@ function TextAnnotator({ sdoc }: AnnotatorRemasteredProps) { .map((t) => t.text) .join(" "); - const requestBody: SpanAnnotationCreateWithCodeId = { + const requestBody: SpanAnnotationCreate = { code_id: codes[0].id, user_id: user.id, sdoc_id: sdoc.id, diff --git a/frontend/src/views/annotation/TextAnnotator/textAnnotationHooks.ts b/frontend/src/views/annotation/TextAnnotator/textAnnotationHooks.ts index cf7520692..4eb9b7fd6 100644 --- a/frontend/src/views/annotation/TextAnnotator/textAnnotationHooks.ts +++ b/frontend/src/views/annotation/TextAnnotator/textAnnotationHooks.ts @@ -1,7 +1,7 @@ import { useMutation } from "@tanstack/react-query"; import { SpanAnnotationRead } from "../../../api/openapi/models/SpanAnnotationRead.ts"; import { SpanAnnotationReadResolved } from "../../../api/openapi/models/SpanAnnotationReadResolved.ts"; -import { SpanAnnotationUpdateWithCodeId } from "../../../api/openapi/models/SpanAnnotationUpdateWithCodeId.ts"; +import { SpanAnnotationUpdate } from "../../../api/openapi/models/SpanAnnotationUpdate.ts"; import { SpanAnnotationService } from "../../../api/openapi/services/SpanAnnotationService.ts"; import { QueryKey } from "../../../api/QueryKey.ts"; import { FAKE_ANNOTATION_ID } from "../../../api/SpanAnnotationHooks.ts"; @@ -87,7 +87,7 @@ export const useUpdateSpanAnnotation = (visibleUserIds: number[]) => useMutation({ mutationFn: (variables: { spanAnnotationToUpdate: SpanAnnotationRead | SpanAnnotationReadResolved; - requestBody: SpanAnnotationUpdateWithCodeId; + requestBody: SpanAnnotationUpdate; resolve?: boolean | undefined; }) => SpanAnnotationService.updateById({