|
1 | | -from sqlalchemy import Select, exists, select |
| 1 | +from sqlalchemy import exists, select |
2 | 2 | from sqlalchemy.ext.asyncio import AsyncSession |
3 | | -from sqlalchemy.orm import joinedload |
4 | 3 |
|
5 | 4 | from src.api.endpoints.annotate._shared.extract import extract_and_format_get_annotation_result |
| 5 | +from src.api.endpoints.annotate._shared.queries import helper |
6 | 6 | from src.api.endpoints.annotate.all.get.models.response import GetNextURLForAllAnnotationResponse |
7 | 7 | from src.collectors.enums import URLStatus |
8 | 8 | from src.db.models.impl.annotation.agency.user.sqlalchemy import AnnotationAgencyUser |
@@ -35,22 +35,9 @@ async def run( |
35 | 35 | self, |
36 | 36 | session: AsyncSession |
37 | 37 | ) -> GetNextURLForAllAnnotationResponse: |
38 | | - query = ( |
39 | | - Select(URL) |
40 | | - # URL Must be unvalidated |
41 | | - .join( |
42 | | - UnvalidatedURL, |
43 | | - UnvalidatedURL.url_id == URL.id |
44 | | - ) |
45 | | - .join( |
46 | | - URLAnnotationFlagsView, |
47 | | - URLAnnotationFlagsView.url_id == URL.id |
48 | | - ) |
49 | | - .join( |
50 | | - URLAnnotationCount, |
51 | | - URLAnnotationCount.url_id == URL.id |
52 | | - ) |
53 | | - ) |
| 38 | + query = helper.get_select() |
| 39 | + |
| 40 | + # Add user annotation-specific joins and conditions |
54 | 41 | if self.batch_id is not None: |
55 | 42 | query = query.join(LinkBatchURL).where(LinkBatchURL.batch_id == self.batch_id) |
56 | 43 | if self.url_id is not None: |
@@ -102,18 +89,11 @@ async def run( |
102 | 89 | ) |
103 | 90 | ) |
104 | 91 | ) |
105 | | - # Add load options |
106 | | - query = query.options( |
107 | | - joinedload(URL.html_content), |
108 | | - joinedload(URL.user_relevant_suggestions), |
109 | | - joinedload(URL.user_record_type_suggestions), |
110 | | - joinedload(URL.name_suggestions), |
111 | | - ) |
112 | 92 |
|
113 | | - query = query.order_by( |
114 | | - URLAnnotationCount.total_anno_count.desc(), |
115 | | - URL.id.asc() |
116 | | - ).limit(1) |
| 93 | + |
| 94 | + # Conclude query with limit and sorting |
| 95 | + query = helper.conclude(query) |
| 96 | + |
117 | 97 | raw_results = (await session.execute(query)).unique() |
118 | 98 | url: URL | None = raw_results.scalars().one_or_none() |
119 | 99 | if url is None: |
|
0 commit comments