|
|
| """
|
| Created on Sat Apr 11 15:03:04 2026
|
|
|
| @author: THYAGHARAJAN
|
| """
|
|
|
| from utils.core_imports import get_current_user
|
|
|
| from fastapi import APIRouter, Depends, HTTPException
|
| import os
|
| import shutil
|
| import threading
|
|
|
|
|
| router = APIRouter()
|
|
|
| @router.delete("/admin/delete-document")
|
| def delete_document(filename: str,current_user: str = Depends(get_current_user)):
|
| from utils.core_imports import get_db, rebuild_faiss_index, get_upload_folder
|
|
|
| filename = filename.strip()
|
|
|
| conn = get_db()
|
| cursor = conn.cursor()
|
|
|
|
|
| cursor.execute(
|
| "SELECT faiss_id FROM document_chunks WHERE TRIM(source)=?",
|
| (filename,)
|
| )
|
|
|
| rows = cursor.fetchall()
|
|
|
| if not rows:
|
| conn.close()
|
| raise HTTPException(status_code=404, detail="Document not found")
|
|
|
|
|
| cursor.execute(
|
| "DELETE FROM document_chunks WHERE TRIM(source)=?",
|
| (filename,)
|
| )
|
|
|
| conn.commit()
|
| conn.close()
|
|
|
|
|
| file_path = os.path.join(get_upload_folder(), filename)
|
| if os.path.exists(file_path):
|
| os.remove(file_path)
|
| rebuild_faiss_index()
|
|
|
|
|
| threading.Thread(target=rebuild_faiss_index).start()
|
|
|
| '''
|
| #used for debugging. Found & was not converted to %26
|
| print(f"Incoming filename: [{filename}]")
|
|
|
| cursor.execute("SELECT DISTINCT source FROM document_chunks")
|
| all_sources = cursor.fetchall()
|
|
|
| print("DB sources:")
|
| for s in all_sources:
|
| print(f"[{s[0]}]")
|
| '''
|
|
|
| return {"message": f"{filename} removed from index"}
|
|
|
|
|
|
|
| @router.delete("/admin/delete-folder")
|
| def delete_folder(folder: str,current_user: str = Depends(get_current_user)):
|
| from utils.core_imports import get_db, rebuild_faiss_index
|
| conn = get_db()
|
| cursor = conn.cursor()
|
|
|
| cursor.execute(
|
| "DELETE FROM document_chunks WHERE source LIKE ?",
|
| (f"%{folder}%",)
|
| )
|
|
|
| deleted_count = cursor.rowcount
|
| conn.commit()
|
| conn.close()
|
| if deleted_count == 0:
|
| raise HTTPException(status_code=404, detail="Folder not found")
|
|
|
| threading.Thread(target=rebuild_faiss_index).start()
|
|
|
| return {"message": f"{folder} folder removed from index"}
|
|
|
|
|
|
|
| @router.delete("/admin/reset-index")
|
| def reset_index(confirm: bool = False,current_user: str = Depends(get_current_user)):
|
| from utils.core_imports import get_db, rebuild_faiss_index, get_upload_folder
|
|
|
| if not confirm:
|
| return {"message": "Set confirm=true to reset index"}
|
|
|
| conn = get_db()
|
| cursor = conn.cursor()
|
|
|
| cursor.execute("DELETE FROM document_chunks")
|
|
|
|
|
| conn.commit()
|
| conn.close()
|
|
|
|
|
| upload_dir = get_upload_folder()
|
|
|
| shutil.rmtree(upload_dir)
|
| os.makedirs(upload_dir, exist_ok=True)
|
|
|
| threading.Thread(target=rebuild_faiss_index).start()
|
|
|
| return {"message": "Index reset completed"}
|
|
|
|
|
|
|
| @router.get("/admin/list-documents")
|
| def list_documents(current_user: str = Depends(get_current_user)):
|
| from utils.core_imports import get_db
|
|
|
| conn = get_db()
|
| cursor = conn.cursor()
|
|
|
| cursor.execute("""
|
| SELECT source, COUNT(*) as chunks
|
| FROM document_chunks
|
| GROUP BY source
|
| """)
|
|
|
| rows = cursor.fetchall()
|
| conn.close()
|
|
|
| docs = [{"document": r[0], "chunks": r[1]} for r in rows]
|
|
|
| return {"documents": docs}
|
|
|