File size: 2,994 Bytes
2756f0f
 
 
 
 
 
 
 
 
 
 
a8788e5
2756f0f
 
 
 
 
 
 
 
3ee736f
2756f0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ee736f
2756f0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ee736f
2756f0f
 
 
 
 
a8788e5
 
 
 
2756f0f
a8788e5
 
2756f0f
 
a8788e5
 
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
79
80
81
"""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