perf: bound public activity aggregation

This commit is contained in:
ik
2026-09-22 20:17:55 +07:00
parent a501b5ea9d
commit 1da9279ddf
5 changed files with 29 additions and 13 deletions
+15 -12
View File
@@ -12,6 +12,7 @@ TACKLE_MEDIA_ROLES = {"tackle_card", "tackle_detail", "rig_diagram", "tackle_scr
KNOWN_MEDIA_ROLES = WATERBODY_MEDIA_ROLES | TACKLE_MEDIA_ROLES
MEDIA_ROLES_BY_ENTITY = {"waterbody": WATERBODY_MEDIA_ROLES, "tackle": TACKLE_MEDIA_ROLES}
_manifest_cache: tuple[Path, int, int, dict] | None = None
_asset_index_cache: tuple[int, dict[str, tuple[str, str | None]]] | None = None
def _read_manifest() -> dict:
@@ -107,21 +108,23 @@ def published_file(digest: str) -> tuple[Path, str] | None:
if len(digest) != 64 or any(char not in "0123456789abcdef" for char in digest):
return None
manifest = _read_manifest()
item = next((row for row in manifest.get("assets", []) if row.get("status") == "approved" and row.get("sha256") == digest), None)
media_type = None
local_path = None
if item:
media_type = item.get("content_type")
local_path = item.get("local_path")
else:
global _asset_index_cache
cache_key = id(manifest)
if _asset_index_cache is None or _asset_index_cache[0] != cache_key:
index: dict[str, tuple[str, str | None]] = {}
for row in manifest.get("assets", []):
if row.get("status") != "approved":
continue
variant = next((candidate for candidate in row.get("derivatives", []) if candidate.get("sha256") == digest), None)
if variant:
media_type = variant.get("content_type")
local_path = variant.get("local_path")
break
if row.get("sha256") and row.get("local_path"):
index[str(row["sha256"])] = (str(row["local_path"]), row.get("content_type"))
for variant in row.get("derivatives", []):
if variant.get("sha256") and variant.get("local_path"):
index.setdefault(str(variant["sha256"]), (str(variant["local_path"]), variant.get("content_type")))
_asset_index_cache = (cache_key, index)
asset = _asset_index_cache[1].get(digest)
if asset is None:
return None
local_path, media_type = asset
if not local_path:
return None
target = (MEDIA_ROOT / local_path).resolve()