Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Add session delete endpoint. #8

Merged
merged 1 commit into from
Oct 23, 2024
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
3 changes: 3 additions & 0 deletions backend/spielberg/core/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,6 @@ def get(self):

def get_all(self):
return self.db.get_sessions()

def delete(self):
return self.db.delete_session(self.session_id)
27 changes: 27 additions & 0 deletions backend/spielberg/db/sqlite/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,32 @@ def add_or_update_context_msg(
)
self.conn.commit()

def delete_conversation(self, session_id: str) -> bool:
self.cursor.execute(
"DELETE FROM conversations WHERE session_id = ?", (session_id,)
)
self.conn.commit()
return self.cursor.rowcount > 0

def delete_context(self, session_id: str) -> bool:
self.cursor.execute(
"DELETE FROM context_messages WHERE session_id = ?", (session_id,)
)
self.conn.commit()
return self.cursor.rowcount > 0

def delete_session(self, session_id: str) -> bool:
failed_components = []
if not self.delete_conversation(session_id):
failed_components.append("conversation")
if not self.delete_context(session_id):
failed_components.append("context")
self.cursor.execute("DELETE FROM sessions WHERE session_id = ?", (session_id,))
self.conn.commit()
if not self.cursor.rowcount > 0:
failed_components.append("session")
success = len(failed_components) < 3
return success, failed_components

def __del__(self):
self.conn.close()
23 changes: 19 additions & 4 deletions backend/spielberg/entrypoint/api/routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from flask import Blueprint, current_app as app
from flask import Blueprint, request, current_app as app

from spielberg.db import load_db
from spielberg.handler import ChatHandler, SessionHandler, VideoDBHandler, ConfigHandler
Expand Down Expand Up @@ -34,16 +34,31 @@ def get_sessions():
return session_handler.get_sessions()


@session_bp.route("/<session_id>", methods=["GET"])
@session_bp.route("/<session_id>", methods=["GET", "DELETE"])
def get_session(session_id):
"""
Get the session details
Get or delete the session details
"""
if not session_id:
return {"message": f"Please provide {session_id}."}, 400

session_handler = SessionHandler(
db=load_db(os.getenv("SERVER_DB_TYPE", app.config["DB_TYPE"]))
)
session = session_handler.get_session(session_id)
return session
if not session:
return {"message": "Session not found."}, 404

if request.method == "GET":
return session
elif request.method == "DELETE":
success, failed_components = session_handler.delete_session(session_id)
if success:
return {"message": "Session deleted successfully."}, 200
else:
return {
"message": f"Failed to delete the entry for following components: {', '.join(failed_components)}"
}, 500


@videodb_bp.route("/collection", defaults={"collection_id": None}, methods=["GET"])
Expand Down
12 changes: 8 additions & 4 deletions backend/spielberg/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,17 @@ class SessionHandler:
def __init__(self, db: BaseDB, **kwargs):
self.db = db

def get_session(self, session_id):
session = Session(db=self.db, session_id=session_id)
return session.get()

def get_sessions(self):
session = Session(db=self.db)
return session.get_all()

def get_session(self, session_id):
session = Session(db=self.db, session_id=session_id)
return session.get()

def delete_session(self, session_id):
session = Session(db=self.db, session_id=session_id)
return session.delete()


class VideoDBHandler:
Expand Down