Backend API: auth, projects/images/annotations, R2 presign, tracing, thumbnails, pagination, tests, docs
This commit is contained in:
parent
5c5deed2f5
commit
f8ed08b854
49 changed files with 2626 additions and 0 deletions
14
models/__init__.py
Normal file
14
models/__init__.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
"""DB tables. Metadata lives in Postgres; bytes live in R2 (r2_key only)."""
|
||||
from models.tables import (
|
||||
AIJob,
|
||||
ActionEvent,
|
||||
Annotation,
|
||||
ExportJob,
|
||||
Image,
|
||||
LabelClass,
|
||||
Project,
|
||||
User,
|
||||
)
|
||||
|
||||
__all__ = ["AIJob", "ActionEvent", "Annotation", "ExportJob", "Image",
|
||||
"LabelClass", "Project", "User"]
|
||||
129
models/tables.py
Normal file
129
models/tables.py
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
"""SQLAlchemy models. Metadata lives in Postgres; bytes live in R2 (r2_key only)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import JSON, DateTime, ForeignKey, Integer, String, Text, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from src.core.db import Base
|
||||
|
||||
|
||||
def _uid() -> str:
|
||||
return uuid.uuid4().hex[:12]
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
email: Mapped[str] = mapped_column(String(255), unique=True, index=True)
|
||||
name: Mapped[str] = mapped_column(String(255))
|
||||
role: Mapped[str] = mapped_column(String(32), default="annotator") # admin|manager|annotator|reviewer
|
||||
status: Mapped[str] = mapped_column(String(32), default="active")
|
||||
password_hash: Mapped[str] = mapped_column(String(255), default="", server_default="")
|
||||
assigned_project_ids: Mapped[list] = mapped_column(JSON, default=list)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
|
||||
class Project(Base):
|
||||
__tablename__ = "projects"
|
||||
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=_uid)
|
||||
slug: Mapped[str] = mapped_column(String(255), unique=True, index=True)
|
||||
name: Mapped[str] = mapped_column(String(255))
|
||||
description: Mapped[str] = mapped_column(Text, default="", server_default="")
|
||||
# classification | object_detection | segmentation (+ audio later, no schema change)
|
||||
task_type: Mapped[str] = mapped_column(String(32), index=True)
|
||||
status: Mapped[str] = mapped_column(String(32), default="draft")
|
||||
thumbnail_r2_key: Mapped[str | None] = mapped_column(String(1024), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
|
||||
|
||||
classes: Mapped[list["LabelClass"]] = relationship(back_populates="project", cascade="all, delete-orphan")
|
||||
images: Mapped[list["Image"]] = relationship(back_populates="project", cascade="all, delete-orphan")
|
||||
|
||||
|
||||
class LabelClass(Base):
|
||||
__tablename__ = "label_classes"
|
||||
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=_uid)
|
||||
project_id: Mapped[str] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"), index=True)
|
||||
name: Mapped[str] = mapped_column(String(255))
|
||||
color: Mapped[str] = mapped_column(String(32), default="#888888")
|
||||
shortcut_key: Mapped[str | None] = mapped_column(String(8), nullable=True)
|
||||
|
||||
project: Mapped[Project] = relationship(back_populates="classes")
|
||||
|
||||
|
||||
class Image(Base):
|
||||
__tablename__ = "images"
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True, default=_uid)
|
||||
project_id: Mapped[str] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"), index=True)
|
||||
file_name: Mapped[str] = mapped_column(String(1024))
|
||||
r2_key: Mapped[str] = mapped_column(String(1024))
|
||||
thumbnail_r2_key: Mapped[str | None] = mapped_column(String(1024), nullable=True)
|
||||
thumbnail_small_r2_key: Mapped[str | None] = mapped_column(String(1024), nullable=True)
|
||||
width: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
|
||||
height: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
|
||||
# unassigned | in_progress | pending_review | completed (mirrors frontend ImageItem)
|
||||
status: Mapped[str] = mapped_column(String(32), default="unassigned", index=True)
|
||||
assigned_to: Mapped[str | None] = mapped_column(String(36), nullable=True)
|
||||
reviewer_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
object_count: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
|
||||
classification_label_id: Mapped[str | None] = mapped_column(String(32), nullable=True)
|
||||
flags: Mapped[dict] = mapped_column(JSON, default=dict)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
project: Mapped[Project] = relationship(back_populates="images")
|
||||
|
||||
|
||||
class Annotation(Base):
|
||||
"""One row per image (upsert). shapes = list[AnnotationShape] like the frontend."""
|
||||
__tablename__ = "annotations"
|
||||
image_id: Mapped[str] = mapped_column(ForeignKey("images.id", ondelete="CASCADE"), primary_key=True)
|
||||
project_id: Mapped[str] = mapped_column(String(32), index=True)
|
||||
classification_label_id: Mapped[str | None] = mapped_column(String(32), nullable=True)
|
||||
flags: Mapped[dict] = mapped_column(JSON, default=dict)
|
||||
shapes: Mapped[list] = mapped_column(JSON, default=list)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
|
||||
|
||||
|
||||
class ActionEvent(Base):
|
||||
"""One user operation for tracelog/replay: who did what, on which entity,
|
||||
with which payload. Written by the background drainer, never the hot path."""
|
||||
__tablename__ = "action_events"
|
||||
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=_uid)
|
||||
session_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
|
||||
trace_id: Mapped[str | None] = mapped_column(String(32), nullable=True, index=True)
|
||||
user_id: Mapped[str] = mapped_column(String(64), default="anonymous")
|
||||
action: Mapped[str] = mapped_column(String(64), index=True)
|
||||
project_id: Mapped[str | None] = mapped_column(String(32), nullable=True, index=True)
|
||||
image_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
|
||||
payload: Mapped[dict] = mapped_column(JSON, default=dict)
|
||||
result: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
||||
latency_ms: Mapped[float | None] = mapped_column(nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
|
||||
class ExportJob(Base):
|
||||
__tablename__ = "exports"
|
||||
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=_uid)
|
||||
project_id: Mapped[str] = mapped_column(String(32), index=True)
|
||||
format: Mapped[str] = mapped_column(String(32)) # coco_json | yolo_txt | labelme_json | mask_png
|
||||
status: Mapped[str] = mapped_column(String(32), default="queued")
|
||||
r2_key: Mapped[str | None] = mapped_column(String(1024), nullable=True)
|
||||
record_count: Mapped[int] = mapped_column(Integer, default=0)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
|
||||
class AIJob(Base):
|
||||
"""Assist queue (SAM segment / auto-detect / classify). Worker fills result later."""
|
||||
__tablename__ = "ai_jobs"
|
||||
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=_uid)
|
||||
project_id: Mapped[str] = mapped_column(String(32), index=True)
|
||||
image_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
kind: Mapped[str] = mapped_column(String(32)) # segment | detect | classify
|
||||
status: Mapped[str] = mapped_column(String(32), default="queued", index=True)
|
||||
params: Mapped[dict] = mapped_column(JSON, default=dict)
|
||||
result: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
||||
result_r2_key: Mapped[str | None] = mapped_column(String(1024), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
Loading…
Add table
Add a link
Reference in a new issue