Ekran tagu (jeden tag, sort=release_date, 200 scen) sortowal po dacie z `scenes`, a filtrowal po `scene_tags`. Postgres szedl od najnowszej sceny calego katalogu i kazda sprawdzal; przy tagach rzedu 40-60 tys. scen (`rough-sex`) to 46 tys. scen na strone, 7-15 s na zimnym cache. Dwie przebudowy samego zapytania nie pomogly (2x albo 30 s na duzych tagach), cache tez nie, bo wejscia rozkladaja sie na setki tagow. `scene_tags.sort_date` (0042) trzyma ta sama date co `_effective_date_desc()`, pilnowana przez wyzwalacze w bazie, indeks (tag_id, sort_date DESC, scene_id DESC) w 0043. Wypelnianie 23 mln wierszy: scripts/backfill_scene_tags_sort_date.py, partiami z pauza. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
140 lines
5.9 KiB
Python
140 lines
5.9 KiB
Python
import enum
|
|
import uuid
|
|
from datetime import date, datetime
|
|
|
|
from sqlalchemy import (
|
|
Boolean,
|
|
Date,
|
|
DateTime,
|
|
Enum,
|
|
Float,
|
|
ForeignKey,
|
|
Integer,
|
|
SmallInteger,
|
|
String,
|
|
Text,
|
|
UniqueConstraint,
|
|
func,
|
|
)
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.models.base import Base, TimestampMixin, UUIDPKMixin
|
|
|
|
|
|
class FingerprintKind(str, enum.Enum):
|
|
phash = "phash"
|
|
oshash = "oshash"
|
|
md5 = "md5"
|
|
|
|
|
|
class Scene(UUIDPKMixin, TimestampMixin, Base):
|
|
__tablename__ = "scenes"
|
|
|
|
title: Mapped[str] = mapped_column(String, nullable=False)
|
|
title_normalized: Mapped[str] = mapped_column(String, nullable=False, index=True)
|
|
slug: Mapped[str | None] = mapped_column(String, index=True)
|
|
release_date: Mapped[date | None] = mapped_column(Date, index=True)
|
|
studio_id: Mapped[uuid.UUID | None] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("studios.id", ondelete="SET NULL"), index=True
|
|
)
|
|
duration_sec: Mapped[int | None] = mapped_column(Integer)
|
|
description: Mapped[str | None] = mapped_column(Text)
|
|
code: Mapped[str | None] = mapped_column(String(128), index=True)
|
|
director: Mapped[str | None] = mapped_column(String(256))
|
|
# True = scena wpadła masowym backfillem katalogu (deep-crawl, głębokie strony), NIE
|
|
# świeży ingest. Tube podają datę importu jako release_date, więc stary backfill
|
|
# udaje "nowość" (licznik +N w ulubionych). Ten flag pozwala go wykluczyć z "nowych"
|
|
# bez chowania sceny. Ustawiany przy TWORZENIU (deep_crawl page > próg); świeży
|
|
# latest-crawl + delta TPDB/StashDB → False. Patrz app/scheduler/deep_crawl.py.
|
|
backfill: Mapped[bool] = mapped_column(
|
|
Boolean, nullable=False, server_default="false", index=True
|
|
)
|
|
|
|
# Najwyzsza ZNANA jakosc sposrod zywych zrodel sceny (720, 1080, 2160...).
|
|
# NULL = zadne zywe zrodlo nie podalo jakosci, czyli 93,5% katalogu; filtr jakosci
|
|
# takie sceny odsiewa. Odswiezane w `app/scheduler/taxonomy_counts.py`.
|
|
# Denormalizacja jest tu KONIECZNA, nie wygodna: uzasadnienie i pomiary w migracji
|
|
# 0029_scenes_max_quality.
|
|
max_quality_p: Mapped[int | None] = mapped_column(SmallInteger)
|
|
|
|
|
|
class SceneExternalRef(Base):
|
|
__tablename__ = "scene_external_refs"
|
|
|
|
source_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("sources.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
external_id: Mapped[str] = mapped_column(String, primary_key=True)
|
|
scene_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("scenes.id", ondelete="CASCADE"), nullable=False, index=True
|
|
)
|
|
confidence: Mapped[float] = mapped_column(Float, nullable=False, default=1.0, server_default="1.0")
|
|
url: Mapped[str | None] = mapped_column(String(1024))
|
|
first_seen: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now(), nullable=False
|
|
)
|
|
last_seen: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now(), nullable=False
|
|
)
|
|
|
|
|
|
class SceneFingerprint(Base):
|
|
"""Fingerprinty zaciągnięte ze źródeł (TPDB/StashDB), nie liczone lokalnie."""
|
|
|
|
__tablename__ = "scene_fingerprints"
|
|
__table_args__ = (UniqueConstraint("scene_id", "kind", "value"),)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), primary_key=True, server_default=func.gen_random_uuid()
|
|
)
|
|
scene_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("scenes.id", ondelete="CASCADE"), nullable=False, index=True
|
|
)
|
|
kind: Mapped[FingerprintKind] = mapped_column(Enum(FingerprintKind, name="fingerprint_kind"))
|
|
value: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
|
|
source_id: Mapped[uuid.UUID | None] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("sources.id", ondelete="SET NULL")
|
|
)
|
|
|
|
|
|
class ScenePerformer(Base):
|
|
__tablename__ = "scene_performers"
|
|
|
|
scene_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("scenes.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
performer_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("performers.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
role: Mapped[str | None] = mapped_column(String(64))
|
|
position: Mapped[int | None] = mapped_column(Integer)
|
|
as_alias: Mapped[str | None] = mapped_column(String(256))
|
|
#: Skąd wzięło się TO przypisanie. Bez tego nie da się odróżnić obsady z kanonu od
|
|
#: performera doklejonego przez luźny tube-search (ten dopasowuje wynik po JEDNYM
|
|
#: tokenie zapytania, więc „Rapture" łapała każdą scenę ze słowem „rapture",
|
|
#: 30 z 72 jej scen było fałszywych i trzeba je było przeglądać ręcznie).
|
|
#: Trzymamy NAJBARDZIEJ wiarygodne źródło, jakie przypisało: kanoniczne
|
|
#: (tpdb/stashdb) nadpisuje scraperowe, nigdy odwrotnie. NULL = wiersz sprzed
|
|
#: migracji 0026, źródła nie da się odtworzyć wstecz.
|
|
source_id: Mapped[uuid.UUID | None] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("sources.id", ondelete="SET NULL"), index=True
|
|
)
|
|
|
|
|
|
class SceneTag(Base):
|
|
__tablename__ = "scene_tags"
|
|
|
|
scene_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("scenes.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
tag_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("tags.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
source_id: Mapped[uuid.UUID | None] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("sources.id", ondelete="SET NULL")
|
|
)
|
|
# Data sortowania sceny (premiera albo dzien dodania), powielona tu pod indeks
|
|
# "najnowsze sceny tagu" (GOON-64, migracje 0042 i 0043). Pisze ja WYZWALACZ w bazie,
|
|
# nie aplikacja: wartosc podana przy INSERT i tak zostanie nadpisana.
|
|
sort_date: Mapped[date | None] = mapped_column(Date)
|