Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: duplicate revisions annotation endpoint #974

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions annotation/annotation/annotations/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Response,
status,
)
from sqlalchemy import and_, desc
from sqlalchemy import and_, desc, func
from sqlalchemy.orm import Session
from tenant_dependency import TenantData

Expand Down Expand Up @@ -89,15 +89,22 @@ async def get_annotations(
request.dict(), AnnotatedDoc.__name__
)
# Distinct on revision, filter_lib doesn't work right with
# distinct and sorting
subquery = (
db.query(AnnotatedDoc)
.filter(AnnotatedDoc.tenant == x_current_tenant)
.distinct(AnnotatedDoc.revision)
# distinct and sorting. Prioritize latest revisions first
revision_subquery = (
db.query(
AnnotatedDoc.revision, func.max(AnnotatedDoc.date).label("latest")
)
.group_by(AnnotatedDoc.revision)
.subquery()
)
query = db.query(AnnotatedDoc).join(
subquery, AnnotatedDoc.revision == subquery.c.revision
query = (
db.query(AnnotatedDoc)
.join(
revision_subquery,
(AnnotatedDoc.revision == revision_subquery.c.revision)
& (AnnotatedDoc.date == revision_subquery.c.latest),
)
.filter(AnnotatedDoc.tenant == x_current_tenant)
)
query, pagination = filter_lib.form_query(filter_args, query)
return filter_lib.paginate(
Expand Down
Loading