Add session stats and VACUUM on deletion
database.py: count_sessions(), db_size() (db+WAL+SHM), vacuum() (VACUUM + wal_checkpoint(TRUNCATE) to actually reclaim space in WAL mode) server.py: /api/sessions now returns total_sessions + db_size; DELETE /api/sessions/<id> runs vacuum() after deletion
This commit is contained in:
1 parent
31011301ea
commit
a1bbd0cf0f
2 files changed
+25
-1
No files matched your search
@@ -41,6 +41,7 @@ class Database:
|
||||
def __init__(self, db_path=None):
|
||||
if db_path is None:
|
||||
db_path = DEFAULT_DB_PATH
|
||||
self._db_path = db_path
|
||||
os.makedirs(os.path.dirname(db_path), exist_ok=True)
|
||||
self._conn = sqlite3.connect(db_path, check_same_thread=False)
|
||||
self._conn.execute('PRAGMA journal_mode=WAL')
|
||||
@@ -74,6 +75,24 @@ class Database:
|
||||
self._conn.execute('DELETE FROM sessions WHERE id=?', (session_id,))
|
||||
self._conn.commit()
|
||||
|
||||
def count_sessions(self):
|
||||
row = self._conn.execute('SELECT COUNT(*) FROM sessions').fetchone()
|
||||
return row[0]
|
||||
|
||||
def db_size(self):
|
||||
"""Total on-disk size of the SQLite database (db + WAL + SHM)."""
|
||||
total = 0
|
||||
for suffix in ('', '-wal', '-shm'):
|
||||
p = self._db_path + suffix
|
||||
if os.path.exists(p):
|
||||
total += os.path.getsize(p)
|
||||
return total
|
||||
|
||||
def vacuum(self):
|
||||
self._conn.execute('VACUUM')
|
||||
self._conn.execute('PRAGMA wal_checkpoint(TRUNCATE)')
|
||||
self._conn.commit()
|
||||
|
||||
def get_session(self, session_id):
|
||||
row = self._conn.execute(
|
||||
'SELECT id, name, started, ended, mode FROM sessions WHERE id=?',
|
||||
|
||||
@@ -126,7 +126,11 @@ class RequestHandler(BaseHTTPRequestHandler):
|
||||
|
||||
def _handle_list_sessions(self):
|
||||
sessions = self.db.list_sessions()
|
||||
self._send_json({'sessions': sessions})
|
||||
self._send_json({
|
||||
'sessions': sessions,
|
||||
'total_sessions': self.db.count_sessions(),
|
||||
'db_size': self.db.db_size(),
|
||||
})
|
||||
|
||||
def _handle_get_session(self, session_id, params):
|
||||
session = self.db.get_session(session_id)
|
||||
@@ -192,6 +196,7 @@ class RequestHandler(BaseHTTPRequestHandler):
|
||||
if self.capture.session_id == session_id:
|
||||
self.capture.stop_session()
|
||||
self.db.delete_session(session_id)
|
||||
self.db.vacuum()
|
||||
self._send_json({'ok': True})
|
||||
|
||||
def _handle_search(self, session_id, query):
|
||||
|
||||
Reference in new issue
Block a user