File size: 3,410 Bytes
25e4412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""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"})