refactor(core): декомпозиция db_cli, сервисный дамп БД и зачистка контекста
This commit is contained in:
@@ -5,6 +5,10 @@ ROLE: Фасад ядра базы данных с полной обратной
|
|||||||
===============================================================================
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import pandas as pd
|
||||||
|
from config import OUTPUT_DIR
|
||||||
|
|
||||||
from core.connection import get_connection, DB_PATH
|
from core.connection import get_connection, DB_PATH
|
||||||
from core.schema import init_all_tables
|
from core.schema import init_all_tables
|
||||||
|
|
||||||
@@ -33,3 +37,21 @@ from core.repositories.zup_repo import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
init_db = init_all_tables
|
init_db = init_all_tables
|
||||||
|
|
||||||
|
def dump_database_to_excel(out_filename: str = "db_dump_full.xlsx") -> str:
|
||||||
|
"""Создает полный дамп всех ключевых таблиц SQLite в многостраничный Excel."""
|
||||||
|
out_path = os.path.join(OUTPUT_DIR, out_filename)
|
||||||
|
tables = [
|
||||||
|
'scud_logs', 'scud_events_raw', 'zup_staff', 'zup_absences',
|
||||||
|
'anomalies_history', 'ai_knowledge_base', 'chat_messages',
|
||||||
|
'session_states', 'system_prompt_nodes', 'tasks',
|
||||||
|
'exceptions_registry', 'manual_absences', 'person_identity_mapping'
|
||||||
|
]
|
||||||
|
with get_connection() as conn, pd.ExcelWriter(out_path, engine='openpyxl') as writer:
|
||||||
|
for table in tables:
|
||||||
|
try:
|
||||||
|
df = pd.read_sql_query(f"SELECT * FROM {table}", conn)
|
||||||
|
df.to_excel(writer, sheet_name=table[:31], index=False)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return out_path
|
||||||
@@ -54,3 +54,35 @@ def db_clear_chat_history(session_id: str) -> None:
|
|||||||
cursor.execute("DELETE FROM chat_messages WHERE session_id = ?", (session_id,))
|
cursor.execute("DELETE FROM chat_messages WHERE session_id = ?", (session_id,))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
def db_clear_all_chat_context(session_id: str = None, purge_all: bool = False) -> int:
|
||||||
|
"""Удаляет сообщения чата и стейты сессий."""
|
||||||
|
with get_db_connection() as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
if purge_all:
|
||||||
|
if session_id:
|
||||||
|
cursor.execute("DELETE FROM chat_messages WHERE session_id = ?", (session_id,))
|
||||||
|
cursor.execute("DELETE FROM session_states WHERE session_id = ?", (session_id,))
|
||||||
|
else:
|
||||||
|
cursor.execute("DELETE FROM chat_messages")
|
||||||
|
cursor.execute("DELETE FROM session_states")
|
||||||
|
cnt = cursor.rowcount
|
||||||
|
conn.commit()
|
||||||
|
return cnt
|
||||||
|
|
||||||
|
query = """
|
||||||
|
DELETE FROM chat_messages
|
||||||
|
WHERE is_ephemeral = 1
|
||||||
|
OR content LIKE '%Предпросмотр изменений%'
|
||||||
|
OR content LIKE '%Удален пункт:%'
|
||||||
|
OR content LIKE '%добавлен пункт:%'
|
||||||
|
"""
|
||||||
|
if session_id:
|
||||||
|
cursor.execute(query + " AND session_id = ?", (session_id,))
|
||||||
|
cursor.execute("DELETE FROM session_states WHERE session_id = ?", (session_id,))
|
||||||
|
else:
|
||||||
|
cursor.execute(query)
|
||||||
|
cursor.execute("DELETE FROM session_states")
|
||||||
|
cnt = cursor.rowcount
|
||||||
|
conn.commit()
|
||||||
|
return cnt
|
||||||
+8
-47
@@ -7,6 +7,8 @@ from datetime import datetime
|
|||||||
|
|
||||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
|
|
||||||
|
from modules.web_api.llm.db.db_chat import db_clear_all_chat_context
|
||||||
|
from core.database import dump_database_to_excel
|
||||||
from core.repositories.scud_repo import get_building_presence
|
from core.repositories.scud_repo import get_building_presence
|
||||||
from config import DATA_DIR, DATE_TODAY, OUTPUT_DIR, EXCEPTIONS_PATH, normalize_fio
|
from config import DATA_DIR, DATE_TODAY, OUTPUT_DIR, EXCEPTIONS_PATH, normalize_fio
|
||||||
from core.database import (
|
from core.database import (
|
||||||
@@ -297,15 +299,8 @@ def print_rules():
|
|||||||
|
|
||||||
def dump_all_to_excel(out_filename="db_dump_full.xlsx"):
|
def dump_all_to_excel(out_filename="db_dump_full.xlsx"):
|
||||||
"""Дампит всю базу SQLite во многостраничный Excel."""
|
"""Дампит всю базу SQLite во многостраничный Excel."""
|
||||||
out_path = os.path.join(OUTPUT_DIR, out_filename)
|
print(f"\n[🔄] Создание полного дампа БД в файл: {out_filename} ...")
|
||||||
print(f"\n[🔄] Создание полного дампа БД в файл: {out_path} ...")
|
out_path = dump_database_to_excel(out_filename)
|
||||||
with get_connection() as conn, pd.ExcelWriter(out_path, engine='openpyxl') as writer:
|
|
||||||
for table in ['scud_logs', 'zup_staff', 'zup_absences', 'anomalies_history', 'ai_knowledge_base', 'chat_messages', 'session_states', 'system_prompt_nodes', 'tasks', 'exceptions_registry']:
|
|
||||||
try:
|
|
||||||
df = pd.read_sql_query(f"SELECT * FROM {table}", conn)
|
|
||||||
df.to_excel(writer, sheet_name=table[:31], index=False)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
print(f"[✓] Дамп успешно сохранен: {out_path}\n")
|
print(f"[✓] Дамп успешно сохранен: {out_path}\n")
|
||||||
|
|
||||||
|
|
||||||
@@ -394,44 +389,10 @@ def print_chat_messages(session_id=None, limit=50):
|
|||||||
|
|
||||||
|
|
||||||
def purge_chat_context(session_id=None, purge_all=False):
|
def purge_chat_context(session_id=None, purge_all=False):
|
||||||
"""
|
"""Очистка контекста сообщений через репозиторий чата."""
|
||||||
Очистка контекста сообщений:
|
deleted_msgs = db_clear_all_chat_context(session_id=session_id, purge_all=purge_all)
|
||||||
- По умолчанию: удаляет эфемерные сообщения, осиротевшие превью и сбрасывает стейты сессий.
|
mode = "Полная" if purge_all else "Умная"
|
||||||
- purge_all=True (--all): полностью очищает всю таблицу chat_messages и сбрасывает сессии.
|
print(f"\n[✓] {mode} зачистка контекста выполнена! Удалено сообщений: {deleted_msgs}\n")
|
||||||
"""
|
|
||||||
with get_connection() as conn:
|
|
||||||
cursor = conn.cursor()
|
|
||||||
|
|
||||||
if purge_all:
|
|
||||||
if session_id:
|
|
||||||
cursor.execute("DELETE FROM chat_messages WHERE session_id = ?", (session_id,))
|
|
||||||
cursor.execute("DELETE FROM session_states WHERE session_id = ?", (session_id,))
|
|
||||||
else:
|
|
||||||
cursor.execute("DELETE FROM chat_messages")
|
|
||||||
cursor.execute("DELETE FROM session_states")
|
|
||||||
deleted_msgs = cursor.rowcount
|
|
||||||
conn.commit()
|
|
||||||
print(f"\n[✓] Полная очистка истории выполнена! Удалено сообщений: {deleted_msgs}\n")
|
|
||||||
return
|
|
||||||
|
|
||||||
query = """
|
|
||||||
DELETE FROM chat_messages
|
|
||||||
WHERE is_ephemeral = 1
|
|
||||||
OR content LIKE '%Предпросмотр изменений%'
|
|
||||||
OR content LIKE '%Удален пункт:%'
|
|
||||||
OR content LIKE '%добавлен пункт:%'
|
|
||||||
"""
|
|
||||||
if session_id:
|
|
||||||
cursor.execute(query + " AND session_id = ?", (session_id,))
|
|
||||||
cursor.execute("DELETE FROM session_states WHERE session_id = ?", (session_id,))
|
|
||||||
else:
|
|
||||||
cursor.execute(query)
|
|
||||||
cursor.execute("DELETE FROM session_states")
|
|
||||||
|
|
||||||
deleted_msgs = cursor.rowcount
|
|
||||||
conn.commit()
|
|
||||||
|
|
||||||
print(f"\n[✓] Умная зачистка контекста выполнена! Удалено сообщений: {deleted_msgs}\n")
|
|
||||||
|
|
||||||
|
|
||||||
# ⭐️ Новые функции управления исключениями (Exceptions & Whitelist)
|
# ⭐️ Новые функции управления исключениями (Exceptions & Whitelist)
|
||||||
|
|||||||
Reference in New Issue
Block a user