179 lines
10 KiB
Python
179 lines
10 KiB
Python
"""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 ImportJob(Base):
|
|
"""Server-side dataset import (e.g. 3GB zip): polled by the UI/admin."""
|
|
__tablename__ = "imports"
|
|
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=_uid)
|
|
project_id: Mapped[str] = mapped_column(String(32), index=True)
|
|
kind: Mapped[str] = mapped_column(String(32), default="zip-coco")
|
|
status: Mapped[str] = mapped_column(String(32), default="queued", index=True)
|
|
source: Mapped[str] = mapped_column(String(1024), default="")
|
|
total_files: Mapped[int] = mapped_column(Integer, default=0)
|
|
done_files: Mapped[int] = mapped_column(Integer, default=0)
|
|
saved_annotations: Mapped[int] = mapped_column(Integer, default=0)
|
|
error: Mapped[str | None] = mapped_column(Text, 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())
|
|
|
|
|
|
class UploadJob(Base):
|
|
"""Durable browser upload batch; bytes live in object storage."""
|
|
__tablename__ = "upload_jobs"
|
|
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=_uid)
|
|
project_id: Mapped[str] = mapped_column(String(32), index=True)
|
|
kind: Mapped[str] = mapped_column(String(32), default="images")
|
|
status: Mapped[str] = mapped_column(String(32), default="uploading", index=True)
|
|
total_files: Mapped[int] = mapped_column(Integer, default=0)
|
|
uploaded_files: Mapped[int] = mapped_column(Integer, default=0)
|
|
processed_files: Mapped[int] = mapped_column(Integer, default=0)
|
|
failed_files: Mapped[int] = mapped_column(Integer, default=0)
|
|
error: Mapped[str | None] = mapped_column(Text, 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())
|
|
|
|
|
|
class UploadItem(Base):
|
|
__tablename__ = "upload_items"
|
|
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=_uid)
|
|
job_id: Mapped[str] = mapped_column(ForeignKey("upload_jobs.id", ondelete="CASCADE"), index=True)
|
|
project_id: Mapped[str] = mapped_column(String(32), index=True)
|
|
file_name: Mapped[str] = mapped_column(String(1024))
|
|
content_type: Mapped[str] = mapped_column(String(255), default="application/octet-stream")
|
|
size: Mapped[int] = mapped_column(Integer, default=0)
|
|
width: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
|
|
height: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
|
|
r2_key: Mapped[str] = mapped_column(String(1024))
|
|
multipart_upload_id: Mapped[str | None] = mapped_column(String(512), nullable=True, index=True)
|
|
status: Mapped[str] = mapped_column(String(32), default="pending", index=True)
|
|
image_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
error: Mapped[str | None] = mapped_column(Text, 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())
|