coderound / backend /src /routers /sessions.py
ketannnn's picture
feat: implement backend ingestion pipeline with Celery workers, Qdrant integration, and session management
96fe8d8
import uuid
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, func
from ..database import get_db
from ..models.session import Session
from ..models.candidate import Candidate
from ..schemas.session import SessionCreate, SessionResponse
router = APIRouter()
@router.post("", response_model=SessionResponse, status_code=201)
async def create_session(payload: SessionCreate, db: AsyncSession = Depends(get_db)):
session = Session(id=uuid.uuid4(), name=payload.name, description=payload.description)
db.add(session)
await db.commit()
await db.refresh(session)
return session
@router.get("", response_model=list[SessionResponse])
async def list_sessions(db: AsyncSession = Depends(get_db)):
result = await db.execute(select(Session).order_by(Session.created_at.desc()).limit(50))
return result.scalars().all()
@router.get("/{session_id}", response_model=SessionResponse)
async def get_session(session_id: uuid.UUID, db: AsyncSession = Depends(get_db)):
result = await db.execute(select(Session).where(Session.id == session_id))
sess = result.scalar_one_or_none()
if not sess:
raise HTTPException(status_code=404, detail="Session not found")
return sess
from qdrant_client import QdrantClient
from qdrant_client.models import Filter, FieldCondition, MatchValue
from ..config import get_settings
from ..models.match_result import MatchResult
def _get_qdrant() -> QdrantClient:
settings = get_settings()
return QdrantClient(url=settings.qdrant_url, api_key=settings.qdrant_api_key)
@router.delete("/{session_id}", status_code=204)
async def delete_session(session_id: uuid.UUID, db: AsyncSession = Depends(get_db)):
result = await db.execute(select(Session).where(Session.id == session_id))
sess = result.scalar_one_or_none()
if not sess:
raise HTTPException(status_code=404, detail="Session not found")
session_str = str(session_id)
settings = get_settings()
try:
qdrant = _get_qdrant()
qdrant.delete(
collection_name=settings.collection_name,
points_selector=Filter(
must=[
FieldCondition(key="session_id", match=MatchValue(value=session_str))
]
)
)
except Exception as e:
print(f"Warning: Failed deleting Qdrant targets for session {session_str}: {e}")
await db.execute(MatchResult.__table__.delete().where(MatchResult.session_id == session_id))
await db.execute(Candidate.__table__.delete().where(Candidate.session_id == session_id))
await db.delete(sess)
await db.commit()