mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 10:01:00 +01:00
feat(anniversaries): return every match for the day
The 24-item cap was measured against the largest library available and does not earn its place: on a 96k-rom library the day-of-year index range scan costs 3.7 ms whether it matches 114 rows or 22,914, so the cap saved roughly 40 ms and 0.7 MB on a typical day while truncating every single day of the year (median 132 matches/day, and 100% of days exceed 24). Removing it also makes the widget's counter a real total rather than a ceiling, which was the substance of the review comment about "24 / 24" implying a complete list. This makes the 1 January skip load-bearing for response size as well as accuracy: it is the one day that would otherwise answer with 22,914 rows (28% of all dated roms), where the busiest real day is 1,101. Addresses the "Result cap hides anniversaries" review comment on #4396. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -159,11 +159,6 @@ router.include_router(patch_router)
|
||||
# RomUser fields the statuses filter branches on.
|
||||
STATUS_MEMBERSHIP_FIELDS = frozenset({"status", "now_playing", "backlogged", "hidden"})
|
||||
|
||||
# Anniversaries are paged through one card at a time, so the cap is a ceiling on
|
||||
# a widget's payload rather than a page size.
|
||||
DEFAULT_ANNIVERSARY_LIMIT = 24
|
||||
MAX_ANNIVERSARY_LIMIT = 50
|
||||
|
||||
|
||||
def safe_int_or_none(value: Any) -> int | None:
|
||||
if value is None or value == "":
|
||||
@@ -1117,18 +1112,17 @@ def get_anniversary_roms(
|
||||
description="Day of the month, defaulting to today's UTC date.", ge=1, le=31
|
||||
),
|
||||
] = None,
|
||||
limit: Annotated[
|
||||
int,
|
||||
Query(ge=1, le=MAX_ANNIVERSARY_LIMIT, description="Maximum roms to return"),
|
||||
] = DEFAULT_ANNIVERSARY_LIMIT,
|
||||
) -> list[SimpleRomSchema]:
|
||||
"""Roms released on a given day of an earlier year, oldest release first.
|
||||
"""Every rom released on a given day of an earlier year, oldest release first.
|
||||
|
||||
Whole-library, so it takes no scope: it feeds the Home widget, which shows
|
||||
one game at a time and pages through the rest. Clients pass their own local
|
||||
month and day so "today" matches the calendar in front of the user.
|
||||
|
||||
Empty on 1 January, which several providers use for year-only metadata.
|
||||
Uncapped, so the widget's counter is a real total. That makes the 1 January
|
||||
skip below load-bearing for response size as well as for accuracy: several
|
||||
providers park year-only metadata there, which is the one day of the year
|
||||
that would answer with a sizeable fraction of the library.
|
||||
"""
|
||||
perms = get_permissions(request)
|
||||
|
||||
@@ -1146,7 +1140,6 @@ def get_anniversary_roms(
|
||||
today=datetime.now(timezone.utc).date(),
|
||||
month=month,
|
||||
day=day,
|
||||
limit=limit,
|
||||
)
|
||||
if not rom_ids:
|
||||
return []
|
||||
|
||||
@@ -1920,10 +1920,9 @@ class DBRomsHandler(DBBaseHandler):
|
||||
today: date,
|
||||
month: int | None = None,
|
||||
day: int | None = None,
|
||||
limit: int,
|
||||
session: Session = None, # type: ignore
|
||||
) -> list[int]:
|
||||
"""Ids of roms released on a given day of the year, oldest release first.
|
||||
"""Ids of every rom released on a given day of the year, oldest release first.
|
||||
|
||||
`idx_roms_generated_first_release_date` serves both the day-of-year match
|
||||
and the sort as one range scan, and only the id is selected, so the wide
|
||||
@@ -1934,7 +1933,6 @@ class DBRomsHandler(DBBaseHandler):
|
||||
decides the leap-day rollover.
|
||||
month: Calendar month, defaulting to `today`'s.
|
||||
day: Day of the month, defaulting to `today`'s.
|
||||
limit: Maximum ids to return.
|
||||
|
||||
Returns:
|
||||
Rom ids, oldest release first. Empty on 1 January.
|
||||
@@ -1961,7 +1959,6 @@ class DBRomsHandler(DBBaseHandler):
|
||||
.with_only_columns(Rom.id) # type: ignore
|
||||
.where(epoch_ms_in_ranges(Rom.generated_first_release_date, ranges))
|
||||
.order_by(Rom.generated_first_release_date.asc())
|
||||
.limit(limit)
|
||||
)
|
||||
|
||||
return list(session.scalars(id_query).all())
|
||||
|
||||
@@ -174,16 +174,19 @@ def test_undated_roms_are_not_returned(
|
||||
assert response.json() == []
|
||||
|
||||
|
||||
def test_caps_the_result_list(
|
||||
def test_returns_every_match_for_the_day(
|
||||
client: TestClient, access_token: str, platform: Platform
|
||||
) -> None:
|
||||
for year in range(1990, 1996):
|
||||
"""Uncapped, so the widget's counter is a real total rather than a ceiling."""
|
||||
for year in range(1970, 2000):
|
||||
_dated_rom(platform, f"rom_{year}", date(year, 9, 8))
|
||||
|
||||
response = _get(client, access_token, month=9, day=8, limit=2)
|
||||
response = _get(client, access_token, month=9, day=8)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert [rom["name"] for rom in response.json()] == ["rom_1990", "rom_1991"]
|
||||
assert [rom["name"] for rom in response.json()] == [
|
||||
f"rom_{year}" for year in range(1970, 2000)
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@@ -192,7 +195,6 @@ def test_caps_the_result_list(
|
||||
{"month": 13, "day": 1},
|
||||
{"month": 0, "day": 1},
|
||||
{"month": 9, "day": 32},
|
||||
{"month": 9, "day": 8, "limit": 500},
|
||||
],
|
||||
)
|
||||
def test_rejects_out_of_range_parameters(
|
||||
|
||||
@@ -46,7 +46,7 @@ def _dated_rom(platform: Platform, name: str, released: date | None) -> Rom:
|
||||
def _anniversary_names(today: date, month: int | None = None, day: int | None = None):
|
||||
query, _ = db_rom_handler.get_roms_query()
|
||||
ids = db_rom_handler.get_anniversary_rom_ids(
|
||||
query=query, today=today, month=month, day=day, limit=24
|
||||
query=query, today=today, month=month, day=day
|
||||
)
|
||||
by_id = {rom.id: rom.name for rom in db_rom_handler.get_roms_simple_by_ids(ids)}
|
||||
return [by_id[rom_id] for rom_id in ids]
|
||||
@@ -103,16 +103,12 @@ class TestMatching:
|
||||
|
||||
assert _anniversary_names(date(2026, 9, 8)) == ["released_before"]
|
||||
|
||||
def test_respects_the_limit(self, platform: Platform):
|
||||
for year in range(1990, 2000):
|
||||
def test_returns_every_match(self, platform: Platform):
|
||||
"""No cap: the day's whole list is what the widget pages through."""
|
||||
for year in range(1970, 2000):
|
||||
_dated_rom(platform, f"rom_{year}", date(year, 9, 8))
|
||||
|
||||
query, _ = db_rom_handler.get_roms_query()
|
||||
ids = db_rom_handler.get_anniversary_rom_ids(
|
||||
query=query, today=date(2026, 9, 8), limit=3
|
||||
)
|
||||
|
||||
assert len(ids) == 3
|
||||
assert len(_anniversary_names(date(2026, 9, 8))) == 30
|
||||
|
||||
|
||||
class TestYearBoundary:
|
||||
|
||||
Reference in New Issue
Block a user