From 56095c1b1fb23c03e3ee83dc048ca981ecb4c9e1 Mon Sep 17 00:00:00 2001 From: Akshit Sahore Date: Wed, 7 Jan 2026 19:14:03 +0530 Subject: [PATCH 1/2] fix : optimize analysis list with pagination metadata and limit validation. Added otal_count and has_next to response, enforced max limit of 100, and stabilized sorting order. --- spectragraph-api/app/api/routes/analysis.py | 26 ++++++++++++++------ spectragraph-api/app/api/schemas/analysis.py | 13 +++++++++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/spectragraph-api/app/api/routes/analysis.py b/spectragraph-api/app/api/routes/analysis.py index 983285a..8638cd2 100644 --- a/spectragraph-api/app/api/routes/analysis.py +++ b/spectragraph-api/app/api/routes/analysis.py @@ -7,7 +7,7 @@ from spectragraph_core.core.postgre_db import get_db from spectragraph_core.core.models import Analysis, Profile from app.api.deps import get_current_user -from app.api.schemas.analysis import AnalysisRead, AnalysisCreate, AnalysisUpdate +from app.api.schemas.analysis import AnalysisRead, AnalysisCreate, AnalysisUpdate, AnalysisListResponse, PaginationMetadata from typing import Optional from sqlalchemy import or_ @@ -15,22 +15,34 @@ # Get the list of all analyses for the current user -@router.get("", response_model=List[AnalysisRead]) +@router.get("", response_model=AnalysisListResponse) def get_analyses( skip: int = 0,limit: int = 90,search: Optional[str] = None, db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user) ): MAX_LIMIT = 100 - limit = min(limit, MAX_LIMIT) - query= db.query(Analysis).filter(Analysis.owner_id == current_user.id).order_by(Analysis.id.desc()) + if limit > MAX_LIMIT : + raise HTTPException(status_code= status.HTTP_400_BAD_REQUEST, detail=f"Limit for analyses cannot exceed {MAX_LIMIT}") + + query= db.query(Analysis).filter(Analysis.owner_id == current_user.id) if search: search_filter= or_(Analysis.title.ilike(f"%{search}%"),Analysis.description.ilike(f"%{search}%")) query = query.filter(search_filter) - - analyses=query.offset(skip).limit(limit).all() - return analyses + + total_count = query.count() + analyses =query.order_by(Analysis.created_at.desc(),Analysis.id.desc()).offset(skip).limit(limit).all() + + has_next = total_count >(skip + len(analyses)) + + return AnalysisListResponse(items=analyses,metadata=PaginationMetadata( + total_count=total_count, + limit=limit, + skip=skip, + has_next=has_next + ) + ) # Create a New analysis diff --git a/spectragraph-api/app/api/schemas/analysis.py b/spectragraph-api/app/api/schemas/analysis.py index 0ff2f57..b9735e7 100644 --- a/spectragraph-api/app/api/schemas/analysis.py +++ b/spectragraph-api/app/api/schemas/analysis.py @@ -1,6 +1,6 @@ from .base import ORMBase from pydantic import UUID4, BaseModel -from typing import Optional, Any +from typing import Optional, Any, List from datetime import datetime @@ -30,3 +30,14 @@ class AnalysisUpdate(BaseModel): last_updated_at: Optional[datetime] = None owner_id: Optional[UUID4] = None investigation_id: Optional[UUID4] = None + +class PaginationMetadata(BaseModel): + total_count: int + limit: int + skip: int + has_next: bool + + +class AnalysisListResponse(BaseModel): + items: List[AnalysisRead] + metadata: PaginationMetadata From 25e5153fa44b2a5d17e609eb0962a6f03b97079b Mon Sep 17 00:00:00 2001 From: Akshit Sahore Date: Wed, 7 Jan 2026 21:08:11 +0530 Subject: [PATCH 2/2] fix : Added database index on title and description columns to improve query performance. --- spectragraph-core/src/spectragraph_core/core/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/spectragraph-core/src/spectragraph_core/core/models.py b/spectragraph-core/src/spectragraph_core/core/models.py index 8706eed..feac5dd 100644 --- a/spectragraph-core/src/spectragraph_core/core/models.py +++ b/spectragraph-core/src/spectragraph_core/core/models.py @@ -217,6 +217,7 @@ class Analysis(Base): __table_args__ = ( Index("idx_analyses_owner_id", "owner_id"), Index("idx_analyses_investigation_id", "investigation_id"), + Index("idx_analyses_title_description", "title", "description") )