Get recent practice sessions using an optimized query.
(
session: Session, collection_id: uuid.UUID, limit: int = 10
)
| 40 | |
| 41 | |
| 42 | def _get_recent_sessions( |
| 43 | session: Session, collection_id: uuid.UUID, limit: int = 10 |
| 44 | ) -> list[PracticeSessionStats]: |
| 45 | """Get recent practice sessions using an optimized query.""" |
| 46 | statement = ( |
| 47 | select(PracticeSession) |
| 48 | .where( |
| 49 | PracticeSession.collection_id == collection_id, |
| 50 | PracticeSession.is_completed, |
| 51 | ) |
| 52 | .order_by(PracticeSession.created_at.asc()) |
| 53 | .limit(limit) |
| 54 | ) |
| 55 | |
| 56 | sessions = session.exec(statement).all() |
| 57 | return [ |
| 58 | PracticeSessionStats( |
| 59 | id=s.id, |
| 60 | created_at=s.created_at, |
| 61 | cards_practiced=s.cards_practiced, |
| 62 | correct_answers=s.correct_answers, |
| 63 | total_cards=s.total_cards, |
| 64 | is_completed=s.is_completed, |
| 65 | ) |
| 66 | for s in sessions |
| 67 | ] |
| 68 | |
| 69 | |
| 70 | def _get_difficult_cards( |