feat(reports): stabilize on-demand generation, live presence and 1C fallback

This commit is contained in:
2026-09-25 13:00:11 +03:00
parent 2f8cc1bffd
commit 90656944c5
42 changed files with 13410 additions and 6778 deletions
+27 -18
View File
@@ -12,7 +12,7 @@ import pandas as pd
from config import DATE_TODAY
from core.database import load_scud_from_db_by_snapshot
from services.scud_etl.pipeline import load_1c_files_for_date
from services.scud_etl.pipeline import load_best_snapshot_for_date, load_1c_files_for_date
from services.scud_etl.merger import merge_scud_and_1c, calculate_summary_metrics
from services.scud_etl.anomaly_detector import detect_registry_anomalies
from services.snapshots.finder import find_or_create_snapshot_for_time
@@ -27,37 +27,46 @@ def generate_svodka_service(
snapshot_id: Optional[str] = None
) -> Dict[str, Any]:
"""
Формирует оперативную сводку на указанную дату / время:
- target_date: дата сводки (по умолчанию сегодня).
- target_time: время среза (например '14:30').
- snapshot_id: точный ID среза.
Формирует оперативную сводку:
- По умолчанию берет ПОСЛЕДНИЙ готовый снапшот из базы SQLite (без долгого опроса MS SQL).
"""
date_clean = str(target_date or DATE_TODAY).replace('_', '.')
applied_note = ""
# Если передано время, но не указан конкретный snapshot_id — ищем ближайший или запрашиваем экспорт
if target_time and not snapshot_id:
found_id, note = find_or_create_snapshot_for_time(date_clean, target_time, allow_ondemand_export=True)
snapshot_id = found_id
# 1. Загрузка среза СКУД строго из готовых в базе
if snapshot_id:
df_scud = load_scud_from_db_by_snapshot(date_clean, snapshot_param=snapshot_id)
elif target_time:
found_id, note = find_or_create_snapshot_for_time(date_clean, target_time, allow_ondemand_export=False)
applied_note = note
if note:
logger.info(note)
df_scud = load_scud_from_db_by_snapshot(date_clean, snapshot_param=found_id)
else:
# По умолчанию: берем самый свежий существующий срез за дату
df_scud = load_best_snapshot_for_date(date_clean, prefer_final_y=False)
df_scud = load_scud_from_db_by_snapshot(date_clean, snapshot_param=snapshot_id)
if df_scud is None or df_scud.empty:
return {
"status": "error",
"message": f"Срез СКУД за {date_clean} ({applied_note or snapshot_id or 'последний доступный'}) не найден в базе."
"message": f"Срез СКУД за {date_clean} не найден в базе данных."
}
# Извлекаем время фактического среза для имени файла
actual_snap_time = ""
if 'snapshot_time' in df_scud.columns and not df_scud.empty:
raw_st = str(df_scud['snapshot_time'].iloc[0]).strip()
if " " in raw_st:
actual_snap_time = raw_st.split()[1][:5].replace(':', '-')
# 2. Загружаем актуальные кадровые данные 1С (штат + отсутствия + реестры)
df_staff, df_abs = load_1c_files_for_date(date_clean)
# 3. Слияние и расчет
df_merged = merge_scud_and_1c(df_scud, df_staff, df_abs)
metrics = calculate_summary_metrics(df_merged)
anomalies = detect_registry_anomalies(df_merged, df_raw_scud=df_scud)
# Добавляем суффикс времени в имя файла, если сводка строилась на точный срез
time_suffix = f" на {target_time.replace(':', '-')}" if target_time else ""
# 4. Формирование книги Excel
time_suffix = f" на {actual_snap_time}" if actual_snap_time else ""
filename = f"{format_date_ru(date_clean)} сводка{time_suffix}.xlsx"
generate_summary_excel(df_merged, date_str=date_clean, filename=filename)
@@ -68,13 +77,13 @@ def generate_svodka_service(
"status": "success",
"report_type": "SVODKA",
"date": date_clean,
"target_time": target_time,
"snapshot_id": snapshot_id or "AUTO_LATEST",
"target_time": actual_snap_time,
"snapshot_id": snapshot_id or "LATEST_READY",
"filename": filename,
"filepath": full_filepath,
"download_url": f"/api/v1/files/download/reports/{os.path.basename(full_filepath)}",
"metrics": metrics,
"anomalies_count": len(anomalies),
"note": applied_note,
"message": f"Ежедневная сводка на {date_clean} {target_time or ''} успешно сформирована."
"message": f"Ежедневная сводка на {date_clean} успешно сформирована."
}