промежуточный этап. Работает.
This commit is contained in:
+69
-13
@@ -72,8 +72,74 @@ def load_absent_data(date_str):
|
||||
return None
|
||||
|
||||
|
||||
def process_scud_anomalies(df_scud, exceptions=None):
|
||||
"""
|
||||
Вычисляет 'Первую активность' и маркирует аномалии СКУД.
|
||||
Если 'Начало дня' == 'Нет входа', но есть любая физическая активность —
|
||||
записывает первейшее время в 'Первая_активность', сбрасывает 'Пришел' в False и выставляет флаг аномалии.
|
||||
"""
|
||||
if exceptions is None:
|
||||
exceptions = load_exceptions()
|
||||
|
||||
start_col = 'Начало_дня' if 'Начало_дня' in df_scud.columns else ('Начало дня' if 'Начало дня' in df_scud.columns else None)
|
||||
end_col = 'Конец_дня' if 'Конец_дня' in df_scud.columns else ('Конец дня' if 'Конец дня' in df_scud.columns else None)
|
||||
status_col = 'Статус' if 'Статус' in df_scud.columns else None
|
||||
|
||||
first_activities = []
|
||||
anomaly_flags = []
|
||||
is_present_flags = []
|
||||
|
||||
for _, row in df_scud.iterrows():
|
||||
fio = str(row.get('Сотрудник', row.get('fio_raw', ''))).strip()
|
||||
fio_clean = normalize_fio(fio)
|
||||
dept = str(row.get('Подразделение', '')).strip()
|
||||
pos = str(row.get('Должность', '')).strip()
|
||||
|
||||
val_start = str(row.get(start_col, '')).strip() if start_col else ''
|
||||
val_end = str(row.get(end_col, '')).strip() if end_col else ''
|
||||
val_status = str(row.get(status_col, '')).strip().lower() if status_col else ''
|
||||
|
||||
# Извлечение первого зафиксированного события из сырых логов (pLogData), если передавалось
|
||||
raw_first_event = row.get('raw_first_event', None)
|
||||
if pd.isna(raw_first_event) or str(raw_first_event).strip() in ['', 'None', 'nan', '00:00:00']:
|
||||
raw_first_event = None
|
||||
|
||||
has_no_in = (val_start in ['нет входа', 'none', 'nan', '', '00:00:00', '00:00'])
|
||||
has_no_out = (val_end in ['нет выхода', 'none', 'nan', '', '00:00:00', '00:00'])
|
||||
|
||||
first_act = '—'
|
||||
anom_flag = 'NONE'
|
||||
|
||||
# Стандартная проверка присутствия
|
||||
is_present = not ('отсутствовал' in val_status or has_no_in)
|
||||
|
||||
# ЛОГИКА ВЫЧИСЛЕНИЯ ПЕРВОЙ АКТИВНОСТИ И ДЕТЕКТИРОВАНИЯ АНОМАЛИИ
|
||||
if has_no_in:
|
||||
# Если нет входа, но есть проход внутри или зафиксирован выход
|
||||
if raw_first_event is not None or not has_no_out:
|
||||
first_act = str(raw_first_event) if raw_first_event else val_end
|
||||
anom_flag = 'ANOMALY_NO_IN_HAS_ACTIVITY'
|
||||
|
||||
# ЖЕСТКАЯ ВАЛИДАЦИЯ: Если человек не исключен официально — сбрасываем фейковый Пришел = True
|
||||
if not is_excluded(fio, dept, pos, exceptions):
|
||||
is_present = False
|
||||
else:
|
||||
first_act = '—'
|
||||
is_present = False
|
||||
|
||||
first_activities.append(first_act)
|
||||
anomaly_flags.append(anom_flag)
|
||||
is_present_flags.append(is_present)
|
||||
|
||||
df_scud['Первая_активность'] = first_activities
|
||||
df_scud['anomaly_flag'] = anomaly_flags
|
||||
df_scud['Пришел'] = is_present_flags
|
||||
|
||||
return df_scud
|
||||
|
||||
|
||||
def load_scud_data(target_date_type="today"):
|
||||
"""Загружает файл СКУД за 'today' или 'yesterday'."""
|
||||
"""Загружает файл СКУД за 'today' или 'yesterday' и выполняет валидацию аномалий."""
|
||||
target_date = DATE_TODAY if target_date_type == "today" else DATE_YESTERDAY
|
||||
scud_path = find_dated_file("Сотрудники", target_date, search_dirs=[SCUD_DIR])
|
||||
|
||||
@@ -88,19 +154,9 @@ def load_scud_data(target_date_type="today"):
|
||||
df_scud['fio_raw'] = df_scud['Сотрудник'].astype(str) if 'Сотрудник' in df_scud.columns else df_scud[df_scud.columns[0]].astype(str)
|
||||
df_scud['fio_clean'] = df_scud['fio_raw'].apply(clean_scud_fio_light)
|
||||
|
||||
start_col = 'Начало_дня' if 'Начало_дня' in df_scud.columns else ('Начало дня' if 'Начало дня' in df_scud.columns else None)
|
||||
status_col = 'Статус' if 'Статус' in df_scud.columns else None
|
||||
# Применяем обработчик аномалий и первой активности
|
||||
df_scud = process_scud_anomalies(df_scud, exceptions=exceptions)
|
||||
|
||||
def is_person_present(row):
|
||||
val_status = str(row.get(status_col, '')).strip().lower() if status_col else ''
|
||||
val_time = str(row.get(start_col, '')).strip() if start_col else ''
|
||||
|
||||
if 'отсутствовал' in val_status or val_time in ['нет входа', 'none', 'nan', '', '00:00']:
|
||||
return False
|
||||
return True
|
||||
|
||||
df_scud['Пришел'] = df_scud.apply(is_person_present, axis=1)
|
||||
|
||||
df_scud['is_excluded'] = df_scud.apply(
|
||||
lambda r: is_excluded(r.get('Сотрудник', ''), r.get('Подразделение', ''), r.get('Должность', ''), exceptions),
|
||||
axis=1
|
||||
|
||||
Reference in New Issue
Block a user