Files
romm/backend/tests/handler/database/test_stats_handler.py
Spinnich f69e4756a7 perf(stats): aggregate server-stats breakdowns from roms_facets
The Server Stats page ran two full scans of the multi-gigabyte `roms`
table on every visit: the metadata-coverage breakdown counted eleven
provider-id columns per platform, and the region breakdown pulled every
row's regions into Python. Both were returned in the same request as the
cheap summary counts, so the whole page stayed blank until they finished.

Mirror the provider match ids into the narrow `roms_facets` table
(migration 0102, kept in sync by the same triggers as the existing facet
columns) and read both breakdowns from it, so neither query touches
`roms`. Split the v2 Server Stats view into a summary-first request and a
separate breakdown request so the six top cards render immediately.

Fixes #3901

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-23 17:27:09 +00:00

141 lines
5.1 KiB
Python

"""Checks for the Server Stats per-platform breakdowns.
Both breakdowns aggregate from the narrow `roms_facets` mirror (migration 0100,
extended with the provider match ids) rather than scanning the multi-gigabyte
`roms` table. The mirror is trigger-maintained, so these tests write ROMs
through the normal handler and assert the breakdowns read the mirrored values.
"""
from handler.database import db_platform_handler, db_rom_handler, db_stats_handler
from models.platform import Platform
from models.rom import Rom
def _add_platform(slug: str) -> Platform:
return db_platform_handler.add_platform(
Platform(name=slug, slug=slug, fs_slug=slug)
)
def _add_rom(platform: Platform, fs_name: str, **columns) -> Rom:
"""Insert a ROM on `platform`, letting the facet triggers mirror it."""
rom = Rom(
platform_id=platform.id,
name=fs_name,
slug=fs_name,
fs_name=f"{fs_name}.zip",
fs_name_no_tags=fs_name,
fs_name_no_ext=fs_name,
fs_extension="zip",
fs_path=f"{platform.slug}/roms",
**columns,
)
return db_rom_handler.add_rom(rom)
class TestRegionBreakdown:
def test_counts_regions_per_platform(self):
platform_a = _add_platform("platform_a")
platform_b = _add_platform("platform_b")
_add_rom(platform_a, "a1", regions=["USA", "Japan"])
_add_rom(platform_a, "a2", regions=["USA"])
_add_rom(platform_b, "b1", regions=["Europe"])
breakdown = db_stats_handler.get_region_breakdown_by_platform()
assert breakdown[platform_a.id] == [
{"region": "USA", "count": 2},
{"region": "Japan", "count": 1},
]
assert breakdown[platform_b.id] == [{"region": "Europe", "count": 1}]
def test_empty_regions_are_omitted(self):
platform = _add_platform("platform_empty")
_add_rom(platform, "no_region", regions=[])
breakdown = db_stats_handler.get_region_breakdown_by_platform()
assert platform.id not in breakdown
def test_hidden_platforms_are_excluded(self):
platform_a = _add_platform("platform_a")
platform_b = _add_platform("platform_b")
_add_rom(platform_a, "a1", regions=["USA"])
_add_rom(platform_b, "b1", regions=["Europe"])
breakdown = db_stats_handler.get_region_breakdown_by_platform(
hidden_platform_ids=[platform_b.id]
)
assert platform_a.id in breakdown
assert platform_b.id not in breakdown
def test_hidden_roms_are_excluded(self):
platform = _add_platform("platform_a")
_add_rom(platform, "a1", regions=["USA"])
hidden = _add_rom(platform, "a2", regions=["USA"])
breakdown = db_stats_handler.get_region_breakdown_by_platform(
hidden_rom_ids=[hidden.id]
)
assert breakdown[platform.id] == [{"region": "USA", "count": 1}]
class TestMetadataCoverage:
def test_counts_matched_sources_per_platform(self):
platform_a = _add_platform("platform_a")
platform_b = _add_platform("platform_b")
_add_rom(platform_a, "a1", igdb_id=1, ss_id=2)
_add_rom(platform_a, "a2", igdb_id=3)
_add_rom(platform_b, "b1", moby_id=5)
coverage = db_stats_handler.get_metadata_coverage_by_platform()
assert {"source": "igdb", "matched": 2} in coverage[platform_a.id]
assert {"source": "ss", "matched": 1} in coverage[platform_a.id]
assert coverage[platform_b.id] == [{"source": "moby", "matched": 1}]
def test_string_id_sources_are_counted(self):
"""flashpoint/gamelist/libretro ids are strings, not integers."""
platform = _add_platform("platform_a")
_add_rom(platform, "a1", flashpoint_id="abc", libretro_id="snes")
coverage = db_stats_handler.get_metadata_coverage_by_platform()
assert {"source": "flashpoint", "matched": 1} in coverage[platform.id]
assert {"source": "libretro", "matched": 1} in coverage[platform.id]
def test_unmatched_sources_are_omitted(self):
platform = _add_platform("platform_a")
_add_rom(platform, "a1", igdb_id=1)
coverage = db_stats_handler.get_metadata_coverage_by_platform()
sources = {item["source"] for item in coverage[platform.id]}
assert sources == {"igdb"}
def test_hidden_platforms_are_excluded(self):
platform_a = _add_platform("platform_a")
platform_b = _add_platform("platform_b")
_add_rom(platform_a, "a1", igdb_id=1)
_add_rom(platform_b, "b1", igdb_id=2)
coverage = db_stats_handler.get_metadata_coverage_by_platform(
hidden_platform_ids=[platform_b.id]
)
assert platform_a.id in coverage
assert platform_b.id not in coverage
def test_hidden_roms_are_excluded(self):
platform = _add_platform("platform_a")
_add_rom(platform, "a1", igdb_id=1)
hidden = _add_rom(platform, "a2", igdb_id=2)
coverage = db_stats_handler.get_metadata_coverage_by_platform(
hidden_rom_ids=[hidden.id]
)
assert coverage[platform.id] == [{"source": "igdb", "matched": 1}]