mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
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>
198 lines
6.6 KiB
Python
198 lines
6.6 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Sequence
|
|
|
|
from sqlalchemy import distinct, func, select
|
|
from sqlalchemy.orm import InstrumentedAttribute, Session
|
|
from sqlalchemy.sql.selectable import Select
|
|
|
|
from decorators.database import begin_session
|
|
from endpoints.responses.stats import MetadataCoverageItem, RegionBreakdownItem
|
|
from models.assets import Save, Screenshot, State
|
|
from models.rom import METADATA_SOURCE_FACET_COLUMNS, Rom, RomFacets, RomFile
|
|
|
|
from .base_handler import DBBaseHandler
|
|
|
|
|
|
def _exclude_hidden(
|
|
query: Select,
|
|
hidden_platform_ids: Sequence[int] | None,
|
|
hidden_rom_ids: Sequence[int] | None,
|
|
*,
|
|
platform_id_col: InstrumentedAttribute = Rom.platform_id,
|
|
rom_id_col: InstrumentedAttribute = Rom.id,
|
|
) -> Select:
|
|
"""Drop rows for platforms/roms hidden from the caller (admins pass None).
|
|
|
|
The platform/rom id columns are overridable so callers reading the narrow
|
|
`roms_facets` mirror can filter on its columns instead of `roms`.
|
|
"""
|
|
if hidden_platform_ids:
|
|
query = query.where(platform_id_col.not_in(hidden_platform_ids))
|
|
if hidden_rom_ids:
|
|
query = query.where(rom_id_col.not_in(hidden_rom_ids))
|
|
return query
|
|
|
|
|
|
class DBStatsHandler(DBBaseHandler):
|
|
@begin_session
|
|
def get_platforms_count(
|
|
self,
|
|
hidden_platform_ids: Sequence[int] | None = None,
|
|
hidden_rom_ids: Sequence[int] | None = None,
|
|
session: Session = None, # type: ignore
|
|
) -> int:
|
|
"""Get the number of platforms with any roms."""
|
|
query = _exclude_hidden(
|
|
select(func.count(distinct(Rom.platform_id))).select_from(Rom),
|
|
hidden_platform_ids,
|
|
hidden_rom_ids,
|
|
)
|
|
return session.scalar(query) or 0
|
|
|
|
@begin_session
|
|
def get_roms_count(
|
|
self,
|
|
hidden_platform_ids: Sequence[int] | None = None,
|
|
hidden_rom_ids: Sequence[int] | None = None,
|
|
session: Session = None, # type: ignore
|
|
) -> int:
|
|
query = _exclude_hidden(
|
|
select(func.count()).select_from(Rom),
|
|
hidden_platform_ids,
|
|
hidden_rom_ids,
|
|
)
|
|
return session.scalar(query) or 0
|
|
|
|
@begin_session
|
|
def get_saves_count(
|
|
self,
|
|
session: Session = None, # type: ignore
|
|
) -> int:
|
|
return session.scalar(select(func.count()).select_from(Save)) or 0
|
|
|
|
@begin_session
|
|
def get_states_count(
|
|
self,
|
|
session: Session = None, # type: ignore
|
|
) -> int:
|
|
return session.scalar(select(func.count()).select_from(State)) or 0
|
|
|
|
@begin_session
|
|
def get_screenshots_count(
|
|
self,
|
|
session: Session = None, # type: ignore
|
|
) -> int:
|
|
return session.scalar(select(func.count()).select_from(Screenshot)) or 0
|
|
|
|
@begin_session
|
|
def get_total_filesize(
|
|
self,
|
|
hidden_platform_ids: Sequence[int] | None = None,
|
|
hidden_rom_ids: Sequence[int] | None = None,
|
|
session: Session = None, # type: ignore
|
|
) -> int:
|
|
"""Get the total filesize of all roms in the database, in bytes."""
|
|
query = select(func.sum(RomFile.file_size_bytes)).select_from(RomFile)
|
|
if hidden_platform_ids or hidden_rom_ids:
|
|
query = _exclude_hidden(
|
|
query.join(Rom), hidden_platform_ids, hidden_rom_ids
|
|
)
|
|
return session.scalar(query) or 0
|
|
|
|
@begin_session
|
|
def get_platform_filesize(
|
|
self,
|
|
platform_id: int,
|
|
session: Session = None, # type: ignore
|
|
) -> int:
|
|
"""Get the total filesize of all roms in the database, in bytes."""
|
|
return (
|
|
session.scalar(
|
|
select(func.sum(RomFile.file_size_bytes))
|
|
.select_from(RomFile)
|
|
.join(Rom)
|
|
.filter(Rom.platform_id == platform_id)
|
|
)
|
|
or 0
|
|
)
|
|
|
|
@begin_session
|
|
def get_metadata_coverage_by_platform(
|
|
self,
|
|
hidden_platform_ids: Sequence[int] | None = None,
|
|
hidden_rom_ids: Sequence[int] | None = None,
|
|
session: Session = None, # type: ignore
|
|
) -> dict[int, list[MetadataCoverageItem]]:
|
|
"""Get the count of ROMs matched per metadata source, grouped by platform.
|
|
|
|
Aggregates the narrow `roms_facets` mirror instead of `roms`, whose rows
|
|
also carry the raw provider-metadata blobs.
|
|
"""
|
|
rows = session.execute(
|
|
_exclude_hidden(
|
|
select(
|
|
RomFacets.platform_id,
|
|
*(
|
|
func.count(col).label(key)
|
|
for key, col in METADATA_SOURCE_FACET_COLUMNS.items()
|
|
),
|
|
).select_from(RomFacets),
|
|
hidden_platform_ids,
|
|
hidden_rom_ids,
|
|
platform_id_col=RomFacets.platform_id,
|
|
rom_id_col=RomFacets.rom_id,
|
|
).group_by(RomFacets.platform_id)
|
|
).all()
|
|
|
|
result: dict[int, list[MetadataCoverageItem]] = {}
|
|
for row in rows:
|
|
result[row.platform_id] = [
|
|
MetadataCoverageItem(source=key, matched=getattr(row, key))
|
|
for key in METADATA_SOURCE_FACET_COLUMNS
|
|
if getattr(row, key) > 0
|
|
]
|
|
|
|
return result
|
|
|
|
@begin_session
|
|
def get_region_breakdown_by_platform(
|
|
self,
|
|
hidden_platform_ids: Sequence[int] | None = None,
|
|
hidden_rom_ids: Sequence[int] | None = None,
|
|
session: Session = None, # type: ignore
|
|
) -> dict[int, list[RegionBreakdownItem]]:
|
|
"""Get the count of ROMs per region, grouped by platform.
|
|
|
|
Reads the narrow `roms_facets` mirror rather than scanning `roms`.
|
|
"""
|
|
rows = session.execute(
|
|
_exclude_hidden(
|
|
select(RomFacets.platform_id, RomFacets.regions).where(
|
|
RomFacets.regions.is_not(None)
|
|
),
|
|
hidden_platform_ids,
|
|
hidden_rom_ids,
|
|
platform_id_col=RomFacets.platform_id,
|
|
rom_id_col=RomFacets.rom_id,
|
|
)
|
|
).all()
|
|
|
|
counter: dict[int, dict[str, int]] = {}
|
|
for platform_id, regions_list in rows:
|
|
if regions_list:
|
|
if platform_id not in counter:
|
|
counter[platform_id] = {}
|
|
for region in regions_list:
|
|
counter[platform_id][region] = (
|
|
counter[platform_id].get(region, 0) + 1
|
|
)
|
|
|
|
return {
|
|
pid: [
|
|
{"region": r, "count": c}
|
|
for r, c in sorted(regions.items(), key=lambda x: -x[1])
|
|
]
|
|
for pid, regions in counter.items()
|
|
}
|