From 52c53505a14538aa9f7a1f455d3ad3d80decc710 Mon Sep 17 00:00:00 2001 From: cc Date: Tue, 10 Mar 2026 19:27:50 -0400 Subject: [PATCH] refactor: address review feedback - Derive metadata source columns from Rom model instead of hardcoded list - Replace getOrderedCoverage() function calls with a computed map to avoid redundant sorting on each render Co-Authored-By: Claude Opus 4.6 --- backend/handler/database/stats_handler.py | 38 +++++++++++-------- .../Settings/ServerStats/PlatformsStats.vue | 20 +++++----- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/backend/handler/database/stats_handler.py b/backend/handler/database/stats_handler.py index beda76b9d..18a2b8916 100644 --- a/backend/handler/database/stats_handler.py +++ b/backend/handler/database/stats_handler.py @@ -1,5 +1,8 @@ +from __future__ import annotations + from sqlalchemy import distinct, func, select from sqlalchemy.orm import Session +from sqlalchemy.orm.attributes import InstrumentedAttribute from decorators.database import begin_session from models.assets import Save, Screenshot, State @@ -80,30 +83,33 @@ class DBStatsHandler(DBBaseHandler): or 0 ) + # Metadata source columns on the Rom model, keyed by source identifier. + METADATA_SOURCE_COLUMNS: dict[str, "InstrumentedAttribute"] = { + "igdb": Rom.igdb_id, + "ss": Rom.ss_id, + "moby": Rom.moby_id, + "launchbox": Rom.launchbox_id, + "ra": Rom.ra_id, + "hasheous": Rom.hasheous_id, + "tgdb": Rom.tgdb_id, + "flashpoint": Rom.flashpoint_id, + "hltb": Rom.hltb_id, + "gamelist": Rom.gamelist_id, + } + @begin_session def get_metadata_coverage_by_platform( self, session: Session = None, # type: ignore ) -> dict[int, list[dict]]: """Get the count of ROMs matched per metadata source, grouped by platform.""" - source_keys = [ - "igdb", "ss", "moby", "launchbox", "ra", - "hasheous", "tgdb", "flashpoint", "hltb", "gamelist", - ] - rows = session.execute( select( Rom.platform_id, - func.count(Rom.igdb_id).label("igdb"), - func.count(Rom.ss_id).label("ss"), - func.count(Rom.moby_id).label("moby"), - func.count(Rom.launchbox_id).label("launchbox"), - func.count(Rom.ra_id).label("ra"), - func.count(Rom.hasheous_id).label("hasheous"), - func.count(Rom.tgdb_id).label("tgdb"), - func.count(Rom.flashpoint_id).label("flashpoint"), - func.count(Rom.hltb_id).label("hltb"), - func.count(Rom.gamelist_id).label("gamelist"), + *( + func.count(col).label(key) + for key, col in self.METADATA_SOURCE_COLUMNS.items() + ), ) .select_from(Rom) .group_by(Rom.platform_id) @@ -113,7 +119,7 @@ class DBStatsHandler(DBBaseHandler): for row in rows: result[row.platform_id] = [ {"source": key, "matched": getattr(row, key)} - for key in source_keys + for key in self.METADATA_SOURCE_COLUMNS if getattr(row, key) > 0 ] return result diff --git a/frontend/src/components/Settings/ServerStats/PlatformsStats.vue b/frontend/src/components/Settings/ServerStats/PlatformsStats.vue index 263c5a2ae..8815cf7a7 100644 --- a/frontend/src/components/Settings/ServerStats/PlatformsStats.vue +++ b/frontend/src/components/Settings/ServerStats/PlatformsStats.vue @@ -47,14 +47,16 @@ const sourceInfo = computed(() => { return map; }); -function getOrderedCoverage(platformId: number): MetadataCoverageItem[] { - const items = props.metadataCoverage[platformId]; - if (!items) return []; +const orderedCoverageByPlatform = computed(() => { const priority = metadataOptions.value.map((o) => o.value); - return [...items].sort( - (a, b) => priority.indexOf(a.source) - priority.indexOf(b.source), - ); -} + const result: Record = {}; + for (const [id, items] of Object.entries(props.metadataCoverage)) { + result[id] = [...items].sort( + (a, b) => priority.indexOf(a.source) - priority.indexOf(b.source), + ); + } + return result; +}); function getPlatformPercentage( filesize: number | string, @@ -193,11 +195,11 @@ function getCoveragePercent(matched: number, total: number): string { {{ t("common.metadata-coverage") }}