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 <noreply@anthropic.com>
This commit is contained in:
ccandClaude Opus 4.6 committed 2026-03-10 19:27:50 -04:00
1 parent c8fd7fedde
commit 52c53505a1
2 files changed
+33 -25

No files matched your search

+22 -16
View File
@@ -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
@@ -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<string, MetadataCoverageItem[]> = {};
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") }}
</span>
<div
v-if="getOrderedCoverage(platform.id).length > 0"
v-if="orderedCoverageByPlatform[platform.id]?.length > 0"
class="d-flex flex-wrap ga-1"
>
<v-chip
v-for="item in getOrderedCoverage(platform.id)"
v-for="item in orderedCoverageByPlatform[platform.id]"
:key="item.source"
:title="`${sourceInfo[item.source]?.name ?? item.source}: ${item.matched} / ${platform.rom_count}`"
class="chip-fixed"