Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/ada/assets/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"ChangeRecord",
"ManifestError",
"CORE_ARTEFACT_ROLES",
"manifest_summary",
"parse_manifest",
]

Expand Down Expand Up @@ -328,3 +329,20 @@ def parse_manifest(doc: bytes | str) -> AssetManifest:
artefacts=tuple(artefacts),
counts=dict(raw.get("counts") or {}),
)


def manifest_summary(m: AssetManifest) -> dict:
"""The few fields the browser derives its badges from, for the index route to fold in.

Deliberately NOT the whole manifest: ``build.options`` is opaque provider data, and the index
is fetched on every refresh. A field lands here only when a badge or a flag reads it.
"""
return _drop_none(
{
"provider": m.provider,
"node": m.node,
"delivery": m.delivery,
"produced_at": m.produced_at,
"hierarchy_revision": m.hierarchy_revision,
}
)
50 changes: 49 additions & 1 deletion src/ada/comms/rest/routes/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
HIERARCHY_FILENAME,
MANIFEST_FILENAME,
ManifestError,
manifest_summary,
parse_manifest,
)
from ada.assets.projection import HierarchyError, parse_hierarchy
Expand Down Expand Up @@ -82,17 +83,29 @@ async def api_asset_providers(scope_obj: Scope = Depends(scope_from_path)) -> JS
@router.get("/scopes/{scope}/assets/index")
async def api_asset_index(
collection: str | None = None,
manifests: bool = False,
scope_obj: Scope = Depends(scope_from_path),
ctx: RestContext = Depends(rest_context),
) -> JSONResponse:
"""The folded index: subjects -> revisions -> files, plus ``malformed``.

One bounded ``list_prefix`` -- scoped to a collection when one is named, so browsing a
collection costs its own keys rather than a walk of every asset in the scope.

``manifests=true`` (collection required) also folds in, per revision, the few manifest fields
the browser's badges are derived from -- the delivery claim, the producing provider and the
``hierarchy_revision`` a leaf was published against. Read here rather than by the browser
because the alternative is one fetch per subject-revision from the tab, and a manifest is
immutable at its key (barring ``replace``), so the server read is the cheap one.
"""
if manifests and not collection:
raise HTTPException(status_code=400, detail="manifests=true needs a collection")
prefix = f"{ASSET_PREFIX}/{collection}/" if collection else f"{ASSET_PREFIX}/"
index = fold_listing(await _list_asset_keys(ctx, scope_obj, prefix))
return JSONResponse(index.to_dict())
body = index.to_dict()
if manifests:
await _fold_manifest_summaries(ctx, scope_obj, collection, body)
return JSONResponse(body)


@router.get("/scopes/{scope}/assets/tree/{provider}/{collection}")
Expand Down Expand Up @@ -212,6 +225,41 @@ async def api_asset_delivery(

# --- helpers -------------------------------------------------------------------------------------

# Bounded fan-out for the manifest fold: enough to hide per-object latency, few enough that one
# index request cannot monopolise the storage client.
_MANIFEST_READ_CONCURRENCY = 16


async def _fold_manifest_summaries(ctx: RestContext, scope: Scope, collection: str, body: dict) -> None:
"""Attach ``manifest`` (or ``manifest_error``) to every revision entry that has one.

A manifest that cannot be read is reported on its revision, never dropped: the revision is
still listed, and the tab has to be able to say why it cannot badge it.
"""
gate = asyncio.Semaphore(_MANIFEST_READ_CONCURRENCY)

async def one(subject: str, rev: dict) -> None:
key = asset_key(collection, subject, rev["revision"], MANIFEST_FILENAME)
async with gate:
try:
raw = await ctx.storage.get_bytes(scope, key)
except (FileNotFoundError, KeyError):
rev["manifest_error"] = f"listed but not readable: {key}"
return
try:
rev["manifest"] = manifest_summary(parse_manifest(raw))
except ManifestError as exc:
rev["manifest_error"] = str(exc)

await asyncio.gather(
*(
one(entry["subject"], rev)
for entry in body["collections"].get(collection, [])
for rev in entry["revisions"]
if MANIFEST_FILENAME in rev["files"]
)
)


async def _latest_complete_revision(ctx: RestContext, scope: Scope, collection: str, subject: str) -> str | None:
"""Newest revision that actually HAS a manifest.
Expand Down
Loading
Loading