mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
The engine carried its tuning history in its comments: measurement logs for PIVOT_B, the weight a facet held before the company split, the shape of the cold-start feed before the Bayesian average. None of that describes what the code does now, and it belongs in the history rather than beside the values. Also folds serving's ROM loader into feed's, which differed only by the visibility filter applied afterwards. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from sqlalchemy import Float, ForeignKey, Index
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from models.base import BaseModel
|
|
from utils.database import CustomJSON
|
|
|
|
if TYPE_CHECKING:
|
|
from models.rom import Rom
|
|
|
|
|
|
class RomSimilarity(BaseModel):
|
|
"""A precomputed edge of the item-item similarity graph.
|
|
|
|
Written wholesale by the recommendations task so the rows are consistent
|
|
with a single IDF snapshot of the library, and only the top neighbours of
|
|
each ROM are kept.
|
|
|
|
Edges are stored in both directions: the scoring is symmetric but the
|
|
per-ROM top-N cut is not, and duplicating avoids an OR across two indexed
|
|
columns on every read.
|
|
"""
|
|
|
|
__tablename__ = "rom_similarity"
|
|
|
|
__table_args__ = (
|
|
Index("idx_rom_similarity_rom_score", "rom_id", "score"),
|
|
Index("idx_rom_similarity_related_rom_id", "related_rom_id"),
|
|
)
|
|
|
|
rom_id: Mapped[int] = mapped_column(
|
|
ForeignKey("roms.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
related_rom_id: Mapped[int] = mapped_column(
|
|
ForeignKey("roms.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
|
|
score: Mapped[float] = mapped_column(Float(), nullable=False)
|
|
|
|
# The facets that drove the score, e.g. [{"facet": "franchise",
|
|
# "value": "Metroid"}], so the UI can say why without recomputing.
|
|
reasons: Mapped[list[dict[str, Any]] | None] = mapped_column(
|
|
CustomJSON(), default=[]
|
|
)
|
|
|
|
# No ORM-level delete cascade: ROMs are removed with a bulk `delete()`,
|
|
# so the foreign keys' ON DELETE CASCADE is what clears both directions.
|
|
rom: Mapped[Rom] = relationship(
|
|
"Rom",
|
|
foreign_keys=[rom_id],
|
|
back_populates="similar_roms",
|
|
lazy="raise",
|
|
passive_deletes=True,
|
|
)
|
|
related_rom: Mapped[Rom] = relationship(
|
|
"Rom", foreign_keys=[related_rom_id], lazy="raise", passive_deletes=True
|
|
)
|