from datetime import datetime from uuid import UUID from sqlalchemy import Select, func, select, tuple_ from app.common.base_repository import BaseRepository from app.core.pagination import Cursor from app.modules.accounts.models import Account from app.modules.reviews.models import Review from app.modules.sessions.models import SwingSession from app.modules.sessions.summary import summary_query from app.modules.uploads.models import Upload class SessionRepository(BaseRepository[SwingSession]): model = SwingSession async def get_live(self, id_: UUID) -> SwingSession | None: return await self.session.scalar( select(SwingSession).where(SwingSession.id == id_, SwingSession.deleted_at.is_(None))) def _same_athlete(self, s: SwingSession): return ( SwingSession.owner_id == s.owner_id, SwingSession.mode == s.mode, SwingSession.athlete["name"].astext == s.athlete.get("name", ""), SwingSession.deleted_at.is_(None), ) async def previous(self, s: SwingSession) -> SwingSession | None: """The owner's latest earlier analysed session for the same mode and athlete (the delta pill).""" return await self.session.scalar( select(SwingSession) .where(*self._same_athlete(s), SwingSession.analyzed_at.is_not(None), SwingSession.created_at < s.created_at) .order_by(SwingSession.created_at.desc(), SwingSession.id.desc()) .limit(1)) async def ordinal(self, s: SwingSession) -> int: """1-based position among the owner's analysed sessions for the same mode and athlete ("Swing 4"). Pending reps are not counted (they may be abandoned and cleaned up); a pending rep is numbered next. """ analysed_before = await self.session.scalar(select(func.count()).where( *self._same_athlete(s), SwingSession.analyzed_at.is_not(None), SwingSession.created_at <= s.created_at, SwingSession.id != s.id)) or 0 return analysed_before + 1 async def review_with_coach_name(self, session_id: UUID) -> tuple[Review, str] | None: row = (await self.session.execute( select(Review, Account.name).outerjoin(Account, Account.id == Review.coach_id) .where(Review.session_id == session_id))).first() return (row[0], row[1] or "") if row else None def summaries_query(self, owner_id: UUID, *, mode: str | None, is_self: bool | None, athlete_name: str | None, since: datetime | None, cursor: Cursor | None, limit: int) -> Select: query = summary_query().where(SwingSession.owner_id == owner_id) if mode: query = query.where(SwingSession.mode == mode) if is_self is not None: query = query.where(SwingSession.is_self == is_self) if athlete_name: query = query.where(SwingSession.athlete["name"].astext == athlete_name) if since: query = query.where(SwingSession.created_at >= since) if cursor: query = query.where(tuple_(SwingSession.created_at, SwingSession.id) < (cursor.created_at, cursor.id)) return query.order_by(SwingSession.created_at.desc(), SwingSession.id.desc()).limit(limit) class SessionUploadRepository(BaseRepository[Upload]): model = Upload async def object_keys(self, *ids: UUID | None) -> dict[UUID, str]: wanted = [i for i in ids if i] rows = await self.session.execute(select(Upload.id, Upload.object_key).where(Upload.id.in_(wanted))) return {id_: key for id_, key in rows.all()}