Spaces:
Running on T4
Running on T4
File size: 3,226 Bytes
2756f0f be69d1e 2756f0f be69d1e 2756f0f be69d1e 2756f0f be69d1e | 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 | import uuid
from datetime import datetime
from decimal import Decimal
from sqlalchemy import (
Boolean,
CheckConstraint,
Computed,
DateTime,
ForeignKey,
Index,
Integer,
Numeric,
SmallInteger,
String,
Text,
text,
)
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column
from app.common.base_model import Base, CreatedAtMixin, IdMixin
class SwingSession(IdMixin, CreatedAtMixin, Base):
"""One recorded rep. status is derived: pending until analyzed_at is set, then done.
queued_payload holds an ML payload waiting for the report worker (status queued); report_error is the code of
the last queued build that failed (status failed until a report exists).
athlete is a snapshot of the person on camera; marks are the user's four marks;
measurement_payload is the ML API's contract 1.0 payload; report is the AI coach swing report.
"""
__tablename__ = "sessions"
__table_args__ = (
CheckConstraint("mode IN ('h', 'p')", name="mode"),
Index("ix_sessions_owner_id_mode_created_at", "owner_id", "mode", text("created_at DESC")),
Index("ix_sessions_owner_id_analyzed_at", "owner_id", "analyzed_at",
postgresql_where=text("deleted_at IS NULL")),
Index("ix_sessions_pending_created_at", "created_at", postgresql_where=text("analyzed_at IS NULL")),
Index("ix_sessions_queued_created_at", "created_at", postgresql_where=text("queued_payload IS NOT NULL")),
)
owner_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE"))
is_self: Mapped[bool] = mapped_column(Boolean)
athlete: Mapped[dict] = mapped_column(JSONB)
mode: Mapped[str] = mapped_column(String(1))
video_upload_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("uploads.id"))
thumbnail_upload_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("uploads.id"))
duration_sec: Mapped[Decimal] = mapped_column(Numeric(6, 3))
fps: Mapped[int] = mapped_column(SmallInteger)
marks: Mapped[dict] = mapped_column(JSONB)
analysis_id: Mapped[str | None] = mapped_column(Text)
analysis_revision: Mapped[int | None] = mapped_column(Integer)
measurement_payload: Mapped[dict | None] = mapped_column(JSONB)
report: Mapped[dict | None] = mapped_column(JSONB)
overall_score: Mapped[int | None] = mapped_column(
SmallInteger, Computed("((report ->> 'overall_score'))::smallint", persisted=True)
)
analyzed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
# none_as_null: clearing it must store SQL NULL, because the queue is "queued_payload IS NOT NULL".
queued_payload: Mapped[dict | None] = mapped_column(JSONB(none_as_null=True))
report_error: Mapped[str | None] = mapped_column(String(64))
deleted_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
@property
def status(self) -> str:
if self.queued_payload is not None:
return "queued"
if self.analyzed_at:
return "done"
return "failed" if self.report_error else "pending"
|