Spaces:
Running on T4
Running on T4
File size: 3,650 Bytes
2756f0f fe40b87 2756f0f 4598c11 2756f0f fe40b87 2756f0f fe40b87 2756f0f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | 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()}
|