Skip to content

Commit b33d4f3

Browse files
authored
Merge pull request #562 from Police-Data-Accessibility-Project/mc_551_suggestion_nomenclature
Rename annotation tables to consistent nomenclature patterns
2 parents 6b569bc + 7ab6638 commit b33d4f3

File tree

170 files changed

+734
-697
lines changed

Some content is hidden

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

170 files changed

+734
-697
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""Rename suggestion tables to consistent nomenclature
2+
3+
Revision ID: 9292faed37fd
4+
Revises: dfb64594049f
5+
Create Date: 2025-12-18 09:51:20.074946
6+
7+
"""
8+
from typing import Sequence, Union
9+
10+
from alembic import op
11+
import sqlalchemy as sa
12+
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = '9292faed37fd'
16+
down_revision: Union[str, None] = 'dfb64594049f'
17+
branch_labels: Union[str, Sequence[str], None] = None
18+
depends_on: Union[str, Sequence[str], None] = None
19+
20+
OLD_NEW_TABLE_MAPPING = {
21+
# Anonymous Suggestions
22+
"anonymous_annotation_agency": "annotation__agency__anon",
23+
"anonymous_annotation_location": "annotation__location__anon",
24+
"anonymous_annotation_record_type": "annotation__record_type__anon",
25+
"anonymous_annotation_url_type": "annotation__url_type__anon",
26+
# User Suggestions
27+
"user_url_agency_suggestions": "annotation__agency__user",
28+
"user_location_suggestions": "annotation__location__user",
29+
"user_record_type_suggestions": "annotation__record_type__user",
30+
"user_url_type_suggestions": "annotation__url_type__user",
31+
# Auto suggestions
32+
"auto_location_id_subtasks": "annotation__location__auto__subtasks",
33+
"location_id_subtask_suggestions": "annotation__location__auto__suggestions",
34+
"url_auto_agency_id_subtasks": "annotation__agency__auto__subtasks",
35+
"agency_id_subtask_suggestions": "annotation__agency__auto__suggestions",
36+
"auto_record_type_suggestions": "annotation__record_type__auto",
37+
"auto_relevant_suggestions": "annotation__url_type__auto",
38+
# Name suggestions
39+
"url_name_suggestions": "annotation__name__suggestions",
40+
"link__anonymous_sessions__name_suggestions": "annotation__name__anon__endorsements",
41+
"link_user_name_suggestions": "annotation__name__user__endorsements",
42+
}
43+
44+
def upgrade() -> None:
45+
for old_table_name, new_table_name in OLD_NEW_TABLE_MAPPING.items():
46+
op.rename_table(
47+
old_table_name=old_table_name,
48+
new_table_name=new_table_name
49+
)
50+
51+
52+
def downgrade() -> None:
53+
pass

src/api/endpoints/annotate/_shared/extract.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
from src.api.endpoints.annotate.all.get.queries.name.core import GetNameSuggestionsQueryBuilder
1717
from src.db.dto_converter import DTOConverter
1818
from src.db.dtos.url.mapping_.simple import SimpleURLMapping
19+
from src.db.models.impl.annotation.agency.user.sqlalchemy import AnnotationAgencyUser
1920
from src.db.models.impl.url.core.sqlalchemy import URL
20-
from src.db.models.impl.url.suggestion.agency.user import UserURLAgencySuggestion
2121

2222

2323
async def extract_and_format_get_annotation_result(
@@ -55,7 +55,7 @@ async def extract_and_format_get_annotation_result(
5555
batch_info=await GetAnnotationBatchInfoQueryBuilder(
5656
batch_id=batch_id,
5757
models=[
58-
UserURLAgencySuggestion,
58+
AnnotationAgencyUser,
5959
]
6060
).run(session),
6161
location_suggestions=location_suggestions,

src/api/endpoints/annotate/all/get/queries/agency/requester.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
from src.db.helpers.query import exists_url
99
from src.db.helpers.session import session_helper as sh
1010
from src.db.models.impl.agency.sqlalchemy import Agency
11+
from src.db.models.impl.annotation.agency.auto.subtask.sqlalchemy import AnnotationAgencyAutoSubtask
12+
from src.db.models.impl.annotation.agency.auto.suggestion.sqlalchemy import AnnotationAgencyAutoSuggestion
13+
from src.db.models.impl.annotation.agency.user.sqlalchemy import AnnotationAgencyUser
1114
from src.db.models.impl.link.user_suggestion_not_found.agency.sqlalchemy import LinkUserSuggestionAgencyNotFound
12-
from src.db.models.impl.url.suggestion.agency.subtask.sqlalchemy import URLAutoAgencyIDSubtask
13-
from src.db.models.impl.url.suggestion.agency.suggestion.sqlalchemy import AgencyIDSubtaskSuggestion
14-
from src.db.models.impl.url.suggestion.agency.user import UserURLAgencySuggestion
1515
from src.db.templates.requester import RequesterBase
1616

1717

@@ -36,10 +36,10 @@ async def get_agency_suggestions(self) -> list[SuggestionModel]:
3636
.where(
3737
or_(
3838
exists_url(
39-
UserURLAgencySuggestion
39+
AnnotationAgencyUser
4040
),
4141
exists_url(
42-
URLAutoAgencyIDSubtask
42+
AnnotationAgencyAutoSubtask
4343
)
4444
)
4545
)
@@ -49,34 +49,34 @@ async def get_agency_suggestions(self) -> list[SuggestionModel]:
4949
# Number of users who suggested each agency
5050
user_suggestions_cte = (
5151
select(
52-
UserURLAgencySuggestion.url_id,
53-
UserURLAgencySuggestion.agency_id,
54-
func.count(UserURLAgencySuggestion.user_id).label('user_count')
52+
AnnotationAgencyUser.url_id,
53+
AnnotationAgencyUser.agency_id,
54+
func.count(AnnotationAgencyUser.user_id).label('user_count')
5555
)
5656
.group_by(
57-
UserURLAgencySuggestion.agency_id,
58-
UserURLAgencySuggestion.url_id,
57+
AnnotationAgencyUser.agency_id,
58+
AnnotationAgencyUser.url_id,
5959
)
6060
.cte("user_suggestions")
6161
)
6262

6363
# Maximum confidence of robo annotation, if any
6464
robo_suggestions_cte = (
6565
select(
66-
URLAutoAgencyIDSubtask.url_id,
66+
AnnotationAgencyAutoSubtask.url_id,
6767
Agency.id.label("agency_id"),
68-
func.max(AgencyIDSubtaskSuggestion.confidence).label('robo_confidence')
68+
func.max(AnnotationAgencyAutoSuggestion.confidence).label('robo_confidence')
6969
)
7070
.join(
71-
AgencyIDSubtaskSuggestion,
72-
AgencyIDSubtaskSuggestion.subtask_id == URLAutoAgencyIDSubtask.id
71+
AnnotationAgencyAutoSuggestion,
72+
AnnotationAgencyAutoSuggestion.subtask_id == AnnotationAgencyAutoSubtask.id
7373
)
7474
.join(
7575
Agency,
76-
Agency.id == AgencyIDSubtaskSuggestion.agency_id
76+
Agency.id == AnnotationAgencyAutoSuggestion.agency_id
7777
)
7878
.group_by(
79-
URLAutoAgencyIDSubtask.url_id,
79+
AnnotationAgencyAutoSubtask.url_id,
8080
Agency.id
8181
)
8282
.cte("robo_suggestions")

src/api/endpoints/annotate/all/get/queries/convert.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
from src.api.endpoints.annotate.all.get.models.url_type import URLTypeAnnotationSuggestion
66
from src.core.enums import RecordType
77
from src.db.models.impl.flag.url_validated.enums import URLType
8-
from src.db.models.impl.url.suggestion.record_type.user import UserRecordTypeSuggestion
9-
from src.db.models.impl.url.suggestion.url_type.user import UserURLTypeSuggestion
8+
from src.db.models.impl.annotation.record_type.user.user import AnnotationUserRecordType
9+
from src.db.models.impl.annotation.url_type.user.sqlalchemy import AnnotationUserURLType
1010

1111

1212
def convert_user_url_type_suggestion_to_url_type_annotation_suggestion(
13-
db_suggestions: list[UserURLTypeSuggestion]
13+
db_suggestions: list[AnnotationUserURLType]
1414
) -> list[URLTypeAnnotationSuggestion]:
1515
counter: Counter[URLType] = Counter()
1616
for suggestion in db_suggestions:
@@ -26,7 +26,7 @@ def convert_user_url_type_suggestion_to_url_type_annotation_suggestion(
2626
return anno_suggestions
2727

2828
def convert_user_record_type_suggestion_to_record_type_annotation_suggestion(
29-
db_suggestions: list[UserRecordTypeSuggestion]
29+
db_suggestions: list[AnnotationUserRecordType]
3030
) -> RecordTypeAnnotationResponseOuterInfo:
3131
counter: Counter[RecordType] = Counter()
3232
for suggestion in db_suggestions:

src/api/endpoints/annotate/all/get/queries/core.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
from src.api.endpoints.annotate._shared.queries import helper
66
from src.api.endpoints.annotate.all.get.models.response import GetNextURLForAllAnnotationResponse
77
from src.collectors.enums import URLStatus
8+
from src.db.models.impl.annotation.agency.user.sqlalchemy import AnnotationAgencyUser
9+
from src.db.models.impl.annotation.location.user.sqlalchemy import AnnotationLocationUser
810
from src.db.models.impl.flag.url_suspended.sqlalchemy import FlagURLSuspended
911
from src.db.models.impl.link.batch_url.sqlalchemy import LinkBatchURL
1012
from src.db.models.impl.url.core.sqlalchemy import URL
11-
from src.db.models.impl.url.suggestion.agency.user import UserURLAgencySuggestion
12-
from src.db.models.impl.url.suggestion.location.user.sqlalchemy import UserLocationSuggestion
13-
from src.db.models.impl.url.suggestion.record_type.user import UserRecordTypeSuggestion
14-
from src.db.models.impl.url.suggestion.url_type.user import UserURLTypeSuggestion
13+
from src.db.models.impl.annotation.record_type.user.user import AnnotationUserRecordType
14+
from src.db.models.impl.annotation.url_type.user.sqlalchemy import AnnotationUserURLType
15+
from src.db.models.views.unvalidated_url import UnvalidatedURL
16+
from src.db.models.views.url_anno_count import URLAnnotationCount
17+
from src.db.models.views.url_annotations_flags import URLAnnotationFlagsView
1518
from src.db.queries.base.builder import QueryBuilderBase
1619

1720

@@ -45,35 +48,35 @@ async def run(
4548
URL.status == URLStatus.OK.value,
4649
# Must not have been previously annotated by user
4750
~exists(
48-
select(UserURLTypeSuggestion.url_id)
51+
select(AnnotationUserURLType.url_id)
4952
.where(
50-
UserURLTypeSuggestion.url_id == URL.id,
51-
UserURLTypeSuggestion.user_id == self.user_id,
53+
AnnotationUserURLType.url_id == URL.id,
54+
AnnotationUserURLType.user_id == self.user_id,
5255
)
5356
),
5457
~exists(
55-
select(UserURLAgencySuggestion.url_id)
58+
select(AnnotationAgencyUser.url_id)
5659
.where(
57-
UserURLAgencySuggestion.url_id == URL.id,
58-
UserURLAgencySuggestion.user_id == self.user_id,
60+
AnnotationAgencyUser.url_id == URL.id,
61+
AnnotationAgencyUser.user_id == self.user_id,
5962
)
6063
),
6164
~exists(
6265
select(
63-
UserLocationSuggestion.url_id
66+
AnnotationLocationUser.url_id
6467
)
6568
.where(
66-
UserLocationSuggestion.url_id == URL.id,
67-
UserLocationSuggestion.user_id == self.user_id,
69+
AnnotationLocationUser.url_id == URL.id,
70+
AnnotationLocationUser.user_id == self.user_id,
6871
)
6972
),
7073
~exists(
7174
select(
72-
UserRecordTypeSuggestion.url_id
75+
AnnotationUserRecordType.url_id
7376
)
7477
.where(
75-
UserRecordTypeSuggestion.url_id == URL.id,
76-
UserRecordTypeSuggestion.user_id == self.user_id,
78+
AnnotationUserRecordType.url_id == URL.id,
79+
AnnotationUserRecordType.user_id == self.user_id,
7780
)
7881
),
7982
~exists(

src/api/endpoints/annotate/all/get/queries/location_/requester.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from src.api.endpoints.annotate.all.get.queries._shared.sort import sort_suggestions
77
from src.db.helpers.query import exists_url
88
from src.db.helpers.session import session_helper as sh
9+
from src.db.models.impl.annotation.location.auto.subtask.sqlalchemy import AnnotationLocationAutoSubtask
10+
from src.db.models.impl.annotation.location.auto.suggestion.sqlalchemy import AnnotationLocationAutoSuggestion
11+
from src.db.models.impl.annotation.location.user.sqlalchemy import AnnotationLocationUser
912
from src.db.models.impl.link.user_suggestion_not_found.location.sqlalchemy import LinkUserSuggestionLocationNotFound
10-
from src.db.models.impl.url.suggestion.location.auto.subtask.sqlalchemy import AutoLocationIDSubtask
11-
from src.db.models.impl.url.suggestion.location.auto.suggestion.sqlalchemy import LocationIDSubtaskSuggestion
12-
from src.db.models.impl.url.suggestion.location.user.sqlalchemy import UserLocationSuggestion
1313
from src.db.models.views.location_expanded import LocationExpandedView
1414
from src.db.templates.requester import RequesterBase
1515

@@ -25,10 +25,10 @@ async def get_location_suggestions(self, url_id: int) -> list[SuggestionModel]:
2525
.where(
2626
or_(
2727
exists_url(
28-
UserLocationSuggestion
28+
AnnotationLocationUser
2929
),
3030
exists_url(
31-
AutoLocationIDSubtask
31+
AnnotationLocationAutoSubtask
3232
)
3333
)
3434
)
@@ -37,34 +37,34 @@ async def get_location_suggestions(self, url_id: int) -> list[SuggestionModel]:
3737
# Number of users who suggested each location
3838
user_suggestions_cte = (
3939
select(
40-
UserLocationSuggestion.url_id,
41-
UserLocationSuggestion.location_id,
42-
func.count(UserLocationSuggestion.user_id).label('user_count')
40+
AnnotationLocationUser.url_id,
41+
AnnotationLocationUser.location_id,
42+
func.count(AnnotationLocationUser.user_id).label('user_count')
4343
)
4444
.group_by(
45-
UserLocationSuggestion.location_id,
46-
UserLocationSuggestion.url_id,
45+
AnnotationLocationUser.location_id,
46+
AnnotationLocationUser.url_id,
4747
)
4848
.cte("user_suggestions")
4949
)
5050
# Maximum confidence of robo annotation, if any
5151
robo_suggestions_cte = (
5252
select(
53-
AutoLocationIDSubtask.url_id,
53+
AnnotationLocationAutoSubtask.url_id,
5454
LocationExpandedView.id.label("location_id"),
55-
func.max(LocationIDSubtaskSuggestion.confidence).label('robo_confidence')
55+
func.max(AnnotationLocationAutoSuggestion.confidence).label('robo_confidence')
5656
)
5757
.join(
5858
LocationExpandedView,
59-
LocationExpandedView.id == LocationIDSubtaskSuggestion.location_id
59+
LocationExpandedView.id == AnnotationLocationAutoSuggestion.location_id
6060
)
6161
.join(
62-
AutoLocationIDSubtask,
63-
AutoLocationIDSubtask.id == LocationIDSubtaskSuggestion.subtask_id
62+
AnnotationLocationAutoSubtask,
63+
AnnotationLocationAutoSubtask.id == AnnotationLocationAutoSuggestion.subtask_id
6464
)
6565
.group_by(
6666
LocationExpandedView.id,
67-
AutoLocationIDSubtask.url_id,
67+
AnnotationLocationAutoSubtask.url_id,
6868
)
6969
.cte("robo_suggestions")
7070
)

src/api/endpoints/annotate/all/get/queries/name/core.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
from src.api.endpoints.annotate.all.get.models.name import NameAnnotationSuggestion, NameAnnotationResponseOuterInfo
77
from src.db.helpers.session import session_helper as sh
8-
from src.db.models.impl.link.user_name_suggestion.sqlalchemy import LinkUserNameSuggestion
9-
from src.db.models.impl.url.suggestion.name.enums import NameSuggestionSource
10-
from src.db.models.impl.url.suggestion.name.sqlalchemy import URLNameSuggestion
8+
from src.db.models.impl.annotation.name.suggestion.enums import NameSuggestionSource
9+
from src.db.models.impl.annotation.name.suggestion.sqlalchemy import AnnotationNameSuggestion
10+
from src.db.models.impl.annotation.name.user.sqlalchemy import LinkUserNameSuggestion
1111
from src.db.queries.base.builder import QueryBuilderBase
1212

1313

@@ -23,30 +23,30 @@ def __init__(
2323
async def run(self, session: AsyncSession) -> NameAnnotationResponseOuterInfo:
2424
query = (
2525
select(
26-
URLNameSuggestion.id.label('id'),
27-
URLNameSuggestion.suggestion.label('display_name'),
26+
AnnotationNameSuggestion.id.label('id'),
27+
AnnotationNameSuggestion.suggestion.label('display_name'),
2828
func.count(
2929
LinkUserNameSuggestion.user_id
3030
).label('user_count'),
3131
case(
32-
(URLNameSuggestion.source == NameSuggestionSource.HTML_METADATA_TITLE, 1),
32+
(AnnotationNameSuggestion.source == NameSuggestionSource.HTML_METADATA_TITLE, 1),
3333
else_=0
3434
).label("robo_count")
3535
)
3636
.outerjoin(
3737
LinkUserNameSuggestion,
38-
LinkUserNameSuggestion.suggestion_id == URLNameSuggestion.id,
38+
LinkUserNameSuggestion.suggestion_id == AnnotationNameSuggestion.id,
3939
)
4040
.where(
41-
URLNameSuggestion.url_id == self.url_id,
41+
AnnotationNameSuggestion.url_id == self.url_id,
4242
)
4343
.group_by(
44-
URLNameSuggestion.id,
45-
URLNameSuggestion.suggestion,
44+
AnnotationNameSuggestion.id,
45+
AnnotationNameSuggestion.suggestion,
4646
)
4747
.order_by(
4848
func.count(LinkUserNameSuggestion.user_id).desc(),
49-
URLNameSuggestion.id.asc(),
49+
AnnotationNameSuggestion.id.asc(),
5050
)
5151
.limit(3)
5252
)

0 commit comments

Comments
 (0)