File size: 3,451 Bytes
b2b6341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd1e711
 
b2b6341
 
 
 
 
 
 
 
 
 
 
 
 
 
fd1e711
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2b6341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from fastapi import APIRouter, HTTPException
from backend.database.connection import get_connection
from backend.vectorstore import delete_vectors
from pathlib import Path
import os

router = APIRouter()


@router.get("/sources")
def get_sources():
    """
    GET /sources
    Returns all ingested sources for the frontend dashboard,
    knowledge sources page, and sidebar preview.
    """
    with get_connection() as conn:
        cursor = conn.cursor(dictionary=True)
        cursor.execute("""
            SELECT
                id,
                type,
                title,
                origin,
                language,
                chunk_count  AS chunkCount,
                status,
                progress_percentage,
                error_message,
                created_at   AS createdAt
            FROM sources
            ORDER BY created_at DESC
        """)
        sources = cursor.fetchall()

    # Convert datetime to ISO string for JSON serialization
    for s in sources:
        if s["createdAt"]:
            s["createdAt"] = s["createdAt"].isoformat()

    return {"sources": sources}


@router.get("/sources/{source_id}")
def get_source_progress(source_id: str):
    """
    GET /sources/{source_id}
    Returns details for a single source, including progress_percentage and error_message.
    """
    with get_connection() as conn:
        cursor = conn.cursor(dictionary=True)
        cursor.execute("""
            SELECT
                id,
                type,
                title,
                origin,
                language,
                chunk_count  AS chunkCount,
                status,
                progress_percentage,
                error_message,
                created_at   AS createdAt
            FROM sources
            WHERE id = %s
        """, (source_id,))
        source = cursor.fetchone()

    if not source:
        raise HTTPException(status_code=404, detail="Source not found")

    # Convert datetime to ISO string for JSON serialization
    if source["createdAt"]:
        source["createdAt"] = source["createdAt"].isoformat()

    return source


@router.delete("/sources/{source_id}")
def delete_source(source_id: str):
    """
    DELETE /sources/{id}
    Deletes a source and all its chunks from FAISS, MySQL, and disk.
    """
    with get_connection() as conn:
        cursor = conn.cursor(dictionary=True)

        cursor.execute(
            "SELECT id, type, origin FROM sources WHERE id = %s",
            (source_id,)
        )
        source = cursor.fetchone()

        if not source:
            raise HTTPException(status_code=404, detail="Source not found")

        cursor.execute(
            "SELECT id FROM chunks WHERE source_id = %s",
            (source_id,)
        )
        chunk_rows = cursor.fetchall()
        chunk_ids  = {row["id"] for row in chunk_rows}

    if chunk_ids:
        delete_vectors(chunk_ids)

    with get_connection() as conn:
        cursor = conn.cursor()
        cursor.execute("DELETE FROM sources WHERE id = %s", (source_id,))
        conn.commit()

    if source["type"] == "pdf" and source["origin"]:
        pdf_path = Path(source["origin"])
        if pdf_path.exists():
            os.remove(pdf_path)
            print(f"[Sources] Deleted file: {pdf_path}")

    print(f"[Sources] Deleted source: {source_id} ({len(chunk_ids)} chunks)")
    return {"message": "Source deleted successfully", "id": source_id}