Skip to content

Commit

Permalink
Fix fetching of latest index attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
Weves committed Aug 13, 2023
1 parent 0381715 commit 17c0009
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions backend/danswer/db/index_attempt.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ def get_latest_index_attempts(
db_session: Session,
) -> Sequence[IndexAttempt]:
ids_stmt = select(
IndexAttempt.id, func.max(IndexAttempt.time_created).label("max_updated_at")
IndexAttempt.connector_id,
IndexAttempt.credential_id,
func.max(IndexAttempt.time_created).label("max_time_created"),
)

where_stmts: list[ColumnElement] = []
Expand All @@ -124,10 +126,20 @@ def get_latest_index_attempts(
)
if where_stmts:
ids_stmt = ids_stmt.where(or_(*where_stmts))
ids_stmt = ids_stmt.group_by(IndexAttempt.id)
ids_stmt = ids_stmt.group_by(IndexAttempt.connector_id, IndexAttempt.credential_id)
ids_subqery = ids_stmt.subquery()

stmt = select(IndexAttempt).join(ids_subqery, ids_subqery.c.id == IndexAttempt.id)
stmt = (
select(IndexAttempt)
.join(
ids_subqery,
and_(
ids_subqery.c.connector_id == IndexAttempt.connector_id,
ids_subqery.c.credential_id == IndexAttempt.credential_id,
),
)
.where(IndexAttempt.time_created == ids_subqery.c.max_time_created)
)
return db_session.execute(stmt).scalars().all()


Expand Down

0 comments on commit 17c0009

Please sign in to comment.