Files
scud_ai/services/scud_etl/pipeline.py
T

118 lines
4.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
===============================================================================
FILE: services/scud_etl/pipeline.py
ROLE: Загрузка наилучших срезов СКУД и штата/отсутствий 1С с умным fallback-ом.
===============================================================================
"""
import os
import logging
from typing import Optional, Dict, Any, Tuple
import pandas as pd
from core.connection import get_connection
from core.database import load_scud_from_db_by_snapshot
from config import DATA_DIR
from services.data_loader import load_1c_data_smart, load_staff_data, load_absent_data
logger = logging.getLogger("SCUD_PIPELINE")
def load_best_snapshot_for_date(date_str: str, prefer_final_y: bool = False) -> Optional[pd.DataFrame]:
"""
Загружает наилучший срез СКУД за дату.
Если prefer_final_y=True — отдает предпочтение финишному Y (23:59:59).
"""
with get_connection() as conn:
cursor = conn.cursor()
target_snap_id = None
if prefer_final_y:
cursor.execute("""
SELECT snapshot_id
FROM scud_logs
WHERE log_date = ?
AND (snapshot_id LIKE 'Y%' OR snapshot_time LIKE '%23:59:59' OR snapshot_time LIKE '%22:00:00')
ORDER BY id DESC LIMIT 1
""", (date_str,))
row = cursor.fetchone()
if row:
target_snap_id = row[0]
if not target_snap_id:
cursor.execute("""
SELECT snapshot_id
FROM scud_logs
WHERE log_date = ?
ORDER BY id DESC LIMIT 1
""", (date_str,))
row = cursor.fetchone()
if row:
target_snap_id = row[0]
if not target_snap_id:
return None
return load_scud_from_db_by_snapshot(date_str, snapshot_param=target_snap_id)
def load_1c_files_for_date(date_str: str) -> Tuple[Optional[pd.DataFrame], Optional[pd.DataFrame]]:
clean_date = date_str.replace('_', '.')
try:
from services.share_copier import copy_1c_files_from_share
copy_1c_files_from_share()
except Exception as e:
logger.warning(f"[Pipeline] Ошибка копирования с шары: {e}")
df_staff, df_abs = load_1c_data_smart(clean_date, use_db=True)
# 1. Fallback для штата
if df_staff is None or df_staff.empty:
with get_connection(row_factory=True) as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT snapshot_date
FROM zup_staff
ORDER BY
SUBSTR(snapshot_date, 7, 4) DESC,
SUBSTR(snapshot_date, 4, 2) DESC,
SUBSTR(snapshot_date, 1, 2) DESC,
id DESC
LIMIT 1
""")
row = cursor.fetchone()
if row and row[0]:
fallback_date = row[0]
df_staff = pd.read_sql_query(
"SELECT fio as 'ФИО', fio_clean, department as 'Подразделение', position as 'Должность' FROM zup_staff WHERE snapshot_date = ?",
conn, params=(fallback_date,)
)
logger.info(f"[Pipeline] Для даты {clean_date} применен штат за {fallback_date} ({len(df_staff)} чел.)")
# 2. ⭐️ Гарантированное обогащение свежими ручными командировками и "Иным" прямо из SQLite
try:
from services.manual_absences_repo import get_active_manual_absences_for_date
manual_records = get_active_manual_absences_for_date(clean_date)
if manual_records:
if df_abs is None:
df_abs = pd.DataFrame(columns=['fio_clean', 'Вид_отсутствия'])
existing_fios = set(df_abs['fio_clean'].dropna().tolist()) if not df_abs.empty else set()
new_manual_rows = []
for r in manual_records:
fc = r['fio_clean']
if fc not in existing_fios:
label = "Мест. командир." if r['absence_type'] == 'LOCAL_TRIP' else (r.get('reason') or "Иное")
new_manual_rows.append({
'fio_clean': fc,
'Вид_отсутствия': label
})
existing_fios.add(fc)
if new_manual_rows:
df_abs = pd.concat([df_abs, pd.DataFrame(new_manual_rows)], ignore_index=True)
logger.info(f"[Pipeline] В отчет за {clean_date} подтянуто {len(new_manual_rows)} чел. из реестров командировок/иного.")
except Exception as e:
logger.warning(f"[Pipeline] Ошибка наложения manual_absences: {e}")
return df_staff, df_abs