File size: 2,713 Bytes
5655f74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96fe8d8
 
a0f1a88
 
 
96fe8d8
a0f1a88
96fe8d8
a0f1a88
5655f74
 
 
 
 
 
a0f1a88
 
 
 
 
96fe8d8
 
 
 
 
 
 
 
 
a0f1a88
96fe8d8
a0f1a88
 
 
 
5655f74
 
5ff9d40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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()