Spaces:
Running on T4
Running on T4
Download backend/app/modules/sessions/summary.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 2.99 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/modules/sessions/summary.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/app/modules/sessions/summary.py
-
curl -L -o summary.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/modules/sessions/summary.py
2.99 kB
| """Session summaries: the one list-item shape used by GET /sessions, the coach roster, player page and queue.""" | |
| from collections.abc import Sequence | |
| from datetime import datetime | |
| from uuid import UUID | |
| from sqlalchemy import Select, select | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from sqlalchemy.orm import aliased | |
| from app.common.schemas import ResponseModel | |
| from app.integrations.pitching_reports import report_for_read | |
| from app.integrations.storage import StorageClient | |
| from app.modules.accounts.models import Account | |
| from app.modules.reviews.models import Review | |
| from app.modules.sessions.models import SwingSession | |
| from app.modules.uploads.models import Upload | |
| class FocusSummary(ResponseModel): | |
| metric_id: str | None # null for a general practice focus (nothing could be rated) | |
| display_name: str | |
| drill_id: str | None | |
| class ReviewSummary(ResponseModel): | |
| coach_name: str | |
| at: datetime | |
| class SessionSummary(ResponseModel): | |
| id: UUID | |
| owner_id: UUID | |
| mode: str | |
| is_self: bool | |
| athlete_name: str | |
| status: str | |
| overall_score: int | None | |
| overall_score_is_tentative: bool | |
| focus: FocusSummary | None | |
| review: ReviewSummary | None | |
| thumbnail_url: str | None | |
| created_at: datetime | |
| _Coach = aliased(Account) | |
| def summary_query() -> Select: | |
| """Base select for summaries; callers add where/order_by/limit. Deleted sessions are always excluded.""" | |
| return ( | |
| select(SwingSession, Upload.object_key, Review.updated_at, _Coach.name) | |
| .outerjoin(Upload, Upload.id == SwingSession.thumbnail_upload_id) | |
| .outerjoin(Review, Review.session_id == SwingSession.id) | |
| .outerjoin(_Coach, _Coach.id == Review.coach_id) | |
| .where(SwingSession.deleted_at.is_(None)) | |
| ) | |
| def _focus(report: dict | None) -> FocusSummary | None: | |
| focus = (report or {}).get("focus_area") | |
| if not focus: | |
| return None | |
| return FocusSummary(metric_id=focus.get("metric_id"), display_name=focus["display_name"], | |
| drill_id=(focus.get("drill") or {}).get("drill_id")) | |
| async def load_summaries(db: AsyncSession, storage: StorageClient, query: Select) -> list[SessionSummary]: | |
| rows: Sequence = (await db.execute(query)).all() | |
| summaries = [] | |
| for s, thumb_key, reviewed_at, coach_name in rows: | |
| report = report_for_read(s.mode, s.report, s.measurement_payload) | |
| summaries.append(SessionSummary( | |
| id=s.id, owner_id=s.owner_id, mode=s.mode, is_self=s.is_self, athlete_name=s.athlete.get("name", ""), | |
| status=s.status, overall_score=s.overall_score if report is s.report else None, | |
| overall_score_is_tentative=bool((report or {}).get("overall_score_is_tentative")), focus=_focus(report), | |
| review=ReviewSummary(coach_name=coach_name or "", at=reviewed_at) if reviewed_at else None, | |
| thumbnail_url=storage.presign_get(thumb_key) if thumb_key else None, created_at=s.created_at, | |
| )) | |
| return summaries | |