From 3450d47b1f5ad3050e057f5ab487f20c0f3b1ef3 Mon Sep 17 00:00:00 2001 From: Jiabin Shi <291670497+nathanm82@users.noreply.github.com> Date: Mon, 31 Aug 2026 02:25:31 +0800 Subject: [PATCH] fix: add pagination to artifact_storage.py list_all and API --- backend/app/modules/platform/artifacts_api.py | 8 ++++++-- backend/app/services/artifact_service.py | 10 ++++++---- backend/app/storage/artifact_storage.py | 20 +++++++++++++------ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/backend/app/modules/platform/artifacts_api.py b/backend/app/modules/platform/artifacts_api.py index 846cc916..6bdfd1d6 100644 --- a/backend/app/modules/platform/artifacts_api.py +++ b/backend/app/modules/platform/artifacts_api.py @@ -41,9 +41,13 @@ async def get_artifact(artifact_id: str) -> ArtifactResponse: @router.get("", response_model=ArtifactListResponse) -async def list_artifacts(runId: Optional[str] = Query(None, description="Filter by runId")) -> ArtifactListResponse: +async def list_artifacts( + runId: Optional[str] = Query(None, description="Filter by runId"), + limit: int = Query(100, ge=1, le=1000, description="Maximum number of artifacts to return"), + offset: int = Query(0, ge=0, description="Number of artifacts to skip"), +) -> ArtifactListResponse: service = get_service() - artifacts = service.list_artifacts(runId=runId) + artifacts = service.list_artifacts(runId=runId, limit=limit, offset=offset) return ArtifactListResponse( artifacts=[ArtifactResponse.model_validate(a) for a in artifacts], total=len(artifacts), diff --git a/backend/app/services/artifact_service.py b/backend/app/services/artifact_service.py index b0d07a38..d3f62bab 100644 --- a/backend/app/services/artifact_service.py +++ b/backend/app/services/artifact_service.py @@ -78,20 +78,22 @@ def get_artifact(self, artifact_id: str) -> Optional[Artifact]: """ return self.storage.get(artifact_id) - def list_artifacts(self, runId: Optional[str] = None) -> List[Artifact]: + def list_artifacts(self, runId: Optional[str] = None, limit: int = 100, offset: int = 0) -> List[Artifact]: """ - List Artifacts with optional filters. + List Artifacts with optional filters and pagination. Args: runId: Optional runId filter + limit: Maximum number of artifacts to return (default 100) + offset: Number of artifacts to skip (default 0) Returns: List of artifacts matching filters, sorted by creation time (newest first) """ if runId: - return self.storage.list_by_run(runId) + return self.storage.list_by_run(runId, limit=limit, offset=offset) else: - return self.storage.list_all() + return self.storage.list_all(limit=limit, offset=offset) # Global service instance diff --git a/backend/app/storage/artifact_storage.py b/backend/app/storage/artifact_storage.py index 21a9b24c..c0a82f6c 100644 --- a/backend/app/storage/artifact_storage.py +++ b/backend/app/storage/artifact_storage.py @@ -107,10 +107,14 @@ def get(self, artifact_id: str) -> Optional[Artifact]: return self._deserialize_artifact(artifact_dict) - def list_all(self) -> List[Artifact]: + def list_all(self, limit: int = 100, offset: int = 0) -> List[Artifact]: """ - List all Artifacts. + List all Artifacts with pagination. + Args: + limit: Maximum number of artifacts to return (default 100) + offset: Number of artifacts to skip (default 0) + Returns: List of artifacts, sorted by creation time (newest first) """ @@ -125,14 +129,17 @@ def list_all(self) -> List[Artifact]: # Sort by creation time (newest first) artifacts.sort(key=lambda a: a.createdAt, reverse=True) - return artifacts + # Apply pagination + return artifacts[offset:offset + limit] - def list_by_run(self, runId: str) -> List[Artifact]: + def list_by_run(self, runId: str, limit: int = 100, offset: int = 0) -> List[Artifact]: """ - List all Artifacts for a specific Run. + List all Artifacts for a specific Run with pagination. Args: runId: Run identifier + limit: Maximum number of artifacts to return (default 100) + offset: Number of artifacts to skip (default 0) Returns: List of artifacts linked to this run, sorted by creation time @@ -148,7 +155,8 @@ def list_by_run(self, runId: str) -> List[Artifact]: artifacts.sort(key=lambda a: a.createdAt, reverse=True) - return artifacts + # Apply pagination + return artifacts[offset:offset + limit] def exists(self, artifact_id: str) -> bool: """