banu4prasad commited on
Commit
648e87b
·
1 Parent(s): 56659e7

perf: result page optimize

Browse files
backend/app/api/routes/tests/helpers.py CHANGED
@@ -2,7 +2,7 @@ from datetime import datetime, timezone
2
  from typing import Optional
3
 
4
  from sqlalchemy import func
5
- from sqlalchemy.orm import Session, aliased, joinedload
6
 
7
  from app.models.models import (
8
  PracticeAttemptCounter,
@@ -137,7 +137,10 @@ def _first_attempts_pg(test_id: int, db: Session) -> list[TestAttempt]:
137
 
138
  return (
139
  db.query(FirstAttempt)
140
- .options(joinedload(FirstAttempt.user))
 
 
 
141
  .order_by(
142
  FirstAttempt.score.desc(),
143
  FirstAttempt.id.asc(),
@@ -167,7 +170,10 @@ def _first_attempts_generic(test_id: int, db: Session) -> list[TestAttempt]:
167
 
168
  return (
169
  db.query(TestAttempt)
170
- .options(joinedload(TestAttempt.user))
 
 
 
171
  .join(
172
  first_attempt_ids_subquery,
173
  TestAttempt.id == first_attempt_ids_subquery.c.attempt_id,
 
2
  from typing import Optional
3
 
4
  from sqlalchemy import func
5
+ from sqlalchemy.orm import Session, aliased, joinedload, selectinload
6
 
7
  from app.models.models import (
8
  PracticeAttemptCounter,
 
137
 
138
  return (
139
  db.query(FirstAttempt)
140
+ .options(
141
+ joinedload(FirstAttempt.user),
142
+ selectinload(FirstAttempt.answers),
143
+ )
144
  .order_by(
145
  FirstAttempt.score.desc(),
146
  FirstAttempt.id.asc(),
 
170
 
171
  return (
172
  db.query(TestAttempt)
173
+ .options(
174
+ joinedload(TestAttempt.user),
175
+ selectinload(TestAttempt.answers),
176
+ )
177
  .join(
178
  first_attempt_ids_subquery,
179
  TestAttempt.id == first_attempt_ids_subquery.c.attempt_id,
backend/app/api/routes/tests/results.py CHANGED
@@ -1,9 +1,9 @@
1
  from fastapi import APIRouter, Depends, HTTPException
2
- from sqlalchemy.orm import Session
3
 
4
  from app.api.deps import get_current_user
5
  from app.core.database import get_db
6
- from app.models.models import TestAttempt, TestStatus
7
  from app.api.routes.tests.helpers import (
8
  MAX_REATTEMPTS,
9
  _attempt_progress,
@@ -22,6 +22,10 @@ def get_result(
22
  ):
23
  attempt = (
24
  db.query(TestAttempt)
 
 
 
 
25
  .filter(TestAttempt.id == attempt_id, TestAttempt.user_id == current_user.id)
26
  .first()
27
  )
 
1
  from fastapi import APIRouter, Depends, HTTPException
2
+ from sqlalchemy.orm import Session, joinedload, selectinload
3
 
4
  from app.api.deps import get_current_user
5
  from app.core.database import get_db
6
+ from app.models.models import Test, TestAttempt, TestStatus
7
  from app.api.routes.tests.helpers import (
8
  MAX_REATTEMPTS,
9
  _attempt_progress,
 
22
  ):
23
  attempt = (
24
  db.query(TestAttempt)
25
+ .options(
26
+ joinedload(TestAttempt.test).selectinload(Test.questions),
27
+ selectinload(TestAttempt.answers),
28
+ )
29
  .filter(TestAttempt.id == attempt_id, TestAttempt.user_id == current_user.id)
30
  .first()
31
  )