From 66a17bde45d92a46fbe6a84da2a7e364f5b2d2eb Mon Sep 17 00:00:00 2001 From: manoraga Date: Fri, 11 Sep 2026 15:25:44 +0300 Subject: [PATCH] =?UTF-8?q?refactor(core):=20=D0=B4=D0=B5=D0=BA=D0=BE?= =?UTF-8?q?=D0=BC=D0=BF=D0=BE=D0=B7=D0=B8=D1=86=D0=B8=D1=8F=20db=5Fcli,=20?= =?UTF-8?q?=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81=D0=BD=D1=8B=D0=B9=20=D0=B4?= =?UTF-8?q?=D0=B0=D0=BC=D0=BF=20=D0=91=D0=94=20=D0=B8=20=D0=B7=D0=B0=D1=87?= =?UTF-8?q?=D0=B8=D1=81=D1=82=D0=BA=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=82=D0=B5?= =?UTF-8?q?=D0=BA=D1=81=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/database.py | 24 +++++++++++++- modules/web_api/llm/db/db_chat.py | 34 ++++++++++++++++++- scripts/db_cli.py | 55 +++++-------------------------- 3 files changed, 64 insertions(+), 49 deletions(-) diff --git a/core/database.py b/core/database.py index 1dca47f..8ecb937 100644 --- a/core/database.py +++ b/core/database.py @@ -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.schema import init_all_tables @@ -32,4 +36,22 @@ from core.repositories.zup_repo import ( add_department_synonym_to_db ) -init_db = init_all_tables \ No newline at end of file +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 \ No newline at end of file diff --git a/modules/web_api/llm/db/db_chat.py b/modules/web_api/llm/db/db_chat.py index 6e20e88..8835ab2 100644 --- a/modules/web_api/llm/db/db_chat.py +++ b/modules/web_api/llm/db/db_chat.py @@ -53,4 +53,36 @@ def db_clear_chat_history(session_id: str) -> None: cursor = conn.cursor() cursor.execute("DELETE FROM chat_messages WHERE session_id = ?", (session_id,)) conn.commit() - conn.close() \ No newline at end of file + 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 \ No newline at end of file diff --git a/scripts/db_cli.py b/scripts/db_cli.py index 17204fa..9751d0e 100644 --- a/scripts/db_cli.py +++ b/scripts/db_cli.py @@ -7,6 +7,8 @@ from datetime import datetime 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 config import DATA_DIR, DATE_TODAY, OUTPUT_DIR, EXCEPTIONS_PATH, normalize_fio from core.database import ( @@ -297,15 +299,8 @@ def print_rules(): def dump_all_to_excel(out_filename="db_dump_full.xlsx"): """Дампит всю базу SQLite во многостраничный Excel.""" - out_path = os.path.join(OUTPUT_DIR, out_filename) - print(f"\n[🔄] Создание полного дампа БД в файл: {out_path} ...") - 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"\n[🔄] Создание полного дампа БД в файл: {out_filename} ...") + out_path = dump_database_to_excel(out_filename) 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): - """ - Очистка контекста сообщений: - - По умолчанию: удаляет эфемерные сообщения, осиротевшие превью и сбрасывает стейты сессий. - - purge_all=True (--all): полностью очищает всю таблицу chat_messages и сбрасывает сессии. - """ - 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") + """Очистка контекста сообщений через репозиторий чата.""" + deleted_msgs = db_clear_all_chat_context(session_id=session_id, purge_all=purge_all) + mode = "Полная" if purge_all else "Умная" + print(f"\n[✓] {mode} зачистка контекста выполнена! Удалено сообщений: {deleted_msgs}\n") # ⭐️ Новые функции управления исключениями (Exceptions & Whitelist)