mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 18:42:34 +01:00
* fix(platforms): count only the roms the caller can see in rom_count and fs_size_bytes Platform.rom_count and fs_size_bytes are column properties over every rom row on the platform, and the platform endpoints only filter hidden platforms. So the platform header's "In library" count and "On disk" size included roms hidden from the caller (directly, or by an age limit), while the gallery left them out. Server Stats was off the same way: its per-platform metadata coverage divides /api/stats' visibility-filtered match counts by the unfiltered rom_count, so a restricted user saw coverage too low and platform counts and sizes too high. For a restricted caller, the list, detail and update endpoints now subtract the totals of the roms RomVisibilityFilter hides (DBStatsHandler.get_hidden_rom_totals_by_platform, next to the other per-platform aggregates). Direct hides are matched by primary key, so a few hidden roms cost a few lookups rather than a pass over every rom on the platform. Admins and other unrestricted callers keep the model's values and skip the query. On a generated library of 50 platforms and 100k roms (MariaDB), the platform list takes ~44 ms; this adds ~4 ms with 100 roms hidden, ~44 ms with 5,000, and ~60 ms with an age limit (min_age isn't indexed, so that case scans the platforms' roms). Fixes #5312 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(platforms): read the hidden roms' totals in the platform query Addresses Greptile's review of #5313: - Counts can become negative: the hidden totals came from a second query, so a rom added between the two could be subtracted without having been counted. They now come from a grouped subquery outer-joined into the statement that computes the full totals, so both read one snapshot (on PostgreSQL's READ COMMITTED too, which a shared transaction wouldn't give). - Counting rules live in endpoints: the endpoint helper is gone. get_platform and get_platforms take the caller's RomVisibilityFilter (unrestricted by default) and return platforms with the visible totals; DBStatsHandler.get_hidden_rom_totals_by_platform is removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(platforms): code-review fixes for visible platform totals - Re-read rows with populate_existing so a shared session never subtracts twice - Narrow the hidden-totals subquery to the one platform get_platform reads - Re-read after update only for restricted callers, and 404 instead of leaking full totals - Test the update endpoint's restricted totals and the shared-session read Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(platforms): simplify pass on visible platform totals Gate the row clause in one condition with a why comment, fold the MariaDB narrowing reason into the docstring, and drop restating comments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(platforms): count only visible roms in /supported and RetroArch listings - Move the hidden-ROM predicate into RomVisibilityFilter.hidden_on_visible_platforms_clause, with the age rule spelled as ranges an index on min_age can serve - get_platforms reads hidden platforms from the visibility filter instead of a second hidden_platform_ids argument - /api/platforms/supported lists a hidden platform as unmatched and counts only the caller's ROMs on the rest - RetroArch WebDAV skips a platform whose ROMs are all hidden from the caller Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * perf(roms): index platform_id, min_age, fs_size_bytes for age-limited totals On a 73k-ROM copy of a real library (MariaDB 13), GET /api/platforms for an age-limited user went from 208 ms to 55 ms: the hidden-totals aggregate is now an index-only scan instead of a full scan of the wide roms rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
141 lines
5.1 KiB
Python
141 lines
5.1 KiB
Python
"""The ROM visibility rule a caller's queries and loaded-ROM checks share."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Callable
|
|
from dataclasses import dataclass
|
|
from functools import cached_property
|
|
from typing import Final, Protocol
|
|
|
|
from sqlalchemy import and_, not_, or_
|
|
from sqlalchemy.orm import InstrumentedAttribute
|
|
from sqlalchemy.sql.elements import ColumnElement
|
|
|
|
from models.rom import Rom
|
|
|
|
|
|
class VisibilityColumns(Protocol):
|
|
"""The ROM columns `RomVisibilityFilter.allows` reads."""
|
|
|
|
@property
|
|
def id(self) -> int: ...
|
|
|
|
@property
|
|
def platform_id(self) -> int: ...
|
|
|
|
@property
|
|
def min_age(self) -> int | None: ...
|
|
|
|
|
|
def _always_visible(rom: VisibilityColumns) -> bool:
|
|
return True
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class RomVisibilityFilter:
|
|
"""Which ROMs a caller may see: none on a hidden platform, none hidden
|
|
directly, and none rated above the age limit."""
|
|
|
|
hidden_platform_ids: frozenset[int] = frozenset()
|
|
hidden_rom_ids: frozenset[int] = frozenset()
|
|
age_limit: int | None = None
|
|
# Whether a ROM no rating covers is hidden while an age rule applies.
|
|
hide_unrated_roms: bool = False
|
|
|
|
@property
|
|
def has_age_rule(self) -> bool:
|
|
return self.age_limit is not None or self.hide_unrated_roms
|
|
|
|
@property
|
|
def is_unrestricted(self) -> bool:
|
|
return not (self.hidden_platform_ids or self.hidden_rom_ids) and (
|
|
not self.has_age_rule
|
|
)
|
|
|
|
def clauses(
|
|
self,
|
|
*,
|
|
rom_id_col: InstrumentedAttribute[int] = Rom.id,
|
|
platform_id_col: InstrumentedAttribute[int] = Rom.platform_id,
|
|
min_age_col: InstrumentedAttribute[int | None] = Rom.min_age,
|
|
) -> list[ColumnElement[bool]]:
|
|
"""WHERE clauses keeping only visible ROMs, on columns a query on the
|
|
`roms_facets` mirror overrides to skip a join to `roms`."""
|
|
clauses = self._row_clauses(platform_id_col, min_age_col)
|
|
if self.hidden_rom_ids:
|
|
clauses.append(rom_id_col.not_in(self.hidden_rom_ids))
|
|
return clauses
|
|
|
|
def row_hidden_clause(self) -> ColumnElement[bool] | None:
|
|
"""WHERE clause matching the `roms` rows a rule other than a direct hide
|
|
keeps out, or None when no such rule applies."""
|
|
clauses = self._row_clauses(Rom.platform_id, Rom.min_age)
|
|
return not_(and_(*clauses)) if clauses else None
|
|
|
|
def hidden_on_visible_platforms_clause(self) -> ColumnElement[bool] | None:
|
|
"""WHERE clause matching the `roms` rows hidden directly or by the age rule,
|
|
or None when neither applies; hidden platforms are left to the caller."""
|
|
clauses: list[ColumnElement[bool]] = []
|
|
if self.hidden_rom_ids:
|
|
clauses.append(Rom.id.in_(self.hidden_rom_ids))
|
|
if self.has_age_rule:
|
|
clauses.append(self._age_hidden_clause(Rom.min_age))
|
|
return or_(*clauses) if clauses else None
|
|
|
|
def _row_clauses(
|
|
self,
|
|
platform_id_col: InstrumentedAttribute[int],
|
|
min_age_col: InstrumentedAttribute[int | None],
|
|
) -> list[ColumnElement[bool]]:
|
|
"""The rules decided by a ROM's row rather than by its id alone, each true
|
|
or false and never NULL so `row_hidden_clause` can negate it."""
|
|
clauses: list[ColumnElement[bool]] = []
|
|
if self.hidden_platform_ids:
|
|
clauses.append(platform_id_col.not_in(self.hidden_platform_ids))
|
|
if self.has_age_rule:
|
|
clauses.append(self._age_clause(min_age_col))
|
|
return clauses
|
|
|
|
def _age_clause(
|
|
self, min_age_col: InstrumentedAttribute[int | None]
|
|
) -> ColumnElement[bool]:
|
|
if self.age_limit is None:
|
|
return min_age_col.is_not(None)
|
|
if self.hide_unrated_roms:
|
|
return and_(min_age_col.is_not(None), min_age_col <= self.age_limit)
|
|
return or_(min_age_col.is_(None), min_age_col <= self.age_limit)
|
|
|
|
def _age_hidden_clause(
|
|
self, min_age_col: InstrumentedAttribute[int | None]
|
|
) -> ColumnElement[bool]:
|
|
"""The negation of `_age_clause`, spelled as ranges an index on `min_age` serves."""
|
|
if self.age_limit is None:
|
|
return min_age_col.is_(None)
|
|
if self.hide_unrated_roms:
|
|
return or_(min_age_col.is_(None), min_age_col > self.age_limit)
|
|
return min_age_col > self.age_limit
|
|
|
|
@cached_property
|
|
def allows(self) -> Callable[[VisibilityColumns], bool]:
|
|
"""Whether a loaded ROM is visible, picked once so a per-ROM loop pays one call."""
|
|
if self.is_unrestricted:
|
|
return _always_visible
|
|
return self._allows_with_age if self.has_age_rule else self._allows
|
|
|
|
def _allows(self, rom: VisibilityColumns) -> bool:
|
|
return (
|
|
rom.platform_id not in self.hidden_platform_ids
|
|
and rom.id not in self.hidden_rom_ids
|
|
)
|
|
|
|
def _allows_with_age(self, rom: VisibilityColumns) -> bool:
|
|
if not self._allows(rom):
|
|
return False
|
|
min_age = rom.min_age
|
|
if min_age is None:
|
|
return not self.hide_unrated_roms
|
|
return self.age_limit is None or min_age <= self.age_limit
|
|
|
|
|
|
UNRESTRICTED: Final = RomVisibilityFilter()
|