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
26 changes: 19 additions & 7 deletions spectragraph-api/app/api/routes/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,42 @@
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_

router = APIRouter()


# 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
Expand Down
13 changes: 12 additions & 1 deletion spectragraph-api/app/api/schemas/analysis.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions spectragraph-core/src/spectragraph_core/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)


Expand Down