Files
scud_ai/core/database.py
T

57 lines
2.0 KiB
Python

"""
===============================================================================
FILE: core/database.py
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
from core.repositories.scud_repo import (
has_scud_logs_for_date,
has_yesterday_final_snapshot,
get_or_create_snapshot_id,
save_scud_to_db,
get_latest_snapshot_time,
load_scud_from_db_by_snapshot,
get_available_snapshots,
delete_snapshot_by_id,
delete_snapshots_by_date
)
from core.repositories.zup_repo import (
save_staff_to_db,
load_staff_from_db,
save_absences_to_db,
load_absences_from_db,
save_anomalies_to_db,
get_all_rules_from_db,
add_rule_to_db,
get_department_synonyms_dict,
add_department_synonym_to_db
)
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