"""Database tables (SQLModel). Shared by the API process and the worker subprocesses.""" from __future__ import annotations import uuid from datetime import UTC, datetime from typing import Any from sqlalchemy import JSON, Column from sqlmodel import Field, SQLModel def new_id() -> str: return uuid.uuid4().hex def utcnow() -> datetime: return datetime.now(UTC) LOCAL_OWNER = "local" # every song when running locally; on the public site, a hash of the visitor's cookie class Song(SQLModel, table=True): id: str = Field(default_factory=new_id, primary_key=True) owner: str = Field(default=LOCAL_OWNER, index=True) client: str = "" # public site: hashed network address of the uploader (per-address active-job limit) source_type: str = "upload" # upload | youtube source_url: str | None = None # canonical YouTube watch URL for source_type "youtube" original_filename: str = "" title: str artist: str | None = None duration_sec: float | None = None created_at: datetime = Field(default_factory=utcnow) status: str = "queued" # queued | processing | ready | failed quality: str = "fast" device_used: str | None = None null_test_snr_db: float | None = None class Stem(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) song_id: str = Field(index=True, foreign_key="song.id") name: str label: str kind: str = "base" # base | extra prompt: str | None = None file_path: str rms_db: float = -200.0 detected: bool = True experimental: bool = False color: str = "#94a3b8" order: int = 0 version: int = 1 # bumped whenever the file is rewritten (cache-busting) class Job(SQLModel, table=True): id: str = Field(default_factory=new_id, primary_key=True) song_id: str = Field(index=True, foreign_key="song.id") type: str # split | extract status: str = "queued" # queued | running | done | failed | cancelled stage: str = "queued" progress: float = 0.0 stage_progress: float = 0.0 message: str | None = None error: str | None = None eta_sec: float | None = None params: dict[str, Any] = Field(default_factory=dict, sa_column=Column(JSON)) pid: int | None = None created_at: datetime = Field(default_factory=utcnow) started_at: datetime | None = None finished_at: datetime | None = None class Mix(SQLModel, table=True): id: str = Field(default_factory=new_id, primary_key=True) song_id: str = Field(index=True, foreign_key="song.id") name: str settings: dict[str, Any] = Field(default_factory=dict, sa_column=Column(JSON)) created_at: datetime = Field(default_factory=utcnow) class Usage(SQLModel, table=True): """One row per job a visitor of the public site started; kept after the song is deleted, so deleting songs doesn't reset the daily limits (see stemdeck/quotas.py).""" id: int | None = Field(default=None, primary_key=True) owner: str = Field(index=True) client: str = Field(index=True) # hashed network address kind: str # split | extract | retry created_at: datetime = Field(default_factory=utcnow, index=True) TERMINAL_JOB_STATUSES = frozenset({"done", "failed", "cancelled"}) ACTIVE_JOB_STATUSES = frozenset({"queued", "running"})