Compare commits
16
Commits
27e8055a3d
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11691701e1 | ||
|
|
90656944c5 | ||
|
|
2f8cc1bffd | ||
|
|
30651672ab | ||
|
|
9c89b6592e | ||
|
|
565cf5af37 | ||
|
|
86f223a21e | ||
|
|
b1796de852 | ||
|
|
aa1141fb88 | ||
|
|
66a17bde45 | ||
|
|
baa5073b88 | ||
|
|
74332aa38a | ||
|
|
6c9b131cf2 | ||
|
|
17c960fa34 | ||
|
|
d391a08224 | ||
|
|
1bb95cd8e1 |
@@ -27,3 +27,10 @@ data/1c/*
|
|||||||
!data/scud/.gitkeep
|
!data/scud/.gitkeep
|
||||||
!data/1c/.gitkeep
|
!data/1c/.gitkeep
|
||||||
!data/static_reason_workers.csv
|
!data/static_reason_workers.csv
|
||||||
|
|
||||||
|
*.db-shm
|
||||||
|
*.db-wal
|
||||||
|
data/scud_orion_ai.db
|
||||||
|
data/uploads/
|
||||||
|
*snapshot.md
|
||||||
|
*.patch
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ else:
|
|||||||
DATE_YESTERDAY = (NOW - timedelta(days=1)).strftime("%d.%m.%Y")
|
DATE_YESTERDAY = (NOW - timedelta(days=1)).strftime("%d.%m.%Y")
|
||||||
|
|
||||||
OLLAMA_URL = "http://10.121.17.227:11434/api/generate"
|
OLLAMA_URL = "http://10.121.17.227:11434/api/generate"
|
||||||
OLLAMA_MODEL = "qwen2.5:14b-instruct-q8_0"
|
OLLAMA_MODEL = "qwen2.5:14b"
|
||||||
MODEL_NAME = OLLAMA_MODEL
|
MODEL_NAME = OLLAMA_MODEL
|
||||||
|
|
||||||
KNOWLEDGE_BASE_PATH = os.path.join(DATA_DIR, "knowledge_base.json")
|
KNOWLEDGE_BASE_PATH = os.path.join(DATA_DIR, "knowledge_base.json")
|
||||||
|
|||||||
@@ -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.connection import get_connection, DB_PATH
|
||||||
from core.schema import init_all_tables
|
from core.schema import init_all_tables
|
||||||
|
|
||||||
@@ -33,3 +37,21 @@ from core.repositories.zup_repo import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
init_db = init_all_tables
|
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
|
||||||
+166
-34
@@ -21,32 +21,50 @@ def has_yesterday_final_snapshot(date_str: str) -> bool:
|
|||||||
with get_connection() as conn:
|
with get_connection() as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"SELECT 1 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') LIMIT 1",
|
"""
|
||||||
|
SELECT 1 FROM scud_logs
|
||||||
|
WHERE log_date = ?
|
||||||
|
AND (snapshot_id LIKE 'Y%' OR snapshot_id LIKE '%_FINAL%' OR snapshot_time LIKE '%23:59:59' OR snapshot_time LIKE '%22:00:00')
|
||||||
|
LIMIT 1
|
||||||
|
""",
|
||||||
(date_str,)
|
(date_str,)
|
||||||
)
|
)
|
||||||
return cursor.fetchone() is not None
|
return cursor.fetchone() is not None
|
||||||
|
|
||||||
|
|
||||||
def get_or_create_snapshot_id(snapshot_time: str, date_str: str = None, is_yesterday: bool = False) -> str:
|
def get_or_create_snapshot_id(snapshot_time: str, date_str: str = None, is_yesterday: bool = False) -> str:
|
||||||
try:
|
"""
|
||||||
dt_snap = datetime.strptime(snapshot_time, "%Y-%m-%d %H:%M:%S").date()
|
Генерирует понятный и уникальный ID снапшота:
|
||||||
date_prefix = dt_snap.strftime("%Y%m%d")
|
- Дата префикса берется строго из даты самих логов (date_str).
|
||||||
except (ValueError, TypeError):
|
- Для итоговых срезов дня: YYYYYMMDD_FINAL (строго с буквой Y в начале).
|
||||||
dt_snap = datetime.now().date()
|
- Для дневных срезов на время: YYYYMMDD_HHMM.
|
||||||
date_prefix = dt_snap.strftime("%Y%m%d")
|
"""
|
||||||
|
|
||||||
if date_str:
|
if date_str:
|
||||||
try:
|
try:
|
||||||
dt_log = datetime.strptime(date_str, "%d.%m.%Y").date()
|
dt_log = datetime.strptime(date_str.replace('_', '.'), "%d.%m.%Y").date()
|
||||||
if dt_log < dt_snap:
|
date_prefix = dt_log.strftime("%Y%m%d")
|
||||||
is_yesterday = True
|
except Exception:
|
||||||
|
dt_log = datetime.now().date()
|
||||||
|
date_prefix = dt_log.strftime("%Y%m%d")
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
dt_snap = datetime.strptime(snapshot_time.split()[0], "%Y-%m-%d").date()
|
||||||
|
date_prefix = dt_snap.strftime("%Y%m%d")
|
||||||
|
except (ValueError, TypeError, IndexError):
|
||||||
|
date_prefix = datetime.now().strftime("%Y%m%d")
|
||||||
|
|
||||||
|
time_part = "2359"
|
||||||
|
try:
|
||||||
|
t_str = snapshot_time.split()[1] if " " in snapshot_time else snapshot_time
|
||||||
|
t_parts = t_str.split(":")
|
||||||
|
time_part = f"{t_parts[0]}{t_parts[1]}"
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
prefix = "Y" if is_yesterday else ""
|
|
||||||
|
|
||||||
with get_connection() as conn:
|
with get_connection() as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
# Если срез с точно таким же временем и датой уже существует — возвращаем его ID
|
||||||
if date_str:
|
if date_str:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"SELECT snapshot_id FROM scud_logs WHERE log_date = ? AND snapshot_time = ? AND snapshot_id IS NOT NULL LIMIT 1",
|
"SELECT snapshot_id FROM scud_logs WHERE log_date = ? AND snapshot_time = ? AND snapshot_id IS NOT NULL LIMIT 1",
|
||||||
@@ -62,28 +80,51 @@ def get_or_create_snapshot_id(snapshot_time: str, date_str: str = None, is_yeste
|
|||||||
if row and row[0]:
|
if row and row[0]:
|
||||||
return row[0]
|
return row[0]
|
||||||
|
|
||||||
|
# Финальный суточный ID: строго с префиксом Y
|
||||||
|
if is_yesterday or time_part in ["2359", "2200"]:
|
||||||
|
base_final_id = f"Y{date_prefix}_FINAL"
|
||||||
|
cursor.execute("SELECT 1 FROM scud_logs WHERE snapshot_id = ? LIMIT 1", (base_final_id,))
|
||||||
|
if not cursor.fetchone():
|
||||||
|
return base_final_id
|
||||||
|
return base_final_id
|
||||||
|
|
||||||
|
# Дневной срез на определенное время
|
||||||
|
base_id = f"{date_prefix}_{time_part}"
|
||||||
|
cursor.execute("SELECT 1 FROM scud_logs WHERE snapshot_id = ? LIMIT 1", (base_id,))
|
||||||
|
if not cursor.fetchone():
|
||||||
|
return base_id
|
||||||
|
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT snapshot_id FROM scud_logs
|
SELECT DISTINCT snapshot_id
|
||||||
WHERE snapshot_id LIKE ? OR snapshot_id LIKE ?
|
FROM scud_logs
|
||||||
ORDER BY snapshot_id DESC LIMIT 1
|
WHERE snapshot_id LIKE ?
|
||||||
""", (f"{date_prefix}-%", f"Y{date_prefix}-%"))
|
""", (f"{base_id}-%",))
|
||||||
|
|
||||||
last_row = cursor.fetchone()
|
rows = cursor.fetchall()
|
||||||
next_seq = 1
|
max_seq = 1
|
||||||
if last_row and last_row[0]:
|
for (s_id,) in rows:
|
||||||
parts = last_row[0].replace("Y", "").split("-")
|
if not s_id:
|
||||||
if len(parts) > 1 and parts[1].isdigit():
|
continue
|
||||||
next_seq = int(parts[1]) + 1
|
try:
|
||||||
|
parts = str(s_id).split('-')
|
||||||
|
if len(parts) >= 2 and parts[-1].isdigit():
|
||||||
|
num = int(parts[-1])
|
||||||
|
if num > max_seq:
|
||||||
|
max_seq = num
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
return f"{prefix}{date_prefix}-{next_seq:03d}"
|
next_seq = max_seq + 1
|
||||||
|
return f"{base_id}-{next_seq:03d}"
|
||||||
|
|
||||||
|
|
||||||
def save_scud_to_db(df_scud: pd.DataFrame, date_str: str, snapshot_time: str = None, is_yesterday: bool = False) -> None:
|
def save_scud_to_db(df_scud: pd.DataFrame, date_str: str, snapshot_time: str = None, is_yesterday: bool = False) -> None:
|
||||||
if df_scud is None or df_scud.empty:
|
if df_scud is None or df_scud.empty:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
now_local_str = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
if not snapshot_time:
|
if not snapshot_time:
|
||||||
snapshot_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
snapshot_time = now_local_str
|
||||||
|
|
||||||
snapshot_id = get_or_create_snapshot_id(snapshot_time, date_str=date_str, is_yesterday=is_yesterday)
|
snapshot_id = get_or_create_snapshot_id(snapshot_time, date_str=date_str, is_yesterday=is_yesterday)
|
||||||
|
|
||||||
@@ -101,7 +142,8 @@ def save_scud_to_db(df_scud: pd.DataFrame, date_str: str, snapshot_time: str = N
|
|||||||
1 if r.get('Пришел', False) else 0,
|
1 if r.get('Пришел', False) else 0,
|
||||||
r.get('anomaly_flag', 'NONE'),
|
r.get('anomaly_flag', 'NONE'),
|
||||||
snapshot_time,
|
snapshot_time,
|
||||||
snapshot_id
|
snapshot_id,
|
||||||
|
now_local_str # ⭐️ Передаем локальное время машины напрямую
|
||||||
)
|
)
|
||||||
for _, r in df_scud.iterrows()
|
for _, r in df_scud.iterrows()
|
||||||
]
|
]
|
||||||
@@ -113,9 +155,9 @@ def save_scud_to_db(df_scud: pd.DataFrame, date_str: str, snapshot_time: str = N
|
|||||||
INSERT INTO scud_logs (
|
INSERT INTO scud_logs (
|
||||||
log_date, fio, fio_clean, department, position,
|
log_date, fio, fio_clean, department, position,
|
||||||
time_in, first_activity, time_out, time_in_building,
|
time_in, first_activity, time_out, time_in_building,
|
||||||
is_present, anomaly_flag, snapshot_time, snapshot_id
|
is_present, anomaly_flag, snapshot_time, snapshot_id, created_at
|
||||||
)
|
)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
""", data_to_insert)
|
""", data_to_insert)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
@@ -135,25 +177,26 @@ def load_scud_from_db_by_snapshot(date_str: str, snapshot_param: str = None) ->
|
|||||||
with get_connection() as conn:
|
with get_connection() as conn:
|
||||||
df = pd.DataFrame()
|
df = pd.DataFrame()
|
||||||
|
|
||||||
# 1. Если передан конкретный ID снапшота (например 'Y20260820-004')
|
|
||||||
if snapshot_param:
|
if snapshot_param:
|
||||||
df = pd.read_sql_query(
|
df = pd.read_sql_query(
|
||||||
"SELECT * FROM scud_logs WHERE snapshot_id = ?",
|
"SELECT * FROM scud_logs WHERE snapshot_id = ?",
|
||||||
conn, params=(str(snapshot_param),)
|
conn, params=(str(snapshot_param),)
|
||||||
)
|
)
|
||||||
|
|
||||||
# 2. Если ищем за дату (для вчерашнего дня строго ищем Y-снапшот)
|
|
||||||
if df.empty and date_str:
|
if df.empty and date_str:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# ⭐️ Жесткий приоритет 1: Ищем снапшот с префиксом 'Y'
|
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"SELECT snapshot_id FROM scud_logs WHERE log_date = ? AND snapshot_id LIKE 'Y%' ORDER BY snapshot_time DESC, id DESC LIMIT 1",
|
"""
|
||||||
|
SELECT snapshot_id FROM scud_logs
|
||||||
|
WHERE log_date = ?
|
||||||
|
AND (snapshot_id LIKE 'Y%' OR snapshot_id LIKE '%_FINAL%')
|
||||||
|
ORDER BY snapshot_time DESC, id DESC LIMIT 1
|
||||||
|
""",
|
||||||
(date_str,)
|
(date_str,)
|
||||||
)
|
)
|
||||||
row = cursor.fetchone()
|
row = cursor.fetchone()
|
||||||
|
|
||||||
# Приоритет 2: Если Y нет (например, за сегодня), берем самый свежий по времени
|
|
||||||
if not row:
|
if not row:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"SELECT snapshot_id FROM scud_logs WHERE log_date = ? ORDER BY snapshot_time DESC, id DESC LIMIT 1",
|
"SELECT snapshot_id FROM scud_logs WHERE log_date = ? ORDER BY snapshot_time DESC, id DESC LIMIT 1",
|
||||||
@@ -196,7 +239,12 @@ def get_available_snapshots(date_str: str = None):
|
|||||||
with get_connection() as conn:
|
with get_connection() as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
query = """
|
query = """
|
||||||
SELECT snapshot_id, log_date, snapshot_time, COUNT(*) as cnt
|
SELECT
|
||||||
|
snapshot_id,
|
||||||
|
log_date,
|
||||||
|
snapshot_time,
|
||||||
|
COUNT(*) as cnt,
|
||||||
|
MIN(created_at) as created_at
|
||||||
FROM scud_logs
|
FROM scud_logs
|
||||||
WHERE snapshot_time IS NOT NULL
|
WHERE snapshot_time IS NOT NULL
|
||||||
"""
|
"""
|
||||||
@@ -225,3 +273,87 @@ def delete_snapshots_by_date(date_str: str) -> int:
|
|||||||
cnt = cursor.rowcount
|
cnt = cursor.rowcount
|
||||||
conn.commit()
|
conn.commit()
|
||||||
return cnt
|
return cnt
|
||||||
|
|
||||||
|
|
||||||
|
def save_raw_events_to_db(df_raw_events: pd.DataFrame, date_str: str) -> int:
|
||||||
|
if df_raw_events is None or df_raw_events.empty:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
records = [
|
||||||
|
(
|
||||||
|
date_str,
|
||||||
|
str(r.get('TimeVal', '')),
|
||||||
|
int(r.get('HozOrgan', 0)),
|
||||||
|
str(r.get('Сотрудник', '')),
|
||||||
|
str(r.get('fio_clean', '')),
|
||||||
|
str(r.get('Подразделение', '')),
|
||||||
|
int(r.get('Event', 0)),
|
||||||
|
int(r.get('Mode', 0)),
|
||||||
|
int(r.get('DoorIndex')) if pd.notna(r.get('DoorIndex')) else None,
|
||||||
|
str(r.get('Direction', 'OTHER'))
|
||||||
|
)
|
||||||
|
for _, r in df_raw_events.iterrows()
|
||||||
|
]
|
||||||
|
|
||||||
|
with get_connection() as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("DELETE FROM scud_events_raw WHERE log_date = ?", (date_str,))
|
||||||
|
cursor.executemany("""
|
||||||
|
INSERT INTO scud_events_raw (
|
||||||
|
log_date, time_val, hoz_organ, fio, fio_clean,
|
||||||
|
department, event_code, mode, door_index, direction
|
||||||
|
)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""", records)
|
||||||
|
conn.commit()
|
||||||
|
return len(records)
|
||||||
|
|
||||||
|
|
||||||
|
def get_building_presence(date_str: str, only_inside: bool = True) -> list[dict]:
|
||||||
|
with get_connection() as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
query = """
|
||||||
|
WITH RankedEvents AS (
|
||||||
|
SELECT
|
||||||
|
hoz_organ,
|
||||||
|
fio,
|
||||||
|
department,
|
||||||
|
time_val,
|
||||||
|
direction,
|
||||||
|
ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY hoz_organ
|
||||||
|
ORDER BY time_val DESC, id DESC
|
||||||
|
) as rn
|
||||||
|
FROM scud_events_raw
|
||||||
|
WHERE log_date = ?
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
hoz_organ,
|
||||||
|
fio,
|
||||||
|
department,
|
||||||
|
time_val,
|
||||||
|
direction
|
||||||
|
FROM RankedEvents
|
||||||
|
WHERE rn = 1
|
||||||
|
"""
|
||||||
|
if only_inside:
|
||||||
|
query += " AND direction = 'IN'"
|
||||||
|
|
||||||
|
query += " ORDER BY fio ASC;"
|
||||||
|
|
||||||
|
cursor.execute(query, (date_str,))
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
|
||||||
|
results = [
|
||||||
|
{
|
||||||
|
"hoz_organ": r[0],
|
||||||
|
"fio": r[1],
|
||||||
|
"department": r[2],
|
||||||
|
"last_event_time": r[3],
|
||||||
|
"direction": r[4],
|
||||||
|
"status": "В здании" if r[4] == "IN" else "Вышел"
|
||||||
|
}
|
||||||
|
for r in rows
|
||||||
|
]
|
||||||
|
results.sort(key=lambda x: x["fio"].lower())
|
||||||
|
return results
|
||||||
@@ -38,6 +38,24 @@ def init_all_tables() -> None:
|
|||||||
);
|
);
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
# 1.1 Сырые физические события турникетов СКУД (для перекуров и оперативного статуса)
|
||||||
|
cursor.execute("""
|
||||||
|
CREATE TABLE IF NOT EXISTS scud_events_raw (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
log_date TEXT NOT NULL,
|
||||||
|
time_val TEXT NOT NULL,
|
||||||
|
hoz_organ INTEGER NOT NULL,
|
||||||
|
fio TEXT NOT NULL,
|
||||||
|
fio_clean TEXT NOT NULL,
|
||||||
|
department TEXT,
|
||||||
|
event_code INTEGER NOT NULL,
|
||||||
|
mode INTEGER NOT NULL,
|
||||||
|
door_index INTEGER DEFAULT 1,
|
||||||
|
direction TEXT NOT NULL, -- 'IN' или 'OUT'
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
""")
|
||||||
|
|
||||||
# 2. Кадровые реестры 1С
|
# 2. Кадровые реестры 1С
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
CREATE TABLE IF NOT EXISTS zup_staff (
|
CREATE TABLE IF NOT EXISTS zup_staff (
|
||||||
@@ -172,5 +190,7 @@ def init_all_tables() -> None:
|
|||||||
cursor.execute("CREATE INDEX IF NOT EXISTS idx_scud_date ON scud_logs(log_date);")
|
cursor.execute("CREATE INDEX IF NOT EXISTS idx_scud_date ON scud_logs(log_date);")
|
||||||
cursor.execute("CREATE INDEX IF NOT EXISTS idx_scud_fio ON scud_logs(fio_clean);")
|
cursor.execute("CREATE INDEX IF NOT EXISTS idx_scud_fio ON scud_logs(fio_clean);")
|
||||||
cursor.execute("CREATE INDEX IF NOT EXISTS idx_mapping_scud ON person_identity_mapping(scud_fio);")
|
cursor.execute("CREATE INDEX IF NOT EXISTS idx_mapping_scud ON person_identity_mapping(scud_fio);")
|
||||||
|
cursor.execute("CREATE INDEX IF NOT EXISTS idx_raw_events_date_hoz ON scud_events_raw(log_date, hoz_organ);")
|
||||||
|
cursor.execute("CREATE INDEX IF NOT EXISTS idx_raw_events_date_time ON scud_events_raw(log_date, time_val);")
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,6 @@
|
|||||||
|
reason
|
||||||
|
отгул
|
||||||
|
дежурство
|
||||||
|
обучение
|
||||||
|
экзамен в Ростехнадзоре
|
||||||
|
выходной день по ТД
|
||||||
|
@@ -1,16 +1,13 @@
|
|||||||
fio,reason,note
|
fio,department,reason,date_from,date_to
|
||||||
Королёва Наталья Александровна,Удаленная работа,Постоянная удаленка
|
Королёва Наталья Александровна,Все,Удаленная работа,,
|
||||||
Николаева Ирина Леонидовна,Удаленная работа,Постоянная удаленка
|
Николаева Ирина Леонидовна,Все,Удаленная работа,,
|
||||||
Познякова Татьяна Сергеевна,Удаленная работа,Постоянная удаленка
|
Познякова Татьяна Сергеевна,Все,Удаленная работа,,
|
||||||
Софьин Никита Сергеевич,Удаленная работа,Постоянная удаленка
|
Чуб Александр Васильевич,Все,Удаленная работа,,
|
||||||
Чуб Александр Васильевич,Удаленная работа,Постоянная удаленка
|
Шуличенко Иван Иванович,Все,Удаленная работа,,
|
||||||
Шуличенко Иван Иванович,Удаленная работа,Постоянная удаленка
|
Пухаренко Юрий Владимирович,Все,Удаленная работа,,
|
||||||
Пухаренко Юрий Владимирович,Удаленная работа,Постоянная удаленка
|
Пшеничный Виктор Петрович,Все,Удаленная работа,,
|
||||||
Пшеничный Виктор Петрович,Удаленная работа,Постоянная удаленка
|
Незнанова Валерия Игоревна,Все,Удаленная работа,,
|
||||||
Незнанова Валерия Игоревна,Удаленная работа,Постоянная удаленка
|
Ковалев Владимир Владимирович,Все,Удаленная работа,,
|
||||||
Ковалев Владимир Владимирович,Удаленная работа,Постоянная удаленка
|
Кожокарь Татьяна Юрьевна,Все,Удаленная работа,,
|
||||||
Кожокарь Татьяна Юрьевна,Удаленная работа,Постоянная удаленка
|
Субетто Юлия Викторовна,Все,Удаленная работа,,
|
||||||
Субетто Юлия Викторовна,Удаленная работа,Постоянная удаленка
|
Чуркина Елена Геннадьевна,Все,Удаленная работа,,
|
||||||
Чуркина Елена Геннадьевна,Удаленная работа,Постоянная удаленка
|
|
||||||
Пушков Александр Александрович,Удаленная работа,Временная удаленка с 26.08.2026 по 04.09.2026
|
|
||||||
Сысоев Алексей Валерьевич,Удаленная работа,Временная удаленка с 25.08.2026 по 27.08.2026
|
|
||||||
|
|||||||
|
+140
-2
@@ -1,6 +1,144 @@
|
|||||||
# 📋 История изменений (CHANGELOG)
|
# Changelog
|
||||||
|
|
||||||
Все ключевые изменения архитектуры, инструментов и модулей проекта SCUD Orion AI фиксируются в данном файле.
|
Все важные изменения проекта документируются в этом файле.
|
||||||
|
|
||||||
|
## [Unreleased]
|
||||||
|
|
||||||
|
### ✨ Добавлено (Added)
|
||||||
|
- **Изолированный домен работы с документами (`services/office/`):**
|
||||||
|
- Создан специализированный пакет для постраничной обработки многостраничных PDF и сканов без ограничений контекста диалога.
|
||||||
|
- Автоматическая сборка распознанного текста в файл Microsoft Word (`.docx`) со стандартизированным оформлением.
|
||||||
|
- Интеграция с роутером чата и выдача контрастной карточки скачивания документа.
|
||||||
|
|
||||||
|
### 📝 Запланировано (Planned)
|
||||||
|
- **Двухфазный OCR-конвейер рукописных виз и резолюций (`services/office/`):**
|
||||||
|
- Рендеринг сканов в высоком разрешении (`250–300 DPI`) для точного захвата линий чернил.
|
||||||
|
- Фаза 2 (Refiner на `qwen2.5:14b`): автоматическое устранение артефактов OCR, склейка абзацев и преобразование списков в таблицы Word.
|
||||||
|
- Распознавание рукописных виз «Согласовано», подписей и дат руководства с выделением в блок `[Резолюция: ...]`.
|
||||||
|
|
||||||
|
## [3.4.0] — 2026-09-25
|
||||||
|
|
||||||
|
### ✨ Добавлено (Added)
|
||||||
|
- **Оперативная On-Demand генерация отчетов из сайдбара:**
|
||||||
|
- Мгновенная сборка книги «Ежедневная сводка» по последнему зафиксированному в SQLite срезу СКУД (без ожидания внешнего опроса Орион).
|
||||||
|
- Строгое разграничение временных рамок отчетов: «Сводка» формируется на текущую смену, а «Детальный» и «Упрощенный» отчеты — строго за прошедшую смену (ВЧЕРА / прошлая пятница) по итоговому срезу `Y`.
|
||||||
|
- **Фоновая полудневная синхронизация 1С (cron):**
|
||||||
|
- Интеграция вызова `copy_1c_files_from_share()` и опроса кадровых отсутствий из базы ЗУП (`ACCOUNT-01`) в цикл получасовых срезов.
|
||||||
|
|
||||||
|
### 🛡️ Исправлено (Fixed)
|
||||||
|
- **Ошибка 404 при скачивании отчетов (`download_report_direct`):**
|
||||||
|
- Добавлен рекурсивный поиск файлов в подпапках `output/reports/{ГОД}/{МЕСЯЦ}/` и декодирование процент-кодированных кириллических имен из URL.
|
||||||
|
- **Ошибки 500 и 0 по штату 1С при создании сводки:**
|
||||||
|
- Реализован каскадный fallback: если файл штата на текущий день еще не выложен на шару, данные штата берутся из последнего сохраненного среза `zup_staff` в SQLite (устранено ложное попадание всех сотрудников в «Нет в ЗУП»).
|
||||||
|
- Исправлены `NameError` импортов (`load_best_snapshot_for_date`, `load_1c_data_smart`).
|
||||||
|
- **Сбои инспекции срезов (`OperationalError: no such column: hoz_organ`):**
|
||||||
|
- Переход на динамический `SELECT *` в роутере инспекции срезов и расчет реального времени в здании до момента фиксации снапшота.
|
||||||
|
|
||||||
|
## [3.3.0] — 2026-09-24
|
||||||
|
|
||||||
|
### ✨ Добавлено (Added)
|
||||||
|
- **Генератор «Упрощенного отчета» (`services/reports/simplified_builder.py`):**
|
||||||
|
- Формирование суточного файла `Упрощенный отчет за ДД.ММ.ГГГГг..xlsx` с выгрузкой на сетевую шару параллельно с детальным отчетом.
|
||||||
|
- Сокращение полных ФИО до эталонного формата с инициалами (`Иванов И.И.`).
|
||||||
|
- Подстановка официальных текстовых заглушек СКУД `Нет входа (0:00)` и `Нет выхода (23:59)` при отсутствии отметок.
|
||||||
|
- Алфавитная сортировка по подразделениям и сотрудникам, форматирование сетки `Calibri 11` с числовым форматом времени `h:mm`.
|
||||||
|
- **Разделы сводки «Не приняты на работу» и «Нет пропуска»:**
|
||||||
|
- В `services/scud_etl/merger.py` и `services/reports/svodka_builder.py` выделена категория физлиц, присутствующих в СКУД, но еще не оформленных в 1С:ЗУП (`not_hired_yet`).
|
||||||
|
- Сотрудники штата без карт доступа теперь явно направляются в блок «Нет пропуска» (`no_scud_pass`).
|
||||||
|
|
||||||
|
### 🔧 Изменено (Changed)
|
||||||
|
- **Исключение не принятых на работу из табеля:**
|
||||||
|
- В `services/reports/otchet_builder.py` сотрудники без проведенного приказа о приеме в 1С исключены из таблицы детального суточного отчета за вчера.
|
||||||
|
- **Восстановление аббревиатур подразделений:**
|
||||||
|
- В `merger.py` восстановлен приоритет компактных названий отделов из базы СКУД Орион (`ОАН`, `ПУ`, `ОИЗ`, `ОА`, `ИГТЛЛ` и др.) взамен длинных кадровых формулировок 1С.
|
||||||
|
- **Инфраструктура нового сервера (Debian 12):**
|
||||||
|
- Исправлены пути виртуального окружения `/home/apushkov/projects/scud_ai/venv/bin/python` в cron-скриптах.
|
||||||
|
- Настроено автоматическое CIFS-монтирование шары отчетов в `/etc/fstab` с правами пользователя `apushkov`.
|
||||||
|
|
||||||
|
### 🛡️ Исправлено (Fixed)
|
||||||
|
- **Сбой генерации детального отчета (`TypeError: NAN/INF not supported`):**
|
||||||
|
- В `otchet_builder.py` включен параметр книги `nan_inf_to_errors: True` и внедрена санитизация пустых ячеек `NaN`/`None`, исключающая аварийное завершение `write_number()`.
|
||||||
|
- **Предупреждения Pandas в логах:**
|
||||||
|
- Устранены ошибки `Boolean Series key will be reindexed to match DataFrame index` в `merger.py` за счет изоляции расчета масок от `df_staff_only`.
|
||||||
|
|
||||||
|
## [2.5.8] - 2026-09-10
|
||||||
|
|
||||||
|
### Добавлено (Added)
|
||||||
|
- **Двухконтурная физическая модель СКУД (Правый PERCo):**
|
||||||
|
- Поддержка проходов через оба турникета (`DoorIndex IN (1, 2)`) для сотрудников дворовых служб (отделы `ЭТО`, `ЛЦ` и др.).
|
||||||
|
- Реестр доступа к правому турникету в `exceptions_registry` с категориями `turnstile_fio` и `turnstile_departments`.
|
||||||
|
- Корректная склейка ФИО (`Name + FirstName + MidName`) в динамическом SQL-фильтре MS SQL.
|
||||||
|
- **Интерактивное модальное окно исключений:**
|
||||||
|
- Замена браузерного `prompt()` на модальную форму в едином Tailwind-стиле с автоподбором сотрудников из 1С:ЗУП (`staff-autocomplete`).
|
||||||
|
- Поле примечания/комментария для фиксации оснований допуска.
|
||||||
|
- **Точное локальное время создания срезов:**
|
||||||
|
- Явная фиксация времени хоста при записи срезов в `scud_logs.created_at`, устраняющая 3-часовое смещение UTC внутри контейнеров.
|
||||||
|
- **Декларативное подтверждение удаления срезов:**
|
||||||
|
- Добавление параметра `confirmed: boolean` в схему `TOOLS_SCHEMA` инструмента `db_delete_snapshots`, исключающее зацикливание подтверждений в LLM.
|
||||||
|
|
||||||
|
### Изменено (Changed)
|
||||||
|
- **Упразднение искусственного автозакрытия смен:**
|
||||||
|
- Полное отключение механизма `calculate_autoclose_time` по согласованию с отделом кадров. При отсутствии физической отметки на турникете статус «Нет выхода» сохраняется без искажения аналитики.
|
||||||
|
- **Математический расчет временных границ срезов:**
|
||||||
|
- Перевод формирования `@EndDate` в `services/scud_export.py` на цепочку функций `DATEADD` от `@StartDate`, устраняющий зависимость от регионального формата даты (`DMY` vs `MDY`).
|
||||||
|
|
||||||
|
## [Unreleased] - 2026-09-07
|
||||||
|
|
||||||
|
### Добавлено
|
||||||
|
- Механизм Gemini-скроллинга диалогового окна: функция `scrollToUserMessageTop` в `core.js` автоматически поднимает отправленный вопрос к верхнему срезу экрана, скрывая громоздкие предыдущие ответы.
|
||||||
|
- CSS-отступ `padding-bottom: 80vh` у `#chat-messages-container`, гарантирующий свободное вертикальное пространство для прокрутки без сдвига строки ввода.
|
||||||
|
- Пункт 2.9 системного промпта, жестко обязывающий модель вызывать нативный инструмент `db_get_rules` при запросах Базы Знаний и регламентов вместо текстовой имитации.
|
||||||
|
|
||||||
|
### Запланировано
|
||||||
|
- Рефакторинг интерпретации аппаратных кодов турникетов PERCO в `services/scud_export.py`: разделение логики разрешения прохода (`Event 28`) и факта проворота створки (`Event 32`), приоритет направления по `log.Mode` (1 — вход, 2 — выход) для устранения фантомных выходов.
|
||||||
|
- Создание DDL-схемы и таблицы `scud_events_raw` в SQLite для гранулярного хранения всех внутридневных перемещений сотрудников (база для учета перекуров и инструмента «Кто в здании»).
|
||||||
|
|
||||||
|
## [0.9.5] - 2026-08-28
|
||||||
|
|
||||||
|
### Добавлено
|
||||||
|
- Механизм отказоустойчивого сохранения отчетов `safe_close_workbook` в `services/excel_exporter.py`: при блокировке файла открытым процессом Excel создается резервный файл с таймстемпом вида `..._timestamp.xlsx`.
|
||||||
|
|
||||||
|
### Изменено
|
||||||
|
- Экспорт всех Excel-документов (`services/scud_export.py`, `services/excel_exporter.py`) полностью переведен с `openpyxl` на `xlsxwriter`.
|
||||||
|
- Скорректирована верстка детального отчета: столбец «Подразделение» выровнен строго по центру, колонка «ФИО» расширена на 10% (ширина 33), а избыточная ширина столбцов «Первая активность» и «Отклонение от нормы» уменьшена до 11 пунктов для корректного размещения на экране без горизонтальной прокрутки.
|
||||||
|
- В модуле сетевой синхронизации `services/share_copier.py` вызов `shutil.copy2` заменен на `shutil.copyfile`, что устранило ошибки метаданных `[Errno 1] Operation not permitted` при монтировании SMB-шары.
|
||||||
|
|
||||||
|
### Исправлено
|
||||||
|
- Устранена системная ошибка структуры OpenXML (`errorXXXXXX_01.xml`, «Обнаружена ошибка в части содержимого книги...»), возникавшая при открытии сгенерированных отчетов в MS Excel.
|
||||||
|
- Восстановлены сворачиваемые интерактивные категории с кнопками `[+]` / `[-]` в файле ежедневной сводки (`outline_settings(symbols_below=False)`).
|
||||||
|
|
||||||
|
## [Unreleased] - 2026-08-28
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- **Генерация Excel-книг (`services/excel_exporter.py`, `services/scud_export.py`):** полный перевод экспорта с `openpyxl` на `xlsxwriter`, что устранило хроническую ошибку структуры OpenXML («В книге обнаружено нечитаемое содержимое...»).
|
||||||
|
- **Группировки в Сводке:** восстановлено корректное отображение интерактивных кнопок `[+]`/`[-]` и сворачивание категорий по умолчанию (`outline_settings`, `level=1`, `collapsed=True`).
|
||||||
|
- **Верстка и пропорции Детального отчета:** центрирование столбца «Подразделение», калибровка ширины колонок «ФИО» (+10%), «Первая активность» и «Отклонение от нормы» для плотного размещения таблицы на одном экране без горизонтальной прокрутки.
|
||||||
|
- **Безопасное сохранение при блокировках:** внедрена функция `safe_close_workbook` для фонового сохранения резервных копий с timestamp, если файл открыт пользователем в Excel.
|
||||||
|
- **Сетевое копирование 1С (`services/share_copier.py`):** замена `shutil.copy2` на `shutil.copyfile` для устранения сбоя `[Errno 1] Operation not permitted` при работе с SMB/CIFS-шарой.
|
||||||
|
|
||||||
|
## [Unreleased] - 2026-08-28
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- **Инкремент Snapshot ID (`core/repositories/scud_repo.py`):** заменена строковая сортировка `ORDER BY snapshot_id DESC` на целочисленный парсинг суффиксов, что устранило залипание номеров снапшотов (`-002`).
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- **Режим тихих почасовых срезов (`--export-only`):** быстрый экспорт данных СКУД напрямую в SQLite без построения отчетов для интеграции в cron.
|
||||||
|
- **Интерактивный справочник CLI (`main_etl.py`):** полный баннер справки по всем аргументам (`-h`, `--help`, `help`).
|
||||||
|
- **Автоматическая ротация промежуточных срезов (`services/snapshots/retention.py`):** ночная очистка устаревших почасовых срезов старше 2 дней с сохранением полуденного (`13:00`) и итогового `Y` (`23:59:59`).
|
||||||
|
|
||||||
|
## [Unreleased] - 2026-08-28
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- **Smart Snap-to-Grid Finder (`services/snapshots/finder.py`):** интеллектуальный поиск ближайшего среза СКУД в SQLite с допуском ±20 минут и поддержкой On-Demand экспорта из MS SQL при отсутствии готового среза.
|
||||||
|
- **Параметр `--time` в `main_etl.py`:** возможность формирования оперативной сводки на произвольное время суток.
|
||||||
|
- **Раздельные генераторы отчетов:**
|
||||||
|
- `services/scud_etl/svodka_generator.py` — оперативная сводка за сегодня с группировками.
|
||||||
|
- `services/scud_etl/otchet_generator.py` — суточный детальный отчет за вчера по финишному срезу `Y` (23:59:59) с учетом рабочего норматива 8.5 ч.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- **Фиксация финишного среза `Y` на `23:59:59`:** обновлено время снапшота в `services/scud_export.py`, `core/repositories/scud_repo.py` и `services/scud_etl/pipeline.py` с сохранением обратной совместимости со срезами на `22:00:00`.
|
||||||
|
- **Путь виртуального окружения в `run_cron_etl.sh`:** скорректирован на актуальный `/home/puh/projects/scud_ai/venv/bin/python`.
|
||||||
|
- **Игнорирование временных файлов SQLite:** добавлены `*.db-shm` и `*.db-wal` в `.gitignore`.
|
||||||
|
|
||||||
## [3.2.0] — 2026-08-27
|
## [3.2.0] — 2026-08-27
|
||||||
### 🧠 Интеллектуальный арбитраж личностей, агрегация мульти-пропусков и автозакрытие смен
|
### 🧠 Интеллектуальный арбитраж личностей, агрегация мульти-пропусков и автозакрытие смен
|
||||||
|
|||||||
+189
-88
@@ -1,147 +1,248 @@
|
|||||||
# План реализации (ROADMAP)
|
# План реализации (ROADMAP)
|
||||||
|
|
||||||
## 1. Очистка от регулярок и костылей (`tool_injector.py`) `[ЗАВЕРШЕНО]`
|
## 1. Очистка от регулярок и костылей (`tool_injector.py`) `[ЗАВЕРШЕНО]`
|
||||||
- [x] Полностью удалить принудительные перехваты текста регулярными выражениями для команд добавления, редактирования и удаления пунктов[cite: 3].
|
- [x] Полностью удалить принудительные перехваты текста регулярными выражениями для команд добавления, редактирования и удаления пунктов.
|
||||||
- [x] Оставить в модуле только базовую санитарную очистку сырых тегов (`<tool_call>`)[cite: 3].
|
- [x] Оставить в модуле только базовую санитарную очистку сырых тегов (`<tool_call>`).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 2. Настройка контекста и инструкций сессии (`agent.py`) `[ЗАВЕРШЕНО]`
|
## 2. Настройка контекста и инструкций сессии (`agent.py`) `[ЗАВЕРШЕНО]`
|
||||||
- [x] Передать управление диалогом языковой модели через системный блок `role: "system"`[cite: 3].
|
- [x] Передать управление диалогом языковой модели через системный блок `role: "system"`.
|
||||||
- [x] При активном состоянии `PROMPT_PREVIEW` передавать модели инструкцию:
|
- [x] При активном состоянии `PROMPT_PREVIEW` передавать модели инструкцию[cite: 2]:
|
||||||
- **Подтверждение / отмена / корректировка:** продолжать работу с превью и вызывать соответствующие инструменты[cite: 3].
|
- **Подтверждение / отмена / корректировка:** продолжать работу с превью и вызывать соответствующие инструменты[cite: 2].
|
||||||
- **Смена темы:** вежливо напомнить об открытом изменении и запросить решение[cite: 3].
|
- **Смена темы:** вежливо напомнить об открытом изменении и запросить решение[cite: 2].
|
||||||
- [x] Обеспечить видимость эфемерных сообщений (`is_ephemeral = 1`) для модели во время активной работы с превью[cite: 3].
|
- [x] Обеспечить видимость эфемерных сообщений (`is_ephemeral = 1`) для модели во время активной работы с превью[cite: 2].
|
||||||
- [x] Внедрить семантический Topic Drift Guard (`idle_turns` = 3) с вопросами и кнопками из `tool_action_registry`[cite: 3].
|
- [x] Внедрить семантический Topic Drift Guard (`idle_turns` = 3) с вопросами и кнопками из `tool_action_registry`[cite: 2].
|
||||||
- [x] Реализовать детерминированный мгновенный сброс сессии и зачистку эфемерного контекста при вызове сторонних инструментов[cite: 3].
|
- [x] Реализовать детерминированный мгновенный сброс сессии и зачистку эфемерного контекста при вызове сторонних инструментов[cite: 2].
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 3. Очистка эфемерных сообщений при завершении (`fast_path.py` / `context_manager.py`) `[ЗАВЕРШЕНО]`
|
## 3. Очистка эфемерных сообщений при завершении (`fast_path.py` / `context_manager.py`) `[ЗАВЕРШЕНО]`
|
||||||
- [x] Настроить удаление временных сообщений превью (`db_purge_ephemeral_messages`) строго в момент нажатия кнопок **«Подтвердить»** или **«Отменить»**[cite: 3].
|
- [x] Настроить удаление временных сообщений превью (`db_purge_ephemeral_messages`) строго в момент нажатия кнопок **«Подтвердить»** или **«Отменить»**[cite: 2].
|
||||||
- [x] Сбрасывать состояние сессии в базе данных после фиксации решения[cite: 3].
|
- [x] Сбрасывать состояние сессии в базе данных после фиксации решения[cite: 2].
|
||||||
- [x] Внедрить прямое точечное применение изменений через `db_apply_prompt_node_action`[cite: 3].
|
- [x] Внедрить прямое точечное применение изменений через `db_apply_prompt_node_action`[cite: 2].
|
||||||
- [x] Добавить обработку фазы `PROMPT_FOLLOWUP` с кнопками завершения и очистки контекста[cite: 3].
|
- [x] Добавить обработку фазы `PROMPT_FOLLOWUP` с кнопками завершения и очистки контекста[cite: 2].
|
||||||
- [x] Создать `context_manager.py` для строгого разграничения служебных tool-пар (`is_ephemeral=1`) и содержательного диалога (`is_ephemeral=0`), сохраняя беседы Topic Drift в истории[cite: 3].
|
- [x] Создать `context_manager.py` для строгого разграничения служебных tool-пар (`is_ephemeral=1`) и содержательного диалога (`is_ephemeral=0`), сохраняя беседы Topic Drift в истории[cite: 2].
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 4. Инлайн-редактор в окне диалога (`core.js`) `[ЗАВЕРШЕНО]`
|
## 4. Инлайн-редактор в окне диалога (`core.js`) `[ЗАВЕРШЕНО]`
|
||||||
- [x] Проверить работу блока ручного редактирования (`inline-prompt-editor-container`) с кнопками **«Сохранить правки»** и **«Свернуть»**[cite: 3].
|
- [x] Проверить работу блока ручного редактирования (`inline-prompt-editor-container`) с кнопками **«Сохранить правки»** и **«Свернуть»**[cite: 2].
|
||||||
- [x] Обеспечить сохранение черновика через API (`/api/v1/chat/draft`) и отображение обновленного текста перед подтверждением[cite: 3].
|
- [x] Обеспечить сохранение черновика через API (`/api/v1/chat/draft`) и отображение обновленного текста перед подтверждением[cite: 2].
|
||||||
- [x] Реализовать двусторонний клиентский Diff-рендерер (одновременная подсветка добавленных строк и зачеркивание удаленных `[УДАЛЕНИЕ]`)[cite: 3].
|
- [x] Реализовать двусторонний клиентский Diff-рендерер (одновременная подсветка добавленных строк и зачеркивание удаленных `[УДАЛЕНИЕ]`)[cite: 2].
|
||||||
- [x] Добавить авто-форматирование и отступы подпунктов (`X.Y.`) при ручном сохранении черновика[cite: 3].
|
- [x] Добавить авто-форматирование и отступы подпунктов (`X.Y.`) при ручном сохранении черновика[cite: 2].
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 5. Тестирование и валидация системного промпта `[ЗАВЕРШЕНО]`
|
## 5. Тестирование и валидация системного промпта `[ЗАВЕРШЕНО]`
|
||||||
- [x] **Нативные вызовы:** проверить добавление, редактирование и удаление пунктов через нативные вызовы модели (`db_prompt_node_edit`)[cite: 3].
|
- [x] **Нативные вызовы:** проверить добавление, редактирование и удаление пунктов через нативные вызовы модели (`db_prompt_node_edit`)[cite: 2].
|
||||||
- [x] **Контекстные сценарии:** проверить поведение модели при смене темы диалога оператором (Guardrail)[cite: 3].
|
- [x] **Контекстные сценарии:** проверить поведение модели при смене темы диалога оператором (Guardrail)[cite: 2].
|
||||||
- [x] **UI и очистка:** проверить ручное редактирование через кнопку в окне чата и последующую очистку контекста (`db_purge_ephemeral_messages`)[cite: 3].
|
- [x] **UI и очистка:** проверить ручное редактирование через кнопку в окне чата и последующую очистку контекста (`db_purge_ephemeral_messages`)[cite: 2].
|
||||||
|
- [x] **Строгий вызов Базы Знаний:** внедрено правило 2.9 в системный промпт для пресечения текстовой имитации вызова `db_get_rules`[cite: 2].
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 6. Распространение архитектурного паттерна на модуль задач (`tasks`) `[ЗАВЕРШЕНО]`
|
## 6. Распространение архитектурного паттерна на модуль задач (`tasks`) `[ЗАВЕРШЕНО]`
|
||||||
- [x] **Масштабирование UI задач:**
|
- [x] **Масштабирование UI задач:**
|
||||||
- Увеличена ширина карточки до `max-w-4xl` и динамическая высота скролла до `70vh`[cite: 3].
|
- Увеличена ширина карточки до `max-w-4xl` и динамическая высота скролла до `70vh`[cite: 2].
|
||||||
- Реализованы переключатели фильтрации: «В работе» (по умолчанию), «В планах», «Готово», «Все»[cite: 3].
|
- Реализованы переключатели фильтрации: «В работе» (по умолчанию), «В планах», «Готово», «Все»[cite: 2].
|
||||||
- Разделены кнопки действия: «В работу» (для плановых) и «Готово» (для текущих)[cite: 3].
|
- Разделены кнопки действия: «В работу» (для плановых) и «Готово» (для текущих)[cite: 2].
|
||||||
- [x] **Инлайн-редактирование карточки задачи:**
|
- [x] **Инлайн-редактирование карточки задачи:**
|
||||||
- Плавная трансформация карточки в 3-строчную форму (текст, дата со встроенным пикером, выпадающий список важности)[cite: 3].
|
- Плавная трансформация карточки в 3-строчную форму (текст, дата со встроенным пикером, выпадающий список важности)[cite: 2].
|
||||||
- Отображение даты создания задачи[cite: 3].
|
- Отображение даты создания задачи[cite: 2].
|
||||||
- Сохранение через REST API эндпоинт `PATCH /api/v1/tasks/{id}`[cite: 3].
|
- Сохранение через REST API эндпоинт `PATCH /api/v1/tasks/{id}`[cite: 2].
|
||||||
- [x] **Детерминированный Fast-Path и двухфазное удаление:**
|
- [x] **Детерминированный Fast-Path и двухфазное удаление:**
|
||||||
- Мгновенная смена статусов без задержек LLM[cite: 3].
|
- Мгновенная смена статусов без задержек LLM[cite: 2].
|
||||||
- Карточка подтверждения удаления с автоочисткой контекста[cite: 3].
|
- Карточка подтверждения удаления с автоочисткой контекста[cite: 2].
|
||||||
- [x] **Генерация отчетов задач в Markdown:**
|
- [x] **Генерация отчетов задач в Markdown:**
|
||||||
- Добавлен инструмент `db_export_tasks_markdown` для формирования Markdown-файла с группировкой по модулям и чекбоксами `[x]` / `[ ]`[cite: 3].
|
- Добавлен инструмент `db_export_tasks_markdown` для формирования Markdown-файла с группировкой по модулям и чекбоксами `[x]` / `[ ]`[cite: 2].
|
||||||
- Реализован роутер безопасной отдачи файлов с сохранением имени (`/api/v1/files/download/...`)[cite: 3].
|
- Реализован роутер безопасной отдачи файлов с сохранением имени (`/api/v1/files/download/...`)[cite: 2].
|
||||||
- [x] **Доменная консолидация задач:**
|
- [x] **Доменная консолидация задач:**
|
||||||
- Реализован консолидированный инструмент `db_tasks_edit(action: ["ADD", "UPDATE", "DELETE", "EXPORT"], ...)`[cite: 3].
|
- Реализован консолидированный инструмент `db_tasks_edit(action: ["ADD", "UPDATE", "DELETE", "EXPORT"], ...)`[cite: 2].
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 7. Распространение на модуль снапшотов (`snapshots`) `[ЗАВЕРШЕНО]`
|
## 7. Распространение на модуль снапшотов (`snapshots`) `[ЗАВЕРШЕНО]`
|
||||||
- [x] **Поддержка Y-снапшотов:** универсальный парсинг и поиск по `snapshot_id LIKE 'Y%'` и срезам за 22:00:00[cite: 3].
|
- [x] **Поддержка Y-снапшотов:** универсальный парсинг и поиск по `snapshot_id LIKE 'Y%'` и срезам за 22:00:00 / 23:59:59[cite: 2].
|
||||||
- [x] **Single Active Snapshot View:** сохранение активного среза в контексте для аналитики с автоматической ротацией и зачисткой при запросе новых дат[cite: 3].
|
- [x] **Single Active Snapshot View:** сохранение активного среза в контексте для аналитики с автоматической ротацией и зачисткой при запросе новых дат[cite: 2].
|
||||||
- [x] **Интерактивный UI с чекбоксами и защитой срезов:**
|
- [x] **Интерактивный UI с чекбоксами и защитой срезов:**
|
||||||
- Чекбоксы в строках дневных срезов и кнопка «Выбрать все» в шапке[cite: 3].
|
- Чекбоксы в строках дневных срезов и кнопка «Выбрать все» в шапке[cite: 2].
|
||||||
- Защита итогового вечернего среза Y (иконка замочка `🔒`, блокировка выбора)[cite: 3].
|
- Защита итогового вечернего среза Y (иконка замочка `🔒`, блокировка выбора)[cite: 2].
|
||||||
- Динамическая кнопка «Удалить выбранные (N)» в подвале карточки[cite: 3].
|
- Динамическая кнопка «Удалить выбранные (N)» в подвале карточки[cite: 2].
|
||||||
- [x] **Детерминированный Fast-Path удаления срезов:**
|
- [x] **Декларативное управление удалением срезов:**
|
||||||
- Двухфазное подтверждение удаления (одиночное и пакетное) с корректным счётчиком количества удаляемых элементов[cite: 3].
|
- Добавлен параметр `confirmed` в схему `TOOLS_SCHEMA` для `db_delete_snapshots`, исключающий зацикливание подтверждений в LLM.
|
||||||
- Защита от сброса фильтра даты (`query_date`) при обновлении карточки после удаления[cite: 3].
|
- Двухфазное подтверждение удаления (одиночное и пакетное) с корректным счётчиком количества удаляемых элементов[cite: 2].
|
||||||
|
- Корректное отображение времени создания срезов в интерфейсе (устранение смещения UTC относительно локального времени сервера).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 8. Глубокий архитектурный рефакторинг ядра и сервисов `[ЗАВЕРШЕНО]`
|
## 8. Глубокий архитектурный рефакторинг ядра и сервисов `[ЗАВЕРШЕНО]`
|
||||||
- [x] **Выделение общего слоя ядра (`core/`):**
|
- [x] **Выделение общего слоя ядра (`core/`):**
|
||||||
- Изолированный пул соединений SQLite (`core/connection.py`) с поддержкой WAL и таймаутов.
|
- Изолированный пул соединений SQLite (`core/connection.py`) с поддержкой WAL и таймаутов[cite: 2].
|
||||||
- DDL-схемы таблиц и индексов (`core/schema.py`).
|
- DDL-схемы таблиц и индексов (`core/schema.py`)[cite: 2].
|
||||||
- Репозитории СКУД и 1С (`core/repositories/scud_repo.py`, `core/repositories/zup_repo.py`).
|
- Репозитории СКУД и 1С (`core/repositories/scud_repo.py`, `core/repositories/zup_repo.py`)[cite: 2].
|
||||||
- Фасад обратной совместимости (`core/database.py`).
|
- Фасад обратной совместимости (`core/database.py`)[cite: 2].
|
||||||
- [x] **Декомпозиция предметных доменов (`services/`):**
|
- [x] **Декомпозиция предметных доменов (`services/`):**
|
||||||
- Сервис задач (`services/tasks/`), системного промпта (`services/prompts/`), срезов СКУД (`services/snapshots/`), базы знаний (`services/knowledge/`).
|
- Сервис задач (`services/tasks/`), системного промпта (`services/prompts/`), срезов СКУД (`services/snapshots/`), базы знаний (`services/knowledge/`)[cite: 2].
|
||||||
- [x] **Рефакторинг ETL-конвейера (`services/scud_etl/`):**
|
- [x] **Рефакторинг ETL-конвейера (`services/scud_etl/`):**
|
||||||
- Оркестратор `pipeline.py`, детектор аномалий `anomaly_detector.py`, слияние `merger.py`.
|
- Оркестратор `pipeline.py`, детектор аномалий `anomaly_detector.py`, слияние `merger.py`[cite: 2].
|
||||||
- [x] **Модульный ИИ-оркестратор (`modules/ai_engine/`):**
|
- [x] **Модульный ИИ-оркестратор (`modules/ai_engine/`):**
|
||||||
- Динамический строитель системного контекста (`context_builder.py`).
|
- Динамический строитель системного контекста (`context_builder.py`)[cite: 2].
|
||||||
- Изолированные обработчики инструментов в `handlers/`.
|
- Изолированные обработчики инструментов в `handlers/`[cite: 2].
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 9. Реляционный реестр исключений, схлопывание мульти-пропусков и автозакрытие 8.5ч `[ЗАВЕРШЕНО]`
|
## 9. Реляционный реестр исключений, схлопывание мульти-пропусков и кадровый аудит `[ЗАВЕРШЕНО]`
|
||||||
- [x] **Реестр исключений в SQLite (`exceptions_registry`):**
|
- [x] **Реестр исключений в SQLite (`exceptions_registry`):**
|
||||||
- Хранение категорий `departments`, `positions`, `fio`, `position_keywords`, `include_fio` в БД.
|
- Хранение категорий `departments`, `positions`, `fio`, `position_keywords`, `include_fio` в БД[cite: 2].
|
||||||
- Доменный сервис `exceptions_repo.py` и полная поддержка в CLI `scripts/db_cli.py exceptions`.
|
- Доменный сервис `exceptions_repo.py` и полная поддержка в CLI `scripts/db_cli.py exceptions`[cite: 2].
|
||||||
- [x] **Агрегация мульти-пропусков физлиц в СКУД:**
|
- [x] **Агрегация мульти-пропусков физлиц в СКУД:**
|
||||||
- Функция `aggregate_scud_by_person` в `merger.py`: объединение событий всех пропусков одного человека (ранний вход, поздний выход, присутствие).
|
- Функция `aggregate_scud_by_person` в `merger.py`: объединение событий всех пропусков одного человека (ранний вход, поздний выход, присутствие)[cite: 2].
|
||||||
- Автоматическая фиксация аномалий дублирования пропусков (`DUPLICATE_SCUD_CARD`).
|
- Автоматическая фиксация аномалий дублирования пропусков (`DUPLICATE_SCUD_CARD`)[cite: 2].
|
||||||
- [x] **Умный выбор ставки совместителей 1С:ЗУП:**
|
- [x] **Умный выбор ставки совместителей 1С:ЗУП:**
|
||||||
- Функция `select_best_zup_position` в `merger.py`: привязка ставки 1С по подразделению физического нахождения в СКУД.
|
- Функция `select_best_zup_position` в `merger.py`: привязка ставки 1С по подразделению физического нахождения в СКУД[cite: 2].
|
||||||
- [x] **Автозакрытие смен по Правилу 8.5ч:**
|
- [x] **Отказ от искусственного автозакрытия смен:**
|
||||||
- Для забывших отметиться на выходе офисных сотрудников: автоматический расчет `Вход + 8ч 30мин`, норма 8 часов и отклонение `0:00` в `excel_exporter.py`.
|
- Полное отключение механизма дорисовывания 8.5 часов (`calculate_autoclose_time`) по согласованию с отделом кадров.
|
||||||
|
- Сохранение честного статуса «Нет выхода» для прозрачности кадрового аудита и выявления нарушений.
|
||||||
- [x] **Кэш сопоставлений личностей (`person_identity_mapping`):**
|
- [x] **Кэш сопоставлений личностей (`person_identity_mapping`):**
|
||||||
- Таблица в SQLite и команды `db_cli.py mapping [list|add|del]`.
|
- Таблица в SQLite и команды `db_cli.py mapping [list|add|del]`[cite: 2].
|
||||||
- Теневой режим ИИ-подсказок по нечетким ФИО в `text_reporter.py`.
|
- Теневой режим ИИ-подсказок по нечетким ФИО в `text_reporter.py`[cite: 2].
|
||||||
- [x] **Каскадный fallback кадровых отсутствий:**
|
- [x] **Каскадный fallback кадровых отсутствий:**
|
||||||
- `MS SQL ЗУП` $\rightarrow$ резервный парсинг `Отсутствия_*.xlsx` $\rightarrow$ `static_reason_workers.csv`.
|
- `MS SQL ЗУП` $\rightarrow$ резервный парсинг `Отсутствия_*.xlsx` $\rightarrow$ `static_reason_workers.csv`[cite: 2].
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 10. Разграничение Сводки и Отчета + Почасовые срезы и Y-23:59:59 `[В РАБОТЕ]`
|
## 10. Разделение генераторов и почасовые срезы `[ЗАВЕРШЕНО]`
|
||||||
- [ ] **Корректировка финишного среза Y на 23:59:59:**
|
- [x] Разделение логики генерации на `svodka_generator.py` и `otchet_generator.py`[cite: 2].
|
||||||
- [ ] Обновить время вечернего среза с `22:00:00` на `23:59:59` в `services/scud_export.py`, `scud_repo.py` и `pipeline.py`.
|
- [x] Перевод времени суточного среза `Y` на `23:59:59`[cite: 2].
|
||||||
- [ ] **Разделение генераторов отчетов:**
|
- [x] Интеллектуальный поиск срезов `services/snapshots/finder.py` (Snap-to-Grid ±20 мин)[cite: 2].
|
||||||
- [ ] Создать `services/scud_etl/svodka_generator.py` (оперативная сводка, последний доступный срез или выбранный дневной).
|
- [x] Флаг `--time` и интерактивный help в `main_etl.py`[cite: 2].
|
||||||
- [ ] Создать `services/scud_etl/otchet_generator.py` (детальный отчет за прошлую смену, строго по единственному итоговому `Y`-снапшоту за 23:59:59).
|
- [x] Флаг `--export-only` для почасового крона[cite: 2].
|
||||||
- [ ] Исключить устаревший `report_generator.py`.
|
- [x] Политика ночной ротации промежуточных срезов `services/snapshots/retention.py`[cite: 2].
|
||||||
- [ ] **Параметризация `main_etl.py`:**
|
- [x] Исправление целочисленного инкремента `snapshot_id`[cite: 2].
|
||||||
- [ ] Добавить поддержку аргументов `--date ДД.ММ.ГГГГ` и `--snapshot ID` для свободного запуска расчета за любой прошлый день.
|
|
||||||
- [ ] **Политика почасовых срезов (Hourly Snapshots) и retention-очистка:**
|
|
||||||
- [ ] Реализовать фоновое снятие часовых срезов в течение рабочего дня.
|
|
||||||
- [ ] Настроить ночную очистку промежуточных дневных срезов с сохранением полуденного (13:00) и финишного (`Y` 23:59:59).
|
|
||||||
- [ ] **Толерантный поиск срезов (Smart Snap-to-Grid, ±15–30 мин):**
|
|
||||||
- [ ] Поиск готового снапшота в SQLite при запросе оператора с возможностью мгновенного построения сводки без лишних запросов к MS SQL Орион.
|
|
||||||
- [ ] Формирование On-Demand запроса в MS SQL Орион при отсутствии подходящего среза.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 11. Интеллектуальный кадровый арбитраж ДО генерации отчетов `[В ПЛАНАХ]`
|
## 11. Стабилизация генератора отчетов и верстки `[ЗАВЕРШЕНО]`
|
||||||
- [ ] **Двухконтурный запуск ИИ:**
|
- [x] **Миграция на `XlsxWriter`:** полный перевод экспорта книг (`scud_export.py`, `excel_exporter.py`) на чистый генератор, устранивший ошибки OpenXML и окна восстановления при открытии[cite: 2].
|
||||||
|
- [x] **Интективные группировки в Сводке:** восстановление сворачивания категорий по умолчанию (`level=1`, `collapsed=True`) и кнопок управления уровнями `[+]`/`[-]` (`outline_settings(symbols_below=False)`)[cite: 2].
|
||||||
|
- [x] **Компактная экранная верстка Детального отчета:** центрирование столбца «Подразделение», расширение столбца «ФИО» (+10%, ширина 33), оптимизация ширины колонок «Первая активность» (11) и «Отклонение от нормы» (11) для отображения без горизонтальной прокрутки[cite: 2].
|
||||||
|
- [x] **Защита от блокировок занятых файлов (Fallback Timestamp):** механизм перехвата `FileCreateError`/`OSError` в `safe_close_workbook` с сохранением копии при открытом в Excel файле[cite: 2].
|
||||||
|
- [x] **Безопасная синхронизация 1С с сетевой шары:** переход на `shutil.copyfile` в `share_copier.py` для устранения сбоев прав доступа (`Operation not permitted`) на SMB/CIFS-ресурсах[cite: 2].
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 12. UI/UX веб-интерфейса и управление реестрами `[ЗАВЕРШЕНО]`
|
||||||
|
- [x] **Паттерн Gemini-скролла:** реализация функции `scrollToUserMessageTop` в `core.js`, позиционирующей свежий вопрос пользователя строго по верхней кромке видимой области[cite: 2].
|
||||||
|
- [x] **Полное скрытие истории:** предыдущие объемные ответы модели гарантированно вытесняются за верхний край экрана[cite: 2].
|
||||||
|
- [x] **Стабилизация панели ввода:** настройка `padding-bottom: 80vh` для `#chat-messages-container` в `index.html`, обеспечивающая необходимый запас высоты прокрутки без сдвига строки ввода текста[cite: 2].
|
||||||
|
- [x] **Модальные формы реестров с автокомплитом 1С:**
|
||||||
|
- Полный отказ от браузерного `prompt()` при работе со списками исключений.
|
||||||
|
- Единое Tailwind-окно для добавления исключений с живым поиском сотрудников по базе 1С:ЗУП (`staff-autocomplete`) и полем для комментария/основания.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 13. Архитектура обработки событий турникетов и двухконтурный учет `[ЗАВЕРШЕНО]`
|
||||||
|
- [x] **Опора на физический факт прохода (Event 32):**
|
||||||
|
- Устранение потери событий из-за рассинхронизации меток Event 28 (разрешение) и Event 32 (проход).
|
||||||
|
- Переход на плоский SQL-запрос выборки первого входа (`Mode = 1`) и последнего выхода (`Mode = 2`).
|
||||||
|
- [x] **Двухконтурная модель турникетов (Левый/Правый PERCo):**
|
||||||
|
- Разрешение использования обоих турникетов (`DoorIndex IN (1, 2)`) для сотрудников дворовых служб (отделы `ЭТО`, `ЛЦ` и др.).
|
||||||
|
- Защита основного пула офисных сотрудников от транзитных отметок на правом турникете (`DoorIndex = 1`).
|
||||||
|
- Реестр «Пр. турникет» (`turnstile_fio`, `turnstile_departments`) в базе данных и веб-панели.
|
||||||
|
- Корректное сопоставление составных ФИО (`Name + FirstName + MidName`) в запросе к `pList`.
|
||||||
|
- [x] **Таблица сырых событий (`scud_events_raw`):**
|
||||||
|
- Создание DDL-схемы таблицы и логирование всех физических проходов дня параллельно со снапшотами.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 14. Миграция инфраструктуры, отказоустойчивость отчетов и Упрощенный отчет `[ЗАВЕРШЕНО]`
|
||||||
|
- [x] **Миграция на новый сервер (Debian 12):**
|
||||||
|
- Актуализация путей виртуального окружения в cron-скриптах (`scripts/cron/*.sh`).
|
||||||
|
- Восстановление прямого подключения к базе 1С:ЗУП (MS SQL Server `ACCOUNT-01`).
|
||||||
|
- Настройка автоматического CIFS-монтирования шары отчетов `//storage/SCUD/Отчеты` в `/etc/fstab` с правами пользователя.
|
||||||
|
- [x] **Разделение статусов наличия в кадровых реестрах (`merger.py`):**
|
||||||
|
- Выделение сотрудников, заведенных в СКУД, но не принятых в ЗУП (`not_hired_yet = True`), в отдельный раздел сводки **«Не приняты на работу»**.
|
||||||
|
- Полное исключение непринятых сотрудников из детального суточного табеля за прошлые смены.
|
||||||
|
- Обособление штатных сотрудников без карт СКУД (`no_scud_pass = True`) в раздел **«Нет пропуска»** с исключением их из «Неизвестных».
|
||||||
|
- Устранение предупреждений `UserWarning: Boolean Series key will be reindexed` при вычислении метрик штата.
|
||||||
|
- [x] **Стабилизация генерации Excel-книг (`otchet_builder.py`):**
|
||||||
|
- Включение защитной опции `nan_inf_to_errors: True` в `xlsxwriter.Workbook`.
|
||||||
|
- Санитизация ячеек от значений `NaN` / `None` / `pd.NA` перед записью, устранившая критический сбой `TypeError: NAN/INF not supported in write_number()`.
|
||||||
|
- [x] **Генератор «Упрощенного отчета» (`services/reports/simplified_builder.py`):**
|
||||||
|
- Создание генератора по эталонному шаблону: заголовок `Упрощенный отчет: c ДД.ММ.ГГГГ по ДД.ММ.ГГГГ`, сокращение ФИО до инициалов (`Фамилия И.О.`), отображение времени с текстовыми заглушками (`Нет входа (0:00)`, `Нет выхода (23:59)`).
|
||||||
|
- Сортировка по подразделениям и алфавиту сотрудников.
|
||||||
|
- Интеграция этапа сборки упрощенного отчета за вчера в ETL-конвейер (`main_etl.py`).
|
||||||
|
- [x] **Компактный динамический сборщик слепков (`make_etl_snapshot.py`):**
|
||||||
|
- Автоматический обход дерева (auto-discovery) с отсечением дублирующих архивных манифестов, временных файлов и пустых `__init__.py`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 15. ПРИОРИТЕТ 1: Внутридневной контроль, экран «Кто в здании» и On-Demand генерация `[В РАБОТЕ]`
|
||||||
|
- [x] **Оперативный контроль «Кто в здании прямо сейчас» (Live Presence):**
|
||||||
|
- [x] REST API эндпоинт `GET /api/v1/presence/live` на базе таблицы `scud_events_raw` и оконной функции последнего события прохода.
|
||||||
|
- [x] Выделенная модальная панель в веб-интерфейсе:
|
||||||
|
- Метрики реального времени: *Штат 1С*, *В здании*, *Вышли*, *На удаленке*, *В командировке/отпуске*, *Не пришли*, *Исключения*.
|
||||||
|
- Быстрый поиск сотрудника и фильтрация по подразделениям.
|
||||||
|
- Учет специфики служебных входов/выходов во двор (флигель/двор).
|
||||||
|
- [x] **On-Demand генерация отчетов из UI и сайдбара:**
|
||||||
|
- [x] Кнопка **«Сводка»** в сайдбаре: моментальный сбор книги по последнему готовому срезу SQLite без блокирующих опросов MS SQL.
|
||||||
|
- [x] Кнопки **«Детальн.»** и **«Упрощ.»**: строгое формирование отчетов за **ВЧЕРА** (с учетом выходных) по финальному суточному срезу `Y` (23:59:59).
|
||||||
|
- [x] Каскадный fallback кадровых файлов: при отсутствии свежего штата за текущее утро автоматическое использование актуального среза из `zup_staff` в SQLite.
|
||||||
|
- [x] **Атомарная публикация и отдача файлов (Report Delivery Engine):**
|
||||||
|
- [x] Устранение 404 ошибок: рекурсивный поиск файлов в иерархии `output/reports/{ГОД}/{МЕСЯЦ}/` и декодирование URL-имён.
|
||||||
|
- [x] Синхронизация при отдаче: автоматическая отдача прямой ссылки на скачивание в браузер.
|
||||||
|
- [ ] **Реестр «Графики работы» (Custom Work Schedules):**
|
||||||
|
- [ ] Таблица в SQLite `work_schedules_registry` (`fio_clean`, `department`, `schedule_type`, `norm_hours`, `time_start_window`, `time_end_window`, `valid_from`, `valid_to`).
|
||||||
|
- [ ] Интеграция индивидуальной нормы часов (4, 7, 11, 24 ч) в калькулятор выработки `calculators.py` вместо константы 8 ч.
|
||||||
|
- [ ] Вкладка управления графиками в сайдбаре «Реестры» с автоподбором сотрудников из 1С.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 16. ПРИОРИТЕТ 2: Кадровый арбитраж до отчетов и интеграция с MS Exchange `[В ПЛАНАХ]`
|
||||||
|
- [ ] **Почтовый агент MS Exchange (Email-Driven Controller & Human-in-the-Loop):**
|
||||||
|
- [ ] Фоновый сервис `services/mail/exchange_bot.py` для опроса служебного ящика по IMAP/SMTP (`imaplib` / `email`) или EWS (`exchangelib`).
|
||||||
|
- [ ] Белый список доверенных отправителей (адреса сотрудников ОК, СБ и руководства).
|
||||||
|
- [ ] **Генерация отчетов по письму:** распознавание запросов в теме/теле писем (*«Сводка на 14:00»*, *«Отчет за 22.09.2026»*) и отправка файла в ответном письме.
|
||||||
|
- [ ] **Разрешение кадровых коллизий через почту:** обработка ответов СБ на утреннюю рассылку аномалий (подтверждение связок в `person_identity_mapping`) и регистрация командировок/служебок в `manual_absences`.
|
||||||
|
- [ ] Двухфазный Guardrail: запрос подтверждения у кадровика по почте при низкой уверенности в сопоставлении тёзок (<90%).
|
||||||
|
- [ ] **Интеллектуальный кадровый арбитраж ДО генерации отчетов:**
|
||||||
- [ ] Перенос арбитража не сопоставившихся персон на этап [2.5] ДО сохранения Excel-файлов `сводка.xlsx` и `отчет.xlsx`.
|
- [ ] Перенос арбитража не сопоставившихся персон на этап [2.5] ДО сохранения Excel-файлов `сводка.xlsx` и `отчет.xlsx`.
|
||||||
- [ ] **Якорный табельный номер (TabNo Matching):**
|
- [ ] **Якорный табельный номер (TabNo Matching):** извлечение `TabNo` из MS SQL СКУД Орион и 1С:ЗУП, защита от смены фамилий и опечаток.
|
||||||
- [ ] Извлечение `TabNo` из MS SQL Орион (`pList.TabNo`) и MS SQL 1С:ЗУП.
|
- [ ] 4-уровневая система предохранителей (Точный матч $\rightarrow$ Табельный номер $\rightarrow$ Кэш связок $\rightarrow$ ИИ-арбитраж).
|
||||||
- [ ] Добавление колонок `scud_tab_no` и `zup_tab_no` в SQLite (`scud_logs`, `zup_staff`, `person_identity_mapping`).
|
|
||||||
- [ ] Защита от смены фамилий и опечаток через неизменяемый табельный номер.
|
|
||||||
- [ ] **Динамическая инвалидация кэша связок:**
|
|
||||||
- [ ] Автоматическая отбраковка записей кэша при увольнении сотрудника или несовпадении подразделения.
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 12. Изолированная песочница кода (Code Execution Sandbox Engine) `[В ПЛАНАХ]`
|
## 17. ПРИОРИТЕТ 3: Векторная база знаний (RAG) и внешний контекст `[В ПЛАНАХ]`
|
||||||
|
- [ ] **Векторный поиск по регламентам компании (ChromaDB / RAG Engine):**
|
||||||
|
- [ ] Встраиваемая векторная база для семантического поиска по ПВТР, положениям о пропускном режиме и должностным инструкциям.
|
||||||
|
- [ ] Мгновенная выдача ссылок на пункты локальных нормативных актов при консультации оператора в чате.
|
||||||
|
- [ ] Семантическая классификация нестандартных формулировок причин отсутствий из писем и заявлений.
|
||||||
|
- [ ] **Внешний контекстный инструмент (Web Search Agent):**
|
||||||
|
- [ ] Нативный инструмент `web_search` для обращения к производственному календарю РФ (переносы праздников, предпраздничные сокращенные смены по ст. 95 ТК РФ).
|
||||||
|
- [ ] Справка по статьям Трудового кодекса РФ (сверхурочные, выходные дни).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 18. Исследовательский трек: Изолированная песочница кода `[ОТЛОЖЕНО]`
|
||||||
- [ ] **Docker/gVisor контур:**
|
- [ ] **Docker/gVisor контур:**
|
||||||
- Создание изолированного контейнера без доступа к внешней сети (network: none) с ограниченными лимитами по памяти и CPU (cgroups)[cite: 3].
|
- [ ] Изолированный контейнер без доступа к внешней сети (`network: none`) с жесткими cgroups-лимитами и монтированием данных в режиме Read-Only.
|
||||||
- Настройка безопасного монтирования только необходимых CSV/Parquet-файлов данных в режиме Read-Only[cite: 3].
|
- [ ] Генерация и выполнение Python/Pandas скриптов для построения сложных графиков и нестандартной статистики на лету.
|
||||||
- [ ] **Динамические Python/Pandas вычисления:**
|
|
||||||
- Инструмент генерации и безопасного выполнения скриптов агрегации и аналитики данных СКУД / 1С на лету[cite: 3].
|
---
|
||||||
- Перехват stdout/stderr, сбор результатов расчетов и графиков с передачей в UI-чата[cite: 3].
|
|
||||||
|
## 19. Доменный модуль канцелярии и документов (`services/office/`) `[В РАБОТЕ]`
|
||||||
|
- [x] **Изоляция офисного домена от кадрового ядра СКУД:**
|
||||||
|
- [x] Создание независимого пакета `services/office/` (`document_extractor.py`, `word_builder.py`, `service.py`).
|
||||||
|
- [x] Снятие ограничений контекста диалога: многостраничный постраничный обход PDF вместо обрезания первой страницы.
|
||||||
|
- [x] Генерация ГОСТ-документов Word (`.docx`) с выдачей контрастной карточки скачивания в интерфейс чата.
|
||||||
|
- [ ] **Двухфазный конвейер OCR и глубокая нормализация (Vision + LLM Refiner):**
|
||||||
|
- [ ] **Фаза 1 (Vision Extraction):** повышение разрешения рендеринга до `250–300 DPI` для чёткой отрисовки тонких линий шариковых ручек и штампов.
|
||||||
|
- [ ] **Фаза 2 (LLM Post-Processing Refiner):** вычитка текста через `qwen2.5:14b`:
|
||||||
|
- Устранение диалогового шума и случайных приветствий модели.
|
||||||
|
- Склейка разорванных строк внутри абзацев.
|
||||||
|
- Автоматическая сборка списков участников и табличных данных в полноценные таблицы Word (`Table Grid`).
|
||||||
|
- [ ] **Специализированное распознавание рукописных виз и резолюций канцелярии:**
|
||||||
|
- [ ] Распознавание наклонного и беглого почерка на полях и в шапке документов (визы «Согласовано», резолюции руководства, подписи, даты).
|
||||||
|
- [ ] Выделение рукописных пометок в стандартизированные структурные блоки: `[Резолюция: ...]` в начале страницы.
|
||||||
|
- [ ] **Two-Pass Context Injection:** перекрёстное сопоставление неразборчивых фамилий и инициалов с реестром участников совещания/штатом из тела самого документа.
|
||||||
+5526
-2225
File diff suppressed because it is too large
Load Diff
+87
-10
@@ -19,6 +19,8 @@ from services.scud_etl.anomaly_detector import detect_registry_anomalies
|
|||||||
from services.scud_etl.svodka_generator import generate_svodka_service
|
from services.scud_etl.svodka_generator import generate_svodka_service
|
||||||
from services.scud_etl.otchet_generator import generate_otchet_service
|
from services.scud_etl.otchet_generator import generate_otchet_service
|
||||||
from services.text_reporter import generate_markdown_report
|
from services.text_reporter import generate_markdown_report
|
||||||
|
from services.snapshots.retention import cleanup_old_intermediate_snapshots
|
||||||
|
from services.snapshots.retention import purge_day_intermediate_snapshots
|
||||||
|
|
||||||
from services.scud_export import run_export
|
from services.scud_export import run_export
|
||||||
from services.share_copier import copy_1c_files_from_share
|
from services.share_copier import copy_1c_files_from_share
|
||||||
@@ -27,19 +29,69 @@ from services.excel_exporter import export_raw_scud
|
|||||||
logging.basicConfig(level=logging.INFO, format="[%(asctime)s] [%(levelname)s] %(message)s")
|
logging.basicConfig(level=logging.INFO, format="[%(asctime)s] [%(levelname)s] %(message)s")
|
||||||
|
|
||||||
|
|
||||||
|
def print_help():
|
||||||
|
print("""
|
||||||
|
===============================================================================
|
||||||
|
🛠️ SCUD ORION AI — СИСТЕМА КОНТРОЛЛИНГА И СВОДНЫХ ОТЧЕТОВ
|
||||||
|
===============================================================================
|
||||||
|
Использование:
|
||||||
|
python main_etl.py [ОПЦИИ]
|
||||||
|
|
||||||
|
Доступные аргументы:
|
||||||
|
-h, --help, help Показать эту справку и выйти
|
||||||
|
--date ДД.ММ.ГГГГ Дата расчета (по умолчанию: текущий рабочий день)
|
||||||
|
--time ЧЧ:ММ Время среза для сводки (например: 14:30)
|
||||||
|
Ищет ближайший срез (±20 мин) или запрашивает On-Demand экспорт
|
||||||
|
--snapshot ID Точный ID снапшота для расчета (например: 20260827-002)
|
||||||
|
--skip-export Пропустить выгрузку СКУД из MS SQL (работать только с SQLite)
|
||||||
|
--export-only ТОЛЬКО сделать экспорт/снапшот СКУД в БД без построения отчетов
|
||||||
|
-d, --debug Включить режим расширенной отладки
|
||||||
|
|
||||||
|
Примеры использования:
|
||||||
|
python main_etl.py
|
||||||
|
👉 Полный суточный цикл: экспорт -> отчет за вчера -> сводка за сегодня -> ИИ.
|
||||||
|
|
||||||
|
python main_etl.py --export-only
|
||||||
|
👉 Почасовой тихий срез в БД (для cron) без генерации отчетов.
|
||||||
|
|
||||||
|
python main_etl.py --date 27.08.2026 --time 12:15 --skip-export
|
||||||
|
👉 Построить сводку за 27.08 на 12:15 без запроса к внешнему MS SQL.
|
||||||
|
|
||||||
|
python main_etl.py --snapshot 20260827-001 --skip-export
|
||||||
|
👉 Расчет отчетов строго по выбранному снапшоту из SQLite.
|
||||||
|
===============================================================================
|
||||||
|
""")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description="Модульный контроллинг СКУД ⟷ 1С")
|
if len(sys.argv) > 1 and sys.argv[1] in ("-h", "--help", "help"):
|
||||||
parser.add_argument("-d", "--debug", action="store_true", help="Режим отладки")
|
print_help()
|
||||||
parser.add_argument("--skip-export", action="store_true", help="Пропустить выгрузку СКУД из MS SQL")
|
sys.exit(0)
|
||||||
parser.add_argument("--date", type=str, default=None, help="Дата расчета в формате ДД.ММ.ГГГГ")
|
|
||||||
parser.add_argument("--time", type=str, default=None, help="Время среза для сводки (например, 14:30)")
|
parser = argparse.ArgumentParser(add_help=False)
|
||||||
parser.add_argument("--snapshot", type=str, default=None, help="Точный ID снапшота для расчета")
|
parser.add_argument("-h", "--help", action="store_true")
|
||||||
|
parser.add_argument("-d", "--debug", action="store_true")
|
||||||
|
parser.add_argument("--skip-export", action="store_true")
|
||||||
|
parser.add_argument("--export-only", action="store_true")
|
||||||
|
parser.add_argument("--date", type=str, default=None)
|
||||||
|
parser.add_argument("--time", type=str, default=None)
|
||||||
|
parser.add_argument("--snapshot", type=str, default=None)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
if args.help:
|
||||||
|
print_help()
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
print("=" * 60)
|
print("=" * 60)
|
||||||
print(f"ЗАПУСК СИСТЕМЫ МОДУЛЬНОГО КОНТРОЛЛИНГА СКУД ⟷ 1С {'[DEBUG]' if args.debug else ''}")
|
print(f"ЗАПУСК СИСТЕМЫ МОДУЛЬНОГО КОНТРОЛЛИНГА СКУД ⟷ 1С {'[DEBUG]' if args.debug else ''}")
|
||||||
print("=" * 60)
|
print("=" * 60)
|
||||||
|
|
||||||
|
# Автоматическая ротация архивных почасовых срезов
|
||||||
|
if not args.skip_export and not args.snapshot:
|
||||||
|
deleted_count = cleanup_old_intermediate_snapshots(days_to_keep_all=2)
|
||||||
|
if deleted_count > 0:
|
||||||
|
print(f"[🧹] Ротация БД: очищено {deleted_count} строк промежуточных архивных срезов.")
|
||||||
|
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
if args.date:
|
if args.date:
|
||||||
today_str = args.date.replace('_', '.')
|
today_str = args.date.replace('_', '.')
|
||||||
@@ -60,6 +112,11 @@ def main():
|
|||||||
else:
|
else:
|
||||||
print("\n[0/5] Пропуск прямого экспорта СКУД из MS SQL (--skip-export)...")
|
print("\n[0/5] Пропуск прямого экспорта СКУД из MS SQL (--skip-export)...")
|
||||||
|
|
||||||
|
# Если запрошен режим тихого почасового среза — выходим без тяжелых генераций
|
||||||
|
if args.export_only:
|
||||||
|
print(f"\n[✓] Режим --export-only: срез зафиксирован в SQLite. Генерация отчетов пропущена.")
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
# [Этап 0.5] Синхронизация файлов с шары 1С
|
# [Этап 0.5] Синхронизация файлов с шары 1С
|
||||||
if not args.snapshot and not args.skip_export:
|
if not args.snapshot and not args.skip_export:
|
||||||
print(f"\n[0.5/5] Проверка и копирование файлов 1С с шары...")
|
print(f"\n[0.5/5] Проверка и копирование файлов 1С с шары...")
|
||||||
@@ -78,12 +135,24 @@ def main():
|
|||||||
if df_scud_today is not None and not df_scud_today.empty:
|
if df_scud_today is not None and not df_scud_today.empty:
|
||||||
export_raw_scud(df_scud_today, filename=f"СКУД_Сырые_данные_{today_str}.xlsx")
|
export_raw_scud(df_scud_today, filename=f"СКУД_Сырые_данные_{today_str}.xlsx")
|
||||||
|
|
||||||
# [Этап 3] Детальный отчет за вчера через otchet_generator
|
# [Этап 3] Детальный и Упрощенный отчеты за вчера
|
||||||
print(f"\n[3/5] Обработка и построение детального отчета за ВЧЕРА ({yesterday_str})...")
|
print(f"\n[3/5] Обработка и построение детального и упрощенного отчетов за ВЧЕРА ({yesterday_str})...")
|
||||||
res_otchet = generate_otchet_service(target_date=yesterday_str)
|
res_otchet = generate_otchet_service(target_date=yesterday_str)
|
||||||
if res_otchet.get("status") == "success":
|
if res_otchet.get("status") == "success":
|
||||||
print(f"[✓] {res_otchet.get('message')}: {res_otchet.get('filepath')}")
|
print(f"[✓] {res_otchet.get('message')}: {res_otchet.get('filepath')}")
|
||||||
|
|
||||||
|
# Генерация Упрощенного отчета за вчера
|
||||||
|
try:
|
||||||
|
from services.reports.simplified_builder import generate_simplified_excel
|
||||||
|
df_scud_y = load_best_snapshot_for_date(yesterday_str, prefer_final_y=True)
|
||||||
|
df_staff_y, df_abs_y = load_1c_files_for_date(yesterday_str)
|
||||||
|
df_merged_y = merge_scud_and_1c(df_scud_y, df_staff_y, df_abs_y)
|
||||||
|
|
||||||
|
simplified_path = generate_simplified_excel(df_merged_y, date_str=yesterday_str)
|
||||||
|
print(f"[✓] Упрощенный отчет за {yesterday_str} успешно сформирован: {simplified_path}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[⚠️] Ошибка формирования упрощенного отчета: {e}")
|
||||||
|
|
||||||
# [Этап 4] Сводка за сегодня через svodka_generator
|
# [Этап 4] Сводка за сегодня через svodka_generator
|
||||||
print(f"\n[4/5] Обработка и построение Ежедневной сводки за {today_str} {args.time or ''}...")
|
print(f"\n[4/5] Обработка и построение Ежедневной сводки за {today_str} {args.time or ''}...")
|
||||||
res_svodka = generate_svodka_service(
|
res_svodka = generate_svodka_service(
|
||||||
@@ -96,6 +165,12 @@ def main():
|
|||||||
if res_svodka.get("note"):
|
if res_svodka.get("note"):
|
||||||
print(f" ℹ️ {res_svodka.get('note')}")
|
print(f" ℹ️ {res_svodka.get('note')}")
|
||||||
|
|
||||||
|
# Если запуск ночной (после 22:00) или закрывающий смену — зачищаем дневные срезы
|
||||||
|
if now.hour >= 22 and not args.skip_export:
|
||||||
|
purged = purge_day_intermediate_snapshots(today_str)
|
||||||
|
if purged > 0:
|
||||||
|
print(f"[🧹] Ночная очистка: удалено {purged} строк дневных срезов за {today_str}. Итоговый Y-срез сохранен.")
|
||||||
|
|
||||||
# [Этап 5] Формирование Markdown-сводки через ИИ-аудитора (Ollama)
|
# [Этап 5] Формирование Markdown-сводки через ИИ-аудитора (Ollama)
|
||||||
print(f"\n[5/5] Формирование Markdown-сводки через ИИ-аудитора (Ollama)...")
|
print(f"\n[5/5] Формирование Markdown-сводки через ИИ-аудитора (Ollama)...")
|
||||||
|
|
||||||
@@ -109,8 +184,10 @@ def main():
|
|||||||
]
|
]
|
||||||
absent_unexplained = df_merged_today[
|
absent_unexplained = df_merged_today[
|
||||||
(df_merged_today['Пришел'] == False) &
|
(df_merged_today['Пришел'] == False) &
|
||||||
(df_merged_today['Вид_отсутствия'].isna() | (df_merged_today['Вид_отсутствия'].astype(str).str.strip() == '')) &
|
(df_merged_today['Вид_отсутствия'].isna() | (df_merged_today['Вид_отсутствия'].astype(str).str.strip().isin(['', 'nan', 'None']))) &
|
||||||
(df_merged_today.get('is_excluded', False) == False)
|
(df_merged_today.get('is_excluded', False) == False) &
|
||||||
|
(df_merged_today.get('not_hired_yet', False) == False) &
|
||||||
|
(df_merged_today.get('no_scud_pass', False) == False)
|
||||||
]
|
]
|
||||||
|
|
||||||
summary_md = generate_markdown_report(
|
summary_md = generate_markdown_report(
|
||||||
|
|||||||
@@ -1,178 +0,0 @@
|
|||||||
"""
|
|
||||||
===============================================================================
|
|
||||||
FILE: modules/ai_engine/agent.py
|
|
||||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
|
||||||
MODULE: modules / ai_engine
|
|
||||||
ROLE: Лаконичный нативный оркестратор Function Calling, диспетчер handlers
|
|
||||||
и менеджер свободных диалогов (Topic Drift).
|
|
||||||
|
|
||||||
AI-CONTEXT-ANCHORS:
|
|
||||||
- ANCHOR[AGENT_PIPELINE_ENTRY]: Главная точка входа process_chat_message.
|
|
||||||
===============================================================================
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import json
|
|
||||||
import logging
|
|
||||||
from typing import List, Dict, Any, Tuple, Optional
|
|
||||||
|
|
||||||
from modules.web_api.llm.schemas import TOOLS_SCHEMA
|
|
||||||
from modules.web_api.llm.core.ollama_client import call_ollama_chat
|
|
||||||
from modules.web_api.llm.core.fast_path import handle_fast_path_intercept
|
|
||||||
from modules.web_api.llm.core.tool_injector import clean_raw_tool_tags, clean_output, inject_tools_if_needed
|
|
||||||
from modules.web_api.llm.core.context_manager import mark_last_user_message_ephemeral, close_tool_session_and_cleanup
|
|
||||||
from modules.web_api.llm.db.db_chat import db_save_chat_message, db_get_chat_history, db_purge_ephemeral_messages
|
|
||||||
from modules.web_api.llm.db.db_prompts import (
|
|
||||||
db_get_session_state, db_clear_session_state, db_set_session_state,
|
|
||||||
db_get_stats, db_get_anomalies, db_get_reference
|
|
||||||
)
|
|
||||||
from services.knowledge.service import get_rules
|
|
||||||
|
|
||||||
from .context_builder import build_agent_system_context
|
|
||||||
from .handlers.task_handler import handle_tasks_call
|
|
||||||
from .handlers.prompt_handler import handle_prompt_call
|
|
||||||
from .handlers.snapshot_handler import handle_snapshots_call
|
|
||||||
|
|
||||||
logger = logging.getLogger("AI_AGENT")
|
|
||||||
logger.setLevel(logging.INFO)
|
|
||||||
|
|
||||||
|
|
||||||
# ANCHOR[AGENT_PIPELINE_ENTRY]
|
|
||||||
def process_chat_message(
|
|
||||||
user_id: int,
|
|
||||||
user_message: str,
|
|
||||||
file_context: str = "",
|
|
||||||
image_b64: Optional[str] = None,
|
|
||||||
chat_history: List[Dict[str, Any]] = None,
|
|
||||||
session_id: str = "web_session_main"
|
|
||||||
) -> Tuple[str, List[Dict[str, Any]], Optional[Dict[str, Any]]]:
|
|
||||||
"""Главный конвейер обработки сообщений чата."""
|
|
||||||
logger.info(f"Сообщение от user_id={user_id}, session_id={session_id}: {user_message}")
|
|
||||||
|
|
||||||
session_state = db_get_session_state(session_id)
|
|
||||||
if not session_state:
|
|
||||||
db_purge_ephemeral_messages(session_id)
|
|
||||||
|
|
||||||
full_user_content = f"{user_message}\n\n[СОДЕРЖИМОЕ ПРИКРЕПЛЕННОГО ФАЙЛА]:\n{file_context}" if file_context else user_message
|
|
||||||
|
|
||||||
# 1. Быстрый Fast-Path перехват кнопок подтверждения
|
|
||||||
fast_path_res = handle_fast_path_intercept(session_id, user_message, full_user_content, session_state)
|
|
||||||
if fast_path_res:
|
|
||||||
return fast_path_res
|
|
||||||
|
|
||||||
db_save_chat_message(session_id, "user", full_user_content, is_ephemeral=0)
|
|
||||||
db_history = db_get_chat_history(session_id, limit=20)
|
|
||||||
system_prompt = build_agent_system_context(user_id, session_state)
|
|
||||||
|
|
||||||
user_msg_obj = {"role": "user", "content": full_user_content}
|
|
||||||
|
|
||||||
try:
|
|
||||||
if image_b64:
|
|
||||||
user_msg_obj["images"] = [image_b64]
|
|
||||||
messages = [{"role": "system", "content": "Строгий модуль OCR. Перепиши весь текст буква в букву."}, user_msg_obj]
|
|
||||||
msg = call_ollama_chat(messages, is_vision=True)
|
|
||||||
else:
|
|
||||||
clean_history = [dict(m) for m in db_history]
|
|
||||||
for m in clean_history: m.pop("images", None)
|
|
||||||
messages = [{"role": "system", "content": system_prompt}] + clean_history + [user_msg_obj]
|
|
||||||
msg = call_ollama_chat(messages, tools=TOOLS_SCHEMA, is_vision=False)
|
|
||||||
|
|
||||||
raw_reply = msg.get("content", "")
|
|
||||||
tool_calls = msg.get("tool_calls", [])
|
|
||||||
|
|
||||||
# 2. Гибридный семантический классификатор намерений (Fallback Safety Net)
|
|
||||||
tool_calls = inject_tools_if_needed(user_message, raw_reply, tool_calls)
|
|
||||||
|
|
||||||
# 3. Исполнение инструментов через изолированные handlers
|
|
||||||
if tool_calls:
|
|
||||||
tool = tool_calls[0]
|
|
||||||
fn_name = tool["function"]["name"]
|
|
||||||
fn_args = tool["function"].get("arguments", {})
|
|
||||||
if isinstance(fn_args, str):
|
|
||||||
try: fn_args = json.loads(fn_args)
|
|
||||||
except Exception: fn_args = {}
|
|
||||||
|
|
||||||
logger.info(f"Вызов инструмента: {fn_name} с аргументами: {fn_args}")
|
|
||||||
close_tool_session_and_cleanup(session_id, close_reason=f"ACTIVATE_{fn_name}")
|
|
||||||
mark_last_user_message_ephemeral(session_id)
|
|
||||||
|
|
||||||
state_data = session_state.get("data_json") or {} if session_state else {}
|
|
||||||
|
|
||||||
if fn_name in ["db_get_tasks", "db_tasks_edit", "db_add_task", "db_update_task_status", "db_delete_task"]:
|
|
||||||
return handle_tasks_call(fn_name, fn_args, user_id, session_id)
|
|
||||||
|
|
||||||
elif fn_name in ["db_get_system_prompt", "db_prompt_node_edit"]:
|
|
||||||
return handle_prompt_call(fn_name, fn_args, session_id)
|
|
||||||
|
|
||||||
elif fn_name in ["db_get_snapshots", "db_delete_snapshots"]:
|
|
||||||
return handle_snapshots_call(fn_name, fn_args, session_id, user_message, state_data)
|
|
||||||
|
|
||||||
elif fn_name == "db_get_rules":
|
|
||||||
res_str = json.dumps(get_rules(), ensure_ascii=False)
|
|
||||||
elif fn_name == "db_get_stats":
|
|
||||||
res_str = json.dumps(db_get_stats(), ensure_ascii=False)
|
|
||||||
elif fn_name == "db_get_anomalies":
|
|
||||||
res_str = json.dumps(db_get_anomalies(limit=fn_args.get("limit", 100), date_str=fn_args.get("date_str")), ensure_ascii=False)
|
|
||||||
elif fn_name == "db_get_reference":
|
|
||||||
res_str = json.dumps(db_get_reference(category=fn_args.get("category")), ensure_ascii=False)
|
|
||||||
else:
|
|
||||||
res_str = "{}"
|
|
||||||
|
|
||||||
messages.append(msg)
|
|
||||||
messages.append({"role": "tool", "content": res_str})
|
|
||||||
sec_msg = call_ollama_chat(messages, is_vision=False)
|
|
||||||
final_content = clean_raw_tool_tags(clean_output(sec_msg.get("content", ""))) or "Запрос выполнен."
|
|
||||||
db_save_chat_message(session_id, "assistant", final_content, is_ephemeral=0)
|
|
||||||
return final_content, db_get_chat_history(session_id), None
|
|
||||||
|
|
||||||
# 4. Обычный содержательный диалог и управление Topic Drift
|
|
||||||
final_reply = clean_raw_tool_tags(clean_output(raw_reply)) or "Запрос обработан."
|
|
||||||
for artifact in ["почемучка,", "почемучка!", "почемучка?", "почемучка", "почемучто,", "почемучто", "почему-то"]:
|
|
||||||
if final_reply.lower().startswith(artifact):
|
|
||||||
final_reply = final_reply[len(artifact):].lstrip(",.!?:; -")
|
|
||||||
|
|
||||||
action_payload = None
|
|
||||||
|
|
||||||
# Обработка ответа "нет / спасибо" в режиме открытого инструмента
|
|
||||||
if session_state and any(kw in user_message.lower() for kw in ["нет", "спасибо", "не надо", "готово", "хватит"]):
|
|
||||||
close_tool_session_and_cleanup(session_id, close_reason="USER_DISMISSED_TOOL")
|
|
||||||
db_save_chat_message(session_id, "assistant", final_reply, is_ephemeral=0)
|
|
||||||
return final_reply, db_get_chat_history(session_id), None
|
|
||||||
|
|
||||||
# Инкремент счётчика шагов в сторону от инструмента (Topic Drift)
|
|
||||||
if session_state and session_state.get("state_type") in ["PROMPT_FOLLOWUP", "PROMPT_PREVIEW", "SNAPSHOTS_VIEW"]:
|
|
||||||
state_type = session_state.get("state_type")
|
|
||||||
state_data = session_state.get("data_json") or {}
|
|
||||||
if not isinstance(state_data, dict):
|
|
||||||
state_data = {}
|
|
||||||
|
|
||||||
idle_turns = state_data.get("idle_turns", 0) + 1
|
|
||||||
state_data["idle_turns"] = idle_turns
|
|
||||||
|
|
||||||
if idle_turns >= 4:
|
|
||||||
# 4-й шаг не по теме: бесшумно закрываем сессию и вычищаем эфемерные карточки
|
|
||||||
close_tool_session_and_cleanup(session_id, close_reason="TOPIC_DRIFT_TIMEOUT")
|
|
||||||
elif idle_turns == 3:
|
|
||||||
# 3-й шаг: выводим вежливое напоминание с кнопками
|
|
||||||
tool_label = "системным промптом" if "PROMPT" in state_type else "снапшотами СКУД"
|
|
||||||
guard_question = f"Желаете продолжить работу с {tool_label}?"
|
|
||||||
final_reply += f"\n\n💡 *Напоминание:* {guard_question}"
|
|
||||||
action_payload = {
|
|
||||||
"type": "FOLLOW_UP_ACTION",
|
|
||||||
"buttons": [
|
|
||||||
{"label": "Показать снова", "value": "покажи системный промпт" if "PROMPT" in state_type else "покажи снапшоты", "style": "primary"},
|
|
||||||
{"label": "Завершить", "value": "нет, спасибо", "style": "secondary"}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
db_set_session_state(session_id, state_type, state_data)
|
|
||||||
else:
|
|
||||||
# 1-й и 2-й шаг: фиксируем обновленный счётчик
|
|
||||||
db_set_session_state(session_id, state_type, state_data)
|
|
||||||
|
|
||||||
is_ephem_reply = 1 if "актуальный системный промпт:" in final_reply.lower() else 0
|
|
||||||
db_save_chat_message(session_id, "assistant", final_reply, is_ephemeral=is_ephem_reply)
|
|
||||||
return final_reply, db_get_chat_history(session_id), action_payload
|
|
||||||
|
|
||||||
except Exception as ex:
|
|
||||||
logger.exception(f"Ошибка в агенте: {ex}")
|
|
||||||
return f"Внутренняя ошибка сервера: {ex}", db_get_chat_history(session_id), None
|
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
"""
|
|
||||||
===============================================================================
|
|
||||||
FILE: modules/ai_engine/context_builder.py
|
|
||||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
|
||||||
MODULE: modules / ai_engine
|
|
||||||
ROLE: Динамическая сборка системного промпта из БД, календаря и контекста сессий.
|
|
||||||
|
|
||||||
AI-CONTEXT-ANCHORS:
|
|
||||||
- ANCHOR[CONTEXT_BUILDER_MAIN]: Формирование полного системного контекста для LLM.
|
|
||||||
===============================================================================
|
|
||||||
"""
|
|
||||||
|
|
||||||
from typing import Dict, Any, Optional
|
|
||||||
from services.prompts.service import get_active_system_prompt
|
|
||||||
from modules.web_api.llm.core.calendar_utils import get_dynamic_calendar_context
|
|
||||||
|
|
||||||
|
|
||||||
# ANCHOR[CONTEXT_BUILDER_MAIN]
|
|
||||||
def build_agent_system_context(
|
|
||||||
user_id: int,
|
|
||||||
session_state: Optional[Dict[str, Any]]
|
|
||||||
) -> str:
|
|
||||||
"""Формирует полный динамический системный контекст агента с правилами из БД."""
|
|
||||||
|
|
||||||
# 1. Загружаем актуальный базовый системный промпт из SQLite БД
|
|
||||||
base_system_prompt = get_active_system_prompt()
|
|
||||||
|
|
||||||
# 2. Серверный календарь и контекст дат
|
|
||||||
calendar_context = get_dynamic_calendar_context()
|
|
||||||
user_info = f"Пользователь ID={user_id}" if user_id != 0 else "Гость"
|
|
||||||
|
|
||||||
# 3. Контекст текущего активного стейта сессии
|
|
||||||
current_state_type = session_state.get("state_type") if session_state else None
|
|
||||||
state_data = session_state.get("data_json") or {} if session_state else {}
|
|
||||||
if not isinstance(state_data, dict):
|
|
||||||
state_data = {}
|
|
||||||
|
|
||||||
active_state_context = ""
|
|
||||||
if current_state_type == "PROMPT_PREVIEW":
|
|
||||||
active_state_context = (
|
|
||||||
"\n[ТЕКУЩИЙ РЕЖИМ: ПРЕДПРОСМОТР СИСТЕМНОГО ПРОМПТА]\n"
|
|
||||||
"- Открыт предпросмотр изменений промпта. Для любых правок вызывай db_prompt_node_edit.\n"
|
|
||||||
)
|
|
||||||
elif current_state_type == "PROMPT_FOLLOWUP":
|
|
||||||
active_state_context = (
|
|
||||||
"\n[ТЕКУЩИЙ РЕЖИМ: СЕССИЯ РЕДАКТИРОВАНИЯ ПРОМПТА]\n"
|
|
||||||
"- Оператор просматривает или редактирует системный промпт.\n"
|
|
||||||
"- На любые команды вида 'удали пункт X.Y' или 'удали X.Y' ТЫ ОБЯЗАН ВЫЗВАТЬ db_prompt_node_edit с action='DELETE', section_id=X, item_id=Y.\n"
|
|
||||||
"- На любые команды 'добавь пункт X.Y ...' вызывай action='ADD'.\n"
|
|
||||||
"- Запрещено путать ADD и DELETE.\n"
|
|
||||||
)
|
|
||||||
elif current_state_type == "SNAPSHOTS_VIEW":
|
|
||||||
active_date = state_data.get("query_date", "выбранную дату")
|
|
||||||
active_state_context = (
|
|
||||||
f"\n[ТЕКУЩИЙ РЕЖИМ: ПРОСМОТР СНАПШОТОВ СКУД]\n"
|
|
||||||
f"- Сейчас на экране отображаются снапшоты за {active_date}.\n"
|
|
||||||
f"- Ты можешь форматировать, фильтровать или анализировать этот текущий срез.\n"
|
|
||||||
f"- Если оператор запрашивает ДРУГУЮ дату или день недели (например: 'за вчера', 'а за 13.08') — "
|
|
||||||
f"ТЫ ОБЯЗАН СРАЗУ ВЫЗВАТЬ db_get_snapshots(date_str=...).\n"
|
|
||||||
f"- Запрещено генерировать текст за другую дату по памяти.\n"
|
|
||||||
)
|
|
||||||
elif current_state_type == "SNAPSHOT_DELETE_CONFIRM":
|
|
||||||
active_state_context = "\n[ВНИМАНИЕ: ОЖИДАЕТСЯ ПОДТВЕРЖДЕНИЕ УДАЛЕНИЯ СНАПШОТОВ]\n"
|
|
||||||
elif current_state_type == "TASK_DELETE_CONFIRM":
|
|
||||||
active_state_context = "\n[ВНИМАНИЕ: ОЖИДАЕТСЯ ПОДТВЕРЖДЕНИЕ УДАЛЕНИЯ ЗАДАЧИ]\n"
|
|
||||||
|
|
||||||
# 4. Сборка полного системного сообщения
|
|
||||||
return (
|
|
||||||
f"{base_system_prompt}\n\n"
|
|
||||||
f"СТРОГИЕ ПРАВИЛА ВЫЗОВА ИНСТРУМЕНТОВ:\n"
|
|
||||||
f"1. Для системного промпта:\n"
|
|
||||||
f" - 'добавь X.Y Текст' -> СРАЗУ вызывай db_prompt_node_edit(action='ADD', section_id=X, item_id=Y, content='Текст').\n"
|
|
||||||
f" - 'удали X.Y' -> СРАЗУ вызывай db_prompt_node_edit(action='DELETE', section_id=X, item_id=Y, content='').\n"
|
|
||||||
f" - 'покажи системный промпт' -> СРАЗУ вызывай db_get_system_prompt().\n"
|
|
||||||
f"2. Для задач:\n"
|
|
||||||
f" - 'удали задачу N' -> СРАЗУ вызывай db_tasks_edit(action='DELETE', task_id='N').\n"
|
|
||||||
f" - 'возьми в работу N' -> СРАЗУ вызывай db_tasks_edit(action='UPDATE', task_id='N', status='IN_PROGRESS').\n"
|
|
||||||
f" - 'заверши N' / 'готово N' -> СРАЗУ вызывай db_tasks_edit(action='UPDATE', task_id='N', status='COMPLETED').\n"
|
|
||||||
f"3. Запрещено задавать вопросы и писать подтверждения текстом — СРАЗУ вызывай соответствующий инструмент!\n\n"
|
|
||||||
f"[СИСТЕМНЫЙ КАЛЕНДАРЬ СЕРВЕРА]\n"
|
|
||||||
f"- Пользователь: {user_info}\n"
|
|
||||||
f"- {calendar_context}\n"
|
|
||||||
f"{active_state_context}"
|
|
||||||
)
|
|
||||||
@@ -1,98 +0,0 @@
|
|||||||
"""
|
|
||||||
===============================================================================
|
|
||||||
FILE: modules/ai_engine/handlers/prompt_handler.py
|
|
||||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
|
||||||
MODULE: modules / ai_engine / handlers
|
|
||||||
ROLE: Изолированная обработка команд управления системным промптом.
|
|
||||||
===============================================================================
|
|
||||||
"""
|
|
||||||
|
|
||||||
from typing import Dict, Any, Tuple, Optional
|
|
||||||
from services.prompts.service import get_active_system_prompt, create_prompt_preview
|
|
||||||
from modules.web_api.llm.db.db_chat import db_save_chat_message, db_get_chat_history
|
|
||||||
from modules.web_api.llm.db.db_prompts import db_set_session_state
|
|
||||||
|
|
||||||
|
|
||||||
# ANCHOR[PROMPT_HANDLER_DISPATCH]
|
|
||||||
def handle_prompt_call(
|
|
||||||
fn_name: str,
|
|
||||||
fn_args: Dict[str, Any],
|
|
||||||
session_id: str
|
|
||||||
) -> Tuple[str, list, Optional[Dict[str, Any]]]:
|
|
||||||
"""Обрабатывает вызовы просмотра и изменения системного промпта."""
|
|
||||||
|
|
||||||
# 1. Просмотр промпта с кнопкой быстрого перехода в редактор
|
|
||||||
if fn_name == "db_get_system_prompt":
|
|
||||||
active_prompt = get_active_system_prompt()
|
|
||||||
reply_text = f"Актуальный системный промпт:\n\n{active_prompt}"
|
|
||||||
|
|
||||||
# Сохраняем состояние сессии для возможности мгновенного редактирования и подтверждения
|
|
||||||
db_set_session_state(session_id, "PROMPT_PREVIEW", {
|
|
||||||
"draft_text": active_prompt,
|
|
||||||
"action": "MANUAL_EDIT",
|
|
||||||
"idle_turns": 0
|
|
||||||
})
|
|
||||||
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=1)
|
|
||||||
|
|
||||||
return reply_text, db_get_chat_history(session_id), {
|
|
||||||
"type": "PROMPT_PREVIEW",
|
|
||||||
"raw_draft": active_prompt,
|
|
||||||
"baseline_prompt": active_prompt,
|
|
||||||
"buttons": [
|
|
||||||
{"label": "✏️ Редактировать", "value": "action:open_editor", "style": "secondary"},
|
|
||||||
{"label": "Готово", "value": "нет, спасибо", "style": "secondary"}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
# 2. Предпросмотр точечных или пакетных изменений (ADD / EDIT / DELETE / BATCH_DELETE)
|
|
||||||
action = str(fn_args.get("action", "ADD")).upper()
|
|
||||||
nodes_list = fn_args.get("nodes_list", [])
|
|
||||||
delete_nodes_tuples = []
|
|
||||||
|
|
||||||
if nodes_list:
|
|
||||||
for n_str in nodes_list:
|
|
||||||
parts = str(n_str).strip().split(".")
|
|
||||||
if len(parts) == 2 and parts[0].isdigit() and parts[1].isdigit():
|
|
||||||
delete_nodes_tuples.append((int(parts[0]), int(parts[1])))
|
|
||||||
|
|
||||||
sec_id = None
|
|
||||||
itm_id = None
|
|
||||||
try:
|
|
||||||
if fn_args.get("section_id") is not None:
|
|
||||||
sec_id = int(str(fn_args.get("section_id")).strip())
|
|
||||||
if fn_args.get("item_id") is not None:
|
|
||||||
itm_id = int(str(fn_args.get("item_id")).strip())
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
content = str(fn_args.get("content", "")).strip()
|
|
||||||
|
|
||||||
merged_prompt, diff_html, baseline_prompt = create_prompt_preview(
|
|
||||||
action=action,
|
|
||||||
section_id=sec_id,
|
|
||||||
item_id=itm_id,
|
|
||||||
content=content,
|
|
||||||
delete_nodes=delete_nodes_tuples if delete_nodes_tuples else None
|
|
||||||
)
|
|
||||||
|
|
||||||
db_set_session_state(session_id, "PROMPT_PREVIEW", {
|
|
||||||
"draft_text": merged_prompt,
|
|
||||||
"action": "MANUAL_EDIT",
|
|
||||||
"section_id": sec_id,
|
|
||||||
"item_id": itm_id,
|
|
||||||
"content": content,
|
|
||||||
"idle_turns": 0
|
|
||||||
})
|
|
||||||
|
|
||||||
preview_reply = f"Предпросмотр изменений системного промпта:\n\n{diff_html}\n\nДля применения подтвердите действие, отредактируйте или отмените."
|
|
||||||
db_save_chat_message(session_id, "assistant", preview_reply, is_ephemeral=1)
|
|
||||||
return preview_reply, db_get_chat_history(session_id), {
|
|
||||||
"type": "PROMPT_PREVIEW",
|
|
||||||
"raw_draft": merged_prompt,
|
|
||||||
"baseline_prompt": baseline_prompt,
|
|
||||||
"buttons": [
|
|
||||||
{"label": "Подтвердить", "value": "подтверждаю", "style": "primary"},
|
|
||||||
{"label": "Отменить", "value": "отмена", "style": "danger"},
|
|
||||||
{"label": "✏️ Редактировать", "value": "action:open_editor", "style": "secondary"}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
"""
|
|
||||||
===============================================================================
|
|
||||||
FILE: modules/ai_engine/handlers/snapshot_handler.py
|
|
||||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
|
||||||
MODULE: modules / ai_engine / handlers
|
|
||||||
ROLE: Изолированная обработка запросов просмотра и удаления срезов СКУД.
|
|
||||||
|
|
||||||
AI-CONTEXT-ANCHORS:
|
|
||||||
- ANCHOR[SNAPSHOT_HANDLER_DISPATCH]: Обработка db_get_snapshots и db_delete_snapshots.
|
|
||||||
===============================================================================
|
|
||||||
"""
|
|
||||||
|
|
||||||
from typing import Dict, Any, Tuple, Optional, List
|
|
||||||
from services.snapshots.service import get_snapshots_registry, delete_snapshots_safely
|
|
||||||
from modules.web_api.llm.core.calendar_utils import parse_relative_date_ru
|
|
||||||
from modules.web_api.llm.db.db_chat import db_save_chat_message, db_get_chat_history
|
|
||||||
from modules.web_api.llm.db.db_prompts import db_set_session_state
|
|
||||||
|
|
||||||
|
|
||||||
# ANCHOR[SNAPSHOT_HANDLER_DISPATCH]
|
|
||||||
def handle_snapshots_call(
|
|
||||||
fn_name: str,
|
|
||||||
fn_args: Dict[str, Any],
|
|
||||||
session_id: str,
|
|
||||||
user_message: str,
|
|
||||||
state_data: Dict[str, Any]
|
|
||||||
) -> Tuple[str, list, Optional[Dict[str, Any]]]:
|
|
||||||
"""Обрабатывает запросы реестра и безопасного удаления срезов."""
|
|
||||||
|
|
||||||
# 1. Получение срезов
|
|
||||||
if fn_name == "db_get_snapshots":
|
|
||||||
date_param = fn_args.get("date_str")
|
|
||||||
if not date_param and user_message:
|
|
||||||
date_param = parse_relative_date_ru(user_message)
|
|
||||||
|
|
||||||
snapshots_res = get_snapshots_registry(date_str=date_param)
|
|
||||||
query_date = snapshots_res.get("query_date", "выбранную дату")
|
|
||||||
|
|
||||||
db_set_session_state(session_id, "SNAPSHOTS_VIEW", snapshots_res)
|
|
||||||
reply_text = f"Реестр срезов СКУД за {query_date}:"
|
|
||||||
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=1)
|
|
||||||
return reply_text, db_get_chat_history(session_id), {
|
|
||||||
"type": "SNAPSHOTS_CARD",
|
|
||||||
"data": snapshots_res
|
|
||||||
}
|
|
||||||
|
|
||||||
# 2. Удаление срезов (двухфазное подтверждение)
|
|
||||||
raw_id = fn_args.get("snapshot_id") or fn_args.get("day_str")
|
|
||||||
raw_ids = fn_args.get("snapshot_ids") or []
|
|
||||||
is_confirmed = fn_args.get("confirmed", False)
|
|
||||||
|
|
||||||
if raw_id and not raw_ids:
|
|
||||||
if isinstance(raw_id, str) and "," in raw_id:
|
|
||||||
raw_ids = [s.strip() for s in raw_id.split(",")]
|
|
||||||
else:
|
|
||||||
raw_ids = [raw_id]
|
|
||||||
|
|
||||||
safe_ids = [s.strip() for s in raw_ids if s and not str(s).strip().startswith("Y")]
|
|
||||||
if not safe_ids:
|
|
||||||
reply_text = "⚠️ Итоговый срез Y защищен от удаления. Выберите дневные снапшоты."
|
|
||||||
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=1)
|
|
||||||
return reply_text, db_get_chat_history(session_id), None
|
|
||||||
|
|
||||||
if not is_confirmed:
|
|
||||||
query_date = state_data.get("query_date", "")
|
|
||||||
db_set_session_state(session_id, "SNAPSHOT_DELETE_CONFIRM", {
|
|
||||||
"snapshot_ids": safe_ids,
|
|
||||||
"query_date": query_date,
|
|
||||||
"idle_turns": 0
|
|
||||||
})
|
|
||||||
ids_str = ", ".join(safe_ids)
|
|
||||||
reply_text = f"Вы действительно хотите удалить дневные снапшоты: {ids_str}?"
|
|
||||||
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=1)
|
|
||||||
return reply_text, db_get_chat_history(session_id), {
|
|
||||||
"type": "SNAPSHOT_DELETE_CONFIRM",
|
|
||||||
"buttons": [
|
|
||||||
{"label": f"Удалить ({len(safe_ids)} шт.)", "value": f"подтверждаю удаление снапшотов {ids_str}", "style": "danger"},
|
|
||||||
{"label": "Отмена", "value": "отмена", "style": "secondary"}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
delete_snapshots_safely(snapshot_ids=safe_ids)
|
|
||||||
query_date = state_data.get("query_date", "")
|
|
||||||
updated_data = get_snapshots_registry(date_str=query_date)
|
|
||||||
|
|
||||||
reply_text = f"✅ Успешно удалено снапшотов: {len(safe_ids)} шт."
|
|
||||||
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=1)
|
|
||||||
return reply_text, db_get_chat_history(session_id), {
|
|
||||||
"type": "SNAPSHOTS_CARD",
|
|
||||||
"data": updated_data
|
|
||||||
}
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
"""
|
|
||||||
===============================================================================
|
|
||||||
FILE: modules/ai_engine/handlers/task_handler.py
|
|
||||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
|
||||||
MODULE: modules / ai_engine / handlers
|
|
||||||
ROLE: Изолированная обработка вызовов инструментов задач (get, edit, delete, export).
|
|
||||||
|
|
||||||
AI-CONTEXT-ANCHORS:
|
|
||||||
- ANCHOR[TASK_HANDLER_DISPATCH]: Обработка вызовов db_get_tasks и db_tasks_edit.
|
|
||||||
===============================================================================
|
|
||||||
"""
|
|
||||||
|
|
||||||
from typing import Dict, Any, Tuple, Optional
|
|
||||||
from services.tasks.service import get_tasks, execute_task_action
|
|
||||||
from modules.web_api.llm.db.db_chat import db_save_chat_message, db_get_chat_history
|
|
||||||
from modules.web_api.llm.db.db_prompts import db_set_session_state
|
|
||||||
|
|
||||||
|
|
||||||
# ANCHOR[TASK_HANDLER_DISPATCH]
|
|
||||||
def handle_tasks_call(
|
|
||||||
fn_name: str,
|
|
||||||
fn_args: Dict[str, Any],
|
|
||||||
user_id: int,
|
|
||||||
session_id: str
|
|
||||||
) -> Tuple[str, list, Optional[Dict[str, Any]]]:
|
|
||||||
"""Обрабатывает нативные tool-вызовы по задачам."""
|
|
||||||
|
|
||||||
# 1. Просмотр задач
|
|
||||||
if fn_name == "db_get_tasks":
|
|
||||||
raw_tasks = get_tasks(user_id, status=fn_args.get("status"))
|
|
||||||
reply_text = "Вот интерактивный список ваших текущих задач:"
|
|
||||||
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=1)
|
|
||||||
return reply_text, db_get_chat_history(session_id), {
|
|
||||||
"type": "TASK_INTERACTIVE_CARD",
|
|
||||||
"tasks": raw_tasks
|
|
||||||
}
|
|
||||||
|
|
||||||
# 2. Модификация / Удаление / Экспорт
|
|
||||||
action = (fn_args.get("action") or "UPDATE").upper()
|
|
||||||
if fn_name == "db_add_task": action = "ADD"
|
|
||||||
elif fn_name == "db_delete_task": action = "DELETE"
|
|
||||||
elif fn_name == "db_update_task_status": action = "UPDATE"
|
|
||||||
|
|
||||||
# Двухфазное подтверждение удаления
|
|
||||||
if action == "DELETE":
|
|
||||||
task_id_raw = str(fn_args.get("task_id", "")).replace("#", "").replace("TASK-", "").strip()
|
|
||||||
db_set_session_state(session_id, "TASK_DELETE_CONFIRM", {"task_id": task_id_raw, "idle_turns": 0})
|
|
||||||
reply_text = f"Вы действительно хотите удалить задачу #{task_id_raw}?"
|
|
||||||
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=1)
|
|
||||||
return reply_text, db_get_chat_history(session_id), {
|
|
||||||
"type": "TASK_DELETE_CONFIRM",
|
|
||||||
"buttons": [
|
|
||||||
{"label": f"Удалить #{task_id_raw}", "value": f"подтверждаю удаление задачи {task_id_raw}", "style": "danger"},
|
|
||||||
{"label": "Отмена", "value": "отмена", "style": "secondary"}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
# Экспорт в Markdown
|
|
||||||
elif action == "EXPORT":
|
|
||||||
export_res = execute_task_action(
|
|
||||||
user_id=user_id,
|
|
||||||
action="EXPORT",
|
|
||||||
filename=fn_args.get("filename"),
|
|
||||||
status=fn_args.get("status")
|
|
||||||
)
|
|
||||||
reply_text = export_res.get("message", "Отчет по задачам успешно экспортирован.")
|
|
||||||
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=0)
|
|
||||||
|
|
||||||
action_payload = None
|
|
||||||
if export_res.get("status") == "success":
|
|
||||||
action_payload = {
|
|
||||||
"type": "FILE_DOWNLOAD_CARD",
|
|
||||||
"filename": export_res.get("filename"),
|
|
||||||
"download_url": export_res.get("download_url"),
|
|
||||||
"tasks_count": export_res.get("tasks_count")
|
|
||||||
}
|
|
||||||
return reply_text, db_get_chat_history(session_id), action_payload
|
|
||||||
|
|
||||||
# Создание и обновление
|
|
||||||
else:
|
|
||||||
res = execute_task_action(
|
|
||||||
user_id=user_id,
|
|
||||||
action=action,
|
|
||||||
task_id=fn_args.get("task_id"),
|
|
||||||
title=fn_args.get("title"),
|
|
||||||
priority=fn_args.get("priority", "MEDIUM"),
|
|
||||||
status=fn_args.get("status"),
|
|
||||||
module=fn_args.get("module", "general"),
|
|
||||||
due_date=fn_args.get("due_date")
|
|
||||||
)
|
|
||||||
raw_tasks = get_tasks(user_id)
|
|
||||||
reply_text = res.get("message", "Операция над задачами выполнена.")
|
|
||||||
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=1)
|
|
||||||
return reply_text, db_get_chat_history(session_id), {
|
|
||||||
"type": "TASK_INTERACTIVE_CARD",
|
|
||||||
"tasks": raw_tasks
|
|
||||||
}
|
|
||||||
+90
-195
@@ -3,19 +3,11 @@
|
|||||||
FILE: modules/web_api/llm/agent.py
|
FILE: modules/web_api/llm/agent.py
|
||||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
PROJECT: SCUD Orion AI (Unified Architecture)
|
||||||
MODULE: web_api / llm (Core Agent Coordinator)
|
MODULE: web_api / llm (Core Agent Coordinator)
|
||||||
ROLE: Нативный оркестратор диалога, диспетчер инструментов (Function Calling)
|
ROLE: Нативный оркестратор диалога, диспетчер Function Calling,
|
||||||
и управление сессионными стейтами через ContextManager.
|
передача активных срезов в контекст модели и терминальные вызовы.
|
||||||
|
|
||||||
AI-CONTEXT-ANCHORS:
|
|
||||||
- ANCHOR[AGENT_IMPORTS]: Системные и доменные импорты.
|
|
||||||
- ANCHOR[AGENT_MAIN_PIPELINE]: Основная точка входа process_chat_message.
|
|
||||||
- ANCHOR[AGENT_SYSTEM_PROMPT]: Формирование динамического контекста.
|
|
||||||
- ANCHOR[AGENT_TOOL_DISPATCHER]: Исполнение нативных вызовов инструментов.
|
|
||||||
- ANCHOR[AGENT_TOPIC_DRIFT]: Защита контекста и обработка свободных тем.
|
|
||||||
===============================================================================
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# ANCHOR[AGENT_IMPORTS]
|
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
@@ -27,7 +19,7 @@ from .db_tools import (
|
|||||||
db_apply_prompt_node_action,
|
db_apply_prompt_node_action,
|
||||||
db_get_tool_action,
|
db_get_tool_action,
|
||||||
db_get_tasks,
|
db_get_tasks,
|
||||||
db_update_task_status,
|
db_update_task_details,
|
||||||
db_delete_task,
|
db_delete_task,
|
||||||
db_add_task,
|
db_add_task,
|
||||||
db_tasks_edit,
|
db_tasks_edit,
|
||||||
@@ -70,7 +62,6 @@ if not logger.handlers:
|
|||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
|
|
||||||
|
|
||||||
# ANCHOR[AGENT_MAIN_PIPELINE]
|
|
||||||
def process_chat_message(
|
def process_chat_message(
|
||||||
user_id: int,
|
user_id: int,
|
||||||
user_message: str,
|
user_message: str,
|
||||||
@@ -90,7 +81,7 @@ def process_chat_message(
|
|||||||
|
|
||||||
full_user_content = f"{user_message}\n\n[СОДЕРЖИМОЕ ПРИКРЕПЛЕННОГО ФАЙЛА]:\n{file_context}" if file_context else user_message
|
full_user_content = f"{user_message}\n\n[СОДЕРЖИМОЕ ПРИКРЕПЛЕННОГО ФАЙЛА]:\n{file_context}" if file_context else user_message
|
||||||
|
|
||||||
# 1. Быстрый перехват строго системных кнопок UI (подтверждение превью промпта)
|
# 1. Быстрый перехват системных кнопок UI и терминальных действий без задержек LLM
|
||||||
fast_path_res = handle_fast_path_intercept(session_id, user_message, full_user_content, session_state)
|
fast_path_res = handle_fast_path_intercept(session_id, user_message, full_user_content, session_state)
|
||||||
if fast_path_res:
|
if fast_path_res:
|
||||||
return fast_path_res
|
return fast_path_res
|
||||||
@@ -99,7 +90,6 @@ def process_chat_message(
|
|||||||
db_save_chat_message(session_id, "user", full_user_content, is_ephemeral=0)
|
db_save_chat_message(session_id, "user", full_user_content, is_ephemeral=0)
|
||||||
db_history = db_get_chat_history(session_id, limit=20)
|
db_history = db_get_chat_history(session_id, limit=20)
|
||||||
|
|
||||||
# ANCHOR[AGENT_SYSTEM_PROMPT]
|
|
||||||
calendar_context = get_dynamic_calendar_context()
|
calendar_context = get_dynamic_calendar_context()
|
||||||
user_info = f"Пользователь ID={user_id}" if user_id != 0 else "Гость"
|
user_info = f"Пользователь ID={user_id}" if user_id != 0 else "Гость"
|
||||||
|
|
||||||
@@ -111,52 +101,63 @@ def process_chat_message(
|
|||||||
|
|
||||||
active_state_context = ""
|
active_state_context = ""
|
||||||
if current_state_type == "PROMPT_PREVIEW":
|
if current_state_type == "PROMPT_PREVIEW":
|
||||||
active_state_context = (
|
active_state_context = "\n[ТЕКУЩИЙ РЕЖИМ: ПРЕДПРОСМОТР СИСТЕМНОГО ПРОМПТА]\n- Открыт предпросмотр изменений промпта.\n"
|
||||||
"\n[ТЕКУЩИЙ РЕЖИМ: ПРЕДПРОСМОТР СИСТЕМНОГО ПРОМПТА]\n"
|
|
||||||
"- Открыт предпросмотр изменений промпта. Для любых правок вызывай db_prompt_node_edit.\n"
|
|
||||||
)
|
|
||||||
elif current_state_type == "PROMPT_FOLLOWUP":
|
|
||||||
active_state_context = (
|
|
||||||
"\n[ТЕКУЩИЙ РЕЖИМ: СЕССИЯ РЕДАКТИРОВАНИЯ ПРОМПТА]\n"
|
|
||||||
"- Оператор просматривает или редактирует системный промпт.\n"
|
|
||||||
"- На любые команды вида 'удали пункт X.Y' или 'удали X.Y' ТЫ ОБЯЗАН ВЫЗВАТЬ db_prompt_node_edit с action='DELETE', section_id=X, item_id=Y.\n"
|
|
||||||
"- На любые команды 'добавь пункт X.Y ...' вызывай action='ADD'.\n"
|
|
||||||
"- Запрещено путать ADD и DELETE.\n"
|
|
||||||
)
|
|
||||||
elif current_state_type == "SNAPSHOTS_VIEW":
|
elif current_state_type == "SNAPSHOTS_VIEW":
|
||||||
active_date = state_data.get("query_date", "выбранную дату")
|
active_date = state_data.get("query_date", "выбранную дату")
|
||||||
|
active_state_context = f"\n[ТЕКУЩИЙ РЕЖИМ: ПРОСМОТР СНАПШОТОВ СКУД]\n- Отображаются срезы за {active_date}.\n"
|
||||||
|
elif current_state_type == "SNAPSHOT_INSPECT":
|
||||||
|
# ⭐️ Защита от залипания: если вопрос бытовой или отвлеченный, выходим из жесткого режима инспекции
|
||||||
|
msg_l = user_message.lower().strip()
|
||||||
|
scud_terms = ["срез", "скуд", "вход", "выход", "здани", "присутств", "отсутств", "кто в", "кто сейчас", "1с", "зуп", "турникет", "карточк", "инспекци"]
|
||||||
|
if not any(t in msg_l for t in scud_terms) and len(msg_l.split()) <= 12:
|
||||||
|
db_clear_session_state(session_id)
|
||||||
|
current_state_type = None
|
||||||
|
active_state_context = ""
|
||||||
|
else:
|
||||||
|
snap_id = state_data.get("snapshot_id", "")
|
||||||
|
snap_date = state_data.get("log_date", "")
|
||||||
|
records = state_data.get("records", [])
|
||||||
|
|
||||||
|
lines = []
|
||||||
|
for r in records:
|
||||||
|
st = "Присутствовал" if r.get("is_present") else "Отсутствовал"
|
||||||
|
lines.append(f"- {r.get('fio')}: Отдел={r.get('department')}, Вход={r.get('time_in')}, Активность={r.get('first_activity')}, Выход={r.get('time_out')}, ВремяВЗдании={r.get('in_building')}, Статус={st}")
|
||||||
|
|
||||||
|
dump_str = "\n".join(lines)
|
||||||
active_state_context = (
|
active_state_context = (
|
||||||
f"\n[ТЕКУЩИЙ РЕЖИМ: ПРОСМОТР СНАПШОТОВ СКУД]\n"
|
f"\n[ТЕКУЩИЙ РЕЖИМ: АКТИВНА ИНСПЕКЦИЯ СРЕЗА СКУД #{snap_id} ЗА {snap_date}]\n"
|
||||||
f"- Сейчас на экране отображаются снапшоты за {active_date}.\n"
|
f"Оператор сейчас изучает срез #{snap_id}. При любых вопросах о сотрудниках, фильтрации по входам, выходам, времени или отделам:\n"
|
||||||
f"- Ты можешь форматировать, фильтровать или анализировать этот текущий срез.\n"
|
f"1. ТЫ ОБЯЗАН ответить обычным текстом, проанализировав список ниже.\n"
|
||||||
f"- Если оператор запрашивает ДРУГУЮ дату или день недели, отличную от {active_date} (например: 'за вчера', 'а за 13.08', 'покажи за сегодня') — "
|
f"2. КАТЕГОРИЧЕСКИ ЗАПРЕЩЕНО вызывать инструменты (tools), такие как db_get_snapshots!\n"
|
||||||
f"ТЫ ОБЯЗАН СРАЗУ ВЫЗВАТЬ db_get_snapshots(date_str=...).\n"
|
f"Список сотрудников в активном срезе:\n{dump_str}\n"
|
||||||
f"- Запрещено генерировать текст за другую дату по памяти.\n"
|
|
||||||
)
|
|
||||||
elif current_state_type == "SNAPSHOT_DELETE_CONFIRM":
|
|
||||||
active_state_context = (
|
|
||||||
"\n[ВНИМАНИЕ: ОЖИДАЕТСЯ ПОДТВЕРЖДЕНИЕ УДАЛЕНИЯ СНАПШОТОВ]\n"
|
|
||||||
)
|
|
||||||
elif current_state_type == "TASK_DELETE_CONFIRM":
|
|
||||||
active_state_context = (
|
|
||||||
"\n[ВНИМАНИЕ: ОЖИДАЕТСЯ ПОДТВЕРЖДЕНИЕ УДАЛЕНИЯ ЗАДАЧИ]\n"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# ⭐️ Промпт с поддержкой Topic Drift и защитой от переспросов по задачам
|
||||||
system_prompt_content = (
|
system_prompt_content = (
|
||||||
f"Ты — интеллектуальный ассистент SCUD Orion AI.\n"
|
f"Ты — интеллектуальный ассистент SCUD Orion AI.\n"
|
||||||
f"Ты управляешь задачами, аналитикой СКУД и системными настройками исключительно через инструменты (tools).\n\n"
|
f"Твоя основная роль — помощь оператору в кадровом аудите СКУД/1С, управлении задачами и настройками системы.\n\n"
|
||||||
f"СТРОГИЕ ПРАВИЛА:\n"
|
f"СТРОГИЕ ПРАВИЛА:\n"
|
||||||
f"1. К оператору всегда обращайся на Вы.\n"
|
f"1. К оператору всегда обращайся на Вы.\n"
|
||||||
f"2. Для получения данных всегда вызывай соответствующий инструмент:\n"
|
f"2. Для работы с данными системы ВСЕГДА вызывай соответствующие инструменты (tools):\n"
|
||||||
f" - Снапшоты и срезы логов СКУД за любые даты и дни недели -> db_get_snapshots\n"
|
f" - Снапшоты и срезы СКУД -> db_get_snapshots\n"
|
||||||
f" - Удаление снапшотов -> db_delete_snapshots\n"
|
f" - Удаление срезов -> db_delete_snapshots\n"
|
||||||
f" - Задачи и бэклог (просмотр, создание, смена статуса, удаление, экспорт) -> db_get_tasks, db_tasks_edit\n"
|
f" - Задачи и бэклог -> db_get_tasks, db_tasks_edit\n"
|
||||||
f" - Системный промпт -> db_get_system_prompt, db_prompt_node_edit\n"
|
f" - Системный промпт -> db_get_system_prompt, db_prompt_node_edit\n"
|
||||||
|
f" - База знаний и регламенты -> db_get_rules\n"
|
||||||
f" - Аномалии СКУД/1С -> db_get_anomalies\n"
|
f" - Аномалии СКУД/1С -> db_get_anomalies\n"
|
||||||
f" - База знаний -> db_get_rules\n"
|
|
||||||
f" - Статистика БД -> db_get_stats\n"
|
|
||||||
f" - Справка -> db_get_reference\n"
|
f" - Справка -> db_get_reference\n"
|
||||||
f"3. Запрещено сочинять данные от себя без вызова инструментов.\n\n"
|
f"3. ЗАДАЧИ:\n"
|
||||||
|
f" - При любых запросах на просмотр задач (включая опечатки вроде 'змдачи', 'таски', 'дела', 'покажи задачи') — "
|
||||||
|
f"ТЫ ОБЯЗАН СРАЗУ ВЫЗВАТЬ db_get_tasks без лишних вопросов!\n"
|
||||||
|
f" - КАТЕГОРИЧЕСКИ ЗАПРЕЩЕНО переспрашивать у оператора фильтры, статус или категорию задач текстом! "
|
||||||
|
f"Интерактивная карточка в интерфейсе содержит все нужные фильтры.\n"
|
||||||
|
f"4. ПРАВИЛА КОМПАНИИ: Инструмент db_get_rules вызывай СТРОГО при вопросах о внутренних регламентах, "
|
||||||
|
"политиках или локальных правилах НАШЕЙ компании (например, 'покажи правила компании', 'какие у нас регламенты'). "
|
||||||
|
"При вопросах по Трудовому кодексу РФ (ТК РФ), законам РФ или общим юридическим нормам — отвечай подробно "
|
||||||
|
"из своих профессиональных знаний, не вызывая db_get_rules!"
|
||||||
|
f"5. Запрещено выдумывать факты и цифры по системе СКУД/1С без вызова инструментов.\n"
|
||||||
|
f"6. ОБЩИЙ ДИАЛОГ: На любые отвлечённые, познавательные, научные или бытовые вопросы "
|
||||||
|
f"(расстояние между планетами или городами, программирование, кругозор) отвечай полно, доброжелательно и интересно, не отказывая пользователю.\n\n"
|
||||||
f"[СИСТЕМНЫЙ КАЛЕНДАРЬ СЕРВЕРА]\n"
|
f"[СИСТЕМНЫЙ КАЛЕНДАРЬ СЕРВЕРА]\n"
|
||||||
f"- Пользователь: {user_info}\n"
|
f"- Пользователь: {user_info}\n"
|
||||||
f"- {calendar_context}\n"
|
f"- {calendar_context}\n"
|
||||||
@@ -165,7 +166,6 @@ def process_chat_message(
|
|||||||
|
|
||||||
user_msg_object = {"role": "user", "content": full_user_content}
|
user_msg_object = {"role": "user", "content": full_user_content}
|
||||||
|
|
||||||
# ANCHOR[AGENT_TOOL_DISPATCHER]
|
|
||||||
try:
|
try:
|
||||||
if image_b64:
|
if image_b64:
|
||||||
user_msg_object["images"] = [image_b64]
|
user_msg_object["images"] = [image_b64]
|
||||||
@@ -201,7 +201,7 @@ def process_chat_message(
|
|||||||
|
|
||||||
logger.info(f"Вызов функции (Tool): {fn_name} с аргументами: {fn_args}")
|
logger.info(f"Вызов функции (Tool): {fn_name} с аргументами: {fn_args}")
|
||||||
|
|
||||||
# Ротация контекста: закрываем старую сессию инструмента
|
# Ротация контекста
|
||||||
close_tool_session_and_cleanup(session_id, close_reason=f"ACTIVATE_{fn_name}")
|
close_tool_session_and_cleanup(session_id, close_reason=f"ACTIVATE_{fn_name}")
|
||||||
session_state = None
|
session_state = None
|
||||||
mark_last_user_message_ephemeral(session_id)
|
mark_last_user_message_ephemeral(session_id)
|
||||||
@@ -217,7 +217,7 @@ def process_chat_message(
|
|||||||
}
|
}
|
||||||
|
|
||||||
# 2. Единый диспетчер задач
|
# 2. Единый диспетчер задач
|
||||||
elif fn_name in ["db_tasks_edit", "db_add_task", "db_update_task_status", "db_delete_task"]:
|
elif fn_name in ["db_tasks_edit", "db_add_task", "db_update_task_details", "db_delete_task"]:
|
||||||
action = fn_args.get("action", "UPDATE").upper()
|
action = fn_args.get("action", "UPDATE").upper()
|
||||||
if fn_name == "db_add_task": action = "ADD"
|
if fn_name == "db_add_task": action = "ADD"
|
||||||
elif fn_name == "db_delete_task": action = "DELETE"
|
elif fn_name == "db_delete_task": action = "DELETE"
|
||||||
@@ -239,7 +239,7 @@ def process_chat_message(
|
|||||||
elif action == "EXPORT":
|
elif action == "EXPORT":
|
||||||
export_res = db_export_tasks_markdown(
|
export_res = db_export_tasks_markdown(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
filename=fn_args.get("filename"),
|
filename=fn_args.get("filename") or "ROADMAP.md",
|
||||||
status_filter=fn_args.get("status")
|
status_filter=fn_args.get("status")
|
||||||
)
|
)
|
||||||
reply_text = export_res.get("message", "Отчет по задачам успешно экспортирован.")
|
reply_text = export_res.get("message", "Отчет по задачам успешно экспортирован.")
|
||||||
@@ -277,96 +277,46 @@ def process_chat_message(
|
|||||||
# 3. Системный промпт
|
# 3. Системный промпт
|
||||||
elif fn_name == "db_get_system_prompt":
|
elif fn_name == "db_get_system_prompt":
|
||||||
active_prompt = db_get_active_system_prompt()
|
active_prompt = db_get_active_system_prompt()
|
||||||
reply_text = f"Актуальный системный промпт:\n\n{active_prompt}"
|
reply_text = (
|
||||||
|
"📋 **АКТУАЛЬНЫЙ СИСТЕМНЫЙ ПРОМПТ ИЗ БАЗЫ ДАННЫХ:**\n\n"
|
||||||
|
f"```text\n{active_prompt}\n```\n\n"
|
||||||
|
"Вы можете добавить, отредактировать или удалить любой пункт."
|
||||||
|
)
|
||||||
db_set_session_state(session_id, "PROMPT_FOLLOWUP", {"idle_turns": 0})
|
db_set_session_state(session_id, "PROMPT_FOLLOWUP", {"idle_turns": 0})
|
||||||
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=1)
|
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=1)
|
||||||
return reply_text, db_get_chat_history(session_id), None
|
return reply_text, db_get_chat_history(session_id), {
|
||||||
|
"type": "PROMPT_VIEW",
|
||||||
elif fn_name == "db_prompt_node_edit":
|
|
||||||
action = str(fn_args.get("action", "ADD")).upper()
|
|
||||||
|
|
||||||
try:
|
|
||||||
sec_id = int(str(fn_args.get("section_id", 3)).strip())
|
|
||||||
except Exception:
|
|
||||||
sec_id = 3
|
|
||||||
|
|
||||||
try:
|
|
||||||
itm_id = int(str(fn_args.get("item_id", 1)).strip())
|
|
||||||
except Exception:
|
|
||||||
itm_id = 1
|
|
||||||
|
|
||||||
content = str(fn_args.get("content", "")).strip()
|
|
||||||
baseline_prompt = db_get_active_system_prompt()
|
|
||||||
|
|
||||||
with get_db_connection() as temp_conn:
|
|
||||||
temp_cursor = temp_conn.cursor()
|
|
||||||
temp_cursor.execute("""
|
|
||||||
SELECT section_id, item_id, content
|
|
||||||
FROM system_prompt_nodes
|
|
||||||
WHERE prompt_name = 'main_agent' AND is_active = 1
|
|
||||||
ORDER BY section_id, item_id
|
|
||||||
""")
|
|
||||||
existing_nodes = temp_cursor.fetchall()
|
|
||||||
|
|
||||||
nodes_dict = {(sec, itm): txt for sec, itm, txt in existing_nodes}
|
|
||||||
nodes_dict_for_draft = {k: v for k, v in nodes_dict.items() if k != (sec_id, itm_id)} if action == "DELETE" else dict(nodes_dict)
|
|
||||||
if action != "DELETE":
|
|
||||||
nodes_dict_for_draft[(sec_id, itm_id)] = content
|
|
||||||
|
|
||||||
draft_lines = []
|
|
||||||
curr_sec = None
|
|
||||||
for (s_id, i_id), txt in sorted(nodes_dict_for_draft.items()):
|
|
||||||
if i_id == 0:
|
|
||||||
if curr_sec is not None: draft_lines.append("")
|
|
||||||
draft_lines.append(f"{s_id}. {txt}")
|
|
||||||
curr_sec = s_id
|
|
||||||
else:
|
|
||||||
draft_lines.append(f" {s_id}.{i_id}. {txt}")
|
|
||||||
merged_prompt = "\n".join(draft_lines)
|
|
||||||
|
|
||||||
diff_lines = []
|
|
||||||
curr_sec = None
|
|
||||||
display_nodes = dict(nodes_dict)
|
|
||||||
if action != "DELETE":
|
|
||||||
display_nodes[(sec_id, itm_id)] = content
|
|
||||||
|
|
||||||
for (s_id, i_id), txt in sorted(display_nodes.items()):
|
|
||||||
if i_id == 0:
|
|
||||||
if curr_sec is not None: diff_lines.append("")
|
|
||||||
diff_lines.append(f"{s_id}. {txt}")
|
|
||||||
curr_sec = s_id
|
|
||||||
else:
|
|
||||||
if s_id == sec_id and i_id == itm_id:
|
|
||||||
line_str = f' <span class="line-through text-rose-600 font-bold bg-rose-50 px-1.5 py-0.5 rounded border border-rose-300 opacity-80">{s_id}.{i_id}. {txt} [УДАЛЕНИЕ]</span>' if action == "DELETE" else f' <span class="text-rose-600 font-bold bg-rose-50 px-1.5 py-0.5 rounded border border-rose-300">{s_id}.{i_id}. {txt}</span>'
|
|
||||||
else:
|
|
||||||
line_str = f" {s_id}.{i_id}. {txt}"
|
|
||||||
diff_lines.append(line_str)
|
|
||||||
|
|
||||||
diff_html = "\n".join(diff_lines)
|
|
||||||
|
|
||||||
db_set_session_state(session_id, "PROMPT_PREVIEW", {
|
|
||||||
"draft_text": merged_prompt,
|
|
||||||
"action": action,
|
|
||||||
"section_id": sec_id,
|
|
||||||
"item_id": itm_id,
|
|
||||||
"content": content,
|
|
||||||
"idle_turns": 0
|
|
||||||
})
|
|
||||||
|
|
||||||
preview_reply = f"Предпросмотр изменений системного промпта:\n\n{diff_html}\n\nДля применения подтвердите действие, отредактируйте или отмените."
|
|
||||||
db_save_chat_message(session_id, "assistant", preview_reply, is_ephemeral=1)
|
|
||||||
return preview_reply, db_get_chat_history(session_id), {
|
|
||||||
"type": "PROMPT_PREVIEW",
|
|
||||||
"raw_draft": merged_prompt,
|
|
||||||
"baseline_prompt": baseline_prompt,
|
|
||||||
"buttons": [
|
"buttons": [
|
||||||
{"label": "Подтвердить", "value": "подтверждаю", "style": "primary"},
|
{"label": "✏️ Редактировать промпт", "value": "action:open_editor", "style": "primary"},
|
||||||
{"label": "Отменить", "value": "отмена", "style": "danger"},
|
{"label": "База знаний", "value": "покажи правила компании", "style": "secondary"}
|
||||||
{"label": "✏️ Редактировать", "value": "action:open_editor", "style": "secondary"}
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
# 4. Снапшоты СКУД
|
# 4. База знаний и правила компании (терминальный возврат)
|
||||||
|
elif fn_name == "db_get_rules":
|
||||||
|
rules_data = db_get_rules()
|
||||||
|
if isinstance(rules_data, dict) and "rules" in rules_data:
|
||||||
|
rules_list = rules_data["rules"]
|
||||||
|
elif isinstance(rules_data, list):
|
||||||
|
rules_list = rules_data
|
||||||
|
else:
|
||||||
|
rules_list = [str(rules_data)]
|
||||||
|
|
||||||
|
formatted_rules = "\n\n".join([f"{i+1}. {r.get('rule_text', r) if isinstance(r, dict) else r}" for i, r in enumerate(rules_list)])
|
||||||
|
reply_text = (
|
||||||
|
"📖 **БАЗА ЗНАНИЙ И ПРАВИЛА КОМПАНИИ (КАДРОВЫЙ АРБИТРАЖ):**\n\n"
|
||||||
|
f"```text\n{formatted_rules}\n```"
|
||||||
|
)
|
||||||
|
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=1)
|
||||||
|
return reply_text, db_get_chat_history(session_id), {
|
||||||
|
"type": "RULES_VIEW",
|
||||||
|
"buttons": [
|
||||||
|
{"label": "✏️ Редактировать правила", "value": "action:open_rules_editor", "style": "primary"},
|
||||||
|
{"label": "📋 Системный промпт", "value": "покажи системный промпт", "style": "secondary"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
# 5. Снапшоты СКУД
|
||||||
elif fn_name == "db_get_snapshots":
|
elif fn_name == "db_get_snapshots":
|
||||||
snapshots_res = db_get_snapshots(session_id=session_id, date_str=fn_args.get("date_str"), original_user_message=user_message)
|
snapshots_res = db_get_snapshots(session_id=session_id, date_str=fn_args.get("date_str"), original_user_message=user_message)
|
||||||
query_date = snapshots_res.get("query_date", "выбранную дату")
|
query_date = snapshots_res.get("query_date", "выбранную дату")
|
||||||
@@ -423,40 +373,13 @@ def process_chat_message(
|
|||||||
"data": updated_snapshots_res
|
"data": updated_snapshots_res
|
||||||
}
|
}
|
||||||
|
|
||||||
elif fn_name == "db_delete_snapshots":
|
# 6. Прочие сервисные инструменты
|
||||||
raw_id = fn_args.get("snapshot_id") or fn_args.get("day_str")
|
|
||||||
raw_ids = fn_args.get("snapshot_ids") or ([raw_id] if raw_id else [])
|
|
||||||
|
|
||||||
# Защита: итоговые Y-срезы удалять запрещено
|
|
||||||
safe_ids = [s for s in raw_ids if not str(s).startswith("Y")]
|
|
||||||
if not safe_ids:
|
|
||||||
reply_text = "⚠️ Итоговый срез Y защищен от удаления. Выберите дневные снапшоты."
|
|
||||||
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=1)
|
|
||||||
return reply_text, db_get_chat_history(session_id), None
|
|
||||||
|
|
||||||
# Прямое выполнение удаления в SQLite без лишних текстовых подтверждений
|
|
||||||
del_res = db_delete_snapshots(snapshot_ids=safe_ids)
|
|
||||||
|
|
||||||
# Получаем свежий реестр за ту же дату
|
|
||||||
query_date = fn_args.get("day_str", "")
|
|
||||||
updated_snapshots_res = db_get_snapshots(session_id=session_id, date_str=query_date)
|
|
||||||
|
|
||||||
reply_text = f"✅ Успешно удалено снапшотов: {len(safe_ids)} шт."
|
|
||||||
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=1)
|
|
||||||
return reply_text, db_get_chat_history(session_id), {
|
|
||||||
"type": "SNAPSHOTS_CARD",
|
|
||||||
"data": updated_snapshots_res
|
|
||||||
}
|
|
||||||
|
|
||||||
# 5. Сервисные запросы
|
|
||||||
elif fn_name == "db_get_current_server_time":
|
elif fn_name == "db_get_current_server_time":
|
||||||
tool_result_content = json.dumps(db_get_current_server_time(), ensure_ascii=False)
|
tool_result_content = json.dumps(db_get_current_server_time(), ensure_ascii=False)
|
||||||
elif fn_name == "db_get_stats":
|
elif fn_name == "db_get_stats":
|
||||||
tool_result_content = json.dumps(db_get_stats(), ensure_ascii=False)
|
tool_result_content = json.dumps(db_get_stats(), ensure_ascii=False)
|
||||||
elif fn_name == "db_get_anomalies":
|
elif fn_name == "db_get_anomalies":
|
||||||
tool_result_content = json.dumps(db_get_anomalies(limit=fn_args.get("limit", 100), date_str=fn_args.get("date_str")), ensure_ascii=False)
|
tool_result_content = json.dumps(db_get_anomalies(limit=fn_args.get("limit", 100), date_str=fn_args.get("date_str")), ensure_ascii=False)
|
||||||
elif fn_name == "db_get_rules":
|
|
||||||
tool_result_content = json.dumps(db_get_rules(), ensure_ascii=False)
|
|
||||||
elif fn_name == "db_get_reference":
|
elif fn_name == "db_get_reference":
|
||||||
tool_result_content = json.dumps(db_get_reference(category=fn_args.get("category")), ensure_ascii=False)
|
tool_result_content = json.dumps(db_get_reference(category=fn_args.get("category")), ensure_ascii=False)
|
||||||
else:
|
else:
|
||||||
@@ -468,43 +391,15 @@ def process_chat_message(
|
|||||||
raw_content = sec_msg.get("content", "").strip().replace("**", "").replace("*", "")
|
raw_content = sec_msg.get("content", "").strip().replace("**", "").replace("*", "")
|
||||||
final_content = clean_raw_tool_tags(clean_output(raw_content)) or "Запрос выполнен."
|
final_content = clean_raw_tool_tags(clean_output(raw_content)) or "Запрос выполнен."
|
||||||
|
|
||||||
for artifact in ["почемучка,", "почемучка!", "почемучка?", "почемучка", "почемучто", "почему-то"]:
|
db_save_chat_message(session_id, "assistant", final_content, is_ephemeral=0)
|
||||||
if final_content.lower().startswith(artifact):
|
|
||||||
final_content = final_content[len(artifact):].lstrip(",.!?:; -")
|
|
||||||
|
|
||||||
is_ephem = 1 if fn_name in ["db_get_snapshots", "db_get_system_prompt", "db_prompt_node_edit"] else 0
|
|
||||||
db_save_chat_message(session_id, "assistant", final_content, is_ephemeral=is_ephem)
|
|
||||||
return final_content, db_get_chat_history(session_id), None
|
return final_content, db_get_chat_history(session_id), None
|
||||||
|
|
||||||
# ANCHOR[AGENT_TOPIC_DRIFT]
|
# Свободный диалог (Topic Drift)
|
||||||
raw_str = msg.get("content", "").strip().replace("**", "").replace("*", "")
|
raw_str = msg.get("content", "").strip().replace("**", "").replace("*", "")
|
||||||
final_reply = clean_raw_tool_tags(clean_output(raw_str)) or "Запрос обработан."
|
final_reply = clean_raw_tool_tags(clean_output(raw_str)) or "Запрос обработан."
|
||||||
|
|
||||||
for artifact in ["почемучка,", "почемучка!", "почемучка?", "почемучка", "почемучто", "почему-то"]:
|
|
||||||
if final_reply.lower().startswith(artifact):
|
|
||||||
final_reply = final_reply[len(artifact):].lstrip(",.!?:; -")
|
|
||||||
|
|
||||||
action_payload = None
|
|
||||||
|
|
||||||
if session_state and session_state.get("state_type") in ["PROMPT_FOLLOWUP", "PROMPT_PREVIEW"]:
|
|
||||||
idle_turns += 1
|
|
||||||
if idle_turns > 3:
|
|
||||||
db_clear_session_state(session_id)
|
|
||||||
db_purge_ephemeral_messages(session_id)
|
|
||||||
action_payload = None
|
|
||||||
session_state = None
|
|
||||||
elif idle_turns == 3:
|
|
||||||
tool_action = db_get_tool_action("db_confirm_prompt_preview")
|
|
||||||
guard_question = tool_action.get("follow_up_question", "Желаете продолжить работу с системным промптом?") if tool_action else "Желаете продолжить работу с системным промптом?"
|
|
||||||
buttons = tool_action.get("buttons", []) if tool_action else []
|
|
||||||
final_reply += f"\n\n💡 *Напоминание:* {guard_question}"
|
|
||||||
action_payload = {"type": "PROMPT_FOLLOWUP", "buttons": buttons}
|
|
||||||
db_set_session_state(session_id, "PROMPT_FOLLOWUP", {"idle_turns": idle_turns})
|
|
||||||
else:
|
|
||||||
db_set_session_state(session_id, session_state.get("state_type"), {"idle_turns": idle_turns})
|
|
||||||
|
|
||||||
db_save_chat_message(session_id, "assistant", final_reply, is_ephemeral=0)
|
db_save_chat_message(session_id, "assistant", final_reply, is_ephemeral=0)
|
||||||
return final_reply, db_get_chat_history(session_id), action_payload
|
return final_reply, db_get_chat_history(session_id), None
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logger.exception(f"Непредвиденная ошибка агента: {ex}")
|
logger.exception(f"Непредвиденная ошибка агента: {ex}")
|
||||||
|
|||||||
@@ -3,42 +3,247 @@
|
|||||||
FILE: modules/web_api/llm/core/fast_path.py
|
FILE: modules/web_api/llm/core/fast_path.py
|
||||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
PROJECT: SCUD Orion AI (Unified Architecture)
|
||||||
MODULE: web_api / llm / core
|
MODULE: web_api / llm / core
|
||||||
ROLE: Детерминированный мгновенный перехват нажатий кнопок подтверждения
|
ROLE: Мгновенный перехват UI-действий, инспекции срезов, экспорта и Diff-превью.
|
||||||
(без задержек LLM и обращения к Ollama).
|
|
||||||
|
|
||||||
AI-CONTEXT-ANCHORS:
|
|
||||||
- ANCHOR[FAST_PATH_MAIN]: Точка входа handle_fast_path_intercept.
|
|
||||||
===============================================================================
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
import difflib
|
||||||
import logging
|
import logging
|
||||||
from typing import Dict, Any, Tuple, Optional
|
from typing import Dict, Any, Tuple, Optional
|
||||||
|
|
||||||
# Прямые вызовы чистых доменных сервисов
|
|
||||||
from services.prompts.service import save_full_prompt_draft, apply_prompt_action, get_active_system_prompt
|
from services.prompts.service import save_full_prompt_draft, apply_prompt_action, get_active_system_prompt
|
||||||
from services.tasks.service import get_tasks, delete_task
|
from services.knowledge.service import get_rules, add_rule
|
||||||
|
from services.tasks.service import get_tasks, delete_task, execute_task_action
|
||||||
from services.snapshots.service import get_snapshots_registry, delete_snapshots_safely
|
from services.snapshots.service import get_snapshots_registry, delete_snapshots_safely
|
||||||
|
|
||||||
# Чат и сессии
|
|
||||||
from modules.web_api.llm.db.db_chat import db_save_chat_message, db_get_chat_history, db_purge_ephemeral_messages
|
from modules.web_api.llm.db.db_chat import db_save_chat_message, db_get_chat_history, db_purge_ephemeral_messages
|
||||||
from modules.web_api.llm.db.db_prompts import db_set_session_state, db_clear_session_state, db_get_tool_action
|
from modules.web_api.llm.db.db_prompts import db_set_session_state, db_clear_session_state
|
||||||
from modules.web_api.llm.core.context_manager import close_tool_session_and_cleanup
|
from modules.web_api.llm.core.context_manager import close_tool_session_and_cleanup
|
||||||
|
|
||||||
logger = logging.getLogger("FAST_PATH")
|
logger = logging.getLogger("FAST_PATH")
|
||||||
|
|
||||||
|
|
||||||
# ANCHOR[FAST_PATH_MAIN]
|
def _generate_prompt_diff_html(baseline_text: str, draft_text: str) -> str:
|
||||||
|
base_lines = [line.rstrip() for line in baseline_text.strip().splitlines()]
|
||||||
|
draft_lines = [line.rstrip() for line in draft_text.strip().splitlines()]
|
||||||
|
|
||||||
|
matcher = difflib.SequenceMatcher(None, base_lines, draft_lines)
|
||||||
|
diff_html_lines = []
|
||||||
|
|
||||||
|
for tag, i1, i2, j1, j2 in matcher.get_opcodes():
|
||||||
|
if tag == 'equal':
|
||||||
|
for line in base_lines[i1:i2]:
|
||||||
|
diff_html_lines.append(line)
|
||||||
|
elif tag == 'delete':
|
||||||
|
for line in base_lines[i1:i2]:
|
||||||
|
diff_html_lines.append(
|
||||||
|
f'<span class="line-through text-rose-600 font-bold bg-rose-50 px-1.5 py-0.5 rounded border border-rose-300 opacity-80">{line} [УДАЛЕНИЕ]</span>'
|
||||||
|
)
|
||||||
|
elif tag == 'insert':
|
||||||
|
for line in draft_lines[j1:j2]:
|
||||||
|
diff_html_lines.append(
|
||||||
|
f'<span class="text-rose-600 font-bold bg-rose-50 px-1.5 py-0.5 rounded border border-rose-300">{line}</span>'
|
||||||
|
)
|
||||||
|
elif tag == 'replace':
|
||||||
|
for line in base_lines[i1:i2]:
|
||||||
|
diff_html_lines.append(
|
||||||
|
f'<span class="line-through text-rose-600 font-bold bg-rose-50 px-1.5 py-0.5 rounded border border-rose-300 opacity-80">{line} [УДАЛЕНИЕ]</span>'
|
||||||
|
)
|
||||||
|
for line in draft_lines[j1:j2]:
|
||||||
|
diff_html_lines.append(
|
||||||
|
f'<span class="text-rose-600 font-bold bg-rose-50 px-1.5 py-0.5 rounded border border-rose-300">{line}</span>'
|
||||||
|
)
|
||||||
|
|
||||||
|
return "\n".join(diff_html_lines)
|
||||||
|
|
||||||
|
|
||||||
def handle_fast_path_intercept(
|
def handle_fast_path_intercept(
|
||||||
session_id: str,
|
session_id: str,
|
||||||
user_message: str,
|
user_message: str,
|
||||||
full_user_content: str,
|
full_user_content: str,
|
||||||
session_state: Optional[Dict[str, Any]]
|
session_state: Optional[Dict[str, Any]]
|
||||||
) -> Optional[Tuple[str, list, Optional[Dict[str, Any]]]]:
|
) -> Optional[Tuple[str, list, Optional[Dict[str, Any]]]]:
|
||||||
"""
|
msg_raw = user_message.strip()
|
||||||
Мгновенный перехват нажатий кнопок подтверждения (Fast-Path).
|
msg_lower = msg_raw.lower()
|
||||||
Возвращает (reply, history, action_type) или None, если требуется передать управление LLM.
|
|
||||||
"""
|
# -------------------------------------------------------------------------
|
||||||
|
# 0.0 ЭКСПОРТ ЗАДАЧ В ФАЙЛ (МГНОВЕННО, БЕЗ ЛИШНИХ ДИАЛОГОВ)
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
if any(msg_lower.startswith(p) for p in ["экспортируй задачи", "экспорт задач", "выгрузи задачи", "скачать задачи"]):
|
||||||
|
filename = "ROADMAP.md"
|
||||||
|
if " в " in msg_lower:
|
||||||
|
parts = msg_raw.split(" в ", 1)[1].strip().split()
|
||||||
|
if parts and ("." in parts[0] or parts[0].endswith("md")):
|
||||||
|
filename = parts[0]
|
||||||
|
|
||||||
|
res = execute_task_action(user_id=1, action="EXPORT", filename=filename)
|
||||||
|
reply = res.get("message", f"Отчет по задачам сформирован в `{filename}`.")
|
||||||
|
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=0)
|
||||||
|
|
||||||
|
action_payload = {
|
||||||
|
"type": "FILE_DOWNLOAD_CARD",
|
||||||
|
"filename": res.get("filename", filename),
|
||||||
|
"download_url": res.get("download_url", "#"),
|
||||||
|
"tasks_count": res.get("tasks_count", 0)
|
||||||
|
}
|
||||||
|
return reply, db_get_chat_history(session_id), action_payload
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
# 0.1 ИНСПЕКЦИЯ КОНКРЕТНОГО СРЕЗА СКУД (ПО ID СНАПШОТА)
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
if msg_lower.startswith("покажи срез ") or msg_lower.startswith("инспекция среза "):
|
||||||
|
target_snap_id = msg_raw.split()[-1].replace("#", "").strip()
|
||||||
|
from core.database import load_scud_from_db_by_snapshot
|
||||||
|
|
||||||
|
df_snap = load_scud_from_db_by_snapshot(date_str="", snapshot_param=target_snap_id)
|
||||||
|
if df_snap is None or df_snap.empty:
|
||||||
|
reply = f"⚠️ Срез СКУД `#{target_snap_id}` не найден в базе данных."
|
||||||
|
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=1)
|
||||||
|
return reply, db_get_chat_history(session_id), None
|
||||||
|
|
||||||
|
total_cnt = len(df_snap)
|
||||||
|
present_cnt = len(df_snap[df_snap['Пришел'] == True]) if 'Пришел' in df_snap.columns else 0
|
||||||
|
absent_cnt = total_cnt - present_cnt
|
||||||
|
|
||||||
|
snap_time = df_snap['snapshot_time'].iloc[0] if 'snapshot_time' in df_snap.columns else '—'
|
||||||
|
log_date = df_snap['log_date'].iloc[0] if 'log_date' in df_snap.columns else '—'
|
||||||
|
|
||||||
|
rows_list = []
|
||||||
|
for _, r in df_snap.iterrows():
|
||||||
|
rows_list.append({
|
||||||
|
"fio": r.get('Сотрудник', r.get('fio', '')),
|
||||||
|
"department": r.get('Подразделение', r.get('department_scud', '—')),
|
||||||
|
"time_in": r.get('Начало_дня', 'Нет входа'),
|
||||||
|
"first_activity": r.get('Первая_активность', '—'),
|
||||||
|
"time_out": r.get('Конец_дня', 'Нет выхода'),
|
||||||
|
"in_building": r.get('Находился_в_здании', '00:00'),
|
||||||
|
"is_present": bool(r.get('Пришел', False)),
|
||||||
|
"anomaly": r.get('anomaly_flag', 'NONE')
|
||||||
|
})
|
||||||
|
|
||||||
|
# Фиксация среза в памяти сессии для последующих вопросов к LLM
|
||||||
|
db_set_session_state(session_id, "SNAPSHOT_INSPECT", {
|
||||||
|
"snapshot_id": target_snap_id,
|
||||||
|
"log_date": log_date,
|
||||||
|
"snapshot_time": snap_time,
|
||||||
|
"records": rows_list,
|
||||||
|
"idle_turns": 0
|
||||||
|
})
|
||||||
|
|
||||||
|
reply = f"🔍 **Инспекция среза #{target_snap_id}** (Дата: {log_date}, Время: {snap_time}). Всего записей: {total_cnt} (Присутствовали: {present_cnt}, Отсутствовали: {absent_cnt})."
|
||||||
|
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=1)
|
||||||
|
|
||||||
|
return reply, db_get_chat_history(session_id), {
|
||||||
|
"type": "SNAPSHOT_INSPECT_CARD",
|
||||||
|
"snapshot_id": target_snap_id,
|
||||||
|
"log_date": log_date,
|
||||||
|
"snapshot_time": snap_time,
|
||||||
|
"total_count": total_cnt,
|
||||||
|
"present_count": present_cnt,
|
||||||
|
"absent_count": absent_cnt,
|
||||||
|
"records": rows_list
|
||||||
|
}
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
# 0.2 ОТКРЫТИЕ ИНЛАЙН-РЕДАКТОРА ПРОМПТА
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
if msg_lower in ["action:open_editor", "редактировать промпт", "открыть редактор"]:
|
||||||
|
active_prompt = get_active_system_prompt()
|
||||||
|
state_data = session_state.get("data_json") if session_state else {}
|
||||||
|
draft_text = state_data.get("draft_text") if isinstance(state_data, dict) and state_data.get("draft_text") else active_prompt
|
||||||
|
|
||||||
|
db_set_session_state(session_id, "PROMPT_PREVIEW", {
|
||||||
|
"draft_text": draft_text,
|
||||||
|
"action": "MANUAL_EDIT",
|
||||||
|
"idle_turns": 0
|
||||||
|
})
|
||||||
|
reply = "✏️ Внесите необходимые изменения в текст промпта и нажмите «Показать превью изменений»:"
|
||||||
|
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=1)
|
||||||
|
return reply, db_get_chat_history(session_id), {
|
||||||
|
"type": "PROMPT_EDITOR",
|
||||||
|
"raw_draft": draft_text,
|
||||||
|
"baseline_prompt": active_prompt
|
||||||
|
}
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
# 0.3 ОТКРЫТИЕ ИНЛАЙН-РЕДАКТОРА ПРАВИЛ
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
if msg_lower in ["action:open_rules_editor", "редактировать правила", "изменить правила"]:
|
||||||
|
rules_data = get_rules()
|
||||||
|
rules_list = rules_data.get("rules", []) if isinstance(rules_data, dict) else (rules_data if isinstance(rules_data, list) else [str(rules_data)])
|
||||||
|
raw_rules_text = "\n".join([f"{i+1}. {r.get('rule_text', r) if isinstance(r, dict) else r}" for i, r in enumerate(rules_list)])
|
||||||
|
|
||||||
|
db_set_session_state(session_id, "RULES_PREVIEW", {
|
||||||
|
"draft_text": raw_rules_text,
|
||||||
|
"action": "MANUAL_EDIT_RULES",
|
||||||
|
"idle_turns": 0
|
||||||
|
})
|
||||||
|
reply = "✏️ Редактор базы знаний и правил компании. Внесите изменения и нажмите «Показать превью изменений»:"
|
||||||
|
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=1)
|
||||||
|
return reply, db_get_chat_history(session_id), {
|
||||||
|
"type": "RULES_EDITOR",
|
||||||
|
"raw_draft": raw_rules_text,
|
||||||
|
"baseline_prompt": raw_rules_text
|
||||||
|
}
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
# 0.4 ПОСТУПЛЕНИЕ ДРАФТА ПРОМПТА -> DIFF-ПРЕВЬЮ
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
if msg_raw.startswith("action:save_draft_prompt:::"):
|
||||||
|
new_draft_content = msg_raw.replace("action:save_draft_prompt:::", "").strip()
|
||||||
|
active_prompt = get_active_system_prompt()
|
||||||
|
diff_html = _generate_prompt_diff_html(active_prompt, new_draft_content)
|
||||||
|
|
||||||
|
db_set_session_state(session_id, "PROMPT_PREVIEW", {
|
||||||
|
"draft_text": new_draft_content,
|
||||||
|
"action": "MANUAL_EDIT",
|
||||||
|
"idle_turns": 0
|
||||||
|
})
|
||||||
|
|
||||||
|
preview_reply = f"Предпросмотр изменений системного промпта:\n\n{diff_html}\n\nДля применения подтвердите действие, отредактируйте или отмените."
|
||||||
|
db_save_chat_message(session_id, "assistant", preview_reply, is_ephemeral=1)
|
||||||
|
return preview_reply, db_get_chat_history(session_id), {
|
||||||
|
"type": "PROMPT_PREVIEW",
|
||||||
|
"raw_draft": new_draft_content,
|
||||||
|
"baseline_prompt": active_prompt,
|
||||||
|
"diff_html": diff_html,
|
||||||
|
"buttons": [
|
||||||
|
{"label": "Подтвердить", "value": "подтверждаю", "style": "primary"},
|
||||||
|
{"label": "Отменить", "value": "отмена", "style": "danger"},
|
||||||
|
{"label": "✏️ Редактировать", "value": "action:open_editor", "style": "secondary"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
# 0.5 ПОСТУПЛЕНИЕ ДРАФТА ПРАВИЛ -> DIFF-ПРЕВЬЮ
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
if msg_raw.startswith("action:save_draft_rules:::"):
|
||||||
|
new_draft_content = msg_raw.replace("action:save_draft_rules:::", "").strip()
|
||||||
|
rules_data = get_rules()
|
||||||
|
rules_list = rules_data.get("rules", []) if isinstance(rules_data, dict) else (rules_data if isinstance(rules_data, list) else [str(rules_data)])
|
||||||
|
baseline_rules = "\n".join([f"{i+1}. {r.get('rule_text', r) if isinstance(r, dict) else r}" for i, r in enumerate(rules_list)])
|
||||||
|
diff_html = _generate_prompt_diff_html(baseline_rules, new_draft_content)
|
||||||
|
|
||||||
|
db_set_session_state(session_id, "RULES_PREVIEW", {
|
||||||
|
"draft_text": new_draft_content,
|
||||||
|
"action": "MANUAL_EDIT_RULES",
|
||||||
|
"idle_turns": 0
|
||||||
|
})
|
||||||
|
|
||||||
|
preview_reply = f"Предпросмотр изменений базы знаний компании:\n\n{diff_html}\n\nПодтвердите сохранение или отмените действие."
|
||||||
|
db_save_chat_message(session_id, "assistant", preview_reply, is_ephemeral=1)
|
||||||
|
return preview_reply, db_get_chat_history(session_id), {
|
||||||
|
"type": "PROMPT_PREVIEW",
|
||||||
|
"raw_draft": new_draft_content,
|
||||||
|
"baseline_prompt": baseline_rules,
|
||||||
|
"diff_html": diff_html,
|
||||||
|
"buttons": [
|
||||||
|
{"label": "Подтвердить", "value": "подтверждаю сохранение правил", "style": "primary"},
|
||||||
|
{"label": "Отменить", "value": "отмена", "style": "danger"},
|
||||||
|
{"label": "✏️ Редактировать", "value": "action:open_rules_editor", "style": "secondary"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
if not session_state:
|
if not session_state:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -47,38 +252,22 @@ def handle_fast_path_intercept(
|
|||||||
if not isinstance(state_data, dict):
|
if not isinstance(state_data, dict):
|
||||||
state_data = {}
|
state_data = {}
|
||||||
|
|
||||||
msg_lower = user_message.strip().lower()
|
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
# 1. ПОДТВЕРЖДЕНИЕ ПРЕВЬЮ СИСТЕМНОГО ПРОМПТА
|
# 1. ПОДТВЕРЖДЕНИЕ ПРОМПТА
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
if state_type == "PROMPT_PREVIEW":
|
if state_type == "PROMPT_PREVIEW":
|
||||||
is_confirm = msg_lower in ["подтверждаю", "да", "сохраняй", "применить", "ок", "подтвердить"]
|
is_confirm = msg_lower in ["подтверждаю", "да", "сохраняй", "применить", "ок", "подтвердить"]
|
||||||
is_cancel = msg_lower in ["отмена", "отменить", "нет", "отклонить"]
|
is_cancel = msg_lower in ["отмена", "отменить", "нет", "отклонить"]
|
||||||
|
|
||||||
if is_confirm:
|
if is_confirm:
|
||||||
action = state_data.get("action", "MANUAL_EDIT")
|
|
||||||
draft_text = state_data.get("draft_text", "")
|
draft_text = state_data.get("draft_text", "")
|
||||||
|
if draft_text:
|
||||||
# Применение изменений
|
|
||||||
if action == "MANUAL_EDIT" and draft_text:
|
|
||||||
save_full_prompt_draft(draft_text, prompt_name="main_agent")
|
|
||||||
else:
|
|
||||||
sec_id = state_data.get("section_id")
|
|
||||||
itm_id = state_data.get("item_id")
|
|
||||||
content = state_data.get("content", "")
|
|
||||||
if sec_id is not None and itm_id is not None:
|
|
||||||
apply_prompt_action(action=action, section_id=sec_id, item_id=itm_id, content=content)
|
|
||||||
elif draft_text:
|
|
||||||
save_full_prompt_draft(draft_text, prompt_name="main_agent")
|
save_full_prompt_draft(draft_text, prompt_name="main_agent")
|
||||||
|
|
||||||
# Закрытие сессии и зачистка
|
|
||||||
close_tool_session_and_cleanup(session_id, close_reason="PROMPT_APPLIED_SUCCESSFULLY")
|
close_tool_session_and_cleanup(session_id, close_reason="PROMPT_APPLIED_SUCCESSFULLY")
|
||||||
db_clear_session_state(session_id)
|
db_clear_session_state(session_id)
|
||||||
|
|
||||||
tool_action = db_get_tool_action("db_confirm_prompt_preview")
|
reply = "✅ Системный промпт успешно сохранен и применен в базе данных."
|
||||||
reply = tool_action.get("success_template", "✅ Системный промпт успешно сохранен и применен в базе данных.") if tool_action else "✅ Системный промпт успешно сохранен и применен в базе данных."
|
|
||||||
|
|
||||||
db_save_chat_message(session_id, "user", full_user_content, is_ephemeral=0)
|
db_save_chat_message(session_id, "user", full_user_content, is_ephemeral=0)
|
||||||
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=0)
|
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=0)
|
||||||
return reply, db_get_chat_history(session_id), None
|
return reply, db_get_chat_history(session_id), None
|
||||||
@@ -87,80 +276,40 @@ def handle_fast_path_intercept(
|
|||||||
close_tool_session_and_cleanup(session_id, close_reason="PROMPT_EDIT_CANCELLED")
|
close_tool_session_and_cleanup(session_id, close_reason="PROMPT_EDIT_CANCELLED")
|
||||||
db_clear_session_state(session_id)
|
db_clear_session_state(session_id)
|
||||||
|
|
||||||
tool_action = db_get_tool_action("db_cancel_prompt_preview")
|
reply = "❌ Изменения системного промпта отменены."
|
||||||
reply = tool_action.get("success_template", "❌ Изменения системного промпта отменены.") if tool_action else "❌ Изменения системного промпта отменены."
|
|
||||||
|
|
||||||
db_save_chat_message(session_id, "user", full_user_content, is_ephemeral=0)
|
db_save_chat_message(session_id, "user", full_user_content, is_ephemeral=0)
|
||||||
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=0)
|
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=0)
|
||||||
return reply, db_get_chat_history(session_id), None
|
return reply, db_get_chat_history(session_id), None
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
# 2. ПОДТВЕРЖДЕНИЕ УДАЛЕНИЯ СНАПШОТОВ СКУД
|
# 2. ПОДТВЕРЖДЕНИЕ ПРАВИЛ
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
elif state_type == "SNAPSHOT_DELETE_CONFIRM":
|
elif state_type == "RULES_PREVIEW":
|
||||||
is_confirm = "подтверждаю удаление снапшот" in msg_lower or msg_lower in ["подтверждаю", "да", "удалить"]
|
is_confirm = "сохранение правил" in msg_lower or msg_lower in ["подтверждаю", "да", "сохраняй", "применить"]
|
||||||
is_cancel = msg_lower in ["отмена", "отменить", "нет"]
|
is_cancel = msg_lower in ["отмена", "отменить", "нет"]
|
||||||
|
|
||||||
if is_confirm:
|
if is_confirm:
|
||||||
snap_ids = state_data.get("snapshot_ids", [])
|
draft_text = state_data.get("draft_text", "")
|
||||||
query_date = state_data.get("query_date", "")
|
lines = [l.strip() for l in draft_text.splitlines() if l.strip()]
|
||||||
|
for line in lines:
|
||||||
|
clean_line = line
|
||||||
|
if line[0].isdigit() and "." in line[:5]:
|
||||||
|
clean_line = line.split(".", 1)[1].strip()
|
||||||
|
add_rule(clean_line)
|
||||||
|
|
||||||
# Безопасное удаление через сервис
|
close_tool_session_and_cleanup(session_id, close_reason="RULES_SAVED_SUCCESS")
|
||||||
del_res = delete_snapshots_safely(snapshot_ids=snap_ids)
|
|
||||||
|
|
||||||
close_tool_session_and_cleanup(session_id, close_reason="SNAPSHOTS_DELETED")
|
|
||||||
|
|
||||||
# Получаем свежий список за ту же дату
|
|
||||||
updated_data = get_snapshots_registry(date_str=query_date if query_date else None)
|
|
||||||
db_set_session_state(session_id, "SNAPSHOTS_VIEW", updated_data)
|
|
||||||
|
|
||||||
reply = f"✅ Успешно удалено снапшотов: {len(snap_ids)} шт."
|
|
||||||
db_save_chat_message(session_id, "user", full_user_content, is_ephemeral=1)
|
|
||||||
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=1)
|
|
||||||
|
|
||||||
return reply, db_get_chat_history(session_id), {
|
|
||||||
"type": "SNAPSHOTS_CARD",
|
|
||||||
"data": updated_data
|
|
||||||
}
|
|
||||||
|
|
||||||
elif is_cancel:
|
|
||||||
close_tool_session_and_cleanup(session_id, close_reason="SNAPSHOT_DELETE_CANCELLED")
|
|
||||||
db_clear_session_state(session_id)
|
db_clear_session_state(session_id)
|
||||||
|
|
||||||
reply = "❌ Удаление снапшотов отменено."
|
reply = "✅ База знаний и правила компании успешно сохранены в базе данных."
|
||||||
db_save_chat_message(session_id, "user", full_user_content, is_ephemeral=0)
|
db_save_chat_message(session_id, "user", full_user_content, is_ephemeral=0)
|
||||||
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=0)
|
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=0)
|
||||||
return reply, db_get_chat_history(session_id), None
|
return reply, db_get_chat_history(session_id), None
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
|
||||||
# 3. ПОДТВЕРЖДЕНИЕ УДАЛЕНИЯ ЗАДАЧИ
|
|
||||||
# -------------------------------------------------------------------------
|
|
||||||
elif state_type == "TASK_DELETE_CONFIRM":
|
|
||||||
is_confirm = "подтверждаю удаление задачи" in msg_lower or msg_lower in ["подтверждаю", "да", "удалить"]
|
|
||||||
is_cancel = msg_lower in ["отмена", "отменить", "нет"]
|
|
||||||
|
|
||||||
if is_confirm:
|
|
||||||
task_id = state_data.get("task_id")
|
|
||||||
delete_task(user_id=1, task_id=str(task_id))
|
|
||||||
|
|
||||||
close_tool_session_and_cleanup(session_id, close_reason="TASK_DELETED")
|
|
||||||
db_clear_session_state(session_id)
|
|
||||||
|
|
||||||
raw_tasks = get_tasks(user_id=1)
|
|
||||||
reply = f"🗑 Задача #{task_id} удалена."
|
|
||||||
db_save_chat_message(session_id, "user", full_user_content, is_ephemeral=1)
|
|
||||||
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=1)
|
|
||||||
|
|
||||||
return reply, db_get_chat_history(session_id), {
|
|
||||||
"type": "TASK_INTERACTIVE_CARD",
|
|
||||||
"tasks": raw_tasks
|
|
||||||
}
|
|
||||||
|
|
||||||
elif is_cancel:
|
elif is_cancel:
|
||||||
close_tool_session_and_cleanup(session_id, close_reason="TASK_DELETE_CANCELLED")
|
close_tool_session_and_cleanup(session_id, close_reason="RULES_EDIT_CANCELLED")
|
||||||
db_clear_session_state(session_id)
|
db_clear_session_state(session_id)
|
||||||
|
|
||||||
reply = "❌ Удаление задачи отменено."
|
reply = "❌ Изменение правил компании отменено."
|
||||||
db_save_chat_message(session_id, "user", full_user_content, is_ephemeral=0)
|
db_save_chat_message(session_id, "user", full_user_content, is_ephemeral=0)
|
||||||
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=0)
|
db_save_chat_message(session_id, "assistant", reply, is_ephemeral=0)
|
||||||
return reply, db_get_chat_history(session_id), None
|
return reply, db_get_chat_history(session_id), None
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ from typing import Dict, Any, Optional
|
|||||||
logger = logging.getLogger("OLLAMA_CLIENT")
|
logger = logging.getLogger("OLLAMA_CLIENT")
|
||||||
|
|
||||||
OLLAMA_URL = "http://10.121.17.227:11434/api/chat"
|
OLLAMA_URL = "http://10.121.17.227:11434/api/chat"
|
||||||
TEXT_MODEL = "qwen2.5:14b-instruct-q8_0"
|
TEXT_MODEL = "qwen2.5:14b"
|
||||||
VISION_MODEL = "qwen2.5vl:7b-q8_0"
|
VISION_MODEL = "qwen2.5vl:7b-q8_0"
|
||||||
|
|
||||||
LLM_OPTIONS = {
|
LLM_OPTIONS = {
|
||||||
|
|||||||
@@ -3,11 +3,7 @@
|
|||||||
FILE: modules/web_api/llm/core/tool_injector.py
|
FILE: modules/web_api/llm/core/tool_injector.py
|
||||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
PROJECT: SCUD Orion AI (Unified Architecture)
|
||||||
MODULE: web_api / llm / core
|
MODULE: web_api / llm / core
|
||||||
ROLE: Семантический анализ намерений оператора (Intent Classifier) и
|
ROLE: Базовая санитарная очистка вывода и тегов инструментов.
|
||||||
детерминированная сборка вызовов инструментов при сбоях нативного Function Calling.
|
|
||||||
|
|
||||||
AI-CONTEXT-ANCHORS:
|
|
||||||
- ANCHOR[INTENT_INJECTOR_MAIN]: Точка входа inject_tools_if_needed.
|
|
||||||
===============================================================================
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -32,107 +28,9 @@ def clean_output(text: str) -> str:
|
|||||||
return text.strip() if text else ""
|
return text.strip() if text else ""
|
||||||
|
|
||||||
|
|
||||||
# ANCHOR[INTENT_INJECTOR_MAIN]
|
|
||||||
def inject_tools_if_needed(user_message: str, raw_reply: str, existing_tool_calls: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
def inject_tools_if_needed(user_message: str, raw_reply: str, existing_tool_calls: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
Гибридный семантический анализатор:
|
Модель управляется через TOOLS_SCHEMA и системный контекст.
|
||||||
Если модель ответила текстом с сомнениями или пропустила tool_call,
|
Любые синтетические перехваты текста регулярными выражениями отключены согласно ROADMAP.
|
||||||
распознает доменное намерение и конструирует синтетический tool_call.
|
|
||||||
"""
|
"""
|
||||||
if existing_tool_calls:
|
|
||||||
return existing_tool_calls
|
|
||||||
|
|
||||||
msg_clean = user_message.strip().lower()
|
|
||||||
|
|
||||||
# 1. СЕМАНТИКА: Просмотр системного промпта
|
|
||||||
# Паттерны: "покажи системный промпт", "выведи промпт", "текущий промпт", "какой сейчас системный промпт"
|
|
||||||
if "промпт" in msg_clean and any(kw in msg_clean for kw in ["покажи", "выведи", "какой", "дай", "текст", "актуальн"]):
|
|
||||||
logger.info("[IntentInjector] Распознано намерение просмотра системного промпта")
|
|
||||||
return [{
|
|
||||||
"function": {
|
|
||||||
"name": "db_get_system_prompt",
|
|
||||||
"arguments": {}
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
|
|
||||||
# 2. СЕМАНТИКА: Удаление задач
|
|
||||||
# Паттерны: "удали задачу 37", "убери 37 задачу", "сотри таску #37", "сними с повестки задачу 37"
|
|
||||||
if any(kw in msg_clean for kw in ["удали", "удалить", "убери", "сотри", "сними"]) and any(kw in msg_clean for kw in ["задач", "таск"]):
|
|
||||||
task_match = re.search(r'#?\s*(\d+)', msg_clean)
|
|
||||||
if task_match:
|
|
||||||
task_id = task_match.group(1)
|
|
||||||
logger.info(f"[IntentInjector] Распознано намерение удаления задачи: #{task_id}")
|
|
||||||
return [{
|
|
||||||
"function": {
|
|
||||||
"name": "db_tasks_edit",
|
|
||||||
"arguments": {"action": "DELETE", "task_id": task_id}
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
|
|
||||||
# 3. СЕМАНТИКА: Управление системным промптом (удаление и мульти-удаление)
|
|
||||||
# Паттерны: "удали 1.8 и 3.4", "удали пункт 2.3", "вычеркни 1.8, 3.4 из промпта"
|
|
||||||
if any(kw in msg_clean for kw in ["удали", "удалить", "убери", "вычеркни", "сотри"]) and not any(kw in msg_clean for kw in ["задач", "снапшот", "срез"]):
|
|
||||||
node_matches = re.findall(r'(\d+)[\.\s]+(\d+)', user_message)
|
|
||||||
if node_matches:
|
|
||||||
formatted_nodes = [f"{s}.{i}" for s, i in node_matches]
|
|
||||||
logger.info(f"[IntentInjector] Распознано намерение удаления узлов промпта: {formatted_nodes}")
|
|
||||||
return [{
|
|
||||||
"function": {
|
|
||||||
"name": "db_prompt_node_edit",
|
|
||||||
"arguments": {
|
|
||||||
"action": "BATCH_DELETE" if len(formatted_nodes) > 1 else "DELETE",
|
|
||||||
"section_id": int(node_matches[0][0]),
|
|
||||||
"item_id": int(node_matches[0][1]),
|
|
||||||
"nodes_list": formatted_nodes,
|
|
||||||
"content": ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
|
|
||||||
# 4. СЕМАНТИКА: Добавление пункта промпта
|
|
||||||
# Паттерны: "добавь 3.4 Текст", "добавь пункт 3.4 Текст", "впиши в 3.4 Текст"
|
|
||||||
if any(kw in msg_clean for kw in ["добавь", "добавить", "впиши", "запиши"]) and not any(kw in msg_clean for kw in ["задач", "таск"]):
|
|
||||||
add_match = re.search(r'(\d+)[\.\s]+(\d+)[\.\s\:\-]+(.*)', user_message)
|
|
||||||
if add_match:
|
|
||||||
sec_id = int(add_match.group(1))
|
|
||||||
itm_id = int(add_match.group(2))
|
|
||||||
content = add_match.group(3).strip()
|
|
||||||
logger.info(f"[IntentInjector] Распознано намерение добавления узла промпта: {sec_id}.{itm_id}")
|
|
||||||
return [{
|
|
||||||
"function": {
|
|
||||||
"name": "db_prompt_node_edit",
|
|
||||||
"arguments": {
|
|
||||||
"action": "ADD",
|
|
||||||
"section_id": sec_id,
|
|
||||||
"item_id": itm_id,
|
|
||||||
"content": content
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
|
|
||||||
# 5. СЕМАНТИКА: Смена статуса задач
|
|
||||||
if any(kw in msg_clean for kw in ["в работу", "начни", "стартуй", "за работу"]):
|
|
||||||
task_match = re.search(r'#?\s*(\d+)', msg_clean)
|
|
||||||
if task_match:
|
|
||||||
task_id = task_match.group(1)
|
|
||||||
logger.info(f"[IntentInjector] Распознано намерение взятия в работу задачи: #{task_id}")
|
|
||||||
return [{
|
|
||||||
"function": {
|
|
||||||
"name": "db_tasks_edit",
|
|
||||||
"arguments": {"action": "UPDATE", "task_id": task_id, "status": "IN_PROGRESS"}
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
|
|
||||||
if any(kw in msg_clean for kw in ["заверши", "закрой", "готово", "выполнено"]):
|
|
||||||
task_match = re.search(r'#?\s*(\d+)', msg_clean)
|
|
||||||
if task_match:
|
|
||||||
task_id = task_match.group(1)
|
|
||||||
logger.info(f"[IntentInjector] Распознано намерение закрытия задачи: #{task_id}")
|
|
||||||
return [{
|
|
||||||
"function": {
|
|
||||||
"name": "db_tasks_edit",
|
|
||||||
"arguments": {"action": "UPDATE", "task_id": task_id, "status": "COMPLETED"}
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
|
|
||||||
return existing_tool_calls
|
return existing_tool_calls
|
||||||
@@ -1,17 +1,8 @@
|
|||||||
"""
|
"""
|
||||||
|
===============================================================================
|
||||||
FILE: modules/web_api/llm/db/connection.py
|
FILE: modules/web_api/llm/db/connection.py
|
||||||
|
ROLE: Реэкспорт единого подключения к БД из core.connection.
|
||||||
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
import os
|
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
# Динамический путь к общей БД в корне проекта
|
from core.connection import get_connection as get_db_connection, DB_PATH
|
||||||
BASE_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../"))
|
|
||||||
DB_PATH = os.path.join(BASE_ROOT, "data", "scud_orion_ai.db")
|
|
||||||
|
|
||||||
def get_db_connection() -> sqlite3.Connection:
|
|
||||||
conn = sqlite3.connect(DB_PATH, timeout=30.0)
|
|
||||||
conn.row_factory = sqlite3.Row
|
|
||||||
conn.execute("PRAGMA foreign_keys = ON;")
|
|
||||||
conn.execute("PRAGMA journal_mode = WAL;")
|
|
||||||
conn.execute("PRAGMA synchronous = NORMAL;")
|
|
||||||
return conn
|
|
||||||
@@ -7,7 +7,7 @@ from .connection import get_db_connection
|
|||||||
|
|
||||||
def db_save_chat_message(session_id: str, role: str, content: str, is_ephemeral: int = 0) -> None:
|
def db_save_chat_message(session_id: str, role: str, content: str, is_ephemeral: int = 0) -> None:
|
||||||
"""Сохраняет сообщение в БД (is_ephemeral=1 для временных служебных шагов, 0 для постоянных)."""
|
"""Сохраняет сообщение в БД (is_ephemeral=1 для временных служебных шагов, 0 для постоянных)."""
|
||||||
conn = get_db_connection()
|
conn = get_db_connection(row_factory=True)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
INSERT INTO chat_messages (session_id, role, content, is_ephemeral)
|
INSERT INTO chat_messages (session_id, role, content, is_ephemeral)
|
||||||
@@ -17,20 +17,19 @@ def db_save_chat_message(session_id: str, role: str, content: str, is_ephemeral:
|
|||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
def db_get_chat_history(session_id: str, limit: int = 20) -> List[Dict[str, Any]]:
|
def db_get_chat_history(session_id: str, limit: int = 50) -> list:
|
||||||
"""Получает последние сообщения истории диалога в хронологическом порядке."""
|
"""Возвращает историю сообщений диалога для сессии."""
|
||||||
conn = get_db_connection()
|
with get_db_connection(row_factory=True) as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT role, content
|
SELECT role, content, is_ephemeral, created_at
|
||||||
FROM chat_messages
|
FROM chat_messages
|
||||||
WHERE session_id = ?
|
WHERE session_id = ?
|
||||||
ORDER BY id DESC
|
ORDER BY id DESC
|
||||||
LIMIT ?
|
LIMIT ?
|
||||||
""", (session_id, limit))
|
""", (session_id, limit))
|
||||||
rows = cursor.fetchall()
|
rows = cursor.fetchall()
|
||||||
conn.close()
|
return [{"role": r["role"], "content": r["content"], "is_ephemeral": r["is_ephemeral"]} for r in reversed(rows)]
|
||||||
return [{"role": r["role"], "content": r["content"]} for r in reversed(rows)]
|
|
||||||
|
|
||||||
|
|
||||||
def db_purge_ephemeral_messages(session_id: str) -> int:
|
def db_purge_ephemeral_messages(session_id: str) -> int:
|
||||||
@@ -38,7 +37,7 @@ def db_purge_ephemeral_messages(session_id: str) -> int:
|
|||||||
Физически удаляет все временные служебные сообщения выбранной сессии
|
Физически удаляет все временные служебные сообщения выбранной сессии
|
||||||
после завершения сценария работы с инструментом.
|
после завершения сценария работы с инструментом.
|
||||||
"""
|
"""
|
||||||
conn = get_db_connection()
|
conn = get_db_connection(row_factory=True)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("DELETE FROM chat_messages WHERE session_id = ? AND is_ephemeral = 1", (session_id,))
|
cursor.execute("DELETE FROM chat_messages WHERE session_id = ? AND is_ephemeral = 1", (session_id,))
|
||||||
deleted = cursor.rowcount
|
deleted = cursor.rowcount
|
||||||
@@ -49,8 +48,40 @@ def db_purge_ephemeral_messages(session_id: str) -> int:
|
|||||||
|
|
||||||
def db_clear_chat_history(session_id: str) -> None:
|
def db_clear_chat_history(session_id: str) -> None:
|
||||||
"""Полная очистка всех сообщений сессии."""
|
"""Полная очистка всех сообщений сессии."""
|
||||||
conn = get_db_connection()
|
conn = get_db_connection(row_factory=True)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("DELETE FROM chat_messages WHERE session_id = ?", (session_id,))
|
cursor.execute("DELETE FROM chat_messages WHERE session_id = ?", (session_id,))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
def db_clear_all_chat_context(session_id: str = None, purge_all: bool = False) -> int:
|
||||||
|
"""Удаляет сообщения чата и стейты сессий."""
|
||||||
|
with get_db_connection(row_factory=True) 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
|
||||||
@@ -18,7 +18,7 @@ logger = logging.getLogger("DB_PROMPTS")
|
|||||||
|
|
||||||
def init_prompt_nodes_table():
|
def init_prompt_nodes_table():
|
||||||
"""Создает реляционную таблицу узлов промпта и заполняет базовыми данными."""
|
"""Создает реляционную таблицу узлов промпта и заполняет базовыми данными."""
|
||||||
with get_db_connection() as conn:
|
with get_db_connection(row_factory=True) as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
CREATE TABLE IF NOT EXISTS system_prompt_nodes (
|
CREATE TABLE IF NOT EXISTS system_prompt_nodes (
|
||||||
@@ -40,7 +40,7 @@ def init_prompt_nodes_table():
|
|||||||
# Раздел 1
|
# Раздел 1
|
||||||
(1, 0, "РОЛЬ И ЗАДАЧИ АССИСТЕНТА"),
|
(1, 0, "РОЛЬ И ЗАДАЧИ АССИСТЕНТА"),
|
||||||
(1, 1, "Управление бэклогом задач проекта (через db_get_tasks, db_add_task, db_update_task_status, db_delete_task)."),
|
(1, 1, "Управление бэклогом задач проекта (через db_get_tasks, db_add_task, db_update_task_status, db_delete_task)."),
|
||||||
(1, 2, "Консультация по правилам и арбитражу кадровых данных/СКУД из базы знаний (через db_get_rules)."),
|
(1, 2, "Консультация по внутренним регламентам компании (через db_get_rules). Вопросы по ТК РФ и законам поясняй напрямую."),
|
||||||
(1, 3, "Предоставление справки о возможностях и примерах команд (СТРОГО через db_get_reference)."),
|
(1, 3, "Предоставление справки о возможностях и примерах команд (СТРОГО через db_get_reference)."),
|
||||||
(1, 4, "Просмотр аномалий СКУД ⟷ 1С (СТРОГО через db_get_anomalies)."),
|
(1, 4, "Просмотр аномалий СКУД ⟷ 1С (СТРОГО через db_get_anomalies)."),
|
||||||
(1, 5, "Поддержка диалога с операторами и администраторами системы."),
|
(1, 5, "Поддержка диалога с операторами и администраторами системы."),
|
||||||
@@ -73,7 +73,7 @@ def init_prompt_nodes_table():
|
|||||||
def db_get_active_system_prompt(prompt_name: str = "main_agent") -> str:
|
def db_get_active_system_prompt(prompt_name: str = "main_agent") -> str:
|
||||||
"""Собирает структурированный текст промпта из реляционной таблицы узлов."""
|
"""Собирает структурированный текст промпта из реляционной таблицы узлов."""
|
||||||
init_prompt_nodes_table()
|
init_prompt_nodes_table()
|
||||||
with get_db_connection() as conn:
|
with get_db_connection(row_factory=True) as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT section_id, item_id, content
|
SELECT section_id, item_id, content
|
||||||
@@ -104,7 +104,7 @@ def db_get_active_system_prompt(prompt_name: str = "main_agent") -> str:
|
|||||||
def db_apply_prompt_node_action(action: str, section_id: int, item_id: int, content: str = "", prompt_name: str = "main_agent"):
|
def db_apply_prompt_node_action(action: str, section_id: int, item_id: int, content: str = "", prompt_name: str = "main_agent"):
|
||||||
"""Прямое добавление, изменение или удаление узла в БД."""
|
"""Прямое добавление, изменение или удаление узла в БД."""
|
||||||
init_prompt_nodes_table()
|
init_prompt_nodes_table()
|
||||||
with get_db_connection() as conn:
|
with get_db_connection(row_factory=True) as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
action_clean = action.upper()
|
action_clean = action.upper()
|
||||||
if action_clean in ["ADD", "UPDATE"]:
|
if action_clean in ["ADD", "UPDATE"]:
|
||||||
@@ -125,7 +125,7 @@ def db_apply_prompt_node_action(action: str, section_id: int, item_id: int, cont
|
|||||||
|
|
||||||
|
|
||||||
def db_get_tool_action(tool_name: str) -> Optional[Dict[str, Any]]:
|
def db_get_tool_action(tool_name: str) -> Optional[Dict[str, Any]]:
|
||||||
conn = get_db_connection()
|
conn = get_db_connection(row_factory=True)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT tool_name, category, bypass_llm, success_template,
|
SELECT tool_name, category, bypass_llm, success_template,
|
||||||
@@ -144,7 +144,7 @@ def db_get_tool_action(tool_name: str) -> Optional[Dict[str, Any]]:
|
|||||||
|
|
||||||
|
|
||||||
def db_get_rules() -> List[Dict[str, Any]]:
|
def db_get_rules() -> List[Dict[str, Any]]:
|
||||||
conn = get_db_connection()
|
conn = get_db_connection(row_factory=True)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("SELECT id, rule_text FROM ai_knowledge_base ORDER BY id ASC")
|
cursor.execute("SELECT id, rule_text FROM ai_knowledge_base ORDER BY id ASC")
|
||||||
rows = cursor.fetchall()
|
rows = cursor.fetchall()
|
||||||
@@ -154,7 +154,7 @@ def db_get_rules() -> List[Dict[str, Any]]:
|
|||||||
|
|
||||||
def db_set_session_state(session_id: str, state_type: str, data: Any) -> None:
|
def db_set_session_state(session_id: str, state_type: str, data: Any) -> None:
|
||||||
"""Сохраняет состояние сессии в SQLite."""
|
"""Сохраняет состояние сессии в SQLite."""
|
||||||
conn = get_db_connection()
|
conn = get_db_connection(row_factory=True)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
payload_str = json.dumps(data, ensure_ascii=False) if isinstance(data, (dict, list)) else (str(data) if data is not None else "")
|
payload_str = json.dumps(data, ensure_ascii=False) if isinstance(data, (dict, list)) else (str(data) if data is not None else "")
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
@@ -170,7 +170,7 @@ def db_set_session_state(session_id: str, state_type: str, data: Any) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def db_get_session_state(session_id: str) -> Optional[Dict[str, Any]]:
|
def db_get_session_state(session_id: str) -> Optional[Dict[str, Any]]:
|
||||||
conn = get_db_connection()
|
conn = get_db_connection(row_factory=True)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("SELECT session_id, state_type, pending_data, updated_at FROM session_states WHERE session_id = ?", (session_id,))
|
cursor.execute("SELECT session_id, state_type, pending_data, updated_at FROM session_states WHERE session_id = ?", (session_id,))
|
||||||
row = cursor.fetchone()
|
row = cursor.fetchone()
|
||||||
@@ -187,7 +187,7 @@ def db_get_session_state(session_id: str) -> Optional[Dict[str, Any]]:
|
|||||||
|
|
||||||
|
|
||||||
def db_clear_session_state(session_id: str) -> None:
|
def db_clear_session_state(session_id: str) -> None:
|
||||||
conn = get_db_connection()
|
conn = get_db_connection(row_factory=True)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("DELETE FROM session_states WHERE session_id = ?", (session_id,))
|
cursor.execute("DELETE FROM session_states WHERE session_id = ?", (session_id,))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
@@ -195,7 +195,7 @@ def db_clear_session_state(session_id: str) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def db_get_stats() -> Dict[str, Any]:
|
def db_get_stats() -> Dict[str, Any]:
|
||||||
conn = get_db_connection()
|
conn = get_db_connection(row_factory=True)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
tables = ['scud_logs', 'zup_staff', 'zup_absences', 'anomalies_history', 'ai_knowledge_base', 'system_prompt_nodes', 'session_states', 'tasks']
|
tables = ['scud_logs', 'zup_staff', 'zup_absences', 'anomalies_history', 'ai_knowledge_base', 'system_prompt_nodes', 'session_states', 'tasks']
|
||||||
stats = {}
|
stats = {}
|
||||||
@@ -210,7 +210,7 @@ def db_get_stats() -> Dict[str, Any]:
|
|||||||
|
|
||||||
|
|
||||||
def db_get_anomalies(limit: int = 100, date_str: Optional[str] = None) -> Dict[str, Any]:
|
def db_get_anomalies(limit: int = 100, date_str: Optional[str] = None) -> Dict[str, Any]:
|
||||||
conn = get_db_connection()
|
conn = get_db_connection(row_factory=True)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
query = "SELECT anomaly_date, fio, anomaly_type, details FROM anomalies_history"
|
query = "SELECT anomaly_date, fio, anomaly_type, details FROM anomalies_history"
|
||||||
params = []
|
params = []
|
||||||
@@ -226,7 +226,7 @@ def db_get_anomalies(limit: int = 100, date_str: Optional[str] = None) -> Dict[s
|
|||||||
|
|
||||||
|
|
||||||
def db_get_reference(category: Optional[str] = None) -> Dict[str, Any]:
|
def db_get_reference(category: Optional[str] = None) -> Dict[str, Any]:
|
||||||
conn = get_db_connection()
|
conn = get_db_connection(row_factory=True)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
query = "SELECT category, title, example_prompt, description FROM system_reference"
|
query = "SELECT category, title, example_prompt, description FROM system_reference"
|
||||||
params = []
|
params = []
|
||||||
|
|||||||
@@ -22,6 +22,9 @@ from services.tasks.service import (
|
|||||||
delete_task as db_delete_task,
|
delete_task as db_delete_task,
|
||||||
execute_task_action as db_tasks_edit
|
execute_task_action as db_tasks_edit
|
||||||
)
|
)
|
||||||
|
# Защитный алиас для обратной совместимости
|
||||||
|
db_update_task_status = db_update_task_details
|
||||||
|
|
||||||
from services.tasks.exporter import export_tasks_to_markdown as db_export_tasks_markdown
|
from services.tasks.exporter import export_tasks_to_markdown as db_export_tasks_markdown
|
||||||
from services.tasks.repository import normalize_task_id
|
from services.tasks.repository import normalize_task_id
|
||||||
|
|
||||||
|
|||||||
@@ -69,14 +69,18 @@ TOOLS_SCHEMA = [
|
|||||||
"type": "function",
|
"type": "function",
|
||||||
"function": {
|
"function": {
|
||||||
"name": "db_get_tasks",
|
"name": "db_get_tasks",
|
||||||
"description": "Просмотр реестра задач и бэклога текущего пользователя.",
|
"description": (
|
||||||
|
"Просмотр реестра задач и бэклога текущего пользователя.\n"
|
||||||
|
"Вызывай этот инструмент ВСЕГДА при любых запросах просмотра задач ('покажи задачи', 'мои задачи', опечатки 'змдачи').\n"
|
||||||
|
"Запрещено переспрашивать статус или параметры текстом: просто вызывай функцию с аргументами {}."
|
||||||
|
),
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"status": {
|
"status": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": ["ALL", "IN_PROGRESS", "PLANNED", "COMPLETED"],
|
"enum": ["ALL", "IN_PROGRESS", "PLANNED", "COMPLETED"],
|
||||||
"description": "Опциональный фильтр статуса задач"
|
"description": "Опциональный фильтр статуса задач (по умолчанию ALL)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,7 +146,11 @@ TOOLS_SCHEMA = [
|
|||||||
"type": "function",
|
"type": "function",
|
||||||
"function": {
|
"function": {
|
||||||
"name": "db_get_snapshots",
|
"name": "db_get_snapshots",
|
||||||
"description": "Получение списка снапшотов и срезов логов СКУД из базы данных за конкретную дату.",
|
"description": (
|
||||||
|
"Получение реестра/списка доступных снапшотов (файлов срезов) СКУД.\n"
|
||||||
|
"ВЫЗЫВАТЬ ТОЛЬКО при прямом запросе на список срезов ('покажи срезы', 'какие есть снапшоты', 'срезы за дату').\n"
|
||||||
|
"КАТЕГОРИЧЕСКИ ЗАПРЕЩЕНО вызывать эту функцию, если пользователь спрашивает о людях, сотрудниках, входах или выходах внутри уже открытого среза!"
|
||||||
|
),
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
@@ -158,17 +166,21 @@ TOOLS_SCHEMA = [
|
|||||||
"type": "function",
|
"type": "function",
|
||||||
"function": {
|
"function": {
|
||||||
"name": "db_delete_snapshots",
|
"name": "db_delete_snapshots",
|
||||||
"description": "Удаление снапшотов СКУД по идентификатору или дате.",
|
"description": "Удаление дневных снапшотов СКУД по идентификатору или дате.",
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"snapshot_id": {
|
"snapshot_id": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Идентификатор конкретного снапшота"
|
"description": "Идентификатор или список идентификаторов через запятую"
|
||||||
},
|
},
|
||||||
"day_str": {
|
"day_str": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Дата всех снапшотов за день"
|
"description": "Дата всех снапшотов за день"
|
||||||
|
},
|
||||||
|
"confirmed": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "Флаг окончательного подтверждения удаления пользователем"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,12 +26,19 @@ from fastapi import FastAPI, HTTPException
|
|||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from fastapi.responses import FileResponse, JSONResponse
|
from fastapi.responses import FileResponse, JSONResponse
|
||||||
from fastapi.exceptions import RequestValidationError
|
from fastapi.exceptions import RequestValidationError
|
||||||
|
from routers.manual_absences import router as manual_absences_router
|
||||||
|
|
||||||
from routers.auth import router as auth_router
|
from routers.auth import router as auth_router
|
||||||
from routers.admin import router as admin_router
|
from routers.admin import router as admin_router
|
||||||
from routers.tasks import router as tasks_router
|
from routers.tasks import router as tasks_router
|
||||||
from routers.chat import router as chat_router
|
from routers.chat import router as chat_router
|
||||||
from routers.files import router as files_router
|
from routers.files import router as files_router
|
||||||
|
from routers.exceptions import router as exceptions_router
|
||||||
|
from routers.snapshots import router as snapshots_router
|
||||||
|
from routers.remote_workers import router as remote_workers_router
|
||||||
|
from routers.context import router as context_router
|
||||||
|
from routers.presence import router as presence_router
|
||||||
|
from routers.reports import router as reports_router
|
||||||
|
|
||||||
# ANCHOR[APP_CONFIG]
|
# ANCHOR[APP_CONFIG]
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
@@ -61,6 +68,13 @@ app.include_router(admin_router)
|
|||||||
app.include_router(tasks_router)
|
app.include_router(tasks_router)
|
||||||
app.include_router(chat_router)
|
app.include_router(chat_router)
|
||||||
app.include_router(files_router)
|
app.include_router(files_router)
|
||||||
|
app.include_router(exceptions_router)
|
||||||
|
app.include_router(snapshots_router)
|
||||||
|
app.include_router(remote_workers_router)
|
||||||
|
app.include_router(context_router)
|
||||||
|
app.include_router(manual_absences_router)
|
||||||
|
app.include_router(presence_router)
|
||||||
|
app.include_router(reports_router)
|
||||||
|
|
||||||
# ANCHOR[ROOT_STATIC_ROUTES]
|
# ANCHOR[ROOT_STATIC_ROUTES]
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
@@ -80,6 +94,12 @@ async def favicon():
|
|||||||
@app.get("/{file_path:path}")
|
@app.get("/{file_path:path}")
|
||||||
def serve_static_fallback(file_path: str):
|
def serve_static_fallback(file_path: str):
|
||||||
clean_path = file_path.lstrip("/")
|
clean_path = file_path.lstrip("/")
|
||||||
|
|
||||||
|
# Жесткая блокировка скрытых файлов (.env, .git) и служебных форматов
|
||||||
|
forbidden_patterns = [".env", ".git", ".yml", ".yaml", ".json", ".sql", ".php", ".bak"]
|
||||||
|
if clean_path.startswith(".") or any(p in clean_path.lower() for p in forbidden_patterns):
|
||||||
|
raise HTTPException(status_code=403, detail="Access denied")
|
||||||
|
|
||||||
target = os.path.join(STATIC_DIR, clean_path)
|
target = os.path.join(STATIC_DIR, clean_path)
|
||||||
|
|
||||||
if os.path.isfile(target):
|
if os.path.isfile(target):
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ def login(req: AuthRequest):
|
|||||||
username = req.username.strip().lower()
|
username = req.username.strip().lower()
|
||||||
conn = get_db()
|
conn = get_db()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("SELECT id, username, password_hash, is_admin FROM users WHERE username = ?", (username,))
|
cursor.execute("SELECT id, username, password_hash, full_name, is_admin FROM users WHERE username = ?", (username,))
|
||||||
user = cursor.fetchone()
|
user = cursor.fetchone()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
@@ -82,7 +82,17 @@ def login(req: AuthRequest):
|
|||||||
is_admin = bool(user["is_admin"]) or (user["username"] == "puh")
|
is_admin = bool(user["is_admin"]) or (user["username"] == "puh")
|
||||||
token = create_access_token(user["id"], user["username"], is_admin)
|
token = create_access_token(user["id"], user["username"], is_admin)
|
||||||
|
|
||||||
return {"status": "success", "token": token, "username": user["username"], "is_admin": is_admin}
|
# Возвращаем full_name (если не задано — отдаем username)
|
||||||
|
full_name = user["full_name"] if user["full_name"] else user["username"]
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "success",
|
||||||
|
"token": token,
|
||||||
|
"username": user["username"],
|
||||||
|
"full_name": full_name,
|
||||||
|
"user_id": user["id"],
|
||||||
|
"is_admin": is_admin
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.post("/change-password")
|
@router.post("/change-password")
|
||||||
|
|||||||
+100
-49
@@ -1,22 +1,30 @@
|
|||||||
"""
|
"""
|
||||||
===============================================================================
|
===============================================================================
|
||||||
FILE: modules/web_api/routers/chat.py
|
FILE: modules/web_api/routers/chat.py
|
||||||
ROLE: Обработка сообщений веб-чата с поддержкой токенов и гостевого доступа.
|
ROLE: Роутер чата с чистым разделением:
|
||||||
|
- Диалог и команды СКУД/1С (через agent.py).
|
||||||
|
- Парсинг и извлечение документов без обрезания (через file_parser.py).
|
||||||
===============================================================================
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from fastapi import APIRouter, Header, HTTPException, Request
|
import os
|
||||||
from pydantic import BaseModel
|
import shutil
|
||||||
from typing import Optional, List, Dict, Any
|
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from services.text_reporter import ask_ollama
|
from fastapi import APIRouter, Header, HTTPException, UploadFile, File, Form
|
||||||
from services.knowledge_base import load_knowledge_base
|
from pydantic import BaseModel
|
||||||
from core.database import get_connection
|
|
||||||
|
from llm.agent import process_chat_message
|
||||||
|
from llm.file_parser import parse_uploaded_file
|
||||||
|
from config import BASE_DIR
|
||||||
|
|
||||||
logger = logging.getLogger("CHAT_API")
|
logger = logging.getLogger("CHAT_API")
|
||||||
router = APIRouter(prefix="/api/v1", tags=["Chat"])
|
router = APIRouter(prefix="/api/v1", tags=["Chat"])
|
||||||
|
|
||||||
|
UPLOAD_TMP_DIR = os.path.join(BASE_DIR, "data", "uploads")
|
||||||
|
os.makedirs(UPLOAD_TMP_DIR, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
class ChatMessageRequest(BaseModel):
|
class ChatMessageRequest(BaseModel):
|
||||||
message: str
|
message: str
|
||||||
@@ -25,26 +33,13 @@ class ChatMessageRequest(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
def resolve_user_id(authorization: Optional[str] = None, explicit_user_id: Optional[int] = None) -> int:
|
def resolve_user_id(authorization: Optional[str] = None, explicit_user_id: Optional[int] = None) -> int:
|
||||||
"""
|
|
||||||
Извлекает ID пользователя из Bearer-токена.
|
|
||||||
Если токен не передан или сессия новая — использует user_id=1 по умолчанию,
|
|
||||||
не блокируя работу ошибкой 403 Forbidden.
|
|
||||||
"""
|
|
||||||
if explicit_user_id and explicit_user_id > 0:
|
if explicit_user_id and explicit_user_id > 0:
|
||||||
return explicit_user_id
|
return explicit_user_id
|
||||||
|
|
||||||
if authorization and authorization.startswith("Bearer "):
|
if authorization and authorization.startswith("Bearer "):
|
||||||
token = authorization.replace("Bearer ", "").strip()
|
token = authorization.replace("Bearer ", "").strip()
|
||||||
# Если используется простой токен вида 'user_1' или JWT
|
|
||||||
if token.isdigit():
|
if token.isdigit():
|
||||||
return int(token)
|
return int(token)
|
||||||
elif token.startswith("dev_token_"):
|
|
||||||
try:
|
|
||||||
return int(token.replace("dev_token_", ""))
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Дефолтный пользователь (гостевой / основной аккаунт)
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
@@ -57,41 +52,97 @@ async def chat_endpoint(payload: ChatMessageRequest, authorization: Optional[str
|
|||||||
if not user_msg:
|
if not user_msg:
|
||||||
raise HTTPException(status_code=400, detail="Пустое сообщение")
|
raise HTTPException(status_code=400, detail="Пустое сообщение")
|
||||||
|
|
||||||
logger.info(f"Сообщение от user_id={user_id}, session_id={session_id}: {user_msg}")
|
reply_text, history, action_payload = process_chat_message(
|
||||||
|
user_id=user_id,
|
||||||
# Загружаем контекст базы знаний
|
user_message=user_msg,
|
||||||
kb = load_knowledge_base()
|
session_id=session_id
|
||||||
rules_text = "\n".join([f"- {r}" for r in kb.get("rules", [])])
|
|
||||||
|
|
||||||
system_prompt = (
|
|
||||||
"Ты — ИИ-ассистент системы кадровой безопасности и контроллинга СКУД Orion AI.\n"
|
|
||||||
"Отвечай четко, профессионально и на русском языке.\n"
|
|
||||||
f"Актуальные правила системы:\n{rules_text}"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
|
||||||
reply_text = ask_ollama(user_msg, system_prompt=system_prompt)
|
|
||||||
|
|
||||||
# Сохранение истории в SQLite при необходимости
|
|
||||||
try:
|
|
||||||
with get_connection() as conn:
|
|
||||||
conn.execute(
|
|
||||||
"INSERT INTO chat_messages (session_id, role, content) VALUES (?, ?, ?), (?, ?, ?)",
|
|
||||||
(session_id, "user", user_msg, session_id, "assistant", reply_text)
|
|
||||||
)
|
|
||||||
conn.commit()
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"status": "success",
|
"status": "success",
|
||||||
"user_id": user_id,
|
"user_id": user_id,
|
||||||
"session_id": session_id,
|
"session_id": session_id,
|
||||||
"response": reply_text
|
"response": reply_text,
|
||||||
|
"action_payload": action_payload
|
||||||
}
|
}
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Ошибка вызова нейросети: {e}")
|
|
||||||
|
@router.post("/chat/upload")
|
||||||
|
async def chat_upload_endpoint(
|
||||||
|
file: UploadFile = File(...),
|
||||||
|
message: Optional[str] = Form(""),
|
||||||
|
session_id: Optional[str] = Form("web_session_main"),
|
||||||
|
authorization: Optional[str] = Header(None)
|
||||||
|
):
|
||||||
|
user_id = resolve_user_id(authorization, 1)
|
||||||
|
file_path = os.path.join(UPLOAD_TMP_DIR, file.filename)
|
||||||
|
|
||||||
|
with open(file_path, "wb") as buffer:
|
||||||
|
shutil.copyfileobj(file.file, buffer)
|
||||||
|
|
||||||
|
user_msg = (message or "").strip()
|
||||||
|
msg_lower = user_msg.lower()
|
||||||
|
fn_lower = file.filename.lower()
|
||||||
|
|
||||||
|
# ⭐️ 1. ПРЯМАЯ ИЗОЛИРОВАННАЯ ОБРАБОТКА PDF/СКАНОВ В WORD ЧЕРЕЗ OFFICE МОДУЛЬ
|
||||||
|
ocr_keywords = [
|
||||||
|
"распознай", "распознать", "в word", "в ворд", "для ворда", "для word",
|
||||||
|
"отформатируй", "текст для вставки", "извлеки текст", "сделай документ", "переведи в ворд"
|
||||||
|
]
|
||||||
|
|
||||||
|
if fn_lower.endswith(".pdf") and (any(k in msg_lower for k in ocr_keywords) or not user_msg):
|
||||||
|
logger.info(f"[Office] Прямой запуск распознавания PDF в Word: {file.filename}")
|
||||||
|
from services.office.service import convert_pdf_to_word_service
|
||||||
|
from modules.web_api.llm.db_tools import db_save_chat_message, db_get_chat_history
|
||||||
|
|
||||||
|
# Сохраняем вопрос пользователя в историю
|
||||||
|
prompt_text = user_msg or f"Распознать документ {file.filename} для MS Word"
|
||||||
|
db_save_chat_message(session_id, "user", prompt_text, is_ephemeral=0)
|
||||||
|
|
||||||
|
# Вызываем офисный сервис постраничного OCR и сборки DOCX
|
||||||
|
office_res = convert_pdf_to_word_service(file_path, file.filename)
|
||||||
|
|
||||||
|
reply_text = (
|
||||||
|
f"📄 **{office_res['message']}**\n\n"
|
||||||
|
f"**Фрагмент первой страницы документа:**\n"
|
||||||
|
f"```text\n{office_res['preview_text']}...\n```\n\n"
|
||||||
|
f"Файл готов к скачиванию и редактированию в MS Word."
|
||||||
|
)
|
||||||
|
db_save_chat_message(session_id, "assistant", reply_text, is_ephemeral=0)
|
||||||
|
|
||||||
|
action_payload = {
|
||||||
|
"type": "FILE_DOWNLOAD_CARD",
|
||||||
|
"filename": office_res["filename"],
|
||||||
|
"download_url": office_res["download_url"],
|
||||||
|
"tasks_count": f"{office_res['total_pages']} стр."
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"status": "error",
|
"status": "success",
|
||||||
"response": f"⚠️ Ошибка обработки запроса: {str(e)}"
|
"user_id": user_id,
|
||||||
|
"session_id": session_id,
|
||||||
|
"response": reply_text,
|
||||||
|
"action_payload": action_payload
|
||||||
|
}
|
||||||
|
|
||||||
|
# 2. Если это не задача OCR в Word — отправляем файл в обычный диалог агента
|
||||||
|
parsed = parse_uploaded_file(file_path, file.filename)
|
||||||
|
file_context = parsed.get("context_text", "")
|
||||||
|
image_b64 = parsed.get("image_b64")
|
||||||
|
|
||||||
|
prompt_for_agent = user_msg or f"Проанализируй документ {file.filename}"
|
||||||
|
reply_text, history, action_payload = process_chat_message(
|
||||||
|
user_id=user_id,
|
||||||
|
user_message=prompt_for_agent,
|
||||||
|
file_context=file_context,
|
||||||
|
image_b64=image_b64,
|
||||||
|
session_id=session_id
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "success",
|
||||||
|
"user_id": user_id,
|
||||||
|
"session_id": session_id,
|
||||||
|
"response": reply_text,
|
||||||
|
"action_payload": action_payload
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: modules/web_api/routers/context.py
|
||||||
|
ROLE: REST API мониторинга состояния сессии и очистки памяти диалога.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from routers.auth import get_current_user
|
||||||
|
from modules.web_api.llm.db.db_prompts import db_get_session_state, db_clear_session_state
|
||||||
|
from modules.web_api.llm.db.db_chat import db_get_chat_history, db_purge_ephemeral_messages, db_clear_chat_history
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/v1/context", tags=["Context"])
|
||||||
|
|
||||||
|
|
||||||
|
class SessionActionRequest(BaseModel):
|
||||||
|
session_id: Optional[str] = "web_session_main"
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/state")
|
||||||
|
def api_get_context_state(session_id: str = "web_session_main", current_user = Depends(get_current_user)):
|
||||||
|
state = db_get_session_state(session_id) or {}
|
||||||
|
history = db_get_chat_history(session_id, limit=50)
|
||||||
|
|
||||||
|
ephemeral_count = sum(1 for m in history if dict(m).get("is_ephemeral") == 1)
|
||||||
|
total_messages = len(history)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"session_id": session_id,
|
||||||
|
"active_state": state.get("state_type", "IDLE"),
|
||||||
|
"state_data": state.get("data_json", {}),
|
||||||
|
"total_messages": total_messages,
|
||||||
|
"ephemeral_messages": ephemeral_count
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/purge-ephemeral")
|
||||||
|
def api_purge_ephemeral(req: SessionActionRequest, current_user = Depends(get_current_user)):
|
||||||
|
purged = db_purge_ephemeral_messages(req.session_id)
|
||||||
|
return {"status": "success", "purged_count": purged}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/clear-all")
|
||||||
|
def api_clear_all_context(req: SessionActionRequest, current_user = Depends(get_current_user)):
|
||||||
|
db_clear_session_state(req.session_id)
|
||||||
|
db_clear_chat_history(req.session_id)
|
||||||
|
return {"status": "success", "message": "Контекст сессии полностью очищен"}
|
||||||
@@ -12,11 +12,13 @@ class ExceptionItem(BaseModel):
|
|||||||
comment: Optional[str] = ""
|
comment: Optional[str] = ""
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("")
|
||||||
@router.get("/")
|
@router.get("/")
|
||||||
def api_get_exceptions():
|
def api_get_exceptions():
|
||||||
return get_all_exceptions_from_db()
|
return get_all_exceptions_from_db()
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("")
|
||||||
@router.post("/")
|
@router.post("/")
|
||||||
def api_add_exception(item: ExceptionItem):
|
def api_add_exception(item: ExceptionItem):
|
||||||
if not add_exception_to_db(item.category, item.value, item.comment):
|
if not add_exception_to_db(item.category, item.value, item.comment):
|
||||||
@@ -24,6 +26,7 @@ def api_add_exception(item: ExceptionItem):
|
|||||||
return {"status": "success", "data": item}
|
return {"status": "success", "data": item}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("")
|
||||||
@router.delete("/")
|
@router.delete("/")
|
||||||
def api_delete_exception(category: str, value: str):
|
def api_delete_exception(category: str, value: str):
|
||||||
if not remove_exception_from_db(category, value):
|
if not remove_exception_from_db(category, value):
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
===============================================================================
|
===============================================================================
|
||||||
FILE: modules/web_api/routers/files.py
|
FILE: modules/web_api/routers/files.py
|
||||||
ROLE: Раздача сформированных отчетов и выгрузок с сохранением оригинальных имен
|
ROLE: Раздача сформированных отчетов и выгрузок с сохранением оригинальных имен.
|
||||||
через изолированные UUID-директории инструментов.
|
|
||||||
===============================================================================
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -10,6 +9,7 @@ import os
|
|||||||
import time
|
import time
|
||||||
import shutil
|
import shutil
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
from typing import Optional
|
||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
from fastapi.responses import FileResponse
|
from fastapi.responses import FileResponse
|
||||||
|
|
||||||
@@ -17,13 +17,17 @@ router = APIRouter(prefix="/api/v1/files", tags=["Files"])
|
|||||||
|
|
||||||
BASE_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../"))
|
BASE_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../"))
|
||||||
WEB_OUTPUT_DIR = os.path.join(BASE_ROOT, "output", "web")
|
WEB_OUTPUT_DIR = os.path.join(BASE_ROOT, "output", "web")
|
||||||
os.makedirs(WEB_OUTPUT_DIR, exist_ok=True)
|
REPORTS_DIR = os.path.join(BASE_ROOT, "output", "reports")
|
||||||
|
TEMP_REPORTS_DIR = "/tmp/scud_reports"
|
||||||
|
|
||||||
SESSION_TTL_HOURS = 24 # Срок жизни временных сессионных выгрузок
|
os.makedirs(WEB_OUTPUT_DIR, exist_ok=True)
|
||||||
|
os.makedirs(REPORTS_DIR, exist_ok=True)
|
||||||
|
os.makedirs(TEMP_REPORTS_DIR, exist_ok=True)
|
||||||
|
|
||||||
|
SESSION_TTL_HOURS = 24
|
||||||
|
|
||||||
|
|
||||||
def purge_old_tool_sessions(tool_dir_path: str):
|
def purge_old_tool_sessions(tool_dir_path: str):
|
||||||
"""Удаляет временные UUID-папки старше SESSION_TTL_HOURS внутри инструмента."""
|
|
||||||
if not os.path.exists(tool_dir_path):
|
if not os.path.exists(tool_dir_path):
|
||||||
return
|
return
|
||||||
now = time.time()
|
now = time.time()
|
||||||
@@ -40,9 +44,6 @@ def purge_old_tool_sessions(tool_dir_path: str):
|
|||||||
|
|
||||||
@router.get("/download/{tool_name}/{session_uuid}/{filename}")
|
@router.get("/download/{tool_name}/{session_uuid}/{filename}")
|
||||||
async def download_file(tool_name: str, session_uuid: str, filename: str):
|
async def download_file(tool_name: str, session_uuid: str, filename: str):
|
||||||
"""
|
|
||||||
Безопасная отдача файла с каноническим именем из изолированной директории.
|
|
||||||
"""
|
|
||||||
safe_tool = os.path.basename(tool_name)
|
safe_tool = os.path.basename(tool_name)
|
||||||
safe_uuid = os.path.basename(session_uuid)
|
safe_uuid = os.path.basename(session_uuid)
|
||||||
safe_filename = os.path.basename(filename)
|
safe_filename = os.path.basename(filename)
|
||||||
@@ -52,16 +53,14 @@ async def download_file(tool_name: str, session_uuid: str, filename: str):
|
|||||||
if not os.path.exists(file_path) or not os.path.isfile(file_path):
|
if not os.path.exists(file_path) or not os.path.isfile(file_path):
|
||||||
raise HTTPException(status_code=404, detail="Файл не найден или срок его действия истек")
|
raise HTTPException(status_code=404, detail="Файл не найден или срок его действия истек")
|
||||||
|
|
||||||
# Определение MIME-типа
|
|
||||||
media_type = "application/octet-stream"
|
media_type = "application/octet-stream"
|
||||||
if safe_filename.endswith(".md") or safe_filename.endswith(".txt"):
|
if safe_filename.endswith((".md", ".txt")):
|
||||||
media_type = "text/markdown; charset=utf-8"
|
media_type = "text/markdown; charset=utf-8"
|
||||||
elif safe_filename.endswith(".xlsx"):
|
elif safe_filename.endswith(".xlsx"):
|
||||||
media_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
media_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||||
elif safe_filename.endswith(".pdf"):
|
elif safe_filename.endswith(".pdf"):
|
||||||
media_type = "application/pdf"
|
media_type = "application/pdf"
|
||||||
|
|
||||||
# Корректная кодировка для кириллических имен файлов
|
|
||||||
encoded_filename = urllib.parse.quote(safe_filename)
|
encoded_filename = urllib.parse.quote(safe_filename)
|
||||||
|
|
||||||
return FileResponse(
|
return FileResponse(
|
||||||
@@ -71,3 +70,46 @@ async def download_file(tool_name: str, session_uuid: str, filename: str):
|
|||||||
"Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}"
|
"Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}"
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _find_report_recursively(target_filename: str) -> Optional[str]:
|
||||||
|
"""Рекурсивный поиск файла отчета по имени в /tmp и во всех подпапках output/reports/."""
|
||||||
|
# 1. Проверяем /tmp/scud_reports
|
||||||
|
tmp_path = os.path.join(TEMP_REPORTS_DIR, target_filename)
|
||||||
|
if os.path.exists(tmp_path) and os.path.isfile(tmp_path):
|
||||||
|
return tmp_path
|
||||||
|
|
||||||
|
# 2. Проверяем прямой путь в output/reports
|
||||||
|
direct_path = os.path.join(REPORTS_DIR, target_filename)
|
||||||
|
if os.path.exists(direct_path) and os.path.isfile(direct_path):
|
||||||
|
return direct_path
|
||||||
|
|
||||||
|
# 3. Рекурсивный поиск по подкаталогам (год/месяц)
|
||||||
|
for root, _, files in os.walk(REPORTS_DIR):
|
||||||
|
if target_filename in files:
|
||||||
|
return os.path.join(root, target_filename)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/download/reports/{filename:path}")
|
||||||
|
async def download_report_direct(filename: str):
|
||||||
|
"""
|
||||||
|
Прямое скачивание отчетов:
|
||||||
|
- Декодирует UTF-8 URL (%20 -> пробел).
|
||||||
|
- Ищет файл в output/reports/{YEAR}/{MONTH} и во временном буфере /tmp/scud_reports.
|
||||||
|
"""
|
||||||
|
decoded_name = urllib.parse.unquote(filename).strip()
|
||||||
|
safe_filename = os.path.basename(decoded_name)
|
||||||
|
|
||||||
|
target_path = _find_report_recursively(safe_filename)
|
||||||
|
|
||||||
|
if not target_path or not os.path.exists(target_path):
|
||||||
|
raise HTTPException(status_code=404, detail="Отчет не найден")
|
||||||
|
|
||||||
|
encoded_filename = urllib.parse.quote(safe_filename)
|
||||||
|
return FileResponse(
|
||||||
|
path=target_path,
|
||||||
|
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
|
headers={"Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}"}
|
||||||
|
)
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: modules/web_api/routers/manual_absences.py
|
||||||
|
ROLE: REST API эндпоинты для реестров "Мест. командир.", "Иное" и автокомплита.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
from fastapi import APIRouter, HTTPException, Query
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from typing import Optional, List, Dict, Any
|
||||||
|
|
||||||
|
from services.manual_absences_repo import (
|
||||||
|
search_staff_suggestions,
|
||||||
|
get_static_reasons,
|
||||||
|
add_manual_absence,
|
||||||
|
delete_manual_absence,
|
||||||
|
get_manual_absences_list
|
||||||
|
)
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/v1/manual-absences", tags=["Manual Absences"])
|
||||||
|
|
||||||
|
|
||||||
|
class AddAbsenceRequest(BaseModel):
|
||||||
|
absence_type: str # 'LOCAL_TRIP' или 'OTHER'
|
||||||
|
fio: str
|
||||||
|
reason: Optional[str] = ""
|
||||||
|
department: Optional[str] = ""
|
||||||
|
position: Optional[str] = ""
|
||||||
|
date_start: Optional[str] = None
|
||||||
|
date_end: Optional[str] = None
|
||||||
|
comment: Optional[str] = ""
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/staff-autocomplete")
|
||||||
|
def api_staff_autocomplete(q: str = Query(..., min_length=2)):
|
||||||
|
return search_staff_suggestions(q)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/reasons")
|
||||||
|
def api_get_reasons():
|
||||||
|
return {"reasons": get_static_reasons()}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/")
|
||||||
|
def api_list_manual_absences(type: Optional[str] = None):
|
||||||
|
return {"items": get_manual_absences_list(type)}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/")
|
||||||
|
def api_add_manual_absence(req: AddAbsenceRequest):
|
||||||
|
reason = req.reason or ("Местная командировка" if req.absence_type == "LOCAL_TRIP" else "Иное")
|
||||||
|
res_id = add_manual_absence(
|
||||||
|
absence_type=req.absence_type,
|
||||||
|
fio=req.fio,
|
||||||
|
reason=reason,
|
||||||
|
department=req.department,
|
||||||
|
position=req.position,
|
||||||
|
date_start=req.date_start,
|
||||||
|
date_end=req.date_end,
|
||||||
|
comment=req.comment
|
||||||
|
)
|
||||||
|
if not res_id:
|
||||||
|
raise HTTPException(status_code=400, detail="Не удалось добавить запись")
|
||||||
|
return {"status": "success", "id": res_id}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/{item_id}")
|
||||||
|
def api_delete_manual_absence(item_id: int):
|
||||||
|
if not delete_manual_absence(item_id):
|
||||||
|
raise HTTPException(status_code=404, detail="Запись не найдена")
|
||||||
|
return {"status": "success"}
|
||||||
|
|
||||||
|
class UpdateAbsenceDatesRequest(BaseModel):
|
||||||
|
id: int
|
||||||
|
date_start: Optional[str] = None
|
||||||
|
date_end: Optional[str] = None
|
||||||
|
|
||||||
|
@router.put("/{item_id}")
|
||||||
|
def api_update_manual_absence(item_id: int, req: UpdateAbsenceDatesRequest):
|
||||||
|
with get_connection() as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("""
|
||||||
|
UPDATE manual_absences
|
||||||
|
SET date_start = ?, date_end = ?
|
||||||
|
WHERE id = ?
|
||||||
|
""", (req.date_start, req.date_end, item_id))
|
||||||
|
conn.commit()
|
||||||
|
return {"status": "success"}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: modules/web_api/routers/presence.py
|
||||||
|
ROLE: REST API оперативного статуса присутствия сотрудников в здании.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Query
|
||||||
|
from typing import Optional, Dict, Any
|
||||||
|
|
||||||
|
from services.presence_service import get_live_presence
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/v1/presence", tags=["Presence"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/live")
|
||||||
|
def api_get_live_presence(
|
||||||
|
date_str: Optional[str] = Query(None, description="Дата в формате ДД.ММ.ГГГГ"),
|
||||||
|
force_refresh: bool = Query(False, description="Принудительный опрос MS SQL Орион")
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Возвращает оперативный статус сотрудников («Кто в здании»).
|
||||||
|
По умолчанию возвращает срез моментально из локальной базы SQLite.
|
||||||
|
При force_refresh=true выполняет опрос турникетов в MS SQL Орион.
|
||||||
|
"""
|
||||||
|
return get_live_presence(date_str=date_str, force_refresh=force_refresh)
|
||||||
@@ -0,0 +1,157 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: modules/web_api/routers/remote_workers.py
|
||||||
|
ROLE: REST API реестра удаленщиков (CRUD, редактирование сроков, автоочистка).
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
import pandas as pd
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from typing import Optional, List, Dict, Any
|
||||||
|
|
||||||
|
from routers.auth import get_current_user
|
||||||
|
from config import BASE_DIR, DATA_DIR
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/v1/remote-workers", tags=["RemoteWorkers"])
|
||||||
|
CSV_PATH = os.path.join(DATA_DIR, "static_reason_workers.csv")
|
||||||
|
|
||||||
|
|
||||||
|
def _load_workers() -> List[Dict[str, Any]]:
|
||||||
|
"""Читает CSV, гарантирует структуру колонок и удаляет просроченные записи."""
|
||||||
|
if not os.path.exists(CSV_PATH):
|
||||||
|
return []
|
||||||
|
try:
|
||||||
|
df = pd.read_csv(CSV_PATH, dtype=str, on_bad_lines='skip').fillna("")
|
||||||
|
for col in ["fio", "department", "reason", "date_from", "date_to"]:
|
||||||
|
if col not in df.columns:
|
||||||
|
df[col] = ""
|
||||||
|
|
||||||
|
today_date = datetime.now().date()
|
||||||
|
valid_workers = []
|
||||||
|
has_expired = False
|
||||||
|
|
||||||
|
for row in df.to_dict(orient="records"):
|
||||||
|
fio = str(row.get("fio", "")).strip()
|
||||||
|
if not fio:
|
||||||
|
continue
|
||||||
|
|
||||||
|
d_to_str = str(row.get("date_to", "")).strip()
|
||||||
|
if d_to_str and d_to_str.lower() not in ["nan", "none", ""]:
|
||||||
|
try:
|
||||||
|
d_to = datetime.strptime(d_to_str.replace('_', '.'), "%d.%m.%Y").date()
|
||||||
|
# Если срок завершился вчера или ранее — запись удаляется из файла
|
||||||
|
if today_date > d_to:
|
||||||
|
has_expired = True
|
||||||
|
continue
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
valid_workers.append(row)
|
||||||
|
|
||||||
|
# Синхронная перезапись файла при обнаружении истекших сроков
|
||||||
|
if has_expired:
|
||||||
|
_save_workers(valid_workers)
|
||||||
|
|
||||||
|
return valid_workers
|
||||||
|
except Exception:
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
def _save_workers(workers: List[Dict[str, Any]]):
|
||||||
|
"""Сохраняет актуальный список в CSV с сохранением колонок."""
|
||||||
|
df = pd.DataFrame(workers)
|
||||||
|
for col in ["fio", "department", "reason", "date_from", "date_to"]:
|
||||||
|
if col not in df.columns:
|
||||||
|
df[col] = ""
|
||||||
|
os.makedirs(os.path.dirname(CSV_PATH), exist_ok=True)
|
||||||
|
df.to_csv(CSV_PATH, index=False, encoding="utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
class RemoteWorkerItem(BaseModel):
|
||||||
|
fio: str
|
||||||
|
department: Optional[str] = "Все"
|
||||||
|
reason: Optional[str] = "Удаленная работа"
|
||||||
|
date_from: Optional[str] = ""
|
||||||
|
date_to: Optional[str] = ""
|
||||||
|
|
||||||
|
|
||||||
|
class UpdateDatesRequest(BaseModel):
|
||||||
|
fio: str
|
||||||
|
date_from: Optional[str] = ""
|
||||||
|
date_to: Optional[str] = ""
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("")
|
||||||
|
def api_get_remote_workers(current_user = Depends(get_current_user)):
|
||||||
|
return {"workers": _load_workers()}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("")
|
||||||
|
def api_add_remote_worker(item: RemoteWorkerItem, current_user = Depends(get_current_user)):
|
||||||
|
workers = _load_workers()
|
||||||
|
fio_clean = item.fio.strip()
|
||||||
|
if not fio_clean:
|
||||||
|
raise HTTPException(status_code=400, detail="ФИО не может быть пустым")
|
||||||
|
|
||||||
|
# Если начало не указано — берем сегодня
|
||||||
|
today_str = datetime.now().strftime("%d.%m.%Y")
|
||||||
|
date_from = item.date_from.strip() if item.date_from and item.date_from.strip() else today_str
|
||||||
|
date_to = item.date_to.strip() if item.date_to else ""
|
||||||
|
|
||||||
|
# Проверка на совпадение ФИО (обновление существующей записи)
|
||||||
|
for w in workers:
|
||||||
|
if str(w.get("fio", "")).strip().lower() == fio_clean.lower():
|
||||||
|
w["department"] = item.department.strip() if item.department else (w.get("department") or "Все")
|
||||||
|
w["reason"] = item.reason.strip() if item.reason else (w.get("reason") or "Удаленная работа")
|
||||||
|
w["date_from"] = date_from
|
||||||
|
w["date_to"] = date_to
|
||||||
|
_save_workers(workers)
|
||||||
|
return {"status": "success", "message": "Срок удаленки обновлен", "workers": workers}
|
||||||
|
|
||||||
|
workers.append({
|
||||||
|
"fio": fio_clean,
|
||||||
|
"department": item.department.strip() if item.department else "Все",
|
||||||
|
"reason": item.reason.strip() if item.reason else "Удаленная работа",
|
||||||
|
"date_from": date_from,
|
||||||
|
"date_to": date_to
|
||||||
|
})
|
||||||
|
_save_workers(workers)
|
||||||
|
return {"status": "success", "workers": workers}
|
||||||
|
|
||||||
|
|
||||||
|
@router.put("")
|
||||||
|
def api_update_worker_dates(req: UpdateDatesRequest, current_user = Depends(get_current_user)):
|
||||||
|
"""Редактирование срока удаленки (продление или сокращение)."""
|
||||||
|
workers = _load_workers()
|
||||||
|
target_fio = req.fio.strip().lower()
|
||||||
|
found = False
|
||||||
|
|
||||||
|
for w in workers:
|
||||||
|
if str(w.get("fio", "")).strip().lower() == target_fio:
|
||||||
|
w["date_from"] = req.date_from.strip() if req.date_from is not None else w.get("date_from", "")
|
||||||
|
w["date_to"] = req.date_to.strip() if req.date_to is not None else w.get("date_to", "")
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not found:
|
||||||
|
raise HTTPException(status_code=404, detail="Сотрудник не найден в списке")
|
||||||
|
|
||||||
|
_save_workers(workers)
|
||||||
|
return {"status": "success", "message": "Сроки успешно изменены", "workers": workers}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("")
|
||||||
|
def api_delete_remote_worker(fio: str, current_user = Depends(get_current_user)):
|
||||||
|
workers = _load_workers()
|
||||||
|
fio_clean = fio.strip().lower()
|
||||||
|
initial_len = len(workers)
|
||||||
|
workers = [w for w in workers if str(w.get("fio", "")).strip().lower() != fio_clean]
|
||||||
|
|
||||||
|
if len(workers) == initial_len:
|
||||||
|
raise HTTPException(status_code=404, detail="Сотрудник не найден")
|
||||||
|
|
||||||
|
_save_workers(workers)
|
||||||
|
return {"status": "success", "workers": workers}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: modules/web_api/routers/reports.py
|
||||||
|
ROLE: REST API On-Demand генерации отчетов:
|
||||||
|
- Сводка: за СЕГОДНЯ (оперативный контроль, текущий срез).
|
||||||
|
- Детальный и Упрощенный отчет: строго за ВЧЕРА по финальному срезу Y.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import logging
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from typing import Optional
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from fastapi import APIRouter, HTTPException
|
||||||
|
|
||||||
|
from config import DATE_TODAY, DATE_YESTERDAY
|
||||||
|
from services.scud_etl.svodka_generator import generate_svodka_service
|
||||||
|
from services.scud_etl.otchet_generator import generate_otchet_service
|
||||||
|
from services.reports.simplified_builder import generate_simplified_excel
|
||||||
|
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
|
||||||
|
|
||||||
|
logger = logging.getLogger("REPORTS_API")
|
||||||
|
router = APIRouter(prefix="/api/v1/reports", tags=["Reports"])
|
||||||
|
|
||||||
|
|
||||||
|
def get_previous_workday(target_date_str: str) -> str:
|
||||||
|
"""Вычисляет дату предыдущей рабочей смены (в понедельник возвращает пятницу)."""
|
||||||
|
clean_date = target_date_str.replace('_', '.')
|
||||||
|
try:
|
||||||
|
dt = datetime.strptime(clean_date, "%d.%m.%Y")
|
||||||
|
days_back = 3 if dt.weekday() == 0 else 1
|
||||||
|
return (dt - timedelta(days=days_back)).strftime("%d.%m.%Y")
|
||||||
|
except Exception:
|
||||||
|
return DATE_YESTERDAY
|
||||||
|
|
||||||
|
|
||||||
|
class GenerateReportRequest(BaseModel):
|
||||||
|
date: Optional[str] = None # ДД.ММ.ГГГГ
|
||||||
|
time: Optional[str] = None # ЧЧ:ММ
|
||||||
|
report_type: str # 'SVODKA', 'DETAILED', 'SIMPLIFIED', 'ALL'
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/generate")
|
||||||
|
def api_generate_report(req: GenerateReportRequest):
|
||||||
|
"""
|
||||||
|
Генерирует выбранный отчет:
|
||||||
|
- SVODKA: на текущую дату (date).
|
||||||
|
- DETAILED / SIMPLIFIED: строго за вчерашний рабочий день по финальному срезу Y.
|
||||||
|
"""
|
||||||
|
target_date = (req.date or DATE_TODAY).replace('_', '.')
|
||||||
|
yesterday_date = get_previous_workday(target_date)
|
||||||
|
r_type = req.report_type.upper()
|
||||||
|
results = []
|
||||||
|
|
||||||
|
# 1. Ежедневная сводка (за СЕГОДНЯ)
|
||||||
|
if r_type in ["SVODKA", "ALL"]:
|
||||||
|
res_svodka = generate_svodka_service(
|
||||||
|
target_date=target_date,
|
||||||
|
target_time=req.time
|
||||||
|
)
|
||||||
|
if res_svodka.get("status") == "success":
|
||||||
|
results.append({
|
||||||
|
"type": "Сводка",
|
||||||
|
"filename": res_svodka.get("filename"),
|
||||||
|
"download_url": res_svodka.get("download_url"),
|
||||||
|
"status": "success"
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
results.append({
|
||||||
|
"type": "Сводка",
|
||||||
|
"error": res_svodka.get("message"),
|
||||||
|
"status": "error"
|
||||||
|
})
|
||||||
|
|
||||||
|
# 2. Детальный отчет (строго за ВЧЕРА по финальному срезу Y)
|
||||||
|
if r_type in ["DETAILED", "ALL"]:
|
||||||
|
res_det = generate_otchet_service(target_date=yesterday_date)
|
||||||
|
if res_det.get("status") == "success":
|
||||||
|
results.append({
|
||||||
|
"type": "Детальный отчет",
|
||||||
|
"filename": res_det.get("filename"),
|
||||||
|
"download_url": res_det.get("download_url"),
|
||||||
|
"status": "success"
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
results.append({
|
||||||
|
"type": "Детальный отчет",
|
||||||
|
"error": res_det.get("message"),
|
||||||
|
"status": "error"
|
||||||
|
})
|
||||||
|
|
||||||
|
# 3. Упрощенный отчет (строго за ВЧЕРА по финальному срезу Y)
|
||||||
|
if r_type in ["SIMPLIFIED", "ALL"]:
|
||||||
|
try:
|
||||||
|
df_scud_y = load_best_snapshot_for_date(yesterday_date, prefer_final_y=True)
|
||||||
|
if df_scud_y is not None and not df_scud_y.empty:
|
||||||
|
df_staff_y, df_abs_y = load_1c_files_for_date(yesterday_date)
|
||||||
|
df_merged_y = merge_scud_and_1c(df_scud_y, df_staff_y, df_abs_y)
|
||||||
|
out_path = generate_simplified_excel(df_merged_y, date_str=yesterday_date)
|
||||||
|
filename = os.path.basename(out_path)
|
||||||
|
results.append({
|
||||||
|
"type": "Упрощенный отчет",
|
||||||
|
"filename": filename,
|
||||||
|
"download_url": f"/api/v1/files/download/reports/{filename}",
|
||||||
|
"status": "success"
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
results.append({
|
||||||
|
"type": "Упрощенный отчет",
|
||||||
|
"error": f"Финальный срез СКУД (Y) за {yesterday_date} не найден.",
|
||||||
|
"status": "error"
|
||||||
|
})
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(f"Ошибка формирования упрощенного отчета: {e}")
|
||||||
|
results.append({
|
||||||
|
"type": "Упрощенный отчет",
|
||||||
|
"error": str(e),
|
||||||
|
"status": "error"
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
"date": target_date,
|
||||||
|
"yesterday_date": yesterday_date,
|
||||||
|
"requested_type": r_type,
|
||||||
|
"reports": results
|
||||||
|
}
|
||||||
@@ -0,0 +1,400 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: modules/web_api/routers/snapshots.py
|
||||||
|
ROLE: Роутер срезов СКУД:
|
||||||
|
- Список срезов за период дат (/api/v1/snapshots)
|
||||||
|
- Инспекция среза (/api/v1/snapshots/{snapshot_id}/details и /inspect)
|
||||||
|
- Экспорт среза в Excel (.xlsx) и CSV (UTF-8 с BOM)
|
||||||
|
- Ручное создание среза (/create)
|
||||||
|
- Удаление срезов (пакетное через snapshot_ids)
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import io
|
||||||
|
import urllib.parse
|
||||||
|
import logging
|
||||||
|
from typing import Optional, List, Dict, Any
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from fastapi import APIRouter, HTTPException, Query
|
||||||
|
from fastapi.responses import StreamingResponse, FileResponse
|
||||||
|
from pydantic import BaseModel
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
from core.connection import get_connection
|
||||||
|
import config
|
||||||
|
|
||||||
|
logger = logging.getLogger("SNAPSHOTS_ROUTER")
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/v1/snapshots", tags=["Snapshots"])
|
||||||
|
|
||||||
|
BASE_DIR = getattr(config, "BASE_DIR", Path(__file__).resolve().parent.parent.parent.parent)
|
||||||
|
SNAPSHOTS_DIR = getattr(config, "SNAPSHOTS_DIR", os.path.join(str(BASE_DIR), "exports", "snapshots"))
|
||||||
|
DATE_TODAY = getattr(config, "DATE_TODAY", datetime.now().strftime("%d.%m.%Y"))
|
||||||
|
|
||||||
|
|
||||||
|
class CreateSnapshotRequest(BaseModel):
|
||||||
|
date_str: Optional[str] = None
|
||||||
|
time_str: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
class DeleteSnapshotsRequest(BaseModel):
|
||||||
|
snapshot_ids: List[str]
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# ВСПОМОГАТЕЛЬНЫЕ ФУНКЦИИ РАСЧЕТА СТАТУСА И ВРЕМЕНИ
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
def parse_time_str(t_str: str) -> Optional[datetime]:
|
||||||
|
"""Парсит строку времени HH:MM[:SS] в datetime объект."""
|
||||||
|
if not t_str or str(t_str).strip() in ("Нет входа", "Нет выхода", "—", "-", "None", "nan"):
|
||||||
|
return None
|
||||||
|
for fmt in ("%H:%M:%S", "%H:%M"):
|
||||||
|
try:
|
||||||
|
return datetime.strptime(str(t_str).strip(), fmt)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_row_presence(d: dict, snapshot_time_str: str) -> dict:
|
||||||
|
"""
|
||||||
|
Определяет реальный статус сотрудника и время нахождения в здании.
|
||||||
|
"""
|
||||||
|
fio = d.get("fio") or d.get("Сотрудник") or "—"
|
||||||
|
dept = d.get("department") or d.get("Подразделение") or "—"
|
||||||
|
|
||||||
|
t_in = d.get("time_in") or d.get("Начало_дня") or d.get("first_in") or "Нет входа"
|
||||||
|
first_act = d.get("first_activity") or d.get("Первая_активность") or "—"
|
||||||
|
t_out = d.get("time_out") or d.get("Конец_дня") or d.get("last_out") or "Нет выхода"
|
||||||
|
|
||||||
|
db_in_bld = d.get("in_building") or d.get("Находился_в_здании")
|
||||||
|
db_status = d.get("status") or d.get("Статус") or d.get("Пришел")
|
||||||
|
|
||||||
|
has_in = t_in not in ("Нет входа", "—", "-", "", None, "None")
|
||||||
|
has_act = first_act not in ("—", "-", "", None, "None")
|
||||||
|
has_out = t_out not in ("Нет выхода", "—", "-", "", None, "None")
|
||||||
|
|
||||||
|
# 1. Если нет отметок прохода — сотрудник отсутствовал
|
||||||
|
if not has_in and not has_act and not has_out:
|
||||||
|
final_status = "Отсутствовал (Нет событий)"
|
||||||
|
final_in_bld = "00:00"
|
||||||
|
else:
|
||||||
|
# Сотрудник присутствовал
|
||||||
|
final_status = "Присутствовал"
|
||||||
|
|
||||||
|
# Расчет времени нахождения в здании
|
||||||
|
dt_in = parse_time_str(t_in) or parse_time_str(first_act)
|
||||||
|
dt_out = parse_time_str(t_out)
|
||||||
|
|
||||||
|
if db_in_bld and db_in_bld not in ("00:00", "—", "", "None"):
|
||||||
|
final_in_bld = db_in_bld
|
||||||
|
elif dt_in:
|
||||||
|
if dt_out and dt_out >= dt_in:
|
||||||
|
diff = dt_out - dt_in
|
||||||
|
else:
|
||||||
|
# Если выхода еще нет — считаем до момента фиксации среза
|
||||||
|
dt_snap = parse_time_str(snapshot_time_str) or datetime.now()
|
||||||
|
if dt_snap >= dt_in:
|
||||||
|
diff = dt_snap - dt_in
|
||||||
|
else:
|
||||||
|
diff = timedelta(0)
|
||||||
|
|
||||||
|
total_minutes = int(diff.total_seconds() // 60)
|
||||||
|
hh = total_minutes // 60
|
||||||
|
mm = total_minutes % 60
|
||||||
|
final_in_bld = f"{hh:02d}:{mm:02d}"
|
||||||
|
else:
|
||||||
|
final_in_bld = "00:00"
|
||||||
|
|
||||||
|
# Сохраняем специальные статусы, если они зафиксированы в БД
|
||||||
|
if db_status and "Отсутств" in str(db_status):
|
||||||
|
final_status = db_status
|
||||||
|
|
||||||
|
return {
|
||||||
|
"fio": fio,
|
||||||
|
"department": dept,
|
||||||
|
"time_in": t_in,
|
||||||
|
"first_activity": first_act,
|
||||||
|
"time_out": t_out,
|
||||||
|
"in_building": final_in_bld,
|
||||||
|
"status": final_status
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# 1. СПИСОК СРЕЗОВ (С ПОДДЕРЖКОЙ ДИАПАЗОНА ДАТ)
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
@router.get("", include_in_schema=False)
|
||||||
|
@router.get("/")
|
||||||
|
def list_snapshots(
|
||||||
|
date: Optional[str] = None,
|
||||||
|
date_from: Optional[str] = None,
|
||||||
|
date_to: Optional[str] = None
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Возвращает список срезов за диапазон дат со всеми полями для интерфейса.
|
||||||
|
"""
|
||||||
|
d_from = (date_from or date or DATE_TODAY).replace('_', '.')
|
||||||
|
d_to = (date_to or date or DATE_TODAY).replace('_', '.')
|
||||||
|
|
||||||
|
with get_connection(row_factory=True) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT snapshot_id, COUNT(*) as cnt, MAX(created_at) as created_at, log_date
|
||||||
|
FROM scud_logs
|
||||||
|
WHERE (
|
||||||
|
substr(log_date, 7, 4) || '-' || substr(log_date, 4, 2) || '-' || substr(log_date, 1, 2)
|
||||||
|
BETWEEN
|
||||||
|
substr(?, 7, 4) || '-' || substr(?, 4, 2) || '-' || substr(?, 1, 2)
|
||||||
|
AND
|
||||||
|
substr(?, 7, 4) || '-' || substr(?, 4, 2) || '-' || substr(?, 1, 2)
|
||||||
|
)
|
||||||
|
AND snapshot_id IS NOT NULL AND snapshot_id != ''
|
||||||
|
GROUP BY snapshot_id
|
||||||
|
ORDER BY snapshot_id DESC
|
||||||
|
""", (d_from, d_from, d_from, d_to, d_to, d_to))
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
|
||||||
|
items = []
|
||||||
|
for r in rows:
|
||||||
|
raw_id = r["snapshot_id"]
|
||||||
|
clean_id = str(raw_id).lstrip("#").strip()
|
||||||
|
total_cnt = r["cnt"]
|
||||||
|
|
||||||
|
time_str = "—"
|
||||||
|
if "_" in clean_id:
|
||||||
|
parts = clean_id.split("_")
|
||||||
|
if len(parts) > 1 and len(parts[1]) >= 4 and parts[1][:4].isdigit():
|
||||||
|
time_str = f"{parts[1][:2]}:{parts[1][2:4]}"
|
||||||
|
|
||||||
|
is_final = "FINAL" in clean_id.upper()
|
||||||
|
|
||||||
|
items.append({
|
||||||
|
"id": clean_id,
|
||||||
|
"snapshot_id": clean_id,
|
||||||
|
"label": clean_id,
|
||||||
|
"snapshot_time": time_str,
|
||||||
|
"time": time_str,
|
||||||
|
"record_count": total_cnt,
|
||||||
|
"count": total_cnt,
|
||||||
|
"records_count": total_cnt,
|
||||||
|
"is_final": is_final,
|
||||||
|
"date": r["log_date"],
|
||||||
|
"created_at": r["created_at"] or r["log_date"]
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
"date_from": d_from,
|
||||||
|
"date_to": d_to,
|
||||||
|
"total_snapshots": len(items),
|
||||||
|
"snapshots": items
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# 2. ИНСПЕКЦИЯ СРЕЗА (ДЛЯ МОДАЛЬНОГО ОКНА)
|
||||||
|
# Поддерживает оба пути: /details и /inspect
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
@router.get("/{snapshot_id}/details")
|
||||||
|
@router.get("/{snapshot_id}/inspect")
|
||||||
|
def inspect_snapshot(snapshot_id: str):
|
||||||
|
clean_id = snapshot_id.lstrip("#").strip()
|
||||||
|
|
||||||
|
target_date = ""
|
||||||
|
snapshot_time = "23:59:59"
|
||||||
|
if "_" in clean_id:
|
||||||
|
parts = clean_id.split("_")
|
||||||
|
if len(parts[0]) == 8 and parts[0].isdigit():
|
||||||
|
target_date = f"{parts[0][6:8]}.{parts[0][4:6]}.{parts[0][:4]}"
|
||||||
|
if len(parts) > 1 and len(parts[1]) >= 4 and parts[1][:4].isdigit():
|
||||||
|
snapshot_time = f"{parts[1][:2]}:{parts[1][2:4]}:00"
|
||||||
|
|
||||||
|
rows = []
|
||||||
|
with get_connection(row_factory=True) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
# Безопасная выборка всех полей строки среза
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT * FROM scud_logs
|
||||||
|
WHERE snapshot_id IN (?, ?, ?, ?)
|
||||||
|
ORDER BY id ASC
|
||||||
|
""", (clean_id, f"#{clean_id}", f"#{snapshot_id}", snapshot_id))
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
|
||||||
|
# Резервный сбор на лету из scud_events_raw, если среза нет в scud_logs
|
||||||
|
if not rows and target_date:
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT
|
||||||
|
fio,
|
||||||
|
department,
|
||||||
|
MIN(CASE WHEN direction = 'IN' THEN time_val END) as time_in,
|
||||||
|
NULL as first_activity,
|
||||||
|
MAX(CASE WHEN direction = 'OUT' THEN time_val END) as time_out,
|
||||||
|
'00:00' as in_building,
|
||||||
|
'Присутствовал' as status
|
||||||
|
FROM scud_events_raw
|
||||||
|
WHERE log_date = ?
|
||||||
|
GROUP BY fio_clean
|
||||||
|
ORDER BY fio ASC
|
||||||
|
""", (target_date,))
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
|
||||||
|
if not rows:
|
||||||
|
raise HTTPException(status_code=404, detail=f"Срез '{clean_id}' не найден")
|
||||||
|
|
||||||
|
records = []
|
||||||
|
for r in rows:
|
||||||
|
calc = calculate_row_presence(dict(r), snapshot_time)
|
||||||
|
records.append({
|
||||||
|
"hoz_organ": dict(r).get("hoz_organ") or dict(r).get("tab_num") or "",
|
||||||
|
"fio": calc["fio"],
|
||||||
|
"Сотрудник": calc["fio"],
|
||||||
|
"department": calc["department"],
|
||||||
|
"Подразделение": calc["department"],
|
||||||
|
"time_in": calc["time_in"],
|
||||||
|
"Вход": calc["time_in"],
|
||||||
|
"first_activity": calc["first_activity"],
|
||||||
|
"time_out": calc["time_out"],
|
||||||
|
"Выход": calc["time_out"],
|
||||||
|
"in_building": calc["in_building"],
|
||||||
|
"Находился_в_здании": calc["in_building"],
|
||||||
|
"status": calc["status"],
|
||||||
|
"Пришел": calc["status"]
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "ok",
|
||||||
|
"snapshot_id": clean_id,
|
||||||
|
"date": target_date or DATE_TODAY,
|
||||||
|
"total_records": len(records),
|
||||||
|
"count": len(records),
|
||||||
|
"records": records,
|
||||||
|
"data": records
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# 3. ЭКСПОРТ ДАННЫХ ИНСПЕКЦИИ СРЕЗА (EXCEL / CSV)
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
@router.get("/{snapshot_id}/export")
|
||||||
|
def api_export_snapshot(snapshot_id: str, format: str = Query("xlsx")):
|
||||||
|
clean_id = snapshot_id.lstrip("#").strip()
|
||||||
|
|
||||||
|
target_date = ""
|
||||||
|
snapshot_time = "23:59:59"
|
||||||
|
if "_" in clean_id:
|
||||||
|
parts = clean_id.split("_")
|
||||||
|
if len(parts[0]) == 8 and parts[0].isdigit():
|
||||||
|
target_date = f"{parts[0][6:8]}.{parts[0][4:6]}.{parts[0][:4]}"
|
||||||
|
if len(parts) > 1 and len(parts[1]) >= 4 and parts[1][:4].isdigit():
|
||||||
|
snapshot_time = f"{parts[1][:2]}:{parts[1][2:4]}:00"
|
||||||
|
|
||||||
|
rows = []
|
||||||
|
with get_connection(row_factory=True) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT * FROM scud_logs
|
||||||
|
WHERE snapshot_id IN (?, ?, ?, ?)
|
||||||
|
ORDER BY id ASC
|
||||||
|
""", (clean_id, f"#{clean_id}", f"#{snapshot_id}", snapshot_id))
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
|
||||||
|
if not rows:
|
||||||
|
raise HTTPException(status_code=404, detail=f"Срез '{clean_id}' не найден")
|
||||||
|
|
||||||
|
export_list = []
|
||||||
|
for r in rows:
|
||||||
|
calc = calculate_row_presence(dict(r), snapshot_time)
|
||||||
|
export_list.append({
|
||||||
|
"Сотрудник": calc["fio"],
|
||||||
|
"Подразделение": calc["department"],
|
||||||
|
"Вход": calc["time_in"],
|
||||||
|
"Первая активность": calc["first_activity"],
|
||||||
|
"Выход": calc["time_out"],
|
||||||
|
"В здании": calc["in_building"],
|
||||||
|
"Статус": calc["status"]
|
||||||
|
})
|
||||||
|
|
||||||
|
out_df = pd.DataFrame(export_list)
|
||||||
|
filename_base = f"Инспекция_{clean_id}"
|
||||||
|
|
||||||
|
if format.lower() == "csv":
|
||||||
|
csv_bytes = out_df.to_csv(index=False, sep=";", encoding="utf-8-sig").encode("utf-8-sig")
|
||||||
|
filename = f"{filename_base}.csv"
|
||||||
|
encoded = urllib.parse.quote(filename)
|
||||||
|
return StreamingResponse(
|
||||||
|
io.BytesIO(csv_bytes),
|
||||||
|
media_type="text/csv; charset=utf-8",
|
||||||
|
headers={"Content-Disposition": f"attachment; filename*=UTF-8''{encoded}"}
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
output = io.BytesIO()
|
||||||
|
with pd.ExcelWriter(output, engine="openpyxl") as writer:
|
||||||
|
out_df.to_excel(writer, index=False, sheet_name="Срез")
|
||||||
|
output.seek(0)
|
||||||
|
filename = f"{filename_base}.xlsx"
|
||||||
|
encoded = urllib.parse.quote(filename)
|
||||||
|
return StreamingResponse(
|
||||||
|
output,
|
||||||
|
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
|
headers={"Content-Disposition": f"attachment; filename*=UTF-8''{encoded}"}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# 4. РУЧНОЕ СОЗДАНИЕ И ПАКЕТНОЕ УДАЛЕНИЕ СРЕЗОВ
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
@router.post("/create")
|
||||||
|
def create_snapshot(req: CreateSnapshotRequest):
|
||||||
|
from services.scud_export import run_export
|
||||||
|
target_date = (req.date_str or DATE_TODAY).replace('_', '.')
|
||||||
|
try:
|
||||||
|
run_export(input_date=target_date, save_xlsx=True, debug=False)
|
||||||
|
return {"status": "ok", "message": f"Срез за {target_date} успешно создан"}
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Ошибка создания среза: {e}")
|
||||||
|
raise HTTPException(status_code=500, detail=f"Не удалось создать срез: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("", include_in_schema=False)
|
||||||
|
@router.delete("/")
|
||||||
|
def delete_snapshots(req: DeleteSnapshotsRequest):
|
||||||
|
if not req.snapshot_ids:
|
||||||
|
return {"status": "ok", "deleted": 0}
|
||||||
|
|
||||||
|
with get_connection() as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
for sid in req.snapshot_ids:
|
||||||
|
clean_id = sid.lstrip("#").strip()
|
||||||
|
cursor.execute("DELETE FROM scud_logs WHERE snapshot_id IN (?, ?)", (clean_id, f"#{clean_id}"))
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
return {"status": "ok", "deleted": len(req.snapshot_ids)}
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# 5. СКАЧИВАНИЕ ФИЗИЧЕСКИХ ФАЙЛОВ .XLSX (FALLBACK)
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
@router.get("/{filename}")
|
||||||
|
def download_snapshot_file(filename: str):
|
||||||
|
safe_filename = os.path.basename(filename)
|
||||||
|
file_path = os.path.join(SNAPSHOTS_DIR, safe_filename)
|
||||||
|
|
||||||
|
if not os.path.exists(file_path):
|
||||||
|
raise HTTPException(status_code=404, detail="Файл среза не найден на диске")
|
||||||
|
|
||||||
|
return FileResponse(
|
||||||
|
path=file_path,
|
||||||
|
filename=safe_filename,
|
||||||
|
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||||
|
)
|
||||||
+283
-123
@@ -1,165 +1,325 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="ru">
|
<html lang="ru" class="h-full bg-slate-100">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>SCUD Orion AI — Управление и Аналитика</title>
|
<title>SCUD Orion AI Assistant</title>
|
||||||
<!-- Tailwind CSS CDN -->
|
|
||||||
<script src="https://cdn.tailwindcss.com"></script>
|
<script src="https://cdn.tailwindcss.com"></script>
|
||||||
<!-- FontAwesome Icons -->
|
|
||||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
||||||
<link rel="stylesheet" href="/css/styles.css">
|
<link rel="icon" href="/favicon.ico" type="image/x-icon">
|
||||||
</head>
|
<style>
|
||||||
<body class="bg-slate-100 font-sans h-screen flex overflow-hidden text-slate-800">
|
* { overflow-anchor: none !important; }
|
||||||
|
#chat-messages-container, main { overflow-anchor: none !important; }
|
||||||
|
#chat-messages-container { padding-bottom: clamp(400px, 85vh, 900px) !important; }
|
||||||
|
|
||||||
<!-- Боковая панель (Задачи и Навигация) -->
|
/* ⭐️ Комфортный размер шрифта в стиле Gemini */
|
||||||
<aside id="task-drawer" class="w-80 sm:w-96 bg-white border-r border-slate-200 flex flex-col shrink-0 h-full z-20 shadow-sm transition-all duration-300">
|
#chat-messages-container .message-content,
|
||||||
<!-- Шапка панели задач -->
|
#chat-messages-container .text-sm,
|
||||||
<div class="p-4 border-b border-slate-200 flex items-center justify-between bg-slate-50/70">
|
#chat-messages-container .text-xs,
|
||||||
<div class="flex items-center gap-2">
|
#chat-messages-container p,
|
||||||
<div class="w-8 h-8 rounded-lg bg-indigo-600 flex items-center justify-center text-white shadow-sm">
|
#chat-messages-container li {
|
||||||
<i class="fa-solid fa-list-check text-sm"></i>
|
font-size: 16px !important;
|
||||||
|
line-height: 1.65 !important;
|
||||||
|
color: #1e293b !important; /* slate-800 */
|
||||||
|
}
|
||||||
|
|
||||||
|
#chat-messages-container .user-chat-bubble .text-sm,
|
||||||
|
#chat-messages-container .user-chat-bubble p,
|
||||||
|
#chat-messages-container .user-chat-bubble div {
|
||||||
|
font-size: 16px !important;
|
||||||
|
line-height: 1.6 !important;
|
||||||
|
color: #ffffff !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
#chat-messages-container pre,
|
||||||
|
#chat-messages-container code {
|
||||||
|
font-size: 14.5px !important;
|
||||||
|
line-height: 1.5 !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-chat-bubble { scroll-margin-top: 24px !important; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body class="h-full flex flex-col font-sans antialiased text-slate-800 bg-slate-100 selection:bg-indigo-500 selection:text-white">
|
||||||
|
|
||||||
|
<div id="app-container" class="flex-1 flex overflow-hidden w-full h-full">
|
||||||
|
|
||||||
|
<!-- ЛЕВАЯ КОЛОНКА (САЙДБАР) -->
|
||||||
|
<aside class="w-[420px] md:w-[500px] bg-white border-r border-slate-200 flex flex-col shrink-0 h-full shadow-sm z-10 select-none">
|
||||||
|
|
||||||
|
<!-- НАВИГАЦИОННЫЙ ТАБ-БАР -->
|
||||||
|
<div class="flex items-center justify-between border-b border-slate-200 bg-white px-1 py-1 shrink-0">
|
||||||
|
<button data-tab="tasks" class="sidebar-tab-btn flex-1 py-2 text-center text-xs border-b-2 border-transparent text-slate-500 hover:text-indigo-600 transition">
|
||||||
|
<i class="fa-solid fa-list-check block text-sm mb-0.5"></i> Задачи
|
||||||
|
</button>
|
||||||
|
<button data-tab="snapshots" class="sidebar-tab-btn flex-1 py-2 text-center text-xs border-b-2 border-transparent text-slate-500 hover:text-indigo-600 transition">
|
||||||
|
<i class="fa-solid fa-camera block text-sm mb-0.5"></i> Срезы
|
||||||
|
</button>
|
||||||
|
<button data-tab="registries" class="sidebar-tab-btn flex-1 py-2 text-center text-xs border-b-2 border-transparent text-slate-500 hover:text-indigo-600 transition">
|
||||||
|
<i class="fa-solid fa-address-book block text-sm mb-0.5"></i> Реестры
|
||||||
|
</button>
|
||||||
|
<button data-tab="prompts" class="sidebar-tab-btn flex-1 py-2 text-center text-xs border-b-2 border-transparent text-slate-500 hover:text-indigo-600 transition">
|
||||||
|
<i class="fa-solid fa-terminal block text-sm mb-0.5"></i> Промпт
|
||||||
|
</button>
|
||||||
|
<button data-tab="context" class="sidebar-tab-btn flex-1 py-2 text-center text-xs border-b-2 border-transparent text-slate-500 hover:text-indigo-600 transition">
|
||||||
|
<i class="fa-solid fa-comments block text-sm mb-0.5"></i> Контекст
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- РАБОЧИЕ ОБЛАСТИ ВКЛАДОК -->
|
||||||
|
<div class="flex-1 overflow-y-auto p-2 flex flex-col gap-2">
|
||||||
|
<!-- 1. ЗАДАЧИ -->
|
||||||
|
<div id="sidebar-view-tasks" class="sidebar-view w-full h-full flex flex-col">
|
||||||
|
<div id="tasks-list" class="space-y-2 flex-1"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 2. СРЕЗЫ И ОТЧЕТЫ -->
|
||||||
|
<div id="sidebar-view-snapshots" class="sidebar-view w-full space-y-3 hidden">
|
||||||
|
|
||||||
|
<!-- ПЕРИОД ВЫБОРКИ СРЕЗОВ -->
|
||||||
|
<div class="p-2.5 bg-slate-50 rounded-xl border border-slate-200 shadow-xs space-y-2">
|
||||||
|
<div class="flex items-center justify-between">
|
||||||
|
<span class="text-[11px] font-bold text-slate-600 uppercase flex items-center gap-1.5">
|
||||||
|
<i class="fa-regular fa-calendar text-indigo-500"></i> Период срезов:
|
||||||
|
</span>
|
||||||
|
<button onclick="loadSnapshotsView()" class="text-xs text-indigo-600 hover:text-indigo-800 font-semibold flex items-center gap-1 transition">
|
||||||
|
<i class="fa-solid fa-rotate-right text-[10px]"></i> Найти
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="grid grid-cols-2 gap-2">
|
||||||
|
<div>
|
||||||
|
<label class="text-[10px] text-slate-400 block mb-0.5">С даты:</label>
|
||||||
|
<input type="date" id="snapshots-date-from" onchange="loadSnapshotsView()"
|
||||||
|
class="w-full text-xs px-2 py-1.5 bg-white border border-slate-200 rounded-lg text-slate-700 font-mono focus:outline-none focus:border-indigo-500 cursor-pointer">
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<h2 class="font-bold text-sm text-slate-900 leading-tight">Бэклог задач</h2>
|
<label class="text-[10px] text-slate-400 block mb-0.5">По дату:</label>
|
||||||
<p class="text-[11px] text-slate-500">SCUD Orion AI Roadmap</p>
|
<input type="date" id="snapshots-date-to" onchange="loadSnapshotsView()"
|
||||||
|
class="w-full text-xs px-2 py-1.5 bg-white border border-slate-200 rounded-lg text-slate-700 font-mono focus:outline-none focus:border-indigo-500 cursor-pointer">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<button type="button" onclick="openAddTaskModal()" title="Добавить задачу"
|
<div class="flex items-center gap-1.5 pt-1 border-t border-slate-200/60">
|
||||||
class="px-2.5 py-1.5 bg-indigo-50 hover:bg-indigo-100 text-indigo-700 border border-indigo-200 rounded-lg text-xs font-semibold flex items-center gap-1 transition cursor-pointer">
|
<button onclick="setSnapshotDatePreset('today')" class="px-2 py-0.5 text-[11px] bg-white border border-slate-200 hover:bg-slate-100 rounded-md text-slate-600 font-medium transition shadow-2xs">
|
||||||
<i class="fa-solid fa-plus"></i>
|
Сегодня
|
||||||
<span>Задача</span>
|
</button>
|
||||||
|
<button onclick="setSnapshotDatePreset('yesterday')" class="px-2 py-0.5 text-[11px] bg-white border border-slate-200 hover:bg-slate-100 rounded-md text-slate-600 font-medium transition shadow-2xs">
|
||||||
|
Вчера
|
||||||
|
</button>
|
||||||
|
<button onclick="setSnapshotDatePreset('days3')" class="px-2 py-0.5 text-[11px] bg-white border border-slate-200 hover:bg-slate-100 rounded-md text-slate-600 font-medium transition shadow-2xs">
|
||||||
|
3 дня
|
||||||
|
</button>
|
||||||
|
<button onclick="setSnapshotDatePreset('days7')" class="px-2 py-0.5 text-[11px] bg-white border border-slate-200 hover:bg-slate-100 rounded-md text-slate-600 font-medium transition shadow-2xs">
|
||||||
|
7 дней
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Панель создания срезов -->
|
||||||
|
<div class="p-3 bg-white rounded-xl border border-slate-200 shadow-xs space-y-2">
|
||||||
|
<div class="text-[11px] font-bold text-slate-600 uppercase flex items-center gap-1.5">
|
||||||
|
<i class="fa-regular fa-clock text-indigo-500"></i> Создать срез на дату/время:
|
||||||
|
</div>
|
||||||
|
<div class="grid grid-cols-2 gap-2">
|
||||||
|
<input type="text" id="manual-snapshot-date" placeholder="ДД.ММ.ГГГГ" class="text-xs px-2.5 py-1.5 border border-slate-200 rounded-lg text-center font-mono">
|
||||||
|
<input type="text" id="manual-snapshot-time" placeholder="ЧЧ:ММ" class="text-xs px-2.5 py-1.5 border border-slate-200 rounded-lg text-center font-mono">
|
||||||
|
</div>
|
||||||
|
<button id="btn-create-snapshot" onclick="createSnapshotManual()" class="w-full py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg text-xs font-bold transition flex items-center justify-center gap-1.5">
|
||||||
|
<i class="fa-solid fa-camera"></i> Сделать срез
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Фильтры задач -->
|
<!-- Панель On-Demand генерации отчетов -->
|
||||||
<div class="px-4 py-2.5 border-b border-slate-100 flex items-center justify-between gap-1 text-xs">
|
<div class="p-3 bg-white rounded-xl border border-slate-200 shadow-xs space-y-2">
|
||||||
<button onclick="filterTasksByTab('IN_PROGRESS')" id="tab-in-progress" class="task-tab-btn font-semibold px-2.5 py-1 rounded-md text-indigo-600 bg-indigo-50 transition">В работе</button>
|
<div class="text-[11px] font-bold text-slate-700 uppercase flex items-center gap-1.5">
|
||||||
<button onclick="filterTasksByTab('BACKLOG')" id="tab-backlog" class="task-tab-btn font-medium px-2.5 py-1 rounded-md text-slate-600 hover:bg-slate-100 transition">В планах</button>
|
<i class="fa-solid fa-file-excel text-emerald-600"></i> Сформировать отчет:
|
||||||
<button onclick="filterTasksByTab('COMPLETED')" id="tab-completed" class="task-tab-btn font-medium px-2.5 py-1 rounded-md text-slate-600 hover:bg-slate-100 transition">Готово</button>
|
|
||||||
<button onclick="filterTasksByTab('ALL')" id="tab-all" class="task-tab-btn font-medium px-2.5 py-1 rounded-md text-slate-600 hover:bg-slate-100 transition">Все</button>
|
|
||||||
</div>
|
</div>
|
||||||
|
<div class="grid grid-cols-3 gap-1.5">
|
||||||
<!-- Список задач с прокруткой (ID исправлен на tasks-list) -->
|
<button onclick="generateReportDirect('SVODKA')" class="py-2 bg-emerald-50 hover:bg-emerald-100 text-emerald-700 border border-emerald-200 rounded-lg text-[10px] font-bold transition flex items-center justify-center gap-1">
|
||||||
<div id="tasks-list" class="flex-1 overflow-y-auto p-3 space-y-2.5">
|
<i class="fa-solid fa-table-list"></i> Сводка
|
||||||
<div class="text-center py-8 text-xs text-slate-400">Загрузка задач...</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Подвал панели пользователя -->
|
|
||||||
<div class="p-3 border-t border-slate-200 bg-slate-50/50 flex items-center justify-between text-xs">
|
|
||||||
<div class="flex items-center gap-2">
|
|
||||||
<div class="w-7 h-7 rounded-full bg-slate-300 flex items-center justify-center text-slate-700 font-bold">
|
|
||||||
<i class="fa-solid fa-user text-xs"></i>
|
|
||||||
</div>
|
|
||||||
<div class="truncate">
|
|
||||||
<span id="current-username" class="font-semibold text-slate-900 block truncate">Александр Пушков</span>
|
|
||||||
<span id="user-role-badge" class="text-[10px] text-indigo-600 font-medium">Администратор</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<button onclick="logout()" title="Выйти" class="p-1.5 text-slate-400 hover:text-rose-600 transition">
|
|
||||||
<i class="fa-solid fa-arrow-right-from-bracket"></i>
|
|
||||||
</button>
|
</button>
|
||||||
|
<button onclick="generateReportDirect('SIMPLIFIED')" class="py-2 bg-indigo-50 hover:bg-indigo-100 text-indigo-700 border border-indigo-200 rounded-lg text-[10px] font-bold transition flex items-center justify-center gap-1">
|
||||||
|
<i class="fa-solid fa-file-lines"></i> Упрощ.
|
||||||
|
</button>
|
||||||
|
<button onclick="generateReportDirect('DETAILED')" class="py-2 bg-purple-50 hover:bg-purple-100 text-purple-700 border border-purple-200 rounded-lg text-[10px] font-bold transition flex items-center justify-center gap-1">
|
||||||
|
<i class="fa-solid fa-chart-column"></i> Детальн.
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex items-center justify-between pt-1">
|
||||||
|
<span id="snapshots-count-badge" class="text-xs font-bold text-slate-700">Срезы в базе: 0</span>
|
||||||
|
<button onclick="loadSnapshotsView()" class="text-slate-400 hover:text-indigo-600 text-xs transition" title="Обновить список">
|
||||||
|
<i class="fa-solid fa-rotate-right"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div id="snapshots-list" class="space-y-2"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 3. РЕЕСТРЫ -->
|
||||||
|
<div id="sidebar-view-registries" class="sidebar-view w-full space-y-3 hidden">
|
||||||
|
<div class="grid grid-cols-2 gap-1 p-1 bg-slate-200/60 rounded-xl">
|
||||||
|
<button data-subtab="exceptions" onclick="switchRegistrySubTab('exceptions')" class="registry-subtab-btn py-1 text-[11px] rounded-lg transition text-center flex items-center justify-center gap-1">
|
||||||
|
<i class="fa-solid fa-user-shield text-[10px]"></i> Исключения
|
||||||
|
</button>
|
||||||
|
<button data-subtab="remote" onclick="switchRegistrySubTab('remote')" class="registry-subtab-btn py-1 text-[11px] rounded-lg transition text-center flex items-center justify-center gap-1">
|
||||||
|
<i class="fa-solid fa-house-laptop text-[10px]"></i> Удаленщики
|
||||||
|
</button>
|
||||||
|
<button data-subtab="local_trip" onclick="switchRegistrySubTab('local_trip')" class="registry-subtab-btn py-1 text-[11px] rounded-lg transition text-center flex items-center justify-center gap-1">
|
||||||
|
<i class="fa-solid fa-location-dot text-[10px]"></i> Мест. командир.
|
||||||
|
</button>
|
||||||
|
<button data-subtab="other" onclick="switchRegistrySubTab('other')" class="registry-subtab-btn py-1 text-[11px] rounded-lg transition text-center flex items-center justify-center gap-1">
|
||||||
|
<i class="fa-solid fa-clipboard-question text-[10px]"></i> Иное
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div id="registry-content-container" class="space-y-3"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 4. ПРОМПТ -->
|
||||||
|
<div id="sidebar-view-prompts" class="sidebar-view w-full space-y-3 hidden">
|
||||||
|
<div class="flex items-center justify-between pb-1 border-b border-slate-200">
|
||||||
|
<span class="text-xs font-bold text-slate-800">Системный промпт (Ollama)</span>
|
||||||
|
<button onclick="loadPromptsView()" class="text-slate-400 hover:text-indigo-600 text-xs transition" title="Обновить">
|
||||||
|
<i class="fa-solid fa-rotate-right"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div id="prompts-content-container" class="space-y-2"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 5. КОНТЕКСТ -->
|
||||||
|
<div id="sidebar-view-context" class="sidebar-view w-full space-y-3 hidden">
|
||||||
|
<div class="flex items-center justify-between pb-1 border-b border-slate-200">
|
||||||
|
<span class="text-xs font-bold text-slate-800">Мониторинг сессии</span>
|
||||||
|
<button onclick="loadContextView()" class="text-slate-400 hover:text-indigo-600 text-xs transition" title="Обновить">
|
||||||
|
<i class="fa-solid fa-rotate-right"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div id="context-content-container" class="space-y-2"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- ПОДВАЛ -->
|
||||||
|
<div class="p-3 border-t border-slate-200 bg-slate-50 flex items-center justify-between shrink-0">
|
||||||
|
<div class="flex items-center gap-2.5 min-w-0">
|
||||||
|
<div class="w-8 h-8 rounded-full bg-indigo-600 text-white flex items-center justify-center font-bold text-xs shadow-sm shrink-0">
|
||||||
|
<i class="fa-solid fa-user"></i>
|
||||||
|
</div>
|
||||||
|
<div class="min-w-0 flex flex-col">
|
||||||
|
<span id="user-display-name" class="text-xs font-bold text-slate-800 truncate">Пользователь</span>
|
||||||
|
<span id="user-display-role" class="text-[10px] text-slate-400">Оператор</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex items-center gap-1">
|
||||||
|
<button id="admin-panel-btn" type="button" onclick="openAdminModal()" class="hidden p-1.5 text-slate-400 hover:text-indigo-600 hover:bg-slate-200 rounded-lg transition" title="Управление пользователями">
|
||||||
|
<i class="fa-solid fa-users-gear text-sm"></i>
|
||||||
|
</button>
|
||||||
|
<button type="button" onclick="openProfileModal()" class="p-1.5 text-slate-400 hover:text-slate-700 hover:bg-slate-200 rounded-lg transition" title="Сменить пароль">
|
||||||
|
<i class="fa-solid fa-gear text-sm"></i>
|
||||||
|
</button>
|
||||||
|
<button type="button" onclick="AuthManager.logout()" class="p-1.5 text-slate-400 hover:text-rose-600 hover:bg-rose-50 rounded-lg transition" title="Выйти из системы">
|
||||||
|
<i class="fa-solid fa-arrow-right-from-bracket text-sm"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</aside>
|
</aside>
|
||||||
|
|
||||||
<!-- Основная рабочая область чата -->
|
<!-- ПРАВАЯ ОБЛАСТЬ (ЧАТ И ИИ-АССИСТЕНТ) -->
|
||||||
<main class="flex-1 flex flex-col min-w-0 bg-white relative h-full overflow-hidden">
|
<main class="flex-1 flex flex-col h-full min-w-0 bg-slate-50 relative">
|
||||||
<!-- Верхний заголовок чата -->
|
<header class="h-14 bg-white border-b border-slate-200 px-4 flex items-center justify-between shrink-0 shadow-sm z-10">
|
||||||
<header class="h-14 border-b border-slate-200 px-4 flex items-center justify-between bg-white shrink-0 z-10">
|
<div class="flex items-center gap-2.5">
|
||||||
<div class="flex items-center gap-3">
|
<div class="w-7 h-7 rounded-lg bg-indigo-600 text-white flex items-center justify-center shadow-sm">
|
||||||
<button type="button" onclick="toggleTaskDrawer()" class="p-1.5 text-slate-500 hover:text-indigo-600 hover:bg-slate-100 rounded-lg transition" title="Переключить боковую панель">
|
<i class="fa-solid fa-robot text-xs"></i>
|
||||||
<i class="fa-solid fa-bars"></i>
|
</div>
|
||||||
</button>
|
|
||||||
<div>
|
<div>
|
||||||
<h1 class="font-bold text-sm text-slate-900 flex items-center gap-2">
|
<div class="flex items-center gap-2">
|
||||||
<span>SCUD Orion AI Assistant</span>
|
<h1 class="text-sm font-bold text-slate-800">SCUD Orion AI Assistant</h1>
|
||||||
<span class="inline-flex items-center px-1.5 py-0.5 rounded-full text-[10px] font-semibold bg-emerald-100 text-emerald-800">Online</span>
|
<span class="inline-flex items-center px-1.5 py-0.5 rounded text-[9px] font-semibold bg-emerald-50 text-emerald-700 border border-emerald-200">Online</span>
|
||||||
</h1>
|
</div>
|
||||||
<p class="text-[11px] text-slate-500">Система интеллектуального аудита и контроля СКУД / 1С</p>
|
<p class="text-[10px] text-slate-400">Система интеллектуального аудита и контроля СКУД / 1С</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="flex items-center gap-2 text-xs">
|
|
||||||
<button type="button" onclick="handleActionButtonClick('покажи системный промпт')" class="px-2.5 py-1 text-slate-600 hover:text-indigo-600 hover:bg-slate-100 rounded-md border border-slate-200 transition">
|
<div class="flex items-center gap-2">
|
||||||
<i class="fa-solid fa-terminal mr-1"></i> Промпт
|
<button onclick="window.openPresenceModal()" class="flex items-center gap-1.5 px-3 py-1.5 rounded-xl bg-emerald-50 hover:bg-emerald-100 text-emerald-700 text-xs font-bold border border-emerald-200 transition shadow-xs">
|
||||||
</button>
|
<i class="fa-solid fa-building-user text-xs"></i>
|
||||||
<button type="button" onclick="handleActionButtonClick('покажи снапшоты')" class="px-2.5 py-1 text-slate-600 hover:text-indigo-600 hover:bg-slate-100 rounded-md border border-slate-200 transition">
|
<span>Кто в здании</span>
|
||||||
<i class="fa-solid fa-camera mr-1"></i> Срезы СКУД
|
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<!-- Drop Overlay при перетаскивании файлов -->
|
<!-- ЛЕНТА ЧАТА -->
|
||||||
<div id="drop-overlay" class="hidden absolute inset-0 bg-indigo-600/10 backdrop-blur-[2px] border-2 border-dashed border-indigo-500 rounded-2xl m-4 z-50 items-center justify-center flex-col gap-2 pointer-events-none">
|
<div id="chat-messages-container" class="flex-1 overflow-y-auto p-4 md:p-6 flex flex-col gap-4">
|
||||||
<i class="fa-solid fa-cloud-arrow-up text-3xl text-indigo-600 animate-bounce"></i>
|
<div class="flex gap-3 max-w-4xl mx-auto w-full">
|
||||||
<p class="text-xs font-bold text-indigo-900">Перетащите файл сюда для отправки в диалог</p>
|
<div class="w-8 h-8 rounded-lg bg-indigo-600 text-white flex items-center justify-center shrink-0 shadow-sm mt-0.5">
|
||||||
|
<i class="fa-solid fa-robot text-sm"></i>
|
||||||
|
</div>
|
||||||
|
<div class="flex-1 bg-white border border-slate-200 rounded-2xl rounded-tl-none p-4 shadow-sm">
|
||||||
|
<div class="text-[11px] font-bold text-indigo-600 uppercase tracking-wider mb-1">ИИ-ассистент SCUD Orion AI</div>
|
||||||
|
<div class="text-sm text-slate-700 leading-relaxed">
|
||||||
|
Привет! Вы можете задавать вопросы ассистенту, управлять системным промптом, сверять кадровые нестыковки СКУД и 1С или формировать срезы и отчеты.
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Окно сообщений с центрированием контента -->
|
|
||||||
<div id="chat-window" class="flex-1 p-4 overflow-y-auto bg-slate-50/50 scroll-smooth">
|
|
||||||
<!-- Центрирующая колонка для сообщений -->
|
|
||||||
<div class="max-w-4xl w-full mx-auto space-y-4">
|
|
||||||
|
|
||||||
<!-- Стартовое приветственное сообщение -->
|
|
||||||
<div class="chat-message-card bg-white border border-slate-200 rounded-2xl p-4 shadow-sm w-full">
|
|
||||||
<p class="text-[11px] font-bold text-indigo-600 uppercase tracking-wider mb-1">
|
|
||||||
<i class="fa-solid fa-robot mr-1"></i> ИИ-Ассистент SCUD Orion AI
|
|
||||||
</p>
|
|
||||||
<div class="text-slate-800 text-xs sm:text-sm leading-relaxed">
|
|
||||||
Привет! У каждого пользователя свое изолированное пространство задач. Вы можете задавать вопросы нейросети, прикреплять файлы или ставить персональные задачи.
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Невидимая распорка в самом низу для свободного скролла любого вопроса наверх -->
|
<!-- БЕЙДЖ ПРИКРЕПЛЕННОГО ФАЙЛА -->
|
||||||
<div id="chat-bottom-spacer" class="min-h-[85vh] pointer-events-none w-full"></div>
|
<div id="file-attachment-preview" class="hidden max-w-4xl mx-auto w-full px-4 pt-2">
|
||||||
</div>
|
<div class="inline-flex items-center gap-2 px-3 py-1 bg-indigo-50 border border-indigo-200 rounded-xl text-xs text-indigo-700 shadow-sm">
|
||||||
</div>
|
<i class="fa-solid fa-file text-indigo-600"></i>
|
||||||
|
<span id="file-attachment-name" class="font-medium truncate max-w-xs"></span>
|
||||||
<!-- Центрированная нижняя панель ввода -->
|
<button type="button" onclick="clearAttachedFile()" class="text-indigo-400 hover:text-rose-600 ml-1">
|
||||||
<div class="p-3 bg-white border-t border-slate-200 shrink-0 z-10">
|
|
||||||
<div class="max-w-4xl w-full mx-auto">
|
|
||||||
<!-- Блок предпросмотра прикрепленного файла -->
|
|
||||||
<div id="file-preview-container" class="hidden mb-2 p-2 bg-indigo-50 border border-indigo-200 rounded-xl flex items-center justify-between">
|
|
||||||
<div class="flex items-center gap-2 min-w-0">
|
|
||||||
<i class="fa-solid fa-file-arrow-up text-indigo-600 text-sm"></i>
|
|
||||||
<span id="file-name-display" class="text-xs font-semibold text-slate-800 truncate"></span>
|
|
||||||
<span id="file-size-display" class="text-[10px] text-slate-500"></span>
|
|
||||||
</div>
|
|
||||||
<button type="button" onclick="clearAttachedFile()" class="text-slate-400 hover:text-rose-600 transition p-1">
|
|
||||||
<i class="fa-solid fa-xmark"></i>
|
<i class="fa-solid fa-xmark"></i>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Форма отправки -->
|
|
||||||
<form id="chat-form" onsubmit="sendMessage(event)" class="flex items-end gap-2">
|
|
||||||
<input type="file" id="file-input" class="hidden" onchange="handleFileSelect(event)" />
|
|
||||||
|
|
||||||
<button type="button" onclick="document.getElementById('file-input').click()"
|
|
||||||
title="Прикрепить файл"
|
|
||||||
class="p-2.5 text-slate-500 hover:text-indigo-600 hover:bg-slate-100 rounded-xl transition shrink-0 cursor-pointer">
|
|
||||||
<i class="fa-solid fa-paperclip text-sm"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
<div class="flex-1 bg-slate-100 border border-slate-300 focus-within:border-indigo-600 focus-within:bg-white rounded-2xl p-1.5 transition flex items-center shadow-inner">
|
|
||||||
<textarea id="user-input" rows="1"
|
|
||||||
placeholder="Команда, вопрос или перетащите файл сюда..."
|
|
||||||
class="w-full bg-transparent px-2 text-xs sm:text-sm text-slate-800 focus:outline-none resize-none overflow-y-auto leading-relaxed"
|
|
||||||
style="height: 24px; max-height: 120px;"></textarea>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button type="submit" id="send-btn"
|
<!-- СТРОКА ВВОДА -->
|
||||||
class="bg-indigo-600 hover:bg-indigo-700 active:bg-indigo-800 text-white p-2.5 rounded-xl transition shrink-0 shadow-sm cursor-pointer">
|
<div class="px-4 py-3 bg-white border-t border-slate-200">
|
||||||
<i class="fa-solid fa-paper-plane text-sm"></i>
|
<div id="chat-input-box" class="max-w-4xl mx-auto w-full flex items-end gap-3 bg-slate-50 border border-slate-300 rounded-2xl px-4 py-2.5 transition focus-within:border-indigo-500 focus-within:bg-white focus-within:ring-2 focus-within:ring-indigo-100 shadow-xs">
|
||||||
|
<button type="button" onclick="document.getElementById('file-upload-input').click()" class="text-slate-400 hover:text-indigo-600 p-1.5 transition shrink-0 mb-0.5" title="Прикрепить файл">
|
||||||
|
<i class="fa-solid fa-paperclip text-base"></i>
|
||||||
|
</button>
|
||||||
|
<input type="file" id="file-upload-input" class="hidden" />
|
||||||
|
|
||||||
|
<textarea id="user-input" rows="1" placeholder="Задайте вопрос, отправьте команду (Enter — отправить, Shift+Enter — перенос)..."
|
||||||
|
class="flex-1 bg-transparent border-0 focus:outline-none text-[15.5px] text-slate-800 placeholder:text-slate-400 resize-none py-1 leading-6 min-h-[32px] max-h-36" style="height: 32px;"></textarea>
|
||||||
|
|
||||||
|
<button type="button" onclick="window.sendMessage()" class="w-8 h-8 rounded-xl bg-indigo-600 hover:bg-indigo-700 active:bg-indigo-800 text-white flex items-center justify-center shrink-0 shadow-sm transition mb-0.5">
|
||||||
|
<i class="fa-solid fa-paper-plane text-xs"></i>
|
||||||
</button>
|
</button>
|
||||||
</form>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Скрипты клиентской логики -->
|
<!-- ПОЛНОЭКРАННЫЙ ОВЕРЛЕЙ DRAG & DROP -->
|
||||||
<script src="/js/auth.js"></script>
|
<div id="global-drag-overlay" class="fixed inset-0 bg-indigo-950/70 backdrop-blur-xs z-50 flex items-center justify-center p-8 hidden pointer-events-none transition-all">
|
||||||
<script src="/js/tasks.js"></script>
|
<div class="border-3 border-dashed border-indigo-300 rounded-3xl w-full h-full flex flex-col items-center justify-center text-white gap-4 bg-indigo-900/40">
|
||||||
<script src="/js/chat/task_widget.js"></script>
|
<div class="w-20 h-20 rounded-2xl bg-white/10 flex items-center justify-center shadow-lg backdrop-blur-md border border-white/20">
|
||||||
<script src="/js/chat/core.js"></script>
|
<i class="fa-solid fa-cloud-arrow-up text-4xl text-indigo-200"></i>
|
||||||
<script src="/js/app.js"></script>
|
</div>
|
||||||
|
<div class="text-xl font-bold tracking-wide">Перетащите файл в окно для загрузки</div>
|
||||||
|
<div class="text-sm text-indigo-200">Поддерживаются PDF (документы и сканы), изображения, Excel, CSV, TXT</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- КОНТЕЙНЕР ДИНАМИЧЕСКИХ МОДАЛЬНЫХ ОКОН -->
|
||||||
|
<div id="modals-container"></div>
|
||||||
|
|
||||||
|
<script src="/static/js/auth.js?v=2.6.0"></script>
|
||||||
|
<script src="/static/js/tasks.js?v=2.6.0"></script>
|
||||||
|
<script src="/static/js/manual_absences.js?v=2.6.0"></script>
|
||||||
|
<script src="/static/js/snapshot_inspector.js?v=2.6.0"></script>
|
||||||
|
|
||||||
|
<script src="/static/js/sidebar/core.js?v=2.6.0"></script>
|
||||||
|
<script src="/static/js/sidebar/registries.js?v=2.6.0"></script>
|
||||||
|
<script src="/static/js/sidebar/snapshots.js?v=2.6.0"></script>
|
||||||
|
<script src="/static/js/sidebar/prompts_context.js?v=2.6.0"></script>
|
||||||
|
|
||||||
|
<script src="/static/js/presence.js?v=2.6.0"></script>
|
||||||
|
|
||||||
|
<script src="/static/js/chat/task_widget.js?v=2.6.0"></script>
|
||||||
|
<script src="/static/js/chat/core.js?v=2.6.0"></script>
|
||||||
|
<script src="/static/js/app.js?v=2.6.0"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -1,3 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* ===============================================================================
|
||||||
|
* FILE: modules/web_api/static/js/app.js
|
||||||
|
* ROLE: Главная точка входа UI: динамическая загрузка модальных окон,
|
||||||
|
* маршрутизация авторизации, управление профилем и глобальный стейт.
|
||||||
|
* ===============================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
const AUTH_TOKEN_KEY = "scud_api_auth_token";
|
const AUTH_TOKEN_KEY = "scud_api_auth_token";
|
||||||
const SESSION_ID = "web_session_main";
|
const SESSION_ID = "web_session_main";
|
||||||
const STORAGE_KEY = "scud_chat_input_history";
|
const STORAGE_KEY = "scud_chat_input_history";
|
||||||
@@ -10,9 +18,379 @@ let IS_GUEST = localStorage.getItem("scud_is_guest") === "true";
|
|||||||
let inputHistory = JSON.parse(localStorage.getItem(STORAGE_KEY) || "[]");
|
let inputHistory = JSON.parse(localStorage.getItem(STORAGE_KEY) || "[]");
|
||||||
let historyIndex = -1;
|
let historyIndex = -1;
|
||||||
|
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
// ============================================================================
|
||||||
const userInputEl = document.getElementById("user-input");
|
// 1. ДИНАМИЧЕСКАЯ ЗАГРУЗКА МОДАЛЬНЫХ ОКОН
|
||||||
|
// ============================================================================
|
||||||
|
async function loadModals() {
|
||||||
|
const modalFiles = [
|
||||||
|
'remote_worker_modal.html',
|
||||||
|
'manual_absence_modal.html',
|
||||||
|
'exception_modal.html',
|
||||||
|
'auth_modal.html',
|
||||||
|
'profile_modal.html',
|
||||||
|
'admin_modal.html',
|
||||||
|
'snapshot_inspector_modal.html',
|
||||||
|
'presence_modal.html'
|
||||||
|
];
|
||||||
|
|
||||||
|
const container = document.getElementById('modals-container');
|
||||||
|
if (!container) return;
|
||||||
|
|
||||||
|
for (const file of modalFiles) {
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/static/modals/${file}?v=2.6.0`);
|
||||||
|
if (res.ok) {
|
||||||
|
const html = await res.text();
|
||||||
|
container.insertAdjacentHTML('beforeend', html);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`[Modals] Ошибка загрузки ${file}:`, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// 2. УПРАВЛЕНИЕ АВТОРИЗАЦИЕЙ И ПРОФИЛЕМ В UI
|
||||||
|
// ============================================================================
|
||||||
|
function showAuthModal() {
|
||||||
|
const modal = document.getElementById("auth-modal");
|
||||||
|
if (modal) modal.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
|
||||||
|
function hideAuthModal() {
|
||||||
|
const modal = document.getElementById("auth-modal");
|
||||||
|
if (modal) modal.classList.add("hidden");
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateUIState() {
|
||||||
|
const nameEl = document.getElementById("user-display-name");
|
||||||
|
const roleEl = document.getElementById("user-display-role");
|
||||||
|
const adminBtn = document.getElementById("admin-panel-btn");
|
||||||
|
|
||||||
|
if (nameEl) nameEl.innerText = AuthManager.getFullName();
|
||||||
|
if (roleEl) roleEl.innerText = AuthManager.isAdmin() ? "Администратор" : "Оператор";
|
||||||
|
|
||||||
|
if (adminBtn) {
|
||||||
|
if (AuthManager.isAdmin()) {
|
||||||
|
adminBtn.classList.remove("hidden");
|
||||||
|
} else {
|
||||||
|
adminBtn.classList.add("hidden");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleLoginSubmit(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
const uInput = document.getElementById("auth-username");
|
||||||
|
const pInput = document.getElementById("auth-password");
|
||||||
|
const errEl = document.getElementById("auth-error");
|
||||||
|
if (errEl) errEl.classList.add("hidden");
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/v1/auth/login", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({
|
||||||
|
username: uInput.value.trim(),
|
||||||
|
password: pInput.value
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
|
AuthManager.setSession(data.token, data.username, data.full_name, data.is_admin, data.user_id);
|
||||||
|
hideAuthModal();
|
||||||
|
updateUIState();
|
||||||
|
if (window.SidebarManager) SidebarManager.switchTab('tasks');
|
||||||
|
} else {
|
||||||
|
const err = await res.json();
|
||||||
|
if (errEl) {
|
||||||
|
errEl.innerText = err.detail || "Неверный логин или пароль";
|
||||||
|
errEl.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
if (errEl) {
|
||||||
|
errEl.innerText = "Ошибка соединения с сервером";
|
||||||
|
errEl.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function openProfileModal() {
|
||||||
|
const modal = document.getElementById("profile-modal");
|
||||||
|
if (modal) modal.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeProfileModal() {
|
||||||
|
const modal = document.getElementById("profile-modal");
|
||||||
|
if (modal) modal.classList.add("hidden");
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleChangePassword(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
const oldP = document.getElementById("old-pass").value;
|
||||||
|
const newP = document.getElementById("new-pass").value;
|
||||||
|
const errEl = document.getElementById("pass-error");
|
||||||
|
if (errEl) errEl.classList.add("hidden");
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/v1/auth/change-password", {
|
||||||
|
method: "POST",
|
||||||
|
headers: AuthManager.getAuthHeaders(),
|
||||||
|
body: JSON.stringify({ old_password: oldP, new_password: newP })
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
alert("Пароль успешно изменен");
|
||||||
|
closeProfileModal();
|
||||||
|
} else {
|
||||||
|
const err = await res.json();
|
||||||
|
if (errEl) {
|
||||||
|
errEl.innerText = err.detail || "Ошибка изменения пароля";
|
||||||
|
errEl.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
if (errEl) {
|
||||||
|
errEl.innerText = "Ошибка сети";
|
||||||
|
errEl.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// 3. АДМИНИСТРИРОВАНИЕ ПОЛЬЗОВАТЕЛЕЙ
|
||||||
|
// ============================================================================
|
||||||
|
function openAdminModal() {
|
||||||
|
const modal = document.getElementById("admin-modal");
|
||||||
|
if (modal) {
|
||||||
|
modal.classList.remove("hidden");
|
||||||
|
loadAdminUsers();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeAdminModal() {
|
||||||
|
const modal = document.getElementById("admin-modal");
|
||||||
|
if (modal) modal.classList.add("hidden");
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadAdminUsers() {
|
||||||
|
const listEl = document.getElementById("admin-users-list");
|
||||||
|
if (!listEl) return;
|
||||||
|
listEl.innerHTML = `<div class="text-center py-4 text-xs text-slate-400"><i class="fa-solid fa-spinner fa-spin mr-1"></i> Загрузка...</div>`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/v1/admin/users", { headers: AuthManager.getAuthHeaders() });
|
||||||
|
if (res.ok) {
|
||||||
|
const users = await res.json();
|
||||||
|
listEl.innerHTML = users.map(u => `
|
||||||
|
<div class="flex items-center justify-between p-2.5 bg-slate-50 border border-slate-200 rounded-xl text-xs">
|
||||||
|
<div>
|
||||||
|
<div class="font-bold text-slate-800">${escapeHtml(u.full_name || u.username)} <span class="text-slate-400 font-mono text-[10px]">(${escapeHtml(u.username)})</span></div>
|
||||||
|
<div class="text-[10px] ${u.is_admin ? 'text-indigo-600 font-bold' : 'text-slate-400'}">${u.is_admin ? 'Администратор' : 'Оператор'}</div>
|
||||||
|
</div>
|
||||||
|
<button onclick="deleteAdminUser(${u.id}, '${escapeHtml(u.username)}')" class="text-slate-400 hover:text-rose-600 p-1.5" title="Удалить">
|
||||||
|
<i class="fa-solid fa-trash-can"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
`).join('');
|
||||||
|
} else {
|
||||||
|
listEl.innerHTML = `<div class="text-center py-4 text-xs text-rose-500">Ошибка загрузки пользователей</div>`;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
listEl.innerHTML = `<div class="text-center py-4 text-xs text-rose-500">Ошибка сети</div>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleCreateUser(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
const u = document.getElementById("new-user-username").value;
|
||||||
|
const p = document.getElementById("new-user-password").value;
|
||||||
|
const f = document.getElementById("new-user-fullname").value;
|
||||||
|
const a = document.getElementById("new-user-admin").checked;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/v1/admin/users", {
|
||||||
|
method: "POST",
|
||||||
|
headers: AuthManager.getAuthHeaders(),
|
||||||
|
body: JSON.stringify({ username: u, password: p, full_name: f, is_admin: a })
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
document.getElementById("new-user-username").value = "";
|
||||||
|
document.getElementById("new-user-password").value = "";
|
||||||
|
document.getElementById("new-user-fullname").value = "";
|
||||||
|
document.getElementById("new-user-admin").checked = false;
|
||||||
|
loadAdminUsers();
|
||||||
|
} else {
|
||||||
|
const err = await res.json();
|
||||||
|
alert(err.detail || "Ошибка создания пользователя");
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
alert("Ошибка сети");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteAdminUser(id, username) {
|
||||||
|
if (!confirm(`Удалить пользователя ${username}?`)) return;
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/v1/admin/users/${id}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: AuthManager.getAuthHeaders()
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
loadAdminUsers();
|
||||||
|
} else {
|
||||||
|
const err = await res.json();
|
||||||
|
alert(err.detail || "Ошибка удаления");
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
alert("Ошибка сети");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// 4. ВСПОМОГАТЕЛЬНЫЕ ФОРМАТЕРЫ И МОДАЛКА УДАЛЕНЩИКОВ
|
||||||
|
// ============================================================================
|
||||||
|
function dmyToYmd(str) {
|
||||||
|
if (!str) return "";
|
||||||
|
const parts = str.replace(/_/g, '.').split('.');
|
||||||
|
if (parts.length === 3) return `${parts[2]}-${parts[1].padStart(2, '0')}-${parts[0].padStart(2, '0')}`;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function ymdToDmy(str) {
|
||||||
|
if (!str) return "";
|
||||||
|
const parts = str.split('-');
|
||||||
|
if (parts.length === 3) return `${parts[2]}.${parts[1]}.${parts[0]}`;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function openRemoteWorkerModal(mode = 'ADD', fio = '', dept = 'Все', dateFrom = '', dateTo = '') {
|
||||||
|
const modal = document.getElementById("remote-worker-modal");
|
||||||
|
const titleEl = document.getElementById("remote-modal-title");
|
||||||
|
const modeInput = document.getElementById("rw-mode");
|
||||||
|
const fioInput = document.getElementById("rw-fio");
|
||||||
|
const deptInput = document.getElementById("rw-dept");
|
||||||
|
const fromInput = document.getElementById("rw-date-from");
|
||||||
|
const toInput = document.getElementById("rw-date-to");
|
||||||
|
const errEl = document.getElementById("rw-error");
|
||||||
|
const suggBox = document.getElementById("rw-fio-suggestions");
|
||||||
|
|
||||||
|
if (!modal) return;
|
||||||
|
if (errEl) errEl.classList.add("hidden");
|
||||||
|
if (suggBox) {
|
||||||
|
suggBox.classList.add("hidden");
|
||||||
|
suggBox.innerHTML = "";
|
||||||
|
}
|
||||||
|
if (modeInput) modeInput.value = mode;
|
||||||
|
|
||||||
|
if (mode === 'EDIT') {
|
||||||
|
if (titleEl) titleEl.innerHTML = `<i class="fa-solid fa-pen-to-square text-emerald-600"></i><span>Изменение сроков удаленки</span>`;
|
||||||
|
if (fioInput) {
|
||||||
|
fioInput.value = fio;
|
||||||
|
fioInput.readOnly = true;
|
||||||
|
fioInput.classList.add("bg-slate-100", "text-slate-500", "cursor-not-allowed");
|
||||||
|
}
|
||||||
|
if (deptInput) {
|
||||||
|
deptInput.value = dept || "Все";
|
||||||
|
deptInput.readOnly = true;
|
||||||
|
deptInput.classList.add("bg-slate-100", "text-slate-500", "cursor-not-allowed");
|
||||||
|
}
|
||||||
|
if (fromInput) fromInput.value = dmyToYmd(dateFrom);
|
||||||
|
if (toInput) toInput.value = dmyToYmd(dateTo);
|
||||||
|
} else {
|
||||||
|
if (titleEl) titleEl.innerHTML = `<i class="fa-solid fa-house-laptop text-emerald-600"></i><span>Добавление удаленщика</span>`;
|
||||||
|
if (fioInput) {
|
||||||
|
fioInput.value = "";
|
||||||
|
fioInput.readOnly = false;
|
||||||
|
fioInput.classList.remove("bg-slate-100", "text-slate-500", "cursor-not-allowed");
|
||||||
|
}
|
||||||
|
if (deptInput) {
|
||||||
|
deptInput.value = "Все";
|
||||||
|
deptInput.readOnly = false;
|
||||||
|
deptInput.classList.remove("bg-slate-100", "text-slate-500", "cursor-not-allowed");
|
||||||
|
}
|
||||||
|
const today = new Date().toISOString().split('T')[0];
|
||||||
|
if (fromInput) fromInput.value = today;
|
||||||
|
if (toInput) toInput.value = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
modal.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeRemoteWorkerModal() {
|
||||||
|
const modal = document.getElementById("remote-worker-modal");
|
||||||
|
if (modal) modal.classList.add("hidden");
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleRemoteWorkerSubmit(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
const mode = document.getElementById("rw-mode").value;
|
||||||
|
const fio = document.getElementById("rw-fio").value.trim();
|
||||||
|
const dept = document.getElementById("rw-dept").value.trim() || "Все";
|
||||||
|
const fromVal = ymdToDmy(document.getElementById("rw-date-from").value);
|
||||||
|
const toVal = ymdToDmy(document.getElementById("rw-date-to").value);
|
||||||
|
const errEl = document.getElementById("rw-error");
|
||||||
|
if (errEl) errEl.classList.add("hidden");
|
||||||
|
|
||||||
|
const method = (mode === 'EDIT') ? "PUT" : "POST";
|
||||||
|
const payload = (mode === 'EDIT')
|
||||||
|
? { fio: fio, date_from: fromVal, date_to: toVal }
|
||||||
|
: { fio: fio, department: dept, reason: "Удаленная работа", date_from: fromVal, date_to: toVal };
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/v1/remote-workers", {
|
||||||
|
method: method,
|
||||||
|
headers: AuthManager.getAuthHeaders(),
|
||||||
|
body: JSON.stringify(payload)
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
closeRemoteWorkerModal();
|
||||||
|
if (window.SidebarManager) SidebarManager.switchRegistrySubTab('remote');
|
||||||
|
} else {
|
||||||
|
const err = await res.json();
|
||||||
|
if (errEl) {
|
||||||
|
errEl.innerText = err.detail || "Ошибка сохранения";
|
||||||
|
errEl.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
if (errEl) {
|
||||||
|
errEl.innerText = "Ошибка соединения с сервером";
|
||||||
|
errEl.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// 5. ИНИЦИАЛИЗАЦИЯ ПРИЛОЖЕНИЯ
|
||||||
|
// ============================================================================
|
||||||
|
document.addEventListener("DOMContentLoaded", async () => {
|
||||||
|
// 1. Асинхронная подгрузка модальных окон
|
||||||
|
await loadModals();
|
||||||
|
|
||||||
|
// 2. Инициализация автокомплита исключений после монтирования разметки
|
||||||
|
const excInput = document.getElementById("exception-value-input");
|
||||||
|
const excBox = document.getElementById("exception-suggestions");
|
||||||
|
if (excInput && excBox && typeof setupStaffAutocomplete === "function") {
|
||||||
|
setupStaffAutocomplete(excInput, "exception-suggestions");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Инициализация автокомплита для модалки удаленщиков
|
||||||
|
const rwFioInput = document.getElementById("rw-fio");
|
||||||
|
if (rwFioInput && typeof setupStaffAutocomplete === "function") {
|
||||||
|
setupStaffAutocomplete(rwFioInput, "rw-fio-suggestions");
|
||||||
|
}
|
||||||
|
|
||||||
|
//3.1 Автокомплит для местных командировок и иного
|
||||||
|
const maFioInput = document.getElementById("manual-absence-fio-input");
|
||||||
|
if (maFioInput && typeof setupStaffAutocomplete === "function") {
|
||||||
|
setupStaffAutocomplete(maFioInput, "manual-absence-suggestions");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Авто-высота поля ввода команд
|
||||||
|
const userInputEl = document.getElementById("user-input");
|
||||||
if (userInputEl) {
|
if (userInputEl) {
|
||||||
userInputEl.addEventListener("input", function() {
|
userInputEl.addEventListener("input", function() {
|
||||||
this.style.height = "24px";
|
this.style.height = "24px";
|
||||||
@@ -21,15 +399,19 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 5. Проверка сессии пользователя
|
||||||
if (IS_GUEST) {
|
if (IS_GUEST) {
|
||||||
hideAuthModal();
|
hideAuthModal();
|
||||||
updateUIState();
|
updateUIState();
|
||||||
} else if (API_TOKEN) {
|
} else if (AuthManager && AuthManager.isAuthenticated()) {
|
||||||
hideAuthModal();
|
hideAuthModal();
|
||||||
updateUIState();
|
updateUIState();
|
||||||
if (typeof loadTasks === "function") {
|
if (typeof loadTasks === "function") {
|
||||||
loadTasks();
|
loadTasks();
|
||||||
}
|
}
|
||||||
|
if (window.SidebarManager) {
|
||||||
|
SidebarManager.init();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
showAuthModal();
|
showAuthModal();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,62 +1,75 @@
|
|||||||
/**
|
/**
|
||||||
* ===============================================================================
|
* ===============================================================================
|
||||||
* FILE: static/js/auth.js
|
* FILE: modules/web_api/static/js/auth.js
|
||||||
* ROLE: Управление сессией пользователя, токенами и корректным выходом (Logout).
|
* ROLE: Менеджер сессий, токенов, ФИО и авторизационных заголовков.
|
||||||
* ===============================================================================
|
* ===============================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const AUTH_STORAGE_KEY = "scud_auth_token";
|
||||||
|
const USERNAME_STORAGE_KEY = "scud_username";
|
||||||
|
const FULLNAME_STORAGE_KEY = "scud_full_name";
|
||||||
|
const IS_ADMIN_STORAGE_KEY = "scud_is_admin";
|
||||||
|
const USER_ID_STORAGE_KEY = "scud_user_id";
|
||||||
|
|
||||||
const AuthManager = {
|
const AuthManager = {
|
||||||
getToken() {
|
getToken() {
|
||||||
let token = localStorage.getItem("auth_token") || localStorage.getItem("token");
|
return localStorage.getItem(AUTH_STORAGE_KEY) || "";
|
||||||
if (!token) {
|
|
||||||
// Если токена нет — инициализируем рабочий дефолтный токен
|
|
||||||
token = "dev_token_1";
|
|
||||||
localStorage.setItem("auth_token", token);
|
|
||||||
localStorage.setItem("user_id", "1");
|
|
||||||
}
|
|
||||||
return token;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getUserId() {
|
getUserId() {
|
||||||
return parseInt(localStorage.getItem("user_id") || "1", 10);
|
const uid = localStorage.getItem(USER_ID_STORAGE_KEY);
|
||||||
|
return uid ? parseInt(uid, 10) : 1;
|
||||||
|
},
|
||||||
|
|
||||||
|
getUsername() {
|
||||||
|
return localStorage.getItem(USERNAME_STORAGE_KEY) || "";
|
||||||
|
},
|
||||||
|
|
||||||
|
getFullName() {
|
||||||
|
return localStorage.getItem(FULLNAME_STORAGE_KEY) || this.getUsername() || "Пользователь";
|
||||||
|
},
|
||||||
|
|
||||||
|
isAdmin() {
|
||||||
|
return localStorage.getItem(IS_ADMIN_STORAGE_KEY) === "true";
|
||||||
|
},
|
||||||
|
|
||||||
|
isAuthenticated() {
|
||||||
|
return Boolean(this.getToken());
|
||||||
|
},
|
||||||
|
|
||||||
|
setSession(token, username, fullName, isAdmin, userId = 1) {
|
||||||
|
localStorage.setItem(AUTH_STORAGE_KEY, token);
|
||||||
|
localStorage.setItem(USERNAME_STORAGE_KEY, username);
|
||||||
|
localStorage.setItem(FULLNAME_STORAGE_KEY, fullName || username);
|
||||||
|
localStorage.setItem(IS_ADMIN_STORAGE_KEY, String(isAdmin));
|
||||||
|
localStorage.setItem(USER_ID_STORAGE_KEY, String(userId));
|
||||||
|
localStorage.setItem("scud_api_auth_token", token);
|
||||||
|
localStorage.setItem("auth_token", token);
|
||||||
|
},
|
||||||
|
|
||||||
|
getAuthHeaders() {
|
||||||
|
const token = this.getToken();
|
||||||
|
const headers = { "Content-Type": "application/json" };
|
||||||
|
if (token) {
|
||||||
|
headers["Authorization"] = `Bearer ${token}`;
|
||||||
|
}
|
||||||
|
return headers;
|
||||||
},
|
},
|
||||||
|
|
||||||
logout() {
|
logout() {
|
||||||
console.log("[Auth] Выполняется выход из учетной записи...");
|
console.log("[Auth] Полный выход из системы...");
|
||||||
|
localStorage.clear();
|
||||||
// 1. Полная очистка хранилищ браузера
|
|
||||||
localStorage.removeItem("auth_token");
|
|
||||||
localStorage.removeItem("token");
|
|
||||||
localStorage.removeItem("user_id");
|
|
||||||
sessionStorage.clear();
|
sessionStorage.clear();
|
||||||
|
|
||||||
// 2. Сброс авторизационных cookies (если присутствуют)
|
|
||||||
document.cookie.split(";").forEach((cookie) => {
|
document.cookie.split(";").forEach((cookie) => {
|
||||||
const eqPos = cookie.indexOf("=");
|
const eqPos = cookie.indexOf("=");
|
||||||
const name = eqPos > -1 ? cookie.substr(0, eqPos).trim() : cookie.trim();
|
const name = eqPos > -1 ? cookie.substr(0, eqPos).trim() : cookie.trim();
|
||||||
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`;
|
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`;
|
||||||
});
|
});
|
||||||
|
|
||||||
// 3. Перезагрузка страницы для сброса состояния интерфейса
|
window.location.href = "/";
|
||||||
window.location.reload();
|
|
||||||
},
|
|
||||||
|
|
||||||
init() {
|
|
||||||
// Привязка клика ко всем элементам с классом или id logout
|
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
|
||||||
const logoutBtns = document.querySelectorAll("#logout-btn, .logout-btn, [data-action='logout']");
|
|
||||||
logoutBtns.forEach(btn => {
|
|
||||||
btn.addEventListener("click", (e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
AuthManager.logout();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Инициализация при загрузке скрипта
|
window.AuthManager = AuthManager;
|
||||||
AuthManager.init();
|
|
||||||
|
|
||||||
// Глобальная доступность функции для onclick в HTML
|
|
||||||
window.logout = () => AuthManager.logout();
|
window.logout = () => AuthManager.logout();
|
||||||
@@ -1,67 +1,581 @@
|
|||||||
/**
|
/**
|
||||||
* ===============================================================================
|
* ===============================================================================
|
||||||
* FILE: static/js/chat/core.js
|
* FILE: modules/web_api/static/js/chat/core.js
|
||||||
* ROLE: Отправка сообщений в API с токеном и обработка ошибок.
|
* ROLE: Ядро чата: полноэкранный Drag-and-Drop оверлей, авто-высота инпута (24px),
|
||||||
|
* надежный расчет скролла вопроса к верху окна, крупный шрифт text-sm.
|
||||||
* ===============================================================================
|
* ===============================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
async function sendMessage(userMessageText) {
|
let currentAttachedFile = null;
|
||||||
const chatInput = document.getElementById("chat-input");
|
|
||||||
const message = userMessageText || (chatInput ? chatInput.value.trim() : "");
|
|
||||||
if (!message) return;
|
|
||||||
|
|
||||||
if (chatInput && !userMessageText) {
|
const CHAT_INPUT_STORAGE_KEY = "scud_chat_input_history";
|
||||||
chatInput.value = "";
|
let chatInputHistory = JSON.parse(localStorage.getItem(CHAT_INPUT_STORAGE_KEY) || "[]");
|
||||||
|
let chatHistoryIndex = -1;
|
||||||
|
let temporaryCurrentInput = "";
|
||||||
|
|
||||||
|
function saveCommandToHistory(commandText) {
|
||||||
|
if (!commandText || !commandText.trim()) return;
|
||||||
|
const cleanCmd = commandText.trim();
|
||||||
|
if (cleanCmd.startsWith("action:save_draft_")) return;
|
||||||
|
|
||||||
|
chatInputHistory = chatInputHistory.filter(item => item !== cleanCmd);
|
||||||
|
chatInputHistory.push(cleanCmd);
|
||||||
|
if (chatInputHistory.length > 50) chatInputHistory.shift();
|
||||||
|
localStorage.setItem(CHAT_INPUT_STORAGE_KEY, JSON.stringify(chatInputHistory));
|
||||||
|
chatHistoryIndex = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Отображаем сообщение пользователя в чате
|
// ⭐️ ЕДИНАЯ ФУНКЦИЯ СКРОЛЛА: выравнивание вопроса к верхней границе
|
||||||
if (typeof appendMessageToUI === "function") {
|
function scrollToUserMessageTop() {
|
||||||
appendMessageToUI("user", message);
|
const container = document.getElementById("chat-messages-container");
|
||||||
}
|
if (!container) return;
|
||||||
|
|
||||||
// Подготовка заголовков с гарантированным токеном
|
const userBubbles = container.querySelectorAll(".user-chat-bubble");
|
||||||
const token = (typeof AuthManager !== "undefined") ? AuthManager.getToken() : (localStorage.getItem("auth_token") || "dev_token_1");
|
const targetEl = userBubbles[userBubbles.length - 1];
|
||||||
const userId = (typeof AuthManager !== "undefined") ? AuthManager.getUserId() : parseInt(localStorage.getItem("user_id") || "1", 10);
|
if (!targetEl) return;
|
||||||
const sessionId = localStorage.getItem("chat_session_id") || "web_session_main";
|
|
||||||
|
|
||||||
try {
|
requestAnimationFrame(() => {
|
||||||
const response = await fetch("/api/v1/chat", {
|
const targetScroll = targetEl.offsetTop - container.offsetTop - 12;
|
||||||
method: "POST",
|
|
||||||
headers: {
|
container.scrollTo({
|
||||||
"Content-Type": "application/json",
|
top: Math.max(0, targetScroll),
|
||||||
"Authorization": `Bearer ${token}`
|
behavior: 'smooth'
|
||||||
},
|
});
|
||||||
body: JSON.stringify({
|
|
||||||
message: message,
|
|
||||||
session_id: sessionId,
|
|
||||||
user_id: userId
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.status === 403) {
|
|
||||||
console.warn("[Chat] Получен 403 Forbidden. Сбрасываем сессию и повторяем...");
|
|
||||||
localStorage.removeItem("auth_token");
|
|
||||||
if (typeof appendMessageToUI === "function") {
|
|
||||||
appendMessageToUI("assistant", "⚠️ Сессия была обновлена. Пожалуйста, отправьте сообщение повторно.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateInputHeightAndFade(textarea) {
|
||||||
|
if (!textarea) return;
|
||||||
|
|
||||||
|
if (!textarea.value || textarea.value.trim() === '') {
|
||||||
|
textarea.style.height = '32px';
|
||||||
|
textarea.style.overflowY = 'hidden';
|
||||||
|
textarea.style.maskImage = 'none';
|
||||||
|
textarea.style.webkitMaskImage = 'none';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!response.ok) {
|
textarea.style.height = 'auto';
|
||||||
throw new Error(`Ошибка сервера (HTTP ${response.status})`);
|
const minHeight = 32;
|
||||||
|
const maxHeight = 140;
|
||||||
|
const currentScrollHeight = textarea.scrollHeight;
|
||||||
|
|
||||||
|
if (currentScrollHeight <= minHeight + 2) {
|
||||||
|
textarea.style.height = minHeight + 'px';
|
||||||
|
textarea.style.overflowY = 'hidden';
|
||||||
|
textarea.style.maskImage = 'none';
|
||||||
|
textarea.style.webkitMaskImage = 'none';
|
||||||
|
} else if (currentScrollHeight > maxHeight) {
|
||||||
|
textarea.style.height = maxHeight + 'px';
|
||||||
|
textarea.style.overflowY = 'auto';
|
||||||
|
textarea.style.maskImage = 'linear-gradient(to bottom, transparent 0%, black 14px, black 100%)';
|
||||||
|
textarea.style.webkitMaskImage = 'linear-gradient(to bottom, transparent 0%, black 14px, black 100%)';
|
||||||
|
} else {
|
||||||
|
textarea.style.height = currentScrollHeight + 'px';
|
||||||
|
textarea.style.overflowY = 'hidden';
|
||||||
|
textarea.style.maskImage = 'none';
|
||||||
|
textarea.style.webkitMaskImage = 'none';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
function appendUserMessage(text, filename = null) {
|
||||||
const reply = data.response || data.message || "Ответ получен без текста.";
|
const container = document.getElementById("chat-messages-container");
|
||||||
|
if (!container) return;
|
||||||
|
|
||||||
if (typeof appendMessageToUI === "function") {
|
const msgId = 'user-msg-' + Date.now();
|
||||||
appendMessageToUI("assistant", reply);
|
let fileBadge = '';
|
||||||
|
if (filename) {
|
||||||
|
fileBadge = `
|
||||||
|
<div class="inline-flex items-center gap-1.5 px-2.5 py-1 mb-2 bg-indigo-700/80 rounded-lg text-xs font-semibold text-white shadow-xs">
|
||||||
|
<i class="fa-solid fa-paperclip text-xs"></i>
|
||||||
|
<span class="truncate max-w-xs">${escapeHtml(filename)}</span>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const msgHtml = `
|
||||||
|
<div id="${msgId}" class="user-chat-bubble relative flex gap-3 max-w-4xl mx-auto w-full justify-end pt-3 scroll-mt-4">
|
||||||
|
<div class="flex-1 max-w-2xl bg-indigo-600 text-white rounded-2xl rounded-tr-none p-4 shadow-sm">
|
||||||
|
${fileBadge}
|
||||||
|
<div class="text-sm leading-relaxed whitespace-pre-wrap">${escapeHtml(text)}</div>
|
||||||
|
</div>
|
||||||
|
<div class="w-8 h-8 rounded-lg bg-slate-200 text-slate-600 flex items-center justify-center shrink-0 shadow-sm mt-0.5 font-bold text-sm">
|
||||||
|
<i class="fa-solid fa-user"></i>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
container.insertAdjacentHTML("beforeend", msgHtml);
|
||||||
|
}
|
||||||
|
|
||||||
|
function appendAssistantLoading() {
|
||||||
|
const container = document.getElementById("chat-messages-container");
|
||||||
|
if (!container) return null;
|
||||||
|
|
||||||
|
const loadingId = 'loading-' + Date.now();
|
||||||
|
const html = `
|
||||||
|
<div id="${loadingId}" class="flex gap-3 max-w-4xl mx-auto w-full pt-1">
|
||||||
|
<div class="w-8 h-8 rounded-lg bg-indigo-600 text-white flex items-center justify-center shrink-0 shadow-sm mt-0.5">
|
||||||
|
<i class="fa-solid fa-robot text-sm"></i>
|
||||||
|
</div>
|
||||||
|
<div class="flex-1 bg-white border border-slate-200 rounded-2xl rounded-tl-none p-4 shadow-sm">
|
||||||
|
<div class="text-sm text-slate-500 flex items-center gap-2">
|
||||||
|
<i class="fa-solid fa-spinner fa-spin text-indigo-600"></i>
|
||||||
|
<span>ИИ обрабатывает запрос...</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
container.insertAdjacentHTML("beforeend", html);
|
||||||
|
return loadingId;
|
||||||
|
}
|
||||||
|
|
||||||
|
function appendAssistantMessage(text, buttons = [], actionPayload = null) {
|
||||||
|
const container = document.getElementById("chat-messages-container");
|
||||||
|
if (!container) return;
|
||||||
|
|
||||||
|
let payloadHtml = '';
|
||||||
|
let isHtmlBody = false;
|
||||||
|
|
||||||
|
if (actionPayload) {
|
||||||
|
if (actionPayload.type === 'SNAPSHOTS_CARD' && typeof renderSnapshotsCard === 'function') {
|
||||||
|
payloadHtml = renderSnapshotsCard(actionPayload.data);
|
||||||
|
} else if (actionPayload.type === 'TASK_INTERACTIVE_CARD' && typeof renderInteractiveTaskCard === 'function') {
|
||||||
|
payloadHtml = renderInteractiveTaskCard(actionPayload.tasks);
|
||||||
|
} else if (actionPayload.type === 'FILE_DOWNLOAD_CARD') {
|
||||||
|
const dlUrl = actionPayload.download_url || '#';
|
||||||
|
const fName = actionPayload.filename || 'document.docx';
|
||||||
|
const count = actionPayload.tasks_count || '';
|
||||||
|
payloadHtml = `
|
||||||
|
<div class="mt-3 p-3.5 bg-slate-50 border border-slate-200 rounded-xl flex items-center justify-between gap-3 shadow-xs">
|
||||||
|
<div class="flex items-center gap-3 min-w-0">
|
||||||
|
<div class="w-10 h-10 rounded-xl bg-blue-600 text-white flex items-center justify-center shrink-0 shadow-sm">
|
||||||
|
<i class="fa-solid fa-file-word text-lg"></i>
|
||||||
|
</div>
|
||||||
|
<div class="min-w-0">
|
||||||
|
<div class="text-sm font-bold text-slate-800 truncate">${escapeHtml(fName)}</div>
|
||||||
|
<div class="text-xs text-slate-500">${count ? count + ' · ' : ''}Документ MS Word (.docx)</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<a href="${dlUrl}" download="${escapeHtml(fName)}" target="_blank"
|
||||||
|
class="px-4 py-2.5 bg-emerald-600 hover:bg-emerald-700 active:bg-emerald-800 text-white font-bold rounded-xl text-xs shadow-sm transition flex items-center gap-2 shrink-0">
|
||||||
|
<i class="fa-solid fa-arrow-down-to-line text-sm text-white"></i>
|
||||||
|
<span class="text-white tracking-wide">Скачать файл</span>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
} else if (actionPayload.type === 'SNAPSHOT_INSPECT_CARD') {
|
||||||
|
const records = actionPayload.records || [];
|
||||||
|
const inspectTableId = 'inspect-table-' + Date.now();
|
||||||
|
const searchInputId = 'inspect-search-' + Date.now();
|
||||||
|
|
||||||
|
const rowsHtml = records.map((r, idx) => `
|
||||||
|
<tr class="inspect-row border-b border-slate-100 text-xs ${r.is_present ? 'bg-white' : 'bg-slate-50/60'} hover:bg-indigo-50/40"
|
||||||
|
data-fio="${escapeHtml(r.fio).toLowerCase()}" data-dept="${escapeHtml(r.department).toLowerCase()}">
|
||||||
|
<td class="p-2.5 text-slate-400 text-center font-mono w-10">${idx + 1}</td>
|
||||||
|
<td class="p-2.5 font-medium text-slate-800">${escapeHtml(r.fio)}</td>
|
||||||
|
<td class="p-2.5 text-slate-500 text-center">${escapeHtml(r.department)}</td>
|
||||||
|
<td class="p-2.5 text-center ${r.time_in !== 'Нет входа' ? 'font-bold text-emerald-700' : 'text-slate-400'}">${escapeHtml(r.time_in)}</td>
|
||||||
|
<td class="p-2.5 text-center text-slate-500">${escapeHtml(r.first_activity)}</td>
|
||||||
|
<td class="p-2.5 text-center ${r.time_out !== 'Нет выхода' ? 'font-bold text-slate-800' : 'text-slate-400'}">${escapeHtml(r.time_out)}</td>
|
||||||
|
<td class="p-2.5 text-center font-mono text-slate-600">${escapeHtml(r.in_building)}</td>
|
||||||
|
<td class="p-2.5 text-center font-bold">${r.is_present ? '<span class="text-emerald-600">✓ Да</span>' : '<span class="text-slate-400">Нет</span>'}</td>
|
||||||
|
</tr>
|
||||||
|
`).join('');
|
||||||
|
|
||||||
|
payloadHtml = `
|
||||||
|
<div class="mt-3 bg-white border border-slate-200 rounded-xl overflow-hidden shadow-sm flex flex-col">
|
||||||
|
<div class="px-4 py-3 bg-slate-100/90 border-b border-slate-200 flex items-center justify-between gap-2 flex-wrap">
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<span class="text-sm font-bold text-slate-800">Срез #${escapeHtml(actionPayload.snapshot_id)}</span>
|
||||||
|
<span class="text-xs text-slate-500">· Всего: ${records.length} чел. (Присутствуют: ${actionPayload.present_count || 0})</span>
|
||||||
|
</div>
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<input type="text" id="${searchInputId}" placeholder="Поиск в срезе (ФИО / отдел)..."
|
||||||
|
oninput="window.filterInspectTable('${inspectTableId}', this.value)"
|
||||||
|
class="text-xs px-3 py-1.5 bg-white border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500 w-60" />
|
||||||
|
<button onclick="window.sendChatAction('покажи срезы')" class="text-xs text-indigo-600 hover:underline font-semibold">Все срезы</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="max-h-96 overflow-y-auto">
|
||||||
|
<table id="${inspectTableId}" class="w-full text-left border-collapse">
|
||||||
|
<thead class="bg-slate-50 text-[11px] uppercase text-slate-500 sticky top-0 border-b border-slate-200 shadow-xs">
|
||||||
|
<tr>
|
||||||
|
<th class="p-2.5 text-center w-10">№</th>
|
||||||
|
<th class="p-2.5">Сотрудник</th>
|
||||||
|
<th class="p-2.5 text-center">Отдел</th>
|
||||||
|
<th class="p-2.5 text-center">Вход</th>
|
||||||
|
<th class="p-2.5 text-center">Активность</th>
|
||||||
|
<th class="p-2.5 text-center">Выход</th>
|
||||||
|
<th class="p-2.5 text-center">В здании</th>
|
||||||
|
<th class="p-2.5 text-center">Статус</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>${rowsHtml}</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
} else if (actionPayload.type === 'PROMPT_EDITOR') {
|
||||||
|
const draftText = actionPayload.raw_draft || actionPayload.baseline_prompt || '';
|
||||||
|
const editorId = 'prompt-editor-' + Date.now();
|
||||||
|
payloadHtml = `
|
||||||
|
<div class="mt-3 p-3.5 bg-slate-50 border border-slate-300 rounded-xl flex flex-col gap-2 shadow-inner">
|
||||||
|
<div class="flex items-center justify-between text-xs font-bold text-slate-700">
|
||||||
|
<span><i class="fa-solid fa-pen-to-square text-indigo-600 mr-1"></i> Инлайн-редактор системного промпта:</span>
|
||||||
|
<span class="text-[11px] text-slate-400 font-normal">Прямое редактирование текста</span>
|
||||||
|
</div>
|
||||||
|
<textarea id="${editorId}" rows="14"
|
||||||
|
class="w-full text-xs font-mono p-3 border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500 bg-white leading-relaxed resize-y">${escapeHtml(draftText)}</textarea>
|
||||||
|
<div class="flex items-center justify-end gap-2 pt-1">
|
||||||
|
<button type="button" onclick="window.sendChatAction('отмена')" class="px-3.5 py-1.5 text-xs text-slate-600 hover:bg-slate-200 rounded-lg transition font-medium">Отменить</button>
|
||||||
|
<button type="button" onclick="window.submitPromptDraftToDiff('${editorId}')" class="px-4 py-1.5 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg text-xs font-bold shadow transition flex items-center gap-1.5">
|
||||||
|
<i class="fa-solid fa-eye text-xs"></i>
|
||||||
|
<span>Показать превью изменений</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
} else if (actionPayload.type === 'RULES_EDITOR') {
|
||||||
|
const draftText = actionPayload.raw_draft || actionPayload.baseline_prompt || '';
|
||||||
|
const editorId = 'rules-editor-' + Date.now();
|
||||||
|
payloadHtml = `
|
||||||
|
<div class="mt-3 p-3.5 bg-slate-50 border border-slate-300 rounded-xl flex flex-col gap-2 shadow-inner">
|
||||||
|
<div class="flex items-center justify-between text-xs font-bold text-slate-700">
|
||||||
|
<span><i class="fa-solid fa-book-bookmark text-emerald-600 mr-1"></i> Редактор базы знаний и правил компании:</span>
|
||||||
|
<span class="text-[11px] text-slate-400 font-normal">Прямое изменение правил кадрового арбитража</span>
|
||||||
|
</div>
|
||||||
|
<textarea id="${editorId}" rows="12"
|
||||||
|
class="w-full text-xs font-mono p-3 border border-slate-300 rounded-lg focus:outline-none focus:border-emerald-500 bg-white leading-relaxed resize-y">${escapeHtml(draftText)}</textarea>
|
||||||
|
<div class="flex items-center justify-end gap-2 pt-1">
|
||||||
|
<button type="button" onclick="window.sendChatAction('отмена')" class="px-3.5 py-1.5 text-xs text-slate-600 hover:bg-slate-200 rounded-lg transition font-medium">Отменить</button>
|
||||||
|
<button type="button" onclick="window.submitRulesDraftToDiff('${editorId}')" class="px-4 py-1.5 bg-emerald-600 hover:bg-emerald-700 text-white rounded-lg text-xs font-bold shadow transition flex items-center gap-1.5">
|
||||||
|
<i class="fa-solid fa-eye text-xs"></i>
|
||||||
|
<span>Показать превью изменений</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
} else if (actionPayload.type === 'PROMPT_PREVIEW') {
|
||||||
|
isHtmlBody = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let buttonsHtml = '';
|
||||||
|
if (!actionPayload || (actionPayload.type !== 'PROMPT_EDITOR' && actionPayload.type !== 'RULES_EDITOR')) {
|
||||||
|
const allButtons = (buttons && buttons.length > 0) ? buttons : (actionPayload && actionPayload.buttons ? actionPayload.buttons : []);
|
||||||
|
if (allButtons && Array.isArray(allButtons) && allButtons.length > 0) {
|
||||||
|
buttonsHtml = `
|
||||||
|
<div class="flex flex-wrap gap-2 mt-3.5 pt-2.5 border-t border-slate-100">
|
||||||
|
${allButtons.map(b => `
|
||||||
|
<button onclick="window.sendChatAction('${escapeHtml(b.value || b.action || b.title || '')}')"
|
||||||
|
class="px-3 py-1.5 bg-indigo-50 hover:bg-indigo-100 text-indigo-700 rounded-lg text-xs font-semibold border border-indigo-200 transition">
|
||||||
|
${escapeHtml(b.label || b.title || b.action)}
|
||||||
|
</button>
|
||||||
|
`).join('')}
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const bodyContent = isHtmlBody ? text : escapeHtml(text);
|
||||||
|
|
||||||
|
const html = `
|
||||||
|
<div class="flex gap-3 max-w-4xl mx-auto w-full pt-1">
|
||||||
|
<div class="w-8 h-8 rounded-lg bg-indigo-600 text-white flex items-center justify-center shrink-0 shadow-sm mt-0.5">
|
||||||
|
<i class="fa-solid fa-robot text-sm"></i>
|
||||||
|
</div>
|
||||||
|
<div class="flex-1 bg-white border border-slate-200 rounded-2xl rounded-tl-none p-4 shadow-sm min-w-0">
|
||||||
|
<div class="text-[11px] font-bold text-indigo-600 uppercase tracking-wider mb-1.5">ИИ-ассистент SCUD Orion AI</div>
|
||||||
|
<div class="text-sm text-slate-800 leading-relaxed whitespace-pre-wrap">${bodyContent}</div>
|
||||||
|
${payloadHtml}
|
||||||
|
${buttonsHtml}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
container.insertAdjacentHTML("beforeend", html);
|
||||||
|
}
|
||||||
|
|
||||||
|
window.filterInspectTable = function(tableId, query) {
|
||||||
|
const table = document.getElementById(tableId);
|
||||||
|
if (!table) return;
|
||||||
|
const q = (query || '').trim().toLowerCase();
|
||||||
|
const rows = table.querySelectorAll('.inspect-row');
|
||||||
|
rows.forEach(r => {
|
||||||
|
const fio = r.getAttribute('data-fio') || '';
|
||||||
|
const dept = r.getAttribute('data-dept') || '';
|
||||||
|
if (!q || fio.includes(q) || dept.includes(q)) {
|
||||||
|
r.classList.remove('hidden');
|
||||||
|
} else {
|
||||||
|
r.classList.add('hidden');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
window.sendChatAction = function(actionText) {
|
||||||
|
if (!actionText || !actionText.trim()) return;
|
||||||
|
const input = document.getElementById("user-input");
|
||||||
|
if (input) {
|
||||||
|
input.value = actionText.trim();
|
||||||
|
updateInputHeightAndFade(input);
|
||||||
|
}
|
||||||
|
window.sendMessage();
|
||||||
|
};
|
||||||
|
|
||||||
|
window.submitPromptDraftToDiff = function(editorId) {
|
||||||
|
const textarea = document.getElementById(editorId);
|
||||||
|
if (!textarea) return;
|
||||||
|
const newText = textarea.value.trim();
|
||||||
|
if (!newText) {
|
||||||
|
alert("Текст системного промпта не может быть пустым");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const command = `action:save_draft_prompt:::${newText}`;
|
||||||
|
const input = document.getElementById("user-input");
|
||||||
|
if (input) input.value = command;
|
||||||
|
window.sendMessage();
|
||||||
|
};
|
||||||
|
|
||||||
|
window.submitRulesDraftToDiff = function(editorId) {
|
||||||
|
const textarea = document.getElementById(editorId);
|
||||||
|
if (!textarea) return;
|
||||||
|
const newText = textarea.value.trim();
|
||||||
|
if (!newText) {
|
||||||
|
alert("Правила не могут быть пустыми");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const command = `action:save_draft_rules:::${newText}`;
|
||||||
|
const input = document.getElementById("user-input");
|
||||||
|
if (input) input.value = command;
|
||||||
|
window.sendMessage();
|
||||||
|
};
|
||||||
|
|
||||||
|
window.sendMessage = async function() {
|
||||||
|
const input = document.getElementById("user-input");
|
||||||
|
if (!input) return;
|
||||||
|
|
||||||
|
const messageText = input.value.trim();
|
||||||
|
const fileToSend = currentAttachedFile;
|
||||||
|
|
||||||
|
if (!messageText && !fileToSend) return;
|
||||||
|
|
||||||
|
saveCommandToHistory(messageText);
|
||||||
|
|
||||||
|
input.value = "";
|
||||||
|
input.style.height = '32px';
|
||||||
|
input.style.overflowY = 'hidden';
|
||||||
|
input.style.maskImage = 'none';
|
||||||
|
input.style.webkitMaskImage = 'none';
|
||||||
|
window.clearAttachedFile();
|
||||||
|
|
||||||
|
// 1. Отрисовка сообщения пользователя в ленте
|
||||||
|
if (!messageText.startsWith("action:save_draft_prompt:::") && !messageText.startsWith("action:save_draft_rules:::")) {
|
||||||
|
appendUserMessage(
|
||||||
|
messageText || (fileToSend ? `Прикреплен файл: ${fileToSend.name}` : ''),
|
||||||
|
fileToSend ? fileToSend.name : null
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
appendUserMessage("Сформировать предпросмотр изменений");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Вставка лоадера
|
||||||
|
const loadingId = appendAssistantLoading();
|
||||||
|
|
||||||
|
// 3. Вызов точного скролла
|
||||||
|
scrollToUserMessageTop();
|
||||||
|
|
||||||
|
try {
|
||||||
|
let res;
|
||||||
|
if (fileToSend) {
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append("file", fileToSend);
|
||||||
|
formData.append("message", messageText);
|
||||||
|
formData.append("session_id", "web_session_main");
|
||||||
|
|
||||||
|
res = await fetch("/api/v1/chat/upload", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Authorization": AuthManager.getAuthHeaders()["Authorization"]
|
||||||
|
},
|
||||||
|
body: formData
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.status === 404) {
|
||||||
|
res = await fetch("/api/v1/chat", {
|
||||||
|
method: "POST",
|
||||||
|
headers: AuthManager.getAuthHeaders(),
|
||||||
|
body: JSON.stringify({
|
||||||
|
message: messageText || `Загружен файл: ${fileToSend.name}`,
|
||||||
|
session_id: "web_session_main"
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res = await fetch("/api/v1/chat", {
|
||||||
|
method: "POST",
|
||||||
|
headers: AuthManager.getAuthHeaders(),
|
||||||
|
body: JSON.stringify({
|
||||||
|
message: messageText,
|
||||||
|
session_id: "web_session_main"
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const loaderEl = document.getElementById(loadingId);
|
||||||
|
if (loaderEl) loaderEl.remove();
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
|
const replyText = data.response || data.text || data.message || "Запрос выполнен.";
|
||||||
|
const buttons = data.buttons || [];
|
||||||
|
const actionPayload = data.action_payload || null;
|
||||||
|
|
||||||
|
appendAssistantMessage(replyText, buttons, actionPayload);
|
||||||
|
|
||||||
|
if (window.SidebarManager) {
|
||||||
|
SidebarManager.renderContent();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const err = await res.json().catch(() => ({}));
|
||||||
|
appendAssistantMessage(`⚠️ Ошибка сервера (${res.status}): ${err.detail || "Не удалось получить ответ"}`);
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("[Chat Error]:", err);
|
console.error("Ошибка отправки сообщения:", err);
|
||||||
if (typeof appendMessageToUI === "function") {
|
const loaderEl = document.getElementById(loadingId);
|
||||||
appendMessageToUI("assistant", `⚠️ Не удалось связаться с сервером: ${err.message}`);
|
if (loaderEl) loaderEl.remove();
|
||||||
|
appendAssistantMessage("⚠️ Ошибка соединения с сервером при отправке сообщения.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.clearAttachedFile = function() {
|
||||||
|
currentAttachedFile = null;
|
||||||
|
const preview = document.getElementById("file-attachment-preview");
|
||||||
|
const nameEl = document.getElementById("file-attachment-name");
|
||||||
|
const fileInput = document.getElementById("file-upload-input");
|
||||||
|
|
||||||
|
if (preview) preview.classList.add("hidden");
|
||||||
|
if (nameEl) nameEl.innerText = "";
|
||||||
|
if (fileInput) fileInput.value = "";
|
||||||
|
};
|
||||||
|
|
||||||
|
function handleFileSelected(file) {
|
||||||
|
if (!file) return;
|
||||||
|
currentAttachedFile = file;
|
||||||
|
const preview = document.getElementById("file-attachment-preview");
|
||||||
|
const nameEl = document.getElementById("file-attachment-name");
|
||||||
|
|
||||||
|
if (preview && nameEl) {
|
||||||
|
nameEl.innerText = file.name;
|
||||||
|
preview.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
|
const input = document.getElementById("user-input");
|
||||||
|
const fileInput = document.getElementById("file-upload-input");
|
||||||
|
const overlay = document.getElementById("global-drag-overlay");
|
||||||
|
|
||||||
|
if (input) {
|
||||||
|
input.style.lineHeight = '24px';
|
||||||
|
input.style.height = '32px';
|
||||||
|
|
||||||
|
input.addEventListener("keydown", (e) => {
|
||||||
|
if (e.key === "Enter") {
|
||||||
|
if (e.shiftKey) return;
|
||||||
|
e.preventDefault();
|
||||||
|
window.sendMessage();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (e.key === "ArrowUp") {
|
||||||
|
const isSingleLine = !input.value.includes("\n");
|
||||||
|
const isAtBeginning = input.selectionStart === 0 && input.selectionEnd === 0;
|
||||||
|
|
||||||
|
if (isSingleLine || isAtBeginning) {
|
||||||
|
if (chatInputHistory.length > 0) {
|
||||||
|
e.preventDefault();
|
||||||
|
if (chatHistoryIndex === -1) {
|
||||||
|
temporaryCurrentInput = input.value;
|
||||||
|
chatHistoryIndex = chatInputHistory.length - 1;
|
||||||
|
} else if (chatHistoryIndex > 0) {
|
||||||
|
chatHistoryIndex--;
|
||||||
|
}
|
||||||
|
input.value = chatInputHistory[chatHistoryIndex];
|
||||||
|
updateInputHeightAndFade(input);
|
||||||
|
input.setSelectionRange(input.value.length, input.value.length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (e.key === "ArrowDown") {
|
||||||
|
const isSingleLine = !input.value.includes("\n");
|
||||||
|
const isAtEnd = input.selectionStart === input.value.length;
|
||||||
|
|
||||||
|
if (isSingleLine || isAtEnd) {
|
||||||
|
if (chatHistoryIndex !== -1) {
|
||||||
|
e.preventDefault();
|
||||||
|
if (chatHistoryIndex < chatInputHistory.length - 1) {
|
||||||
|
chatHistoryIndex++;
|
||||||
|
input.value = chatInputHistory[chatHistoryIndex];
|
||||||
|
} else {
|
||||||
|
chatHistoryIndex = -1;
|
||||||
|
input.value = temporaryCurrentInput;
|
||||||
|
}
|
||||||
|
updateInputHeightAndFade(input);
|
||||||
|
input.setSelectionRange(input.value.length, input.value.length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
input.addEventListener("input", function() {
|
||||||
|
updateInputHeightAndFade(this);
|
||||||
|
chatHistoryIndex = -1;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fileInput) {
|
||||||
|
fileInput.addEventListener("change", (e) => {
|
||||||
|
if (e.target.files && e.target.files[0]) {
|
||||||
|
handleFileSelected(e.target.files[0]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let dragCounter = 0;
|
||||||
|
|
||||||
|
window.addEventListener('dragenter', (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
dragCounter++;
|
||||||
|
if (overlay) {
|
||||||
|
overlay.classList.remove('hidden');
|
||||||
|
}
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
window.addEventListener('dragleave', (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
dragCounter--;
|
||||||
|
if (dragCounter <= 0) {
|
||||||
|
dragCounter = 0;
|
||||||
|
if (overlay) {
|
||||||
|
overlay.classList.add('hidden');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
window.addEventListener('dragover', (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
window.addEventListener('drop', (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
dragCounter = 0;
|
||||||
|
if (overlay) {
|
||||||
|
overlay.classList.add('hidden');
|
||||||
|
}
|
||||||
|
|
||||||
|
const dt = e.dataTransfer;
|
||||||
|
if (dt && dt.files && dt.files[0]) {
|
||||||
|
handleFileSelected(dt.files[0]);
|
||||||
|
}
|
||||||
|
}, false);
|
||||||
|
});
|
||||||
@@ -0,0 +1,215 @@
|
|||||||
|
/**
|
||||||
|
* ===============================================================================
|
||||||
|
* FILE: modules/web_api/static/js/manual_absences.js
|
||||||
|
* ROLE: Модальные окна "Мест. командир.", "Иное", универсальный автокомплит ФИО
|
||||||
|
* и синхронизация с боковой панелью SidebarManager.
|
||||||
|
* ===============================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
let activeAbsenceType = 'LOCAL_TRIP'; // 'LOCAL_TRIP' или 'OTHER'
|
||||||
|
let reasonsCache = [];
|
||||||
|
|
||||||
|
async function loadAbsenceReasons() {
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/manual-absences/reasons');
|
||||||
|
if (res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
|
reasonsCache = data.reasons || [];
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Ошибка загрузки причин:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ⭐️ Открытие окна: режим создания (item = null) или редактирования (item = {...})
|
||||||
|
function openManualAbsenceModal(type, editItem = null) {
|
||||||
|
activeAbsenceType = type;
|
||||||
|
const isTrip = (type === 'LOCAL_TRIP');
|
||||||
|
const modal = document.getElementById('manual-absence-modal');
|
||||||
|
const titleEl = document.getElementById('manual-absence-modal-title-text');
|
||||||
|
const reasonBlock = document.getElementById('manual-absence-reason-block');
|
||||||
|
const reasonSelect = document.getElementById('manual-absence-reason-select');
|
||||||
|
|
||||||
|
const idInput = document.getElementById('manual-absence-id');
|
||||||
|
const fioInput = document.getElementById('manual-absence-fio-input');
|
||||||
|
const deptInput = document.getElementById('manual-absence-dept');
|
||||||
|
const posInput = document.getElementById('manual-absence-pos');
|
||||||
|
const startDateInput = document.getElementById('manual-absence-start-date');
|
||||||
|
const endDateInput = document.getElementById('manual-absence-end-date');
|
||||||
|
const errEl = document.getElementById('manual-absence-error');
|
||||||
|
|
||||||
|
if (errEl) errEl.classList.add('hidden');
|
||||||
|
|
||||||
|
if (reasonBlock && reasonSelect) {
|
||||||
|
if (isTrip) {
|
||||||
|
reasonBlock.classList.add('hidden');
|
||||||
|
} else {
|
||||||
|
reasonBlock.classList.remove('hidden');
|
||||||
|
reasonSelect.innerHTML = reasonsCache.map(r => `<option value="${r}">${r}</option>`).join('');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const today = new Date().toISOString().split('T')[0];
|
||||||
|
|
||||||
|
if (editItem) {
|
||||||
|
// Режим РЕДАКТИРОВАНИЯ
|
||||||
|
if (titleEl) titleEl.innerText = isTrip ? 'Изменение сроков командировки' : 'Изменение сроков отсутствия';
|
||||||
|
if (idInput) idInput.value = editItem.id;
|
||||||
|
if (fioInput) {
|
||||||
|
fioInput.value = editItem.fio;
|
||||||
|
fioInput.readOnly = true;
|
||||||
|
fioInput.classList.add('bg-slate-100', 'text-slate-500', 'cursor-not-allowed');
|
||||||
|
}
|
||||||
|
if (deptInput) deptInput.value = editItem.department || '';
|
||||||
|
if (posInput) posInput.value = editItem.position || '';
|
||||||
|
if (startDateInput) startDateInput.value = editItem.date_start ? editItem.date_start.replace(/\./g, '-') : today;
|
||||||
|
if (endDateInput) endDateInput.value = editItem.date_end ? editItem.date_end.replace(/\./g, '-') : today;
|
||||||
|
if (reasonSelect && editItem.reason) reasonSelect.value = editItem.reason;
|
||||||
|
} else {
|
||||||
|
// Режим СОЗДАНИЯ
|
||||||
|
if (titleEl) titleEl.innerText = isTrip ? 'Добавить в командировки' : 'Добавить отсутствие';
|
||||||
|
if (idInput) idInput.value = '';
|
||||||
|
if (fioInput) {
|
||||||
|
fioInput.value = '';
|
||||||
|
fioInput.readOnly = false;
|
||||||
|
fioInput.classList.remove('bg-slate-100', 'text-slate-500', 'cursor-not-allowed');
|
||||||
|
}
|
||||||
|
if (deptInput) deptInput.value = '';
|
||||||
|
if (posInput) posInput.value = '';
|
||||||
|
if (startDateInput) startDateInput.value = today;
|
||||||
|
if (endDateInput) endDateInput.value = today;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (modal) modal.classList.remove('hidden');
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeManualAbsenceModal() {
|
||||||
|
const modal = document.getElementById('manual-absence-modal');
|
||||||
|
if (modal) modal.classList.add('hidden');
|
||||||
|
}
|
||||||
|
|
||||||
|
async function submitManualAbsence(event) {
|
||||||
|
if (event) event.preventDefault();
|
||||||
|
const idVal = document.getElementById('manual-absence-id')?.value.trim();
|
||||||
|
const fio = document.getElementById('manual-absence-fio-input')?.value.trim();
|
||||||
|
const deptVal = document.getElementById('manual-absence-dept')?.value.trim() || '';
|
||||||
|
const posVal = document.getElementById('manual-absence-pos')?.value.trim() || '';
|
||||||
|
const startDateVal = document.getElementById('manual-absence-start-date')?.value || null;
|
||||||
|
const endDateVal = document.getElementById('manual-absence-end-date')?.value || null;
|
||||||
|
const reasonSelect = document.getElementById('manual-absence-reason-select');
|
||||||
|
const errEl = document.getElementById('manual-absence-error');
|
||||||
|
|
||||||
|
if (!fio) {
|
||||||
|
if (errEl) { errEl.innerText = 'Укажите ФИО сотрудника'; errEl.classList.remove('hidden'); }
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
absence_type: activeAbsenceType,
|
||||||
|
fio: fio,
|
||||||
|
department: deptVal,
|
||||||
|
position: posVal,
|
||||||
|
date_start: startDateVal,
|
||||||
|
date_end: endDateVal,
|
||||||
|
reason: activeAbsenceType === 'LOCAL_TRIP' ? 'Местная командировка' : (reasonSelect ? reasonSelect.value : 'Иное')
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
let res;
|
||||||
|
if (idVal) {
|
||||||
|
// Редактирование
|
||||||
|
res = await fetch(`/api/v1/manual-absences/${idVal}`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { ...AuthManager.getAuthHeaders(), 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ id: parseInt(idVal), date_start: startDateVal, date_end: endDateVal })
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Создание
|
||||||
|
res = await fetch('/api/v1/manual-absences/', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { ...AuthManager.getAuthHeaders(), 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(payload)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
closeManualAbsenceModal();
|
||||||
|
if (typeof loadManualAbsencesView === 'function') {
|
||||||
|
loadManualAbsencesView(activeAbsenceType);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const data = await res.json();
|
||||||
|
if (errEl) { errEl.innerText = data.detail || 'Ошибка сохранения'; errEl.classList.remove('hidden'); }
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
if (errEl) { errEl.innerText = 'Сетевая ошибка при сохранении'; errEl.classList.remove('hidden'); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Универсальный автокомплит сотрудников
|
||||||
|
let searchTimeout = null;
|
||||||
|
function setupStaffAutocomplete(inputEl, suggestionsBoxId) {
|
||||||
|
const box = document.getElementById(suggestionsBoxId);
|
||||||
|
if (!inputEl || !box) return;
|
||||||
|
|
||||||
|
inputEl.addEventListener('input', function() {
|
||||||
|
const val = this.value.trim();
|
||||||
|
clearTimeout(searchTimeout);
|
||||||
|
if (val.length < 2) {
|
||||||
|
box.classList.add('hidden');
|
||||||
|
box.innerHTML = '';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
searchTimeout = setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/v1/manual-absences/staff-autocomplete?q=${encodeURIComponent(val)}`);
|
||||||
|
if (!res.ok) return;
|
||||||
|
const items = await res.json();
|
||||||
|
if (items.length === 0) {
|
||||||
|
box.classList.add('hidden');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
box.innerHTML = items.map(it => `
|
||||||
|
<div class="p-2 hover:bg-indigo-50 cursor-pointer border-b border-slate-100 flex flex-col text-xs"
|
||||||
|
onmousedown="selectStaffSuggestion('${escapeHtml(it.fio)}', '${escapeHtml(it.department)}', '${escapeHtml(it.position)}', '${inputEl.id}', '${suggestionsBoxId}')">
|
||||||
|
<span class="font-bold text-slate-800">${escapeHtml(it.fio)}</span>
|
||||||
|
<span class="text-[10px] text-slate-500">${escapeHtml(it.department)} · ${escapeHtml(it.position)}</span>
|
||||||
|
</div>
|
||||||
|
`).join('');
|
||||||
|
box.classList.remove('hidden');
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
}, 200);
|
||||||
|
});
|
||||||
|
|
||||||
|
document.addEventListener('click', (e) => {
|
||||||
|
if (!inputEl.contains(e.target) && !box.contains(e.target)) {
|
||||||
|
box.classList.add('hidden');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function selectStaffSuggestion(fio, dept, pos, targetInputId, boxId) {
|
||||||
|
const targetFioEl = document.getElementById(targetInputId);
|
||||||
|
if (targetFioEl) targetFioEl.value = fio;
|
||||||
|
|
||||||
|
if (targetInputId === 'rw-fio') {
|
||||||
|
const rwDept = document.getElementById('rw-dept');
|
||||||
|
if (rwDept && dept && dept !== '—') rwDept.value = dept;
|
||||||
|
} else if (targetInputId === 'manual-absence-fio-input') {
|
||||||
|
const deptInput = document.getElementById('manual-absence-dept');
|
||||||
|
const posInput = document.getElementById('manual-absence-pos');
|
||||||
|
if (deptInput) deptInput.value = dept;
|
||||||
|
if (posInput) posInput.value = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
const box = document.getElementById(boxId);
|
||||||
|
if (box) box.classList.add('hidden');
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
loadAbsenceReasons();
|
||||||
|
});
|
||||||
@@ -0,0 +1,176 @@
|
|||||||
|
/**
|
||||||
|
* ===============================================================================
|
||||||
|
* FILE: modules/web_api/static/js/presence.js
|
||||||
|
* ROLE: Контроллер экрана оперативного мониторинга («Кто в здании прямо сейчас»).
|
||||||
|
* ===============================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
window.PresenceManager = {
|
||||||
|
records: [],
|
||||||
|
activeTab: 'ALL',
|
||||||
|
|
||||||
|
open() {
|
||||||
|
const modal = document.getElementById('presence-modal');
|
||||||
|
if (modal) {
|
||||||
|
modal.classList.remove('hidden');
|
||||||
|
this.bindEventsOnce();
|
||||||
|
this.loadData(false);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
close() {
|
||||||
|
const modal = document.getElementById('presence-modal');
|
||||||
|
if (modal) modal.classList.add('hidden');
|
||||||
|
},
|
||||||
|
|
||||||
|
bindEventsOnce() {
|
||||||
|
if (this._eventsBound) return;
|
||||||
|
this._eventsBound = true;
|
||||||
|
|
||||||
|
document.getElementById('btn-close-presence-modal')?.addEventListener('click', () => this.close());
|
||||||
|
document.getElementById('btn-presence-force-refresh')?.addEventListener('click', () => this.loadData(true));
|
||||||
|
|
||||||
|
document.querySelectorAll('.presence-tab-btn').forEach(btn => {
|
||||||
|
btn.addEventListener('click', (e) => {
|
||||||
|
document.querySelectorAll('.presence-tab-btn').forEach(b => {
|
||||||
|
b.classList.remove('bg-white', 'shadow-xs', 'text-slate-800');
|
||||||
|
b.classList.add('text-slate-600');
|
||||||
|
});
|
||||||
|
const target = e.currentTarget;
|
||||||
|
target.classList.add('bg-white', 'shadow-xs', 'text-slate-800');
|
||||||
|
target.classList.remove('text-slate-600');
|
||||||
|
this.activeTab = target.getAttribute('data-tab') || 'ALL';
|
||||||
|
this.applyFilters();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('presence-search-input')?.addEventListener('input', () => this.applyFilters());
|
||||||
|
document.getElementById('presence-dept-filter')?.addEventListener('change', () => this.applyFilters());
|
||||||
|
},
|
||||||
|
|
||||||
|
async loadData(forceRefresh = false) {
|
||||||
|
const subtitle = document.getElementById('presence-modal-subtitle');
|
||||||
|
const btnRefresh = document.getElementById('btn-presence-force-refresh');
|
||||||
|
|
||||||
|
if (btnRefresh && forceRefresh) {
|
||||||
|
btnRefresh.disabled = true;
|
||||||
|
btnRefresh.innerHTML = '<span>⏳</span><span>Опрос СКУД...</span>';
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const url = `/api/v1/presence/live?force_refresh=${forceRefresh}`;
|
||||||
|
const res = await fetch(url, { headers: AuthManager.getAuthHeaders() });
|
||||||
|
if (!res.ok) throw new Error('Ошибка ответа сервера');
|
||||||
|
const data = await res.json();
|
||||||
|
this.render(data);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[Presence] Ошибка загрузки:', err);
|
||||||
|
if (subtitle) subtitle.textContent = 'Ошибка загрузки данных присутствия';
|
||||||
|
} finally {
|
||||||
|
if (btnRefresh) {
|
||||||
|
btnRefresh.disabled = false;
|
||||||
|
btnRefresh.innerHTML = '<span>🔄</span><span>Запросить из СКУД</span>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
render(data) {
|
||||||
|
this.records = data.records || [];
|
||||||
|
const m = data.metrics || {};
|
||||||
|
|
||||||
|
// Заголовок
|
||||||
|
const subtitle = document.getElementById('presence-modal-subtitle');
|
||||||
|
const sourceLabel = data.data_source === 'LIVE_MSSQL' ? '🟢 Онлайн срез Орион' : '⏱️ Локальная база СКУД';
|
||||||
|
if (subtitle) {
|
||||||
|
subtitle.textContent = `${sourceLabel} на ${data.latest_event_time || data.timestamp} | Всего в штате 1С: ${m.total_staff || 0} чел.`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Плашки метрик (строго сбалансированы)
|
||||||
|
document.getElementById('metric-total').textContent = m.total_staff || 0;
|
||||||
|
document.getElementById('metric-inside').textContent = m.inside || 0;
|
||||||
|
document.getElementById('metric-outside').textContent = m.outside || 0;
|
||||||
|
document.getElementById('metric-remote').textContent = m.remote || 0;
|
||||||
|
document.getElementById('metric-absence').textContent = m.official_absence || 0;
|
||||||
|
document.getElementById('metric-not-entered').textContent = m.not_entered || 0;
|
||||||
|
document.getElementById('metric-excluded').textContent = m.excluded || 0;
|
||||||
|
|
||||||
|
// Счетчики на табах
|
||||||
|
document.getElementById('tab-cnt-all').textContent = m.total_staff || 0;
|
||||||
|
document.getElementById('tab-cnt-inside').textContent = m.inside || 0;
|
||||||
|
document.getElementById('tab-cnt-outside').textContent = m.outside || 0;
|
||||||
|
document.getElementById('tab-cnt-remote').textContent = m.remote || 0;
|
||||||
|
document.getElementById('tab-cnt-absence').textContent = m.official_absence || 0;
|
||||||
|
document.getElementById('tab-cnt-not-entered').textContent = m.not_entered || 0;
|
||||||
|
document.getElementById('tab-cnt-excluded').textContent = m.excluded || 0;
|
||||||
|
|
||||||
|
// Выпадающий список подразделений
|
||||||
|
const deptSelect = document.getElementById('presence-dept-filter');
|
||||||
|
if (deptSelect) {
|
||||||
|
const depts = Array.from(new Set(this.records.map(r => r.department).filter(Boolean))).sort();
|
||||||
|
deptSelect.innerHTML = '<option value="ALL">Все подразделения</option>' +
|
||||||
|
depts.map(d => `<option value="${escapeHtml(d)}">${escapeHtml(d)}</option>`).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.applyFilters();
|
||||||
|
},
|
||||||
|
|
||||||
|
applyFilters() {
|
||||||
|
const search = (document.getElementById('presence-search-input')?.value || '').toLowerCase().trim();
|
||||||
|
const dept = document.getElementById('presence-dept-filter')?.value || 'ALL';
|
||||||
|
|
||||||
|
const filtered = this.records.filter(r => {
|
||||||
|
if (this.activeTab === 'INSIDE' && r.status !== 'INSIDE') return false;
|
||||||
|
if (this.activeTab === 'OUTSIDE' && r.status !== 'OUTSIDE') return false;
|
||||||
|
if (this.activeTab === 'REMOTE' && r.status !== 'REMOTE') return false;
|
||||||
|
if (this.activeTab === 'ABSENCE' && r.status !== 'OFFICIAL_ABSENCE') return false;
|
||||||
|
if (this.activeTab === 'NOT_ENTERED' && r.status !== 'NOT_ENTERED') return false;
|
||||||
|
if (this.activeTab === 'EXCLUDED' && r.status !== 'EXCLUDED') return false;
|
||||||
|
|
||||||
|
if (dept !== 'ALL' && r.department !== dept) return false;
|
||||||
|
if (search && !r.fio.toLowerCase().includes(search) && !r.position.toLowerCase().includes(search)) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
const tbody = document.getElementById('presence-table-body');
|
||||||
|
if (!tbody) return;
|
||||||
|
|
||||||
|
if (!filtered.length) {
|
||||||
|
tbody.innerHTML = '<tr><td colspan="5" class="py-10 text-center text-slate-400">Сотрудники не найдены</td></tr>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
tbody.innerHTML = filtered.map(r => {
|
||||||
|
let badgeClass = 'bg-slate-100 text-slate-700';
|
||||||
|
if (r.status === 'INSIDE') {
|
||||||
|
badgeClass = r.is_fligel
|
||||||
|
? 'bg-emerald-100 text-emerald-900 border border-emerald-300 font-bold'
|
||||||
|
: 'bg-emerald-100 text-emerald-800 font-bold';
|
||||||
|
} else if (r.status === 'OUTSIDE') {
|
||||||
|
badgeClass = 'bg-amber-100 text-amber-800 font-bold';
|
||||||
|
} else if (r.status === 'REMOTE') {
|
||||||
|
badgeClass = 'bg-blue-100 text-blue-800 font-semibold';
|
||||||
|
} else if (r.status === 'OFFICIAL_ABSENCE') {
|
||||||
|
badgeClass = 'bg-purple-100 text-purple-800 font-medium';
|
||||||
|
} else if (r.status === 'NOT_ENTERED') {
|
||||||
|
badgeClass = 'bg-rose-100 text-rose-800 font-semibold';
|
||||||
|
} else if (r.status === 'EXCLUDED') {
|
||||||
|
badgeClass = 'bg-slate-200 text-slate-600 font-mono text-[10px]';
|
||||||
|
}
|
||||||
|
|
||||||
|
return `
|
||||||
|
<tr class="hover:bg-slate-50 transition-colors">
|
||||||
|
<td class="py-2.5 px-3 font-medium text-slate-800">${escapeHtml(r.fio)}</td>
|
||||||
|
<td class="py-2.5 px-3 text-slate-600 font-mono text-[11px]">${escapeHtml(r.department)}</td>
|
||||||
|
<td class="py-2.5 px-3 text-slate-500">${escapeHtml(r.position)}</td>
|
||||||
|
<td class="py-2.5 px-3 text-center">
|
||||||
|
<span class="px-2 py-0.5 rounded-md text-[11px] ${badgeClass}">${escapeHtml(r.status_label)}</span>
|
||||||
|
</td>
|
||||||
|
<td class="py-2.5 px-3 text-center text-slate-600 font-mono font-semibold">${escapeHtml(r.last_time || '—')}</td>
|
||||||
|
</tr>
|
||||||
|
`;
|
||||||
|
}).join('');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.openPresenceModal = () => window.PresenceManager.open();
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
/**
|
||||||
|
* Роутер табов сайдбара и общие утилиты.
|
||||||
|
*/
|
||||||
|
window.escapeHtml = window.escapeHtml || function (str) {
|
||||||
|
if (str === null || str === undefined) return '';
|
||||||
|
return String(str)
|
||||||
|
.replace(/&/g, '&')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/"/g, '"')
|
||||||
|
.replace(/'/g, ''');
|
||||||
|
};
|
||||||
|
|
||||||
|
const SidebarManager = {
|
||||||
|
currentTab: 'tasks',
|
||||||
|
currentRegistrySubTab: 'exceptions',
|
||||||
|
|
||||||
|
init() {
|
||||||
|
this.bindEvents();
|
||||||
|
this.switchTab('tasks');
|
||||||
|
},
|
||||||
|
|
||||||
|
bindEvents() {
|
||||||
|
document.querySelectorAll('.sidebar-tab-btn, [data-tab]').forEach(btn => {
|
||||||
|
btn.addEventListener('click', (e) => {
|
||||||
|
const tab = e.currentTarget.dataset.tab;
|
||||||
|
if (tab) this.switchTab(tab);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
switchTab(tabName) {
|
||||||
|
this.currentTab = tabName;
|
||||||
|
|
||||||
|
// Переключение стилей кнопок табов
|
||||||
|
document.querySelectorAll('.sidebar-tab-btn, [data-tab]').forEach(btn => {
|
||||||
|
const isActive = btn.dataset.tab === tabName;
|
||||||
|
btn.classList.toggle('text-indigo-600', isActive);
|
||||||
|
btn.classList.toggle('border-indigo-600', isActive);
|
||||||
|
btn.classList.toggle('font-bold', isActive);
|
||||||
|
btn.classList.toggle('text-slate-500', !isActive);
|
||||||
|
btn.classList.toggle('border-transparent', !isActive);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Переключение видимости вьюх
|
||||||
|
document.querySelectorAll('.sidebar-view').forEach(view => {
|
||||||
|
view.classList.add('hidden');
|
||||||
|
});
|
||||||
|
|
||||||
|
const targetView = document.getElementById(`sidebar-view-${tabName}`);
|
||||||
|
if (targetView) targetView.classList.remove('hidden');
|
||||||
|
|
||||||
|
// Вызов профильного загрузчика
|
||||||
|
if (tabName === 'tasks' && typeof loadTasks === 'function') {
|
||||||
|
loadTasks();
|
||||||
|
} else if (tabName === 'snapshots' && typeof loadSnapshotsView === 'function') {
|
||||||
|
loadSnapshotsView();
|
||||||
|
} else if (tabName === 'registries' && typeof switchRegistrySubTab === 'function') {
|
||||||
|
switchRegistrySubTab(this.currentRegistrySubTab);
|
||||||
|
} else if (tabName === 'prompts' && typeof loadPromptsView === 'function') {
|
||||||
|
loadPromptsView();
|
||||||
|
} else if (tabName === 'context' && typeof loadContextView === 'function') {
|
||||||
|
loadContextView();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
SidebarManager.init();
|
||||||
|
});
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
/**
|
||||||
|
* Модуль вкладок "Промпт" и "Контекст".
|
||||||
|
*/
|
||||||
|
|
||||||
|
async function loadPromptsView() {
|
||||||
|
const container = document.getElementById('prompts-content-container');
|
||||||
|
if (!container) return;
|
||||||
|
|
||||||
|
container.innerHTML = `<div class="text-center py-10 text-slate-400 text-xs"><i class="fa-solid fa-spinner fa-spin mr-1"></i> Загрузка промпта...</div>`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/chat', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: AuthManager.getAuthHeaders(),
|
||||||
|
body: JSON.stringify({ message: "покажи системный промпт" })
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
const text = data.response || "Промпт не получен";
|
||||||
|
|
||||||
|
container.innerHTML = `
|
||||||
|
<div class="p-3 bg-white border border-slate-200 rounded-xl shadow-xs space-y-2">
|
||||||
|
<div class="text-[11px] font-bold text-slate-600 flex items-center justify-between">
|
||||||
|
<span>Текущий системный промпт</span>
|
||||||
|
<button onclick="window.sendChatAction && window.sendChatAction('action:open_editor')" class="text-indigo-600 hover:text-indigo-800 text-[10px]">
|
||||||
|
<i class="fa-solid fa-pen-to-square"></i> Редактор
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<pre class="text-[11px] font-mono text-slate-700 bg-slate-50 p-2.5 rounded-lg overflow-x-auto whitespace-pre-wrap leading-relaxed max-h-[65vh] border border-slate-100">${escapeHtml(text)}</pre>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
} catch (e) {
|
||||||
|
container.innerHTML = `<div class="text-center py-8 text-rose-500 text-xs">Не удалось загрузить системный промпт</div>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadContextView() {
|
||||||
|
const container = document.getElementById('context-content-container');
|
||||||
|
if (!container) return;
|
||||||
|
|
||||||
|
container.innerHTML = `<div class="text-center py-10 text-slate-400 text-xs"><i class="fa-solid fa-spinner fa-spin mr-1"></i> Загрузка сессии...</div>`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/context/state?session_id=web_session_main', { headers: AuthManager.getAuthHeaders() });
|
||||||
|
const data = await res.json();
|
||||||
|
|
||||||
|
container.innerHTML = `
|
||||||
|
<div class="p-3 bg-white border border-slate-200 rounded-xl shadow-xs space-y-3">
|
||||||
|
<div class="flex items-center justify-between">
|
||||||
|
<span class="text-xs font-bold text-slate-800">Сессия: web_session_main</span>
|
||||||
|
<span class="px-2 py-0.5 text-[10px] font-bold rounded-full ${data.active_state !== 'IDLE' ? 'bg-amber-100 text-amber-800' : 'bg-slate-100 text-slate-600'}">
|
||||||
|
${escapeHtml(data.active_state)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div class="grid grid-cols-2 gap-2 text-center">
|
||||||
|
<div class="p-2 bg-slate-50 rounded-lg border border-slate-100">
|
||||||
|
<div class="text-xs font-bold text-slate-700">${data.total_messages || 0}</div>
|
||||||
|
<div class="text-[10px] text-slate-400">Всего сообщений</div>
|
||||||
|
</div>
|
||||||
|
<div class="p-2 bg-slate-50 rounded-lg border border-slate-100">
|
||||||
|
<div class="text-xs font-bold text-indigo-600">${data.ephemeral_messages || 0}</div>
|
||||||
|
<div class="text-[10px] text-slate-400">Служебных (UI)</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="space-y-1.5 pt-2 border-t border-slate-100">
|
||||||
|
<button onclick="purgeEphemeralMessages()" class="w-full py-2 bg-indigo-50 hover:bg-indigo-100 text-indigo-700 rounded-lg text-xs font-semibold transition flex items-center justify-center gap-1.5">
|
||||||
|
<i class="fa-solid fa-broom text-[11px]"></i> Очистить служебные карточки
|
||||||
|
</button>
|
||||||
|
<button onclick="clearAllChatContext()" class="w-full py-2 bg-rose-50 hover:bg-rose-100 text-rose-700 rounded-lg text-xs font-semibold transition flex items-center justify-center gap-1.5">
|
||||||
|
<i class="fa-solid fa-trash-can text-[11px]"></i> Полный сброс контекста
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
} catch (e) {
|
||||||
|
container.innerHTML = `<div class="text-center py-8 text-rose-500 text-xs">Не удалось загрузить состояние сессии</div>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function purgeEphemeralMessages() {
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/context/purge-ephemeral', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { ...AuthManager.getAuthHeaders(), 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ session_id: 'web_session_main' })
|
||||||
|
});
|
||||||
|
if (res.ok) loadContextView();
|
||||||
|
} catch (e) { alert('Ошибка сети'); }
|
||||||
|
}
|
||||||
|
|
||||||
|
async function clearAllChatContext() {
|
||||||
|
if (!confirm('Полностью очистить историю сообщений диалога?')) return;
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/context/clear-all', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { ...AuthManager.getAuthHeaders(), 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ session_id: 'web_session_main' })
|
||||||
|
});
|
||||||
|
if (res.ok) location.reload();
|
||||||
|
} catch (e) { alert('Ошибка сети'); }
|
||||||
|
}
|
||||||
@@ -0,0 +1,420 @@
|
|||||||
|
/**
|
||||||
|
* Модуль вкладок "Реестры": исключения, удаленщики, командировки, флигель.
|
||||||
|
*/
|
||||||
|
|
||||||
|
window.openAddExceptionModal = function(category) {
|
||||||
|
const modal = document.getElementById("exception-modal");
|
||||||
|
const catInput = document.getElementById("exception-category-input");
|
||||||
|
const valInput = document.getElementById("exception-value-input");
|
||||||
|
const commInput = document.getElementById("exception-comment-input");
|
||||||
|
const headerEl = document.getElementById("exception-modal-header-text");
|
||||||
|
const labelEl = document.getElementById("exception-value-label");
|
||||||
|
const errEl = document.getElementById("exception-error-msg");
|
||||||
|
|
||||||
|
if (!modal) return;
|
||||||
|
if (errEl) errEl.classList.add("hidden");
|
||||||
|
if (catInput) catInput.value = category;
|
||||||
|
if (valInput) { valInput.value = ""; valInput.focus(); }
|
||||||
|
if (commInput) commInput.value = "";
|
||||||
|
|
||||||
|
const titles = {
|
||||||
|
'include_fio': 'Белый список (ФИО)',
|
||||||
|
'fio': 'Исключенный сотрудник (ФИО)',
|
||||||
|
'departments': 'Исключенный отдел',
|
||||||
|
'positions': 'Исключенная должность',
|
||||||
|
'turnstile_fio': 'Правый турникет (ФИО)',
|
||||||
|
'turnstile_departments': 'Правый турникет (Отделы)',
|
||||||
|
'fligel_fio': 'Флигель (ФИО)',
|
||||||
|
'fligel_departments': 'Флигель (Отделы)'
|
||||||
|
};
|
||||||
|
|
||||||
|
const isDept = category.includes('department');
|
||||||
|
const isPos = category.includes('position');
|
||||||
|
|
||||||
|
if (headerEl) headerEl.innerText = "Добавить в " + (titles[category] || "реестр");
|
||||||
|
if (labelEl) {
|
||||||
|
labelEl.innerText = isDept ? 'Название подразделения:' : (isPos ? 'Название должности:' : 'ФИО сотрудника:');
|
||||||
|
}
|
||||||
|
|
||||||
|
modal.classList.remove("hidden");
|
||||||
|
};
|
||||||
|
|
||||||
|
window.closeExceptionModal = function() {
|
||||||
|
const modal = document.getElementById("exception-modal");
|
||||||
|
if (modal) modal.classList.add("hidden");
|
||||||
|
};
|
||||||
|
|
||||||
|
window.submitExceptionModalForm = async function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
const cat = document.getElementById("exception-category-input")?.value;
|
||||||
|
const val = document.getElementById("exception-value-input")?.value.trim();
|
||||||
|
const comm = document.getElementById("exception-comment-input")?.value.trim() || "";
|
||||||
|
const errEl = document.getElementById("exception-error-msg");
|
||||||
|
|
||||||
|
if (!cat || !val) {
|
||||||
|
if (errEl) { errEl.innerText = "Заполните поле"; errEl.classList.remove("hidden"); }
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/v1/exceptions", {
|
||||||
|
method: "POST",
|
||||||
|
headers: AuthManager.getAuthHeaders(),
|
||||||
|
body: JSON.stringify({ category: cat, value: val, comment: comm })
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
closeExceptionModal();
|
||||||
|
loadExceptionsView();
|
||||||
|
} else {
|
||||||
|
const data = await res.json();
|
||||||
|
if (errEl) { errEl.innerText = data.detail || "Ошибка сохранения"; errEl.classList.remove("hidden"); }
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
if (errEl) { errEl.innerText = "Ошибка соединения"; errEl.classList.remove("hidden"); }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function switchRegistrySubTab(subTab) {
|
||||||
|
if (window.SidebarManager) {
|
||||||
|
SidebarManager.currentRegistrySubTab = subTab;
|
||||||
|
}
|
||||||
|
|
||||||
|
document.querySelectorAll('.registry-subtab-btn').forEach(btn => {
|
||||||
|
const isActive = btn.dataset.subtab === subTab;
|
||||||
|
btn.classList.toggle('text-indigo-600', isActive);
|
||||||
|
btn.classList.toggle('bg-white', isActive);
|
||||||
|
btn.classList.toggle('shadow-xs', isActive);
|
||||||
|
btn.classList.toggle('font-bold', isActive);
|
||||||
|
btn.classList.toggle('text-slate-600', !isActive);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (subTab === 'exceptions') loadExceptionsView();
|
||||||
|
else if (subTab === 'remote') loadRemoteWorkersView();
|
||||||
|
else if (subTab === 'local_trip') loadManualAbsencesView('LOCAL_TRIP');
|
||||||
|
else if (subTab === 'other') loadManualAbsencesView('OTHER');
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadExceptionsView() {
|
||||||
|
const container = document.getElementById('registry-content-container');
|
||||||
|
if (!container) return;
|
||||||
|
|
||||||
|
container.innerHTML = '<div class="text-center py-10 text-slate-400 text-xs"><i class="fa-solid fa-spinner fa-spin mr-1"></i> Загрузка правил...</div>';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/exceptions', { headers: AuthManager.getAuthHeaders() });
|
||||||
|
if (!res.ok) throw new Error('Ошибка сети');
|
||||||
|
const data = await res.json();
|
||||||
|
renderExceptionsView(data || {});
|
||||||
|
} catch (e) {
|
||||||
|
container.innerHTML = '<div class="text-center py-8 text-rose-500 text-xs">Не удалось загрузить реестры исключений</div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderExceptionsView(exceptions) {
|
||||||
|
const container = document.getElementById('registry-content-container');
|
||||||
|
if (!container) return;
|
||||||
|
|
||||||
|
const renderCard = (title, list, category, badgeClass, subBadge) => {
|
||||||
|
badgeClass = badgeClass || 'bg-slate-100 text-slate-700';
|
||||||
|
subBadge = subBadge || '';
|
||||||
|
|
||||||
|
let tags = '<span class="text-[11px] text-slate-400 italic">Список пуст</span>';
|
||||||
|
if (list && list.length > 0) {
|
||||||
|
tags = list.map(item => {
|
||||||
|
const val = escapeHtml(item);
|
||||||
|
return '<span class="inline-flex items-center gap-1.5 px-2 py-0.5 rounded-lg text-xs font-medium ' + badgeClass + '">' +
|
||||||
|
'<span>' + val + '</span>' +
|
||||||
|
'<button data-cat="' + category + '" data-val="' + val + '" class="btn-remove-exc text-slate-400 hover:text-rose-500 transition">' +
|
||||||
|
'<i class="fa-solid fa-xmark text-[10px]"></i>' +
|
||||||
|
'</button>' +
|
||||||
|
'</span>';
|
||||||
|
}).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
const count = list ? list.length : 0;
|
||||||
|
return '<div class="p-3 bg-white rounded-xl border border-slate-200 shadow-xs mb-3">' +
|
||||||
|
'<div class="flex items-center justify-between mb-2">' +
|
||||||
|
'<div class="flex items-center gap-1.5">' +
|
||||||
|
'<span class="text-xs font-bold text-slate-800">' + title + ' (' + count + ')</span>' +
|
||||||
|
subBadge +
|
||||||
|
'</div>' +
|
||||||
|
'<button data-add-cat="' + category + '" class="btn-add-exc text-xs font-bold text-indigo-600 hover:text-indigo-800">+ Добавить</button>' +
|
||||||
|
'</div>' +
|
||||||
|
'<div class="flex flex-wrap gap-1.5">' + tags + '</div>' +
|
||||||
|
'</div>';
|
||||||
|
};
|
||||||
|
|
||||||
|
container.innerHTML =
|
||||||
|
renderCard('Белый список (ФИО)', exceptions.include_fio, 'include_fio', 'bg-indigo-50 text-indigo-700') +
|
||||||
|
renderCard('Исключенные сотрудники (ФИО)', exceptions.fio, 'fio') +
|
||||||
|
renderCard('Исключенные отделы', exceptions.departments, 'departments') +
|
||||||
|
renderCard('Исключенные должности', exceptions.positions, 'positions') +
|
||||||
|
renderCard('Пр. турникет (ФИО)', exceptions.turnstile_fio, 'turnstile_fio', 'bg-emerald-50 text-emerald-700 border border-emerald-200', '<span class="px-1.5 py-0.5 bg-emerald-50 text-emerald-700 rounded text-[9px] font-bold border border-emerald-200">Оба турникета</span>') +
|
||||||
|
renderCard('Пр. турникет (Отделы)', exceptions.turnstile_departments, 'turnstile_departments', 'bg-emerald-50 text-emerald-700 border border-emerald-200', '<span class="px-1.5 py-0.5 bg-emerald-50 text-emerald-700 rounded text-[9px] font-bold border border-emerald-200">Оба турникета</span>') +
|
||||||
|
renderCard('Флигель (ФИО)', exceptions.fligel_fio, 'fligel_fio', 'bg-indigo-50 text-indigo-700 border border-indigo-200', '<span class="px-1.5 py-0.5 bg-indigo-50 text-indigo-700 rounded text-[9px] font-bold border border-indigo-200">Дверь 23</span>') +
|
||||||
|
renderCard('Флигель (Отделы)', exceptions.fligel_departments, 'fligel_departments', 'bg-indigo-50 text-indigo-700 border border-indigo-200', '<span class="px-1.5 py-0.5 bg-indigo-50 text-indigo-700 rounded text-[9px] font-bold border border-indigo-200">Дверь 23</span>');
|
||||||
|
|
||||||
|
container.querySelectorAll('.btn-add-exc').forEach(btn => {
|
||||||
|
btn.addEventListener('click', e => {
|
||||||
|
openAddExceptionModal(e.currentTarget.getAttribute('data-add-cat'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
container.querySelectorAll('.btn-remove-exc').forEach(btn => {
|
||||||
|
btn.addEventListener('click', e => {
|
||||||
|
const target = e.currentTarget;
|
||||||
|
removeExceptionItem(target.getAttribute('data-cat'), target.getAttribute('data-val'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function removeExceptionItem(category, value) {
|
||||||
|
if (!confirm('Удалить "' + value + '" из категории "' + category + '"?')) return;
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/exceptions?category=' + encodeURIComponent(category) + '&value=' + encodeURIComponent(value), {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: AuthManager.getAuthHeaders()
|
||||||
|
});
|
||||||
|
if (res.ok) loadExceptionsView();
|
||||||
|
else alert('Ошибка при удалении');
|
||||||
|
} catch (e) {
|
||||||
|
alert('Ошибка сети');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadRemoteWorkersView() {
|
||||||
|
const container = document.getElementById('registry-content-container');
|
||||||
|
if (!container) return;
|
||||||
|
|
||||||
|
container.innerHTML = '<div class="text-center py-10 text-slate-400 text-xs"><i class="fa-solid fa-spinner fa-spin mr-1"></i> Загрузка удаленщиков...</div>';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/remote-workers', { headers: AuthManager.getAuthHeaders() });
|
||||||
|
const data = await res.json();
|
||||||
|
const workers = data.workers || [];
|
||||||
|
|
||||||
|
let listHtml = '';
|
||||||
|
if (workers.length === 0) {
|
||||||
|
listHtml = '<div class="text-center py-8 text-xs text-slate-400 bg-white border border-slate-200 rounded-xl p-4">Список удаленщиков пуст</div>';
|
||||||
|
} else {
|
||||||
|
listHtml = workers.map(w => {
|
||||||
|
const fio = escapeHtml(w.fio);
|
||||||
|
const dept = escapeHtml(w.department || 'Все');
|
||||||
|
const dFrom = escapeHtml(w.date_from || '—');
|
||||||
|
const dTo = escapeHtml(w.date_to || 'бессрочно');
|
||||||
|
|
||||||
|
return '<div class="p-3 bg-white border border-slate-200 rounded-xl shadow-xs text-xs flex flex-col gap-1.5">' +
|
||||||
|
'<div class="flex items-center justify-between">' +
|
||||||
|
'<span class="font-bold text-slate-800">' + fio + '</span>' +
|
||||||
|
'<div class="flex items-center gap-1.5">' +
|
||||||
|
'<button data-edit-fio="' + fio + '" data-edit-dept="' + (w.department || '') + '" data-edit-from="' + (w.date_from || '') + '" data-edit-to="' + (w.date_to || '') + '" class="btn-edit-remote text-slate-400 hover:text-emerald-600 transition" title="Редактировать"><i class="fa-solid fa-pen-to-square"></i></button>' +
|
||||||
|
'<button data-del-fio="' + fio + '" class="btn-del-remote text-slate-400 hover:text-rose-500 transition" title="Удалить"><i class="fa-solid fa-trash-can"></i></button>' +
|
||||||
|
'</div>' +
|
||||||
|
'</div>' +
|
||||||
|
'<div class="flex items-center justify-between text-[11px] text-slate-500">' +
|
||||||
|
'<span>' + dept + '</span>' +
|
||||||
|
'<span class="font-mono text-[10px] bg-slate-100 px-1.5 py-0.5 rounded">' + dFrom + ' по ' + dTo + '</span>' +
|
||||||
|
'</div>' +
|
||||||
|
'</div>';
|
||||||
|
}).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
container.innerHTML =
|
||||||
|
'<div class="flex items-center justify-between px-1 mb-2">' +
|
||||||
|
'<span class="text-xs font-bold text-slate-700">Удаленные сотрудники: ' + workers.length + '</span>' +
|
||||||
|
'<button id="btn-add-remote" class="px-2.5 py-1 bg-emerald-600 hover:bg-emerald-700 text-white rounded-lg text-xs font-bold shadow-xs transition">+ Добавить</button>' +
|
||||||
|
'</div>' +
|
||||||
|
'<div class="space-y-2">' + listHtml + '</div>';
|
||||||
|
|
||||||
|
const addBtn = document.getElementById('btn-add-remote');
|
||||||
|
if (addBtn) {
|
||||||
|
addBtn.addEventListener('click', () => openRemoteWorkerModal('ADD'));
|
||||||
|
}
|
||||||
|
|
||||||
|
container.querySelectorAll('.btn-edit-remote').forEach(btn => {
|
||||||
|
btn.addEventListener('click', e => {
|
||||||
|
const t = e.currentTarget;
|
||||||
|
openRemoteWorkerModal('EDIT', t.getAttribute('data-edit-fio'), t.getAttribute('data-edit-dept'), t.getAttribute('data-edit-from'), t.getAttribute('data-edit-to'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
container.querySelectorAll('.btn-del-remote').forEach(btn => {
|
||||||
|
btn.addEventListener('click', e => {
|
||||||
|
deleteRemoteWorkerItem(e.currentTarget.getAttribute('data-del-fio'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
container.innerHTML = '<div class="text-center py-8 text-rose-500 text-xs">Не удалось загрузить удаленщиков</div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteRemoteWorkerItem(fio) {
|
||||||
|
if (!confirm('Удалить сотрудника "' + fio + '"?')) return;
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/remote-workers?fio=' + encodeURIComponent(fio), {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: AuthManager.getAuthHeaders()
|
||||||
|
});
|
||||||
|
if (res.ok) loadRemoteWorkersView();
|
||||||
|
} catch (e) {
|
||||||
|
alert('Ошибка сети');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadManualAbsencesView(type) {
|
||||||
|
const container = document.getElementById('registry-content-container');
|
||||||
|
if (!container) return;
|
||||||
|
|
||||||
|
const isTrip = (type === 'LOCAL_TRIP');
|
||||||
|
const titleText = isTrip ? 'Местные командировки' : 'Иные причины';
|
||||||
|
const btnColor = isTrip ? 'bg-indigo-600 hover:bg-indigo-700' : 'bg-purple-600 hover:bg-purple-700';
|
||||||
|
|
||||||
|
container.innerHTML = '<div class="text-center py-10 text-slate-400 text-xs"><i class="fa-solid fa-spinner fa-spin mr-1"></i> Загрузка...</div>';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/manual-absences/?type=' + encodeURIComponent(type), {
|
||||||
|
headers: AuthManager.getAuthHeaders()
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
const items = data.items || [];
|
||||||
|
|
||||||
|
let listHtml = '';
|
||||||
|
if (items.length === 0) {
|
||||||
|
listHtml = '<div class="text-center py-8 text-xs text-slate-400 bg-white border border-slate-200 rounded-xl p-4">Нет записей</div>';
|
||||||
|
} else {
|
||||||
|
listHtml = items.map(it => {
|
||||||
|
const fio = escapeHtml(it.fio);
|
||||||
|
const reason = escapeHtml(it.reason || '');
|
||||||
|
const dept = escapeHtml(it.department || '—');
|
||||||
|
const dStart = it.date_start ? it.date_start.replace(/-/g, '.') : '';
|
||||||
|
const dEnd = it.date_end ? it.date_end.replace(/-/g, '.') : '';
|
||||||
|
|
||||||
|
// Форматирование срока
|
||||||
|
let dateBadge = '';
|
||||||
|
if (dStart && dEnd && dStart === dEnd) {
|
||||||
|
dateBadge = dStart;
|
||||||
|
} else if (dStart && dEnd) {
|
||||||
|
dateBadge = `${dStart} — ${dEnd}`;
|
||||||
|
} else if (dEnd) {
|
||||||
|
dateBadge = `по ${dEnd}`;
|
||||||
|
} else if (dStart) {
|
||||||
|
dateBadge = `с ${dStart}`;
|
||||||
|
} else {
|
||||||
|
dateBadge = 'бессрочно';
|
||||||
|
}
|
||||||
|
|
||||||
|
const badgeBg = isTrip
|
||||||
|
? 'bg-indigo-50 text-indigo-700 border-indigo-200'
|
||||||
|
: 'bg-purple-50 text-purple-700 border-purple-200';
|
||||||
|
|
||||||
|
return `
|
||||||
|
<div class="p-3 bg-white border border-slate-200 rounded-xl shadow-xs text-xs flex flex-col gap-1.5 hover:border-slate-300 transition">
|
||||||
|
<!-- 1-я строка: ФИО и кнопки управления -->
|
||||||
|
<div class="flex items-center justify-between gap-2">
|
||||||
|
<span class="font-bold text-slate-800 truncate" title="${fio}">${fio}</span>
|
||||||
|
<div class="flex items-center gap-1 shrink-0">
|
||||||
|
<button data-edit-id="${it.id}" data-edit-fio="${fio}" data-edit-start="${it.date_start || ''}" data-edit-end="${it.date_end || ''}"
|
||||||
|
class="btn-edit-abs text-slate-400 hover:text-indigo-600 p-1 transition" title="Редактировать сроки">
|
||||||
|
<i class="fa-solid fa-pen-to-square"></i>
|
||||||
|
</button>
|
||||||
|
<button data-del-id="${it.id}"
|
||||||
|
class="btn-del-abs text-slate-400 hover:text-rose-500 p-1 transition" title="Удалить">
|
||||||
|
<i class="fa-solid fa-trash-can"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
${!isTrip && reason ? `<div class="text-[11px] text-slate-600 font-medium">${reason}</div>` : ''}
|
||||||
|
|
||||||
|
<!-- 2-я строка: Отдел слева и Четкий срок отсутствия справа -->
|
||||||
|
<div class="flex items-center justify-between text-[11px] pt-1 border-t border-slate-100">
|
||||||
|
<span class="text-slate-400 truncate max-w-[200px]" title="${dept}">${dept}</span>
|
||||||
|
<span class="font-mono text-[10px] px-2 py-0.5 rounded-md border font-semibold shrink-0 ${badgeBg}">
|
||||||
|
<i class="fa-regular fa-calendar-days mr-1 text-[9px]"></i>${dateBadge}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
container.innerHTML = `
|
||||||
|
<div class="flex items-center justify-between px-1 mb-2">
|
||||||
|
<span class="text-xs font-bold text-slate-700">${titleText}: ${items.length}</span>
|
||||||
|
<button id="btn-add-absence" class="px-2.5 py-1 ${btnColor} text-white rounded-lg text-xs font-bold shadow-xs transition">+ Добавить</button>
|
||||||
|
</div>
|
||||||
|
<div class="space-y-2">${listHtml}</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
const addBtn = document.getElementById('btn-add-absence');
|
||||||
|
if (addBtn) {
|
||||||
|
addBtn.addEventListener('click', () => openManualAbsenceModal(type));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Слушатели кнопок редактирования
|
||||||
|
container.querySelectorAll('.btn-edit-abs').forEach(btn => {
|
||||||
|
btn.addEventListener('click', e => {
|
||||||
|
const t = e.currentTarget;
|
||||||
|
const id = t.getAttribute('data-edit-id');
|
||||||
|
const fio = t.getAttribute('data-edit-fio');
|
||||||
|
const start = t.getAttribute('data-edit-start');
|
||||||
|
const end = t.getAttribute('data-edit-end');
|
||||||
|
|
||||||
|
openManualAbsenceModal(type, {
|
||||||
|
id: id,
|
||||||
|
fio: fio,
|
||||||
|
date_start: start,
|
||||||
|
date_end: end
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Слушатели кнопок удаления
|
||||||
|
container.querySelectorAll('.btn-del-abs').forEach(btn => {
|
||||||
|
btn.addEventListener('click', e => {
|
||||||
|
deleteManualAbsenceRecord(e.currentTarget.getAttribute('data-del-id'), type);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
container.innerHTML = '<div class="text-center py-8 text-rose-500 text-xs">Ошибка загрузки</div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Быстрое модальное окно / диалог изменения дат
|
||||||
|
async function openEditAbsenceDatesModal(id, fio, dateStart, dateEnd, type) {
|
||||||
|
const newEnd = prompt(`Укажите новую дату окончания для сотрудника:\n${fio}\n(формат ГГГГ-ММ-ДД):`, dateEnd || '');
|
||||||
|
if (newEnd === null) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/v1/manual-absences/${id}`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { ...AuthManager.getAuthHeaders(), 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
id: parseInt(id),
|
||||||
|
date_start: dateStart || null,
|
||||||
|
date_end: newEnd.trim() || null
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
loadManualAbsencesView(type);
|
||||||
|
} else {
|
||||||
|
alert('Не удалось обновить дату');
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
alert('Ошибка сети при обновлении');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteManualAbsenceRecord(id, type) {
|
||||||
|
if (!confirm('Удалить эту запись?')) return;
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/manual-absences/' + encodeURIComponent(id), {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: AuthManager.getAuthHeaders()
|
||||||
|
});
|
||||||
|
if (res.ok) loadManualAbsencesView(type);
|
||||||
|
} catch (e) {
|
||||||
|
alert('Ошибка сети');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,308 @@
|
|||||||
|
/**
|
||||||
|
* Модуль вкладки "Срезы": список снапшотов по диапазону дат, создание, инспекция и генерация отчетов.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// 1. Вспомогательные функции форматирования дат
|
||||||
|
function formatDateDDMMYYYY(d) {
|
||||||
|
const day = String(d.getDate()).padStart(2, '0');
|
||||||
|
const month = String(d.getMonth() + 1).padStart(2, '0');
|
||||||
|
const year = d.getFullYear();
|
||||||
|
return `${day}.${month}.${year}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatDateToISO(d) {
|
||||||
|
const year = d.getFullYear();
|
||||||
|
const month = String(d.getMonth() + 1).padStart(2, '0');
|
||||||
|
const day = String(d.getDate()).padStart(2, '0');
|
||||||
|
return `${year}-${month}-${day}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isoToBackendFormat(isoStr) {
|
||||||
|
if (!isoStr) return '';
|
||||||
|
if (isoStr.includes('.')) return isoStr;
|
||||||
|
const [y, m, d] = isoStr.split('-');
|
||||||
|
return `${d}.${m}.${y}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Автозаполнение полей создания среза текущими датой и временем
|
||||||
|
function initManualSnapshotInputs() {
|
||||||
|
const now = new Date();
|
||||||
|
const dateInput = document.getElementById('manual-snapshot-date');
|
||||||
|
const timeInput = document.getElementById('manual-snapshot-time');
|
||||||
|
|
||||||
|
if (dateInput) {
|
||||||
|
dateInput.value = formatDateDDMMYYYY(now);
|
||||||
|
}
|
||||||
|
if (timeInput) {
|
||||||
|
const hh = String(now.getHours()).padStart(2, '0');
|
||||||
|
const mm = String(now.getMinutes()).padStart(2, '0');
|
||||||
|
timeInput.value = `${hh}:${mm}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Быстрые пресеты периода
|
||||||
|
window.setSnapshotDatePreset = function(preset) {
|
||||||
|
const today = new Date();
|
||||||
|
const fromInput = document.getElementById('snapshots-date-from');
|
||||||
|
const toInput = document.getElementById('snapshots-date-to');
|
||||||
|
|
||||||
|
if (!fromInput || !toInput) return;
|
||||||
|
|
||||||
|
if (preset === 'today') {
|
||||||
|
const str = formatDateToISO(today);
|
||||||
|
fromInput.value = str;
|
||||||
|
toInput.value = str;
|
||||||
|
} else if (preset === 'yesterday') {
|
||||||
|
const y = new Date();
|
||||||
|
y.setDate(today.getDate() - 1);
|
||||||
|
const str = formatDateToISO(y);
|
||||||
|
fromInput.value = str;
|
||||||
|
toInput.value = str;
|
||||||
|
} else if (preset === 'days3') {
|
||||||
|
const start = new Date();
|
||||||
|
start.setDate(today.getDate() - 2);
|
||||||
|
fromInput.value = formatDateToISO(start);
|
||||||
|
toInput.value = formatDateToISO(today);
|
||||||
|
} else if (preset === 'days7') {
|
||||||
|
const start = new Date();
|
||||||
|
start.setDate(today.getDate() - 6);
|
||||||
|
fromInput.value = formatDateToISO(start);
|
||||||
|
toInput.value = formatDateToISO(today);
|
||||||
|
}
|
||||||
|
|
||||||
|
loadSnapshotsView();
|
||||||
|
};
|
||||||
|
|
||||||
|
// 4. Загрузка списка срезов с группировкой и сортировкой
|
||||||
|
async function loadSnapshotsView() {
|
||||||
|
// Гарантированно заполняем поля даты и времени создания среза
|
||||||
|
initManualSnapshotInputs();
|
||||||
|
|
||||||
|
const listContainer = document.getElementById('snapshots-list');
|
||||||
|
const countBadge = document.getElementById('snapshots-count-badge');
|
||||||
|
const fromInput = document.getElementById('snapshots-date-from');
|
||||||
|
const toInput = document.getElementById('snapshots-date-to');
|
||||||
|
|
||||||
|
if (!listContainer) return;
|
||||||
|
|
||||||
|
const todayIso = formatDateToISO(new Date());
|
||||||
|
if (fromInput && !fromInput.value) fromInput.value = todayIso;
|
||||||
|
if (toInput && !toInput.value) toInput.value = todayIso;
|
||||||
|
|
||||||
|
const dateFrom = fromInput ? isoToBackendFormat(fromInput.value) : isoToBackendFormat(todayIso);
|
||||||
|
const dateTo = toInput ? isoToBackendFormat(toInput.value) : dateFrom;
|
||||||
|
|
||||||
|
listContainer.innerHTML = `<div class="text-center py-10 text-slate-400 text-xs"><i class="fa-solid fa-spinner fa-spin mr-1"></i> Загрузка срезов...</div>`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const url = `/api/v1/snapshots?date_from=${encodeURIComponent(dateFrom)}&date_to=${encodeURIComponent(dateTo)}`;
|
||||||
|
const res = await fetch(url, { headers: AuthManager.getAuthHeaders() });
|
||||||
|
if (!res.ok) throw new Error('Ошибка сети');
|
||||||
|
const data = await res.json();
|
||||||
|
const snapshots = data.snapshots || [];
|
||||||
|
|
||||||
|
if (countBadge) countBadge.innerText = `Срезы в базе: ${snapshots.length}`;
|
||||||
|
|
||||||
|
if (snapshots.length === 0) {
|
||||||
|
listContainer.innerHTML = `
|
||||||
|
<div class="text-center py-8 text-slate-400 text-xs space-y-2">
|
||||||
|
<p>За выбранный период срезы не найдены</p>
|
||||||
|
<button onclick="setSnapshotDatePreset('yesterday')" class="px-2.5 py-1 text-[11px] bg-slate-100 hover:bg-slate-200 text-slate-600 rounded-lg transition font-medium shadow-2xs">
|
||||||
|
Показать за вчера
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Вспомогательный ключ для сортировки от новых к старым
|
||||||
|
function getSortKey(s) {
|
||||||
|
const sid = (s.snapshot_id || s.id || '').toUpperCase();
|
||||||
|
const digits = sid.replace(/\D/g, '');
|
||||||
|
if (sid.includes('FINAL') && digits.length >= 8) {
|
||||||
|
return `${digits.substring(0, 8)}_235959`;
|
||||||
|
}
|
||||||
|
return digits.padEnd(14, '0');
|
||||||
|
}
|
||||||
|
|
||||||
|
snapshots.sort((a, b) => getSortKey(b).localeCompare(getSortKey(a)));
|
||||||
|
|
||||||
|
// Группировка по дням
|
||||||
|
const groups = {};
|
||||||
|
snapshots.forEach(s => {
|
||||||
|
const sid = s.snapshot_id || s.id || '';
|
||||||
|
let groupDate = s.date || '';
|
||||||
|
|
||||||
|
if (!groupDate) {
|
||||||
|
const digits = sid.replace(/\D/g, '');
|
||||||
|
if (digits.length >= 8) {
|
||||||
|
const y = digits.substring(0, 4);
|
||||||
|
const m = digits.substring(4, 6);
|
||||||
|
const d = digits.substring(6, 8);
|
||||||
|
groupDate = `${d}.${m}.${y}`;
|
||||||
|
} else {
|
||||||
|
groupDate = 'Другие срезы';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!groups[groupDate]) groups[groupDate] = [];
|
||||||
|
groups[groupDate].push(s);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Сортировка дат по убыванию
|
||||||
|
const sortedDates = Object.keys(groups).sort((d1, d2) => {
|
||||||
|
const parseDate = (str) => {
|
||||||
|
const p = str.split('.');
|
||||||
|
return p.length === 3 ? new Date(p[2], p[1] - 1, p[0]).getTime() : 0;
|
||||||
|
};
|
||||||
|
return parseDate(d2) - parseDate(d1);
|
||||||
|
});
|
||||||
|
|
||||||
|
let html = '';
|
||||||
|
for (const dateLabel of sortedDates) {
|
||||||
|
const items = groups[dateLabel];
|
||||||
|
html += `
|
||||||
|
<div class="pt-2 pb-1 flex items-center gap-2">
|
||||||
|
<span class="text-[11px] font-bold text-slate-600 uppercase tracking-wider flex items-center gap-1.5">
|
||||||
|
<i class="fa-regular fa-calendar-days text-indigo-500 text-xs"></i> ${escapeHtml(dateLabel)}
|
||||||
|
</span>
|
||||||
|
<div class="h-px bg-slate-200 flex-1"></div>
|
||||||
|
<span class="text-[10px] text-slate-400 font-semibold">${items.length} срез.</span>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
html += items.map(s => {
|
||||||
|
const sid = s.snapshot_id || s.id;
|
||||||
|
const isFinal = Boolean(s.is_final) || sid.toUpperCase().includes('FINAL');
|
||||||
|
const badgeFinal = isFinal ? `<span class="ml-1.5 px-1.5 py-0.5 bg-amber-100 text-amber-800 rounded text-[9px] font-bold">Финал Y</span>` : '';
|
||||||
|
const cnt = s.record_count ?? s.count ?? s.records_count ?? 0;
|
||||||
|
const timeStr = s.snapshot_time || s.time || '—';
|
||||||
|
|
||||||
|
return `
|
||||||
|
<div class="p-3 bg-white rounded-xl border border-slate-200 hover:border-indigo-200 transition shadow-xs flex items-center justify-between group mb-2">
|
||||||
|
<div>
|
||||||
|
<div class="flex items-center">
|
||||||
|
<span class="text-xs font-bold font-mono text-slate-800">${escapeHtml(sid)}</span>
|
||||||
|
${badgeFinal}
|
||||||
|
</div>
|
||||||
|
<div class="text-[10px] text-slate-400 mt-0.5">${escapeHtml(timeStr)} · ${cnt} зап.</div>
|
||||||
|
</div>
|
||||||
|
<div class="flex items-center gap-1.5">
|
||||||
|
<button onclick="openSnapshotInspector('${escapeHtml(sid)}')" class="px-2.5 py-1 bg-slate-100 hover:bg-indigo-50 text-slate-600 hover:text-indigo-600 rounded-lg text-[10px] font-semibold transition">Инспекция</button>
|
||||||
|
${!isFinal ? `
|
||||||
|
<button onclick="deleteSnapshotItem('${escapeHtml(sid)}')" class="w-6 h-6 flex items-center justify-center text-slate-300 hover:text-rose-500 rounded transition opacity-0 group-hover:opacity-100" title="Удалить срез"><i class="fa-regular fa-trash-can text-[11px]"></i></button>
|
||||||
|
` : ''}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
listContainer.innerHTML = html;
|
||||||
|
} catch (e) {
|
||||||
|
listContainer.innerHTML = `<div class="text-center py-8 text-rose-500 text-xs">Не удалось загрузить срезы</div>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Ручное создание среза
|
||||||
|
async function createSnapshotManual() {
|
||||||
|
const dateVal = document.getElementById('manual-snapshot-date')?.value || '';
|
||||||
|
const timeVal = document.getElementById('manual-snapshot-time')?.value || '';
|
||||||
|
const btn = document.getElementById('btn-create-snapshot');
|
||||||
|
|
||||||
|
if (!dateVal) { alert('Укажите дату среза'); return; }
|
||||||
|
|
||||||
|
btn.disabled = true;
|
||||||
|
btn.innerHTML = `<i class="fa-solid fa-spinner fa-spin mr-1.5"></i> Создание...`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/snapshots/create', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { ...AuthManager.getAuthHeaders(), 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ date_str: dateVal, time_str: timeVal })
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
loadSnapshotsView();
|
||||||
|
} else {
|
||||||
|
const data = await res.json();
|
||||||
|
alert(`Ошибка: ${data.detail || 'Не удалось сформировать срез'}`);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
alert('Ошибка сети');
|
||||||
|
} finally {
|
||||||
|
btn.disabled = false;
|
||||||
|
btn.innerHTML = `<i class="fa-solid fa-camera mr-1.5"></i> Сделать срез`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. Удаление среза
|
||||||
|
async function deleteSnapshotItem(snapshotId) {
|
||||||
|
if (!confirm(`Удалить срез ${snapshotId}?`)) return;
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/snapshots', {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: { ...AuthManager.getAuthHeaders(), 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ snapshot_ids: [snapshotId] })
|
||||||
|
});
|
||||||
|
if (res.ok) loadSnapshotsView();
|
||||||
|
} catch (e) { alert('Ошибка сети'); }
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7. Генерация отчетов On-Demand
|
||||||
|
window.generateReportDirect = async function(reportType) {
|
||||||
|
const fromInput = document.getElementById('snapshots-date-from');
|
||||||
|
const dateInput = fromInput ? isoToBackendFormat(fromInput.value) : '';
|
||||||
|
|
||||||
|
const labelMap = {
|
||||||
|
'SVODKA': 'Сводки',
|
||||||
|
'SIMPLIFIED': 'Упрощенного отчета',
|
||||||
|
'DETAILED': 'Детального отчета'
|
||||||
|
};
|
||||||
|
|
||||||
|
const targetLabel = labelMap[reportType] || 'отчета';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/v1/reports/generate', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { ...AuthManager.getAuthHeaders(), 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
report_type: reportType,
|
||||||
|
date: dateInput || null,
|
||||||
|
time: null // ⭐️ Всегда берем последний готовый срез
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
alert(`Ошибка сервера (${res.status}) при создании ${targetLabel}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await res.json();
|
||||||
|
const rep = data.reports?.[0];
|
||||||
|
|
||||||
|
if (rep && rep.status === 'success' && rep.download_url) {
|
||||||
|
window.open(rep.download_url, '_blank');
|
||||||
|
} else {
|
||||||
|
alert(`Ошибка: ${rep?.error || 'Не удалось сформировать файл'}`);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('[Reports] Ошибка генерации:', e);
|
||||||
|
alert('Сетевая ошибка при запросе формирования отчета');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 8. Инициализация при первичной загрузке страницы
|
||||||
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
const todayIso = formatDateToISO(new Date());
|
||||||
|
const fromInput = document.getElementById('snapshots-date-from');
|
||||||
|
const toInput = document.getElementById('snapshots-date-to');
|
||||||
|
if (fromInput && !fromInput.value) fromInput.value = todayIso;
|
||||||
|
if (toInput && !toInput.value) toInput.value = todayIso;
|
||||||
|
|
||||||
|
initManualSnapshotInputs();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Экспортируем в глобальную область, чтобы core.js при переключении вкладок вызывал эту функцию
|
||||||
|
window.loadSnapshotsView = loadSnapshotsView;
|
||||||
|
window.createSnapshotManual = createSnapshotManual;
|
||||||
|
window.deleteSnapshotItem = deleteSnapshotItem;
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
/**
|
||||||
|
* ===============================================================================
|
||||||
|
* FILE: modules/web_api/static/js/snapshot_inspector.js
|
||||||
|
* ROLE: Управление модальным окном полноразмерной инспекции срезов СКУД
|
||||||
|
* (фильтрация по ФИО, статусам, подразделениям и табличное представление).
|
||||||
|
* ===============================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
let currentInspectorData = [];
|
||||||
|
let currentActiveSnapshotId = null;
|
||||||
|
|
||||||
|
async function openSnapshotInspector(snapshotId) {
|
||||||
|
currentActiveSnapshotId = snapshotId;
|
||||||
|
const modal = document.getElementById("snapshot-inspector-modal");
|
||||||
|
const titleEl = document.getElementById("inspector-modal-title");
|
||||||
|
const subEl = document.getElementById("inspector-modal-subtitle");
|
||||||
|
const tbody = document.getElementById("inspector-table-body");
|
||||||
|
|
||||||
|
if (!modal) return;
|
||||||
|
modal.classList.remove("hidden");
|
||||||
|
titleEl.innerText = `Инспекция среза #${snapshotId}`;
|
||||||
|
subEl.innerText = "Загрузка данных из базы...";
|
||||||
|
tbody.innerHTML = `<tr><td colspan="8" class="text-center py-12 text-slate-400"><i class="fa-solid fa-spinner fa-spin mr-2"></i> Загрузка записей...</td></tr>`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/v1/snapshots/${snapshotId}/details`, { headers: AuthManager.getAuthHeaders() });
|
||||||
|
if (res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
|
currentInspectorData = data.records || [];
|
||||||
|
subEl.innerText = `Дата: ${data.date} · Всего записей: ${currentInspectorData.length}`;
|
||||||
|
populateDepartmentFilter(currentInspectorData);
|
||||||
|
renderInspectorTable(currentInspectorData);
|
||||||
|
} else {
|
||||||
|
tbody.innerHTML = `<tr><td colspan="8" class="text-center py-12 text-rose-500">Ошибка загрузки деталей среза</td></tr>`;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
tbody.innerHTML = `<tr><td colspan="8" class="text-center py-12 text-rose-500">Ошибка сети</td></tr>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeSnapshotInspectorModal() {
|
||||||
|
const modal = document.getElementById("snapshot-inspector-modal");
|
||||||
|
if (modal) modal.classList.add("hidden");
|
||||||
|
}
|
||||||
|
|
||||||
|
function populateDepartmentFilter(records) {
|
||||||
|
const select = document.getElementById("inspector-filter-dept");
|
||||||
|
if (!select) return;
|
||||||
|
const depts = [...new Set(records.map(r => r.department || r.Подразделение || "Без подразделения"))].sort();
|
||||||
|
select.innerHTML = `<option value="ALL">Все подразделения (${depts.length})</option>` +
|
||||||
|
depts.map(d => `<option value="${escapeHtml(d)}">${escapeHtml(d)}</option>`).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderInspectorTable(records) {
|
||||||
|
const tbody = document.getElementById("inspector-table-body");
|
||||||
|
const countEl = document.getElementById("inspector-records-count");
|
||||||
|
if (!tbody) return;
|
||||||
|
|
||||||
|
if (records.length === 0) {
|
||||||
|
tbody.innerHTML = `<tr><td colspan="8" class="text-center py-12 text-slate-400">Нет записей, соответствующих фильтрам</td></tr>`;
|
||||||
|
if (countEl) countEl.innerText = "Показано записей: 0";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
tbody.innerHTML = records.map((r, idx) => {
|
||||||
|
const fio = r.fio || r.Сотрудник || "—";
|
||||||
|
const dept = r.department || r.Подразделение || "—";
|
||||||
|
const timeIn = r.time_in || r.Начало_дня || "Нет входа";
|
||||||
|
const firstAct = r.first_activity || r.Первая_активность || "—";
|
||||||
|
const timeOut = r.time_out || r.Конец_дня || "Нет выхода";
|
||||||
|
const duration = r.duration || r.Находился_в_здании || "00:00";
|
||||||
|
|
||||||
|
// ⭐️ Жестко определяем присутствие по факту наличия времени входа
|
||||||
|
const isPresent = timeIn && timeIn !== "Нет входа" && timeIn !== "—";
|
||||||
|
|
||||||
|
// Корректируем статус: если человек зашел, он точно присутствует
|
||||||
|
let status = r.status || r.Статус || "";
|
||||||
|
if (!status || status.includes("Отсутствовал") || status.includes("Нет событий")) {
|
||||||
|
status = isPresent ? "Присутствовал" : "Отсутствовал";
|
||||||
|
} else if (isPresent && !status.includes("Присутствовал")) {
|
||||||
|
status = "Присутствовал";
|
||||||
|
}
|
||||||
|
|
||||||
|
return `
|
||||||
|
<tr class="hover:bg-slate-50 transition">
|
||||||
|
<td class="p-3 text-slate-400 font-mono text-[11px]">${idx + 1}</td>
|
||||||
|
<td class="p-3 font-bold text-slate-800">${escapeHtml(fio)}</td>
|
||||||
|
<td class="p-3 text-slate-600">${escapeHtml(dept)}</td>
|
||||||
|
<td class="p-3 font-mono ${isPresent ? 'text-emerald-600 font-semibold' : 'text-slate-400'}">${escapeHtml(timeIn)}</td>
|
||||||
|
<td class="p-3 font-mono text-slate-500">${escapeHtml(firstAct)}</td>
|
||||||
|
<td class="p-3 font-mono text-slate-600">${escapeHtml(timeOut)}</td>
|
||||||
|
<td class="p-3 font-mono font-semibold text-slate-700">${escapeHtml(duration)}</td>
|
||||||
|
<td class="p-3">
|
||||||
|
<span class="px-2 py-0.5 rounded-full text-[10px] font-bold ${isPresent ? 'bg-emerald-50 text-emerald-700 border border-emerald-200' : 'bg-slate-100 text-slate-500'}">
|
||||||
|
${escapeHtml(status)}
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
`;
|
||||||
|
}).join('');
|
||||||
|
|
||||||
|
if (countEl) countEl.innerText = `Показано записей: ${records.length} из ${currentInspectorData.length}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function filterInspectorTable() {
|
||||||
|
const query = document.getElementById("inspector-search-input")?.value.toLowerCase() || "";
|
||||||
|
const statusFilter = document.getElementById("inspector-filter-status")?.value || "ALL";
|
||||||
|
const deptFilter = document.getElementById("inspector-filter-dept")?.value || "ALL";
|
||||||
|
|
||||||
|
const filtered = currentInspectorData.filter(r => {
|
||||||
|
const fio = (r.fio || r.Сотрудник || "").toLowerCase();
|
||||||
|
const dept = r.department || r.Подразделение || "Без подразделения";
|
||||||
|
const status = r.status || r.Статус || "";
|
||||||
|
const timeIn = r.time_in || r.Начало_дня || "Нет входа";
|
||||||
|
const isPresent = status.includes("Присутствовал") || timeIn !== "Нет входа";
|
||||||
|
|
||||||
|
const matchesQuery = fio.includes(query) || dept.toLowerCase().includes(query);
|
||||||
|
const matchesDept = deptFilter === "ALL" || dept === deptFilter;
|
||||||
|
|
||||||
|
let matchesStatus = true;
|
||||||
|
if (statusFilter === "PRESENT") matchesStatus = isPresent;
|
||||||
|
if (statusFilter === "ABSENT") matchesStatus = !isPresent;
|
||||||
|
|
||||||
|
return matchesQuery && matchesDept && matchesStatus;
|
||||||
|
});
|
||||||
|
|
||||||
|
renderInspectorTable(filtered);
|
||||||
|
}
|
||||||
|
|
||||||
|
window.downloadInspectorExport = function(format = 'xlsx') {
|
||||||
|
if (!currentActiveSnapshotId) {
|
||||||
|
alert('Срез не выбран');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const cleanId = String(currentActiveSnapshotId).replace(/^#/, '').trim();
|
||||||
|
const url = `/api/v1/snapshots/${encodeURIComponent(cleanId)}/export?format=${format}`;
|
||||||
|
window.open(url, '_blank');
|
||||||
|
};
|
||||||
@@ -1,117 +1,210 @@
|
|||||||
/**
|
/**
|
||||||
===============================================================================
|
* ===============================================================================
|
||||||
FILE: modules/web_api/static/js/tasks.js
|
* FILE: modules/web_api/static/js/tasks.js
|
||||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
* ROLE: Управление персональными задачами оператора (CRUD, фильтры, рендер).
|
||||||
MODULE: web_api / static / js
|
* ===============================================================================
|
||||||
ROLE: Загрузка, фильтрация и рендеринг списка задач в боковой панели (Drawer).
|
|
||||||
===============================================================================
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let currentTaskFilter = 'IN_PROGRESS';
|
window.escapeHtml = window.escapeHtml || function (str) {
|
||||||
|
if (str === null || str === undefined) return '';
|
||||||
|
return String(str)
|
||||||
|
.replace(/&/g, '&')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/"/g, '"')
|
||||||
|
.replace(/'/g, ''');
|
||||||
|
};
|
||||||
|
|
||||||
|
let currentTasksFilter = 'ALL';
|
||||||
|
let tasksCache = [];
|
||||||
|
|
||||||
|
function getTasksContainer() {
|
||||||
|
return document.getElementById("tasks-list-container") ||
|
||||||
|
document.getElementById("sidebar-dynamic-content") ||
|
||||||
|
document.getElementById("tasks-list");
|
||||||
|
}
|
||||||
|
|
||||||
async function loadTasks() {
|
async function loadTasks() {
|
||||||
const token = typeof API_TOKEN !== 'undefined' && API_TOKEN ? API_TOKEN : localStorage.getItem("scud_api_auth_token");
|
const container = getTasksContainer();
|
||||||
if (!token) return;
|
if (!container) return;
|
||||||
|
|
||||||
|
container.innerHTML = `
|
||||||
|
<div class="text-center py-8 text-xs text-slate-400">
|
||||||
|
<i class="fa-solid fa-spinner fa-spin mr-1"></i> Загрузка задач...
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch("/api/v1/tasks", {
|
const res = await fetch("/api/v1/tasks", {
|
||||||
headers: { "Authorization": "Bearer " + token }
|
headers: AuthManager.getAuthHeaders()
|
||||||
});
|
});
|
||||||
if (res.ok) {
|
|
||||||
|
if (!res.ok) {
|
||||||
|
if (res.status === 401) {
|
||||||
|
showAuthModal();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw new Error(`Ошибка сервера (${res.status})`);
|
||||||
|
}
|
||||||
|
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
renderSidebarTasks(data.tasks || []);
|
tasksCache = Array.isArray(data) ? data : (data.tasks || []);
|
||||||
} else {
|
renderTasksUI();
|
||||||
renderSidebarError("Ошибка доступа. Авторизуйтесь снова.");
|
} catch (err) {
|
||||||
}
|
console.error("[Tasks] Ошибка загрузки:", err);
|
||||||
} catch (e) {
|
container.innerHTML = `
|
||||||
console.error("Ошибка загрузки задач:", e);
|
<div class="p-4 text-xs text-rose-500 text-center flex flex-col items-center gap-2">
|
||||||
renderSidebarError("Ошибка сети. Сервер недоступен.");
|
<i class="fa-solid fa-triangle-exclamation text-base"></i>
|
||||||
|
<span>Не удалось загрузить задачи</span>
|
||||||
|
<button onclick="loadTasks()" class="px-2.5 py-1 bg-slate-200 hover:bg-slate-300 text-slate-700 rounded text-[11px] font-semibold transition">Повторить</button>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderSidebarError(msg) {
|
function setTaskFilter(filter) {
|
||||||
// Поддерживаем оба варианта ID (новый и старый) для обратной совместимости
|
currentTasksFilter = filter;
|
||||||
const container = document.getElementById("tasks-list") || document.getElementById("tasks-list-container");
|
renderTasksUI();
|
||||||
if (container) {
|
|
||||||
container.innerHTML = `<div class="text-center py-8 text-xs text-rose-500 font-semibold">${msg}</div>`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function filterTasksByTab(status) {
|
function renderTasksUI() {
|
||||||
currentTaskFilter = status;
|
const container = getTasksContainer();
|
||||||
|
|
||||||
// Сброс стилей всех кнопок-вкладок
|
|
||||||
document.querySelectorAll('.task-tab-btn').forEach(btn => {
|
|
||||||
btn.classList.remove('text-indigo-600', 'bg-indigo-50');
|
|
||||||
btn.classList.add('text-slate-600');
|
|
||||||
});
|
|
||||||
|
|
||||||
// Установка активного стиля для выбранной вкладки
|
|
||||||
const activeBtnId = {
|
|
||||||
'IN_PROGRESS': 'tab-in-progress',
|
|
||||||
'BACKLOG': 'tab-backlog',
|
|
||||||
'COMPLETED': 'tab-completed',
|
|
||||||
'ALL': 'tab-all'
|
|
||||||
}[status];
|
|
||||||
|
|
||||||
if (activeBtnId) {
|
|
||||||
const btn = document.getElementById(activeBtnId);
|
|
||||||
if (btn) {
|
|
||||||
btn.classList.remove('text-slate-600', 'hover:bg-slate-100');
|
|
||||||
btn.classList.add('text-indigo-600', 'bg-indigo-50');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
loadTasks();
|
|
||||||
}
|
|
||||||
|
|
||||||
function renderSidebarTasks(tasks) {
|
|
||||||
const container = document.getElementById("tasks-list") || document.getElementById("tasks-list-container");
|
|
||||||
if (!container) return;
|
if (!container) return;
|
||||||
|
|
||||||
let filtered = tasks;
|
let filtered = tasksCache;
|
||||||
if (currentTaskFilter !== 'ALL') {
|
if (currentTasksFilter === 'IN_PROGRESS') {
|
||||||
filtered = tasks.filter(t => t.status === currentTaskFilter);
|
filtered = tasksCache.filter(t => t.status === 'IN_PROGRESS');
|
||||||
|
} else if (currentTasksFilter === 'BACKLOG') {
|
||||||
|
filtered = tasksCache.filter(t => t.status === 'BACKLOG' || t.status === 'PLANNED');
|
||||||
|
} else if (currentTasksFilter === 'COMPLETED') {
|
||||||
|
filtered = tasksCache.filter(t => t.status === 'COMPLETED' || t.status === 'DONE');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const filtersHtml = `
|
||||||
|
<div class="flex items-center gap-1 p-1 bg-slate-200/70 rounded-lg text-[11px] font-semibold mb-2">
|
||||||
|
<button onclick="setTaskFilter('ALL')" class="flex-1 py-1 rounded text-center transition ${currentTasksFilter === 'ALL' ? 'bg-white text-indigo-700 shadow-sm' : 'text-slate-600 hover:text-slate-900'}">Все</button>
|
||||||
|
<button onclick="setTaskFilter('IN_PROGRESS')" class="flex-1 py-1 rounded text-center transition ${currentTasksFilter === 'IN_PROGRESS' ? 'bg-white text-indigo-700 shadow-sm' : 'text-slate-600 hover:text-slate-900'}">В работе</button>
|
||||||
|
<button onclick="setTaskFilter('BACKLOG')" class="flex-1 py-1 rounded text-center transition ${currentTasksFilter === 'BACKLOG' ? 'bg-white text-indigo-700 shadow-sm' : 'text-slate-600 hover:text-slate-900'}">Планы</button>
|
||||||
|
<button onclick="setTaskFilter('COMPLETED')" class="flex-1 py-1 rounded text-center transition ${currentTasksFilter === 'COMPLETED' ? 'bg-white text-indigo-700 shadow-sm' : 'text-slate-600 hover:text-slate-900'}">Готово</button>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
const addBtnHtml = `
|
||||||
|
<div class="flex items-center justify-between px-1 mb-1.5">
|
||||||
|
<span class="text-xs font-bold text-slate-700">Задачи: ${filtered.length}</span>
|
||||||
|
<button onclick="openCreateTaskModal()" class="px-2 py-1 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg text-[11px] font-bold shadow-sm flex items-center gap-1 transition">
|
||||||
|
<i class="fa-solid fa-plus text-[10px]"></i> Новая
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
if (filtered.length === 0) {
|
if (filtered.length === 0) {
|
||||||
container.innerHTML = `<div class="text-center py-8 text-[11px] font-medium text-slate-400 bg-slate-50 rounded-xl border border-dashed border-slate-200">Нет задач в этой категории</div>`;
|
container.innerHTML = `
|
||||||
|
${filtersHtml}
|
||||||
|
${addBtnHtml}
|
||||||
|
<div class="text-center py-8 text-xs text-slate-400 bg-white border border-slate-200 rounded-xl p-4">
|
||||||
|
Нет задач в выбранной категории
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
container.innerHTML = filtered.map(t => {
|
const itemsHtml = filtered.map(t => {
|
||||||
const isCompleted = t.status === 'COMPLETED';
|
const isDone = t.status === 'COMPLETED' || t.status === 'DONE';
|
||||||
const priorityColor = t.priority === 'HIGH' || t.priority === 'CRITICAL'
|
const priorityColors = {
|
||||||
? 'text-rose-600 bg-rose-50 border-rose-200'
|
'HIGH': 'bg-rose-50 text-rose-700 border-rose-200',
|
||||||
: t.priority === 'MEDIUM'
|
'MEDIUM': 'bg-amber-50 text-amber-700 border-amber-200',
|
||||||
? 'text-amber-600 bg-amber-50 border-amber-200'
|
'LOW': 'bg-slate-50 text-slate-600 border-slate-200'
|
||||||
: 'text-slate-600 bg-slate-50 border-slate-200';
|
};
|
||||||
|
const pClass = priorityColors[t.priority] || priorityColors['MEDIUM'];
|
||||||
|
|
||||||
return `
|
return `
|
||||||
<div class="p-3 bg-white border border-slate-200 rounded-xl shadow-sm hover:border-indigo-400 hover:shadow-md transition cursor-pointer flex flex-col gap-2 group"
|
<div class="flex flex-col p-2.5 bg-white border border-slate-200 rounded-xl text-xs gap-1.5 shadow-sm hover:border-indigo-300 transition">
|
||||||
onclick="handleActionButtonClick('покажи задачу ${t.id}')">
|
|
||||||
|
|
||||||
<div class="flex items-start justify-between gap-2">
|
<div class="flex items-start justify-between gap-2">
|
||||||
<span class="text-[10px] font-bold text-slate-400 uppercase tracking-wider group-hover:text-indigo-500 transition">#${t.id}</span>
|
<div class="flex items-center gap-1.5 min-w-0">
|
||||||
${isCompleted
|
<button onclick="toggleTaskStatus(${t.id}, '${t.status}')" class="text-slate-400 hover:text-indigo-600 transition shrink-0">
|
||||||
? `<span class="text-[10px] font-bold px-1.5 py-0.5 rounded-md bg-emerald-50 text-emerald-600 border border-emerald-200 shadow-sm"><i class="fa-solid fa-check mr-0.5"></i> Готово</span>`
|
<i class="fa-${isDone ? 'solid fa-circle-check text-emerald-500' : 'regular fa-circle'} text-sm"></i>
|
||||||
: `<span class="text-[10px] font-bold px-1.5 py-0.5 rounded-md ${priorityColor} shadow-sm">${t.priority || 'LOW'}</span>`
|
</button>
|
||||||
}
|
<span class="font-bold text-slate-800 ${isDone ? 'line-through text-slate-400' : ''} truncate">${escapeHtml(t.title)}</span>
|
||||||
|
</div>
|
||||||
|
<span class="px-1.5 py-0.5 rounded text-[9px] font-semibold border ${pClass} shrink-0">${t.priority || 'NORMAL'}</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="text-xs font-semibold text-slate-700 leading-snug line-clamp-3">${escapeHtml(t.title || 'Без названия')}</div>
|
<div class="flex items-center justify-between text-[10px] text-slate-400 pt-1 border-t border-slate-100">
|
||||||
|
<span>${t.task_id || ('#' + t.id)} · ${escapeHtml(t.module || 'general')}</span>
|
||||||
<div class="flex items-center justify-between text-[10px] text-slate-400 mt-1">
|
<div class="flex items-center gap-1">
|
||||||
<span class="bg-slate-100 px-1.5 py-0.5 rounded font-mono truncate max-w-[120px]">${escapeHtml(t.module || 'general')}</span>
|
<button onclick="deleteTaskItem(${t.id})" class="text-slate-400 hover:text-rose-600 p-0.5 transition" title="Удалить">
|
||||||
${t.due_date ? `<span class="shrink-0 font-medium text-slate-500"><i class="fa-regular fa-calendar mr-1"></i>${escapeHtml(t.due_date)}</span>` : ''}
|
<i class="fa-solid fa-trash-can text-[11px]"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
}).join('');
|
}).join('');
|
||||||
|
|
||||||
|
container.innerHTML = `
|
||||||
|
${filtersHtml}
|
||||||
|
${addBtnHtml}
|
||||||
|
<div class="flex flex-col gap-1.5 max-h-[70vh] overflow-y-auto">
|
||||||
|
${itemsHtml}
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Глобальная инициализация при загрузке DOM
|
async function toggleTaskStatus(id, currentStatus) {
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
const newStatus = (currentStatus === 'COMPLETED' || currentStatus === 'DONE') ? 'IN_PROGRESS' : 'COMPLETED';
|
||||||
// Небольшая задержка, чтобы гарантировать применение токена
|
try {
|
||||||
setTimeout(loadTasks, 200);
|
const res = await fetch(`/api/v1/tasks/${id}`, {
|
||||||
|
method: "PATCH",
|
||||||
|
headers: AuthManager.getAuthHeaders(),
|
||||||
|
body: JSON.stringify({ status: newStatus })
|
||||||
});
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
loadTasks();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Ошибка смены статуса задачи:", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteTaskItem(id) {
|
||||||
|
if (!confirm("Удалить эту задачу?")) return;
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/v1/tasks/${id}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: AuthManager.getAuthHeaders()
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
loadTasks();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
alert("Ошибка сети при удалении");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function openCreateTaskModal() {
|
||||||
|
const title = prompt("Введите описание новой задачи:");
|
||||||
|
if (!title || !title.trim()) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/v1/tasks", {
|
||||||
|
method: "POST",
|
||||||
|
headers: AuthManager.getAuthHeaders(),
|
||||||
|
body: JSON.stringify({
|
||||||
|
title: title.trim(),
|
||||||
|
priority: "MEDIUM",
|
||||||
|
status: "IN_PROGRESS"
|
||||||
|
})
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
loadTasks();
|
||||||
|
} else {
|
||||||
|
alert("Не удалось создать задачу");
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
alert("Ошибка сети");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.loadTasks = loadTasks;
|
||||||
|
window.setTaskFilter = setTaskFilter;
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<div id="admin-modal" class="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 flex items-center justify-center p-4 hidden">
|
||||||
|
<div class="bg-white rounded-2xl shadow-2xl border border-slate-200 max-w-lg w-full p-6 flex flex-col gap-4 max-h-[85vh]">
|
||||||
|
<div class="flex items-center justify-between">
|
||||||
|
<h3 class="text-sm font-bold text-slate-800 flex items-center gap-1.5">
|
||||||
|
<i class="fa-solid fa-users-gear text-indigo-600"></i> Управление учетными записями
|
||||||
|
</h3>
|
||||||
|
<button onclick="closeAdminModal()" class="text-slate-400 hover:text-slate-600"><i class="fa-solid fa-xmark"></i></button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form onsubmit="handleCreateUser(event)" class="p-3 bg-slate-50 border border-slate-200 rounded-xl flex flex-col gap-2">
|
||||||
|
<span class="text-xs font-bold text-slate-700">Создать нового пользователя:</span>
|
||||||
|
<div class="grid grid-cols-2 gap-2">
|
||||||
|
<input type="text" id="new-user-username" placeholder="Логин" required class="text-xs px-2.5 py-1.5 border border-slate-300 rounded-lg bg-white" />
|
||||||
|
<input type="password" id="new-user-password" placeholder="Пароль" required class="text-xs px-2.5 py-1.5 border border-slate-300 rounded-lg bg-white" />
|
||||||
|
</div>
|
||||||
|
<input type="text" id="new-user-fullname" placeholder="ФИО" class="text-xs px-2.5 py-1.5 border border-slate-300 rounded-lg bg-white" />
|
||||||
|
<div class="flex items-center justify-between">
|
||||||
|
<label class="flex items-center gap-1.5 text-xs text-slate-600 cursor-pointer">
|
||||||
|
<input type="checkbox" id="new-user-admin" class="rounded border-slate-300 text-indigo-600" />
|
||||||
|
<span>Права администратора</span>
|
||||||
|
</label>
|
||||||
|
<button type="submit" class="px-3 py-1 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg text-xs font-bold shadow">
|
||||||
|
Создать
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<div class="flex-1 overflow-y-auto flex flex-col gap-1.5" id="admin-users-list">
|
||||||
|
<div class="text-center py-4 text-xs text-slate-400">Загрузка пользователей...</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<div id="auth-modal" class="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 flex items-center justify-center p-4 hidden">
|
||||||
|
<div class="bg-white rounded-2xl shadow-2xl border border-slate-200 max-w-sm w-full p-6 flex flex-col gap-4">
|
||||||
|
<div class="flex items-center gap-3">
|
||||||
|
<div class="w-10 h-10 rounded-xl bg-indigo-600 text-white flex items-center justify-center font-bold text-lg shadow">
|
||||||
|
<i class="fa-solid fa-shield-halved"></i>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<h2 class="text-sm font-bold text-slate-800">Авторизация в системе</h2>
|
||||||
|
<p class="text-[11px] text-slate-400">SCUD Orion AI Security Access</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form id="auth-form" onsubmit="handleLoginSubmit(event)" class="flex flex-col gap-3">
|
||||||
|
<div>
|
||||||
|
<label class="block text-[11px] font-bold text-slate-600 mb-1">Имя пользователя:</label>
|
||||||
|
<input type="text" id="auth-username" required class="w-full text-xs px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500 bg-slate-50" placeholder="Логин" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label class="block text-[11px] font-bold text-slate-600 mb-1">Пароль:</label>
|
||||||
|
<input type="password" id="auth-password" required class="w-full text-xs px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500 bg-slate-50" placeholder="••••••••" />
|
||||||
|
</div>
|
||||||
|
<div id="auth-error" class="text-[11px] font-semibold text-rose-600 hidden"></div>
|
||||||
|
<button type="submit" class="w-full py-2 bg-indigo-600 hover:bg-indigo-700 text-white font-bold rounded-lg text-xs shadow transition mt-1">
|
||||||
|
Войти в систему
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
<div id="exception-modal" class="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 flex items-center justify-center p-4 hidden">
|
||||||
|
<div class="bg-white rounded-2xl shadow-2xl border border-slate-200 max-w-md w-full p-6 flex flex-col gap-4">
|
||||||
|
<div class="flex items-center justify-between">
|
||||||
|
<h3 id="exception-modal-title" class="text-sm font-bold text-slate-800 flex items-center gap-2">
|
||||||
|
<i class="fa-solid fa-user-shield text-indigo-600"></i>
|
||||||
|
<span id="exception-modal-header-text">Добавление в реестр</span>
|
||||||
|
</h3>
|
||||||
|
<button type="button" onclick="closeExceptionModal()" class="text-slate-400 hover:text-slate-600">
|
||||||
|
<i class="fa-solid fa-xmark"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form id="exception-modal-form" onsubmit="submitExceptionModalForm(event)" class="flex flex-col gap-3">
|
||||||
|
<input type="hidden" id="exception-category-input" value="" />
|
||||||
|
|
||||||
|
<div class="relative">
|
||||||
|
<label id="exception-value-label" class="block text-[11px] font-bold text-slate-600 mb-1">ФИО сотрудника:</label>
|
||||||
|
<input type="text" id="exception-value-input" autocomplete="off" required
|
||||||
|
placeholder="Начните вводить фамилию..."
|
||||||
|
class="w-full text-xs px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500 bg-slate-50 text-slate-800" />
|
||||||
|
<div id="exception-suggestions" class="hidden absolute left-0 right-0 top-full mt-1 bg-white border border-slate-300 rounded-lg shadow-xl z-30 max-h-48 overflow-y-auto"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label class="block text-[11px] font-bold text-slate-600 mb-1">Примечание / основание (опционально):</label>
|
||||||
|
<input type="text" id="exception-comment-input" placeholder="Например: служебная записка, водитель, лаборатория"
|
||||||
|
class="w-full text-xs px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500 bg-slate-50 text-slate-800" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="exception-error-msg" class="text-[11px] font-semibold text-rose-600 hidden"></div>
|
||||||
|
|
||||||
|
<div class="flex items-center justify-end gap-2 mt-2 pt-2 border-t border-slate-100">
|
||||||
|
<button type="button" onclick="closeExceptionModal()"
|
||||||
|
class="px-3.5 py-1.5 text-xs text-slate-600 rounded-lg hover:bg-slate-100 font-medium transition">
|
||||||
|
Отмена
|
||||||
|
</button>
|
||||||
|
<button type="submit" id="exception-submit-btn"
|
||||||
|
class="px-4 py-1.5 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg text-xs font-bold shadow transition flex items-center gap-1.5">
|
||||||
|
<i class="fa-solid fa-check text-xs"></i>
|
||||||
|
<span>Добавить</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
<div id="manual-absence-modal" class="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 flex items-center justify-center p-4 hidden">
|
||||||
|
<div class="bg-white rounded-2xl shadow-2xl border border-slate-200 max-w-md w-full p-6 flex flex-col gap-4">
|
||||||
|
<div class="flex items-center justify-between">
|
||||||
|
<h3 id="manual-absence-modal-title" class="text-sm font-bold text-slate-800 flex items-center gap-2">
|
||||||
|
<i class="fa-solid fa-location-dot text-indigo-600"></i>
|
||||||
|
<span id="manual-absence-modal-title-text">Добавление в реестр</span>
|
||||||
|
</h3>
|
||||||
|
<button type="button" onclick="closeManualAbsenceModal()" class="text-slate-400 hover:text-slate-600 p-1 rounded-lg hover:bg-slate-100 transition">
|
||||||
|
<i class="fa-solid fa-xmark text-base"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form id="manual-absence-form" onsubmit="submitManualAbsence(event)" class="flex flex-col gap-3">
|
||||||
|
<input type="hidden" id="manual-absence-id" value="" />
|
||||||
|
<input type="hidden" id="manual-absence-dept" value="" />
|
||||||
|
<input type="hidden" id="manual-absence-pos" value="" />
|
||||||
|
|
||||||
|
<div class="relative">
|
||||||
|
<label class="block text-[11px] font-bold text-slate-600 mb-1">ФИО сотрудника (автоподбор из 1С):</label>
|
||||||
|
<input type="text" id="manual-absence-fio-input" autocomplete="off" required placeholder="Начните вводить фамилию..."
|
||||||
|
class="w-full text-xs px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500 bg-slate-50 text-slate-800 transition" />
|
||||||
|
<div id="manual-absence-suggestions" class="hidden absolute left-0 right-0 top-full mt-1 bg-white border border-slate-300 rounded-lg shadow-xl z-30 max-h-48 overflow-y-auto"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="manual-absence-reason-block" class="hidden">
|
||||||
|
<label class="block text-[11px] font-bold text-slate-600 mb-1">Причина отсутствия:</label>
|
||||||
|
<select id="manual-absence-reason-select" class="w-full text-xs px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500 bg-slate-50 text-slate-800"></select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="grid grid-cols-2 gap-3">
|
||||||
|
<div>
|
||||||
|
<label class="block text-[11px] font-bold text-slate-600 mb-1">Дата начала:</label>
|
||||||
|
<input type="date" id="manual-absence-start-date" required
|
||||||
|
class="w-full text-xs px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500 bg-slate-50 text-slate-700" />
|
||||||
|
<span class="text-[10px] text-slate-400 mt-0.5 block">Дата выхода/отбытия</span>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label class="block text-[11px] font-bold text-slate-600 mb-1">Дата окончания:</label>
|
||||||
|
<input type="date" id="manual-absence-end-date" required
|
||||||
|
class="w-full text-xs px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500 bg-slate-50 text-slate-700" />
|
||||||
|
<span class="text-[10px] text-slate-400 mt-0.5 block">Включительно</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="manual-absence-error" class="text-[11px] font-semibold text-rose-600 hidden"></div>
|
||||||
|
|
||||||
|
<div class="flex items-center justify-end gap-2 mt-2 pt-3 border-t border-slate-100">
|
||||||
|
<button type="button" onclick="closeManualAbsenceModal()"
|
||||||
|
class="px-3.5 py-1.5 text-xs text-slate-600 rounded-lg hover:bg-slate-100 font-medium transition">
|
||||||
|
Отмена
|
||||||
|
</button>
|
||||||
|
<button type="submit" id="manual-absence-submit-btn"
|
||||||
|
class="px-4 py-1.5 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg text-xs font-bold shadow-xs transition">
|
||||||
|
Сохранить
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
<div id="presence-modal" class="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 hidden flex items-center justify-center p-4">
|
||||||
|
<div class="bg-white rounded-2xl shadow-2xl w-full max-w-6xl h-[88vh] flex flex-col overflow-hidden border border-slate-200">
|
||||||
|
|
||||||
|
<!-- Шапка -->
|
||||||
|
<div class="px-6 py-4 border-b border-slate-200 bg-slate-50/80 flex items-center justify-between">
|
||||||
|
<div class="flex items-center space-x-3">
|
||||||
|
<div class="w-10 h-10 rounded-xl bg-emerald-500/10 text-emerald-600 flex items-center justify-center font-bold text-xl">
|
||||||
|
🏢
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<h3 class="text-lg font-bold text-slate-800">Оперативный мониторинг присутствия</h3>
|
||||||
|
<p id="presence-modal-subtitle" class="text-xs text-slate-500">Загрузка данных...</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="flex items-center space-x-2">
|
||||||
|
<button id="btn-presence-force-refresh" class="px-3 py-1.5 bg-emerald-600 hover:bg-emerald-700 text-white rounded-lg text-xs font-semibold flex items-center space-x-1.5 transition-colors shadow-sm">
|
||||||
|
<span>🔄</span>
|
||||||
|
<span>Запросить из СКУД</span>
|
||||||
|
</button>
|
||||||
|
<button id="btn-close-presence-modal" class="text-slate-400 hover:text-slate-600 p-2 rounded-lg hover:bg-slate-100 transition-colors">
|
||||||
|
✕
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Метрики (плашки) - 7 колонок для точного баланса штата 1С -->
|
||||||
|
<div class="grid grid-cols-2 sm:grid-cols-7 gap-2 p-4 bg-slate-100/60 border-b border-slate-200">
|
||||||
|
<div class="bg-white p-2.5 rounded-xl border border-slate-200/80 text-center">
|
||||||
|
<div class="text-[11px] text-slate-500 font-medium">Штат 1С</div>
|
||||||
|
<div id="metric-total" class="text-lg font-bold text-slate-800">—</div>
|
||||||
|
</div>
|
||||||
|
<div class="bg-emerald-50 p-2.5 rounded-xl border border-emerald-200/80 text-center">
|
||||||
|
<div class="text-[11px] text-emerald-700 font-medium">В здании</div>
|
||||||
|
<div id="metric-inside" class="text-lg font-bold text-emerald-700">—</div>
|
||||||
|
</div>
|
||||||
|
<div class="bg-amber-50 p-2.5 rounded-xl border border-amber-200/80 text-center">
|
||||||
|
<div class="text-[11px] text-amber-700 font-medium">Вышли</div>
|
||||||
|
<div id="metric-outside" class="text-lg font-bold text-amber-700">—</div>
|
||||||
|
</div>
|
||||||
|
<div class="bg-blue-50 p-2.5 rounded-xl border border-blue-200/80 text-center">
|
||||||
|
<div class="text-[11px] text-blue-700 font-medium">Удаленка</div>
|
||||||
|
<div id="metric-remote" class="text-lg font-bold text-blue-700">—</div>
|
||||||
|
</div>
|
||||||
|
<div class="bg-purple-50 p-2.5 rounded-xl border border-purple-200/80 text-center">
|
||||||
|
<div class="text-[11px] text-purple-700 font-medium">Отпуск/Ком.</div>
|
||||||
|
<div id="metric-absence" class="text-lg font-bold text-purple-700">—</div>
|
||||||
|
</div>
|
||||||
|
<div class="bg-rose-50 p-2.5 rounded-xl border border-rose-200/80 text-center">
|
||||||
|
<div class="text-[11px] text-rose-700 font-medium">Не пришли</div>
|
||||||
|
<div id="metric-not-entered" class="text-lg font-bold text-rose-700">—</div>
|
||||||
|
</div>
|
||||||
|
<div class="bg-slate-200/70 p-2.5 rounded-xl border border-slate-300 text-center">
|
||||||
|
<div class="text-[11px] text-slate-600 font-medium">Исключения</div>
|
||||||
|
<div id="metric-excluded" class="text-lg font-bold text-slate-700">—</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Панель управления и фильтрации -->
|
||||||
|
<div class="p-4 border-b border-slate-200 flex flex-wrap items-center justify-between gap-3 bg-white">
|
||||||
|
<!-- Табы статусов -->
|
||||||
|
<div class="flex items-center space-x-1 overflow-x-auto bg-slate-100 p-1 rounded-xl text-xs font-semibold">
|
||||||
|
<button class="presence-tab-btn px-3 py-1.5 rounded-lg bg-white shadow-xs text-slate-800" data-tab="ALL">Все (<span id="tab-cnt-all">0</span>)</button>
|
||||||
|
<button class="presence-tab-btn px-3 py-1.5 rounded-lg text-slate-600 hover:text-slate-900" data-tab="INSIDE">В здании (<span id="tab-cnt-inside">0</span>)</button>
|
||||||
|
<button class="presence-tab-btn px-3 py-1.5 rounded-lg text-slate-600 hover:text-slate-900" data-tab="OUTSIDE">Вышли (<span id="tab-cnt-outside">0</span>)</button>
|
||||||
|
<button class="presence-tab-btn px-3 py-1.5 rounded-lg text-slate-600 hover:text-slate-900" data-tab="REMOTE">Удаленка (<span id="tab-cnt-remote">0</span>)</button>
|
||||||
|
<button class="presence-tab-btn px-3 py-1.5 rounded-lg text-slate-600 hover:text-slate-900" data-tab="ABSENCE">Отсутствуют (<span id="tab-cnt-absence">0</span>)</button>
|
||||||
|
<button class="presence-tab-btn px-3 py-1.5 rounded-lg text-slate-600 hover:text-slate-900" data-tab="NOT_ENTERED">Не пришли (<span id="tab-cnt-not-entered">0</span>)</button>
|
||||||
|
<button class="presence-tab-btn px-3 py-1.5 rounded-lg text-slate-600 hover:text-slate-900" data-tab="EXCLUDED">Исключения (<span id="tab-cnt-excluded">0</span>)</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Поиск по ФИО и фильтр отдела -->
|
||||||
|
<div class="flex items-center space-x-2 w-full sm:w-auto">
|
||||||
|
<input type="text" id="presence-search-input" placeholder="🔍 Поиск сотрудника..." class="text-xs px-3 py-1.5 bg-slate-50 border border-slate-200 rounded-lg w-48 focus:outline-emerald-500 focus:bg-white">
|
||||||
|
<select id="presence-dept-filter" class="text-xs px-3 py-1.5 bg-slate-50 border border-slate-200 rounded-lg max-w-[180px] focus:outline-emerald-500">
|
||||||
|
<option value="ALL">Все подразделения</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Таблица сотрудников -->
|
||||||
|
<div class="flex-1 overflow-y-auto p-4">
|
||||||
|
<table class="w-full text-left text-xs border-collapse">
|
||||||
|
<thead class="sticky top-0 bg-slate-50 border-b border-slate-200 text-slate-500 uppercase tracking-wider">
|
||||||
|
<tr>
|
||||||
|
<th class="py-2.5 px-3 font-semibold">Сотрудник</th>
|
||||||
|
<th class="py-2.5 px-3 font-semibold">Отдел</th>
|
||||||
|
<th class="py-2.5 px-3 font-semibold">Должность</th>
|
||||||
|
<th class="py-2.5 px-3 font-semibold text-center">Статус</th>
|
||||||
|
<th class="py-2.5 px-3 font-semibold text-center">Отметка</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody id="presence-table-body" class="divide-y divide-slate-100">
|
||||||
|
<tr>
|
||||||
|
<td colspan="5" class="py-8 text-center text-slate-400">Загрузка данных...</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<div id="profile-modal" class="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 flex items-center justify-center p-4 hidden">
|
||||||
|
<div class="bg-white rounded-2xl shadow-2xl border border-slate-200 max-w-sm w-full p-6 flex flex-col gap-4">
|
||||||
|
<div class="flex items-center justify-between">
|
||||||
|
<h3 class="text-sm font-bold text-slate-800 flex items-center gap-1.5">
|
||||||
|
<i class="fa-solid fa-key text-indigo-600"></i> Смена пароля
|
||||||
|
</h3>
|
||||||
|
<button onclick="closeProfileModal()" class="text-slate-400 hover:text-slate-600"><i class="fa-solid fa-xmark"></i></button>
|
||||||
|
</div>
|
||||||
|
<form onsubmit="handleChangePassword(event)" class="flex flex-col gap-3">
|
||||||
|
<div>
|
||||||
|
<label class="block text-[11px] font-bold text-slate-600 mb-1">Текущий пароль:</label>
|
||||||
|
<input type="password" id="old-pass" required class="w-full text-xs px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label class="block text-[11px] font-bold text-slate-600 mb-1">Новый пароль (мин. 4 симв.):</label>
|
||||||
|
<input type="password" id="new-pass" required class="w-full text-xs px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500" />
|
||||||
|
</div>
|
||||||
|
<div id="pass-error" class="text-[11px] font-semibold text-rose-600 hidden"></div>
|
||||||
|
<div class="flex items-center justify-end gap-2 mt-2">
|
||||||
|
<button type="button" onclick="closeProfileModal()" class="px-3 py-1.5 text-xs text-slate-600 rounded-lg hover:bg-slate-100 font-medium">Отмена</button>
|
||||||
|
<button type="submit" class="px-4 py-1.5 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg text-xs font-bold shadow transition">Сохранить</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<div id="remote-worker-modal" class="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 flex items-center justify-center p-4 hidden">
|
||||||
|
<div class="bg-white rounded-2xl shadow-2xl border border-slate-200 max-w-md w-full p-6 flex flex-col gap-4">
|
||||||
|
<div class="flex items-center justify-between">
|
||||||
|
<h3 id="remote-modal-title" class="text-sm font-bold text-slate-800 flex items-center gap-2">
|
||||||
|
<i class="fa-solid fa-house-laptop text-emerald-600"></i>
|
||||||
|
<span>Параметры удаленной работы</span>
|
||||||
|
</h3>
|
||||||
|
<button type="button" onclick="closeRemoteWorkerModal()" class="text-slate-400 hover:text-slate-600">
|
||||||
|
<i class="fa-solid fa-xmark"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form id="remote-worker-form" onsubmit="handleRemoteWorkerSubmit(event)" class="flex flex-col gap-3">
|
||||||
|
<input type="hidden" id="rw-mode" value="ADD" />
|
||||||
|
|
||||||
|
<div class="relative">
|
||||||
|
<label class="block text-[11px] font-bold text-slate-600 mb-1">ФИО сотрудника (автоподбор из 1С):</label>
|
||||||
|
<input type="text" id="rw-fio" required autocomplete="off" placeholder="Начните вводить фамилию..."
|
||||||
|
class="w-full text-xs px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:border-emerald-500 bg-slate-50" />
|
||||||
|
<div id="rw-fio-suggestions" class="hidden absolute left-0 right-0 top-full mt-1 bg-white border border-slate-300 rounded-lg shadow-xl z-30 max-h-48 overflow-y-auto"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label class="block text-[11px] font-bold text-slate-600 mb-1">Подразделение:</label>
|
||||||
|
<input type="text" id="rw-dept" placeholder="Все"
|
||||||
|
class="w-full text-xs px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:border-emerald-500 bg-slate-50" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="grid grid-cols-2 gap-3">
|
||||||
|
<div>
|
||||||
|
<label class="block text-[11px] font-bold text-slate-600 mb-1">Дата начала:</label>
|
||||||
|
<input type="date" id="rw-date-from"
|
||||||
|
class="w-full text-xs px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:border-emerald-500 bg-slate-50 text-slate-700" />
|
||||||
|
<span class="text-[10px] text-slate-400 mt-0.5 block">Пусто = с сегодняшнего дня</span>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label class="block text-[11px] font-bold text-slate-600 mb-1">Дата окончания:</label>
|
||||||
|
<input type="date" id="rw-date-to"
|
||||||
|
class="w-full text-xs px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:border-emerald-500 bg-slate-50 text-slate-700" />
|
||||||
|
<span class="text-[10px] text-slate-400 mt-0.5 block">Пусто = бессрочно</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="rw-error" class="text-[11px] font-semibold text-rose-600 hidden"></div>
|
||||||
|
|
||||||
|
<div class="flex items-center justify-end gap-2 mt-2 pt-2 border-t border-slate-100">
|
||||||
|
<button type="button" onclick="closeRemoteWorkerModal()"
|
||||||
|
class="px-3.5 py-1.5 text-xs text-slate-600 rounded-lg hover:bg-slate-100 font-medium transition">
|
||||||
|
Отмена
|
||||||
|
</button>
|
||||||
|
<button type="submit" id="rw-submit-btn"
|
||||||
|
class="px-4 py-1.5 bg-emerald-600 hover:bg-emerald-700 text-white rounded-lg text-xs font-bold shadow transition">
|
||||||
|
Сохранить
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
<div id="snapshot-inspector-modal" class="fixed inset-0 bg-slate-900/60 backdrop-blur-sm z-50 flex items-center justify-center p-4 hidden">
|
||||||
|
<div class="bg-white rounded-2xl shadow-2xl border border-slate-200 w-full max-w-6xl h-[90vh] flex flex-col overflow-hidden">
|
||||||
|
|
||||||
|
<!-- ШАПКА МОДАЛКИ ИНСПЕКЦИИ СРЕЗА -->
|
||||||
|
<div class="px-6 py-4 bg-slate-50 border-b border-slate-200 flex items-center justify-between shrink-0">
|
||||||
|
<div class="flex items-center gap-3">
|
||||||
|
<div class="w-9 h-9 rounded-xl bg-indigo-600 text-white flex items-center justify-center shadow-xs">
|
||||||
|
<i class="fa-solid fa-magnifying-glass-chart text-sm"></i>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<h3 id="inspector-modal-title" class="text-sm font-bold text-slate-800">Инспекция среза</h3>
|
||||||
|
<p id="inspector-modal-subtitle" class="text-[11px] text-slate-400">Загрузка данных...</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- КНОПКИ ЭКСПОРТА И ЗАКРЫТИЯ -->
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<button onclick="downloadInspectorExport('xlsx')"
|
||||||
|
class="px-3 py-1.5 bg-emerald-50 hover:bg-emerald-100 text-emerald-700 border border-emerald-200 rounded-lg text-xs font-semibold transition flex items-center gap-1.5 shadow-xs"
|
||||||
|
title="Скачать срез в формате Excel (.xlsx)">
|
||||||
|
<i class="fa-solid fa-file-excel text-emerald-600"></i>
|
||||||
|
<span>Excel</span>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button onclick="downloadInspectorExport('csv')"
|
||||||
|
class="px-3 py-1.5 bg-slate-100 hover:bg-slate-200 text-slate-700 border border-slate-200 rounded-lg text-xs font-semibold transition flex items-center gap-1.5 shadow-xs"
|
||||||
|
title="Скачать срез в CSV (для анализа нейросетями)">
|
||||||
|
<i class="fa-solid fa-file-csv text-slate-600"></i>
|
||||||
|
<span>CSV</span>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button onclick="closeSnapshotInspectorModal()"
|
||||||
|
class="text-slate-400 hover:text-slate-600 p-2 rounded-lg hover:bg-slate-200 transition ml-1"
|
||||||
|
title="Закрыть">
|
||||||
|
<i class="fa-solid fa-xmark text-base"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- ПАНЕЛЬ ФИЛЬТРОВ И ПОИСКА -->
|
||||||
|
<div class="px-6 py-3 bg-white border-b border-slate-200 flex flex-wrap items-center gap-3 shrink-0">
|
||||||
|
<div class="flex-1 min-w-[240px] relative">
|
||||||
|
<i class="fa-solid fa-magnifying-glass absolute left-3 top-2.5 text-slate-400 text-xs"></i>
|
||||||
|
<input type="text" id="inspector-search-input" oninput="filterInspectorTable()" placeholder="Поиск по ФИО или должности..."
|
||||||
|
class="w-full text-xs pl-8 pr-3 py-2 bg-slate-50 border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<select id="inspector-filter-status" onchange="filterInspectorTable()" class="text-xs px-3 py-2 bg-slate-50 border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500">
|
||||||
|
<option value="ALL">Все статусы</option>
|
||||||
|
<option value="PRESENT">Присутствовал</option>
|
||||||
|
<option value="ABSENT">Отсутствовал</option>
|
||||||
|
<option value="ANOMALY">Аномалии</option>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<select id="inspector-filter-dept" onchange="filterInspectorTable()" class="text-xs px-3 py-2 bg-slate-50 border border-slate-300 rounded-lg focus:outline-none focus:border-indigo-500">
|
||||||
|
<option value="ALL">Все подразделения</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- ТАБЛИЦА ДАННЫХ (СКРОЛЛИРУЕМАЯ ОБЛАСТЬ НА ВСЮ ВЫСОТУ) -->
|
||||||
|
<div class="flex-1 overflow-y-auto p-6 bg-slate-50">
|
||||||
|
<div class="bg-white border border-slate-200 rounded-xl shadow-xs overflow-hidden">
|
||||||
|
<table class="w-full text-left border-collapse">
|
||||||
|
<thead>
|
||||||
|
<tr class="bg-slate-100 border-b border-slate-200 text-[11px] font-bold text-slate-600 uppercase tracking-wider">
|
||||||
|
<th class="p-3">#</th>
|
||||||
|
<th class="p-3">Сотрудник</th>
|
||||||
|
<th class="p-3">Подразделение</th>
|
||||||
|
<th class="p-3">Вход</th>
|
||||||
|
<th class="p-3">Первая акт.</th>
|
||||||
|
<th class="p-3">Выход</th>
|
||||||
|
<th class="p-3">В здании</th>
|
||||||
|
<th class="p-3">Статус</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody id="inspector-table-body" class="divide-y divide-slate-100 text-xs text-slate-700">
|
||||||
|
<tr><td colspan="8" class="text-center py-8 text-slate-400">Выберите срез для инспекции</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- ПОДВАЛ МОДАЛКИ -->
|
||||||
|
<div class="px-6 py-3 bg-white border-t border-slate-200 flex items-center justify-between shrink-0">
|
||||||
|
<span id="inspector-records-count" class="text-xs font-semibold text-slate-500">Записей не найдено</span>
|
||||||
|
<button onclick="closeSnapshotInspectorModal()" class="px-4 py-1.5 bg-slate-100 hover:bg-slate-200 text-slate-700 rounded-lg text-xs font-bold transition">
|
||||||
|
Закрыть
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,20 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
set -e
|
|
||||||
|
|
||||||
# Переход в папку проекта
|
|
||||||
cd /home/puh/projects/scud_ai
|
|
||||||
|
|
||||||
# Создание папки для логов
|
|
||||||
mkdir -p /home/puh/projects/scud_ai/logs
|
|
||||||
|
|
||||||
# Фиксация старта
|
|
||||||
echo "==================================================" >> /home/puh/projects/scud_ai/logs/cron_etl.log
|
|
||||||
echo "[CRON START] $(date '+%Y-%m-%d %H:%M:%S')" >> /home/puh/projects/scud_ai/logs/cron_etl.log
|
|
||||||
echo "==================================================" >> /home/puh/projects/scud_ai/logs/cron_etl.log
|
|
||||||
|
|
||||||
# Запуск main_etl.py через интерпретатор окружения
|
|
||||||
/home/puh/scud_orion_ai_v2/venv/bin/python /home/puh/projects/scud_ai/main_etl.py >> /home/puh/projects/scud_ai/logs/cron_etl.log 2>&1
|
|
||||||
|
|
||||||
# Фиксация окончания
|
|
||||||
echo "[CRON FINISH] $(date '+%Y-%m-%d %H:%M:%S')" >> /home/puh/projects/scud_ai/logs/cron_etl.log
|
|
||||||
echo "" >> /home/puh/projects/scud_ai/logs/cron_etl.log
|
|
||||||
Executable
+14
@@ -0,0 +1,14 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
cd /home/apushkov/projects/scud_ai
|
||||||
|
mkdir -p /home/apushkov/projects/scud_ai/logs
|
||||||
|
|
||||||
|
echo "==================================================" >> /home/apushkov/projects/scud_ai/logs/cron_etl.log
|
||||||
|
echo "[CRON START] $(date '+%Y-%m-%d %H:%M:%S')" >> /home/apushkov/projects/scud_ai/logs/cron_etl.log
|
||||||
|
echo "==================================================" >> /home/apushkov/projects/scud_ai/logs/cron_etl.log
|
||||||
|
|
||||||
|
/home/apushkov/projects/scud_ai/venv/bin/python /home/apushkov/projects/scud_ai/main_etl.py >> /home/apushkov/projects/scud_ai/logs/cron_etl.log 2>&1
|
||||||
|
|
||||||
|
echo "[CRON FINISH] $(date '+%Y-%m-%d %H:%M:%S')" >> /home/apushkov/projects/scud_ai/logs/cron_etl.log
|
||||||
|
echo "" >> /home/apushkov/projects/scud_ai/logs/cron_etl.log
|
||||||
Executable
+9
@@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
cd /home/apushkov/projects/scud_ai
|
||||||
|
mkdir -p /home/apushkov/projects/scud_ai/logs
|
||||||
|
|
||||||
|
echo "[CRON HOURLY START] $(date '+%Y-%m-%d %H:%M:%S')" >> /home/apushkov/projects/scud_ai/logs/cron_hourly.log
|
||||||
|
/home/apushkov/projects/scud_ai/venv/bin/python /home/apushkov/projects/scud_ai/services/scud_export.py >> /home/apushkov/projects/scud_ai/logs/cron_hourly.log 2>&1
|
||||||
|
echo "[CRON HOURLY FINISH] $(date '+%Y-%m-%d %H:%M:%S')" >> /home/apushkov/projects/scud_ai/logs/cron_hourly.log
|
||||||
Executable
+9
@@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
cd /home/apushkov/projects/scud_ai
|
||||||
|
mkdir -p /home/apushkov/projects/scud_ai/logs
|
||||||
|
|
||||||
|
echo "[CRON REPORTS START] $(date '+%Y-%m-%d %H:%M:%S')" >> /home/apushkov/projects/scud_ai/logs/cron_reports.log
|
||||||
|
/home/apushkov/projects/scud_ai/venv/bin/python /home/apushkov/projects/scud_ai/main_etl.py --skip-export >> /home/apushkov/projects/scud_ai/logs/cron_reports.log 2>&1
|
||||||
|
echo "[CRON REPORTS FINISH] $(date '+%Y-%m-%d %H:%M:%S')" >> /home/apushkov/projects/scud_ai/logs/cron_reports.log
|
||||||
+42
-51
@@ -7,6 +7,9 @@ from datetime import datetime
|
|||||||
|
|
||||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
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 config import DATA_DIR, DATE_TODAY, OUTPUT_DIR, EXCEPTIONS_PATH, normalize_fio
|
||||||
from core.database import (
|
from core.database import (
|
||||||
get_connection,
|
get_connection,
|
||||||
@@ -101,7 +104,7 @@ def print_stats():
|
|||||||
with get_connection() as conn:
|
with get_connection() as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
tables = [
|
tables = [
|
||||||
'scud_logs', 'zup_staff', 'zup_absences', 'anomalies_history',
|
'scud_logs', 'scud_events_raw', 'zup_staff', 'zup_absences', 'anomalies_history',
|
||||||
'ai_knowledge_base', 'chat_messages', 'session_states',
|
'ai_knowledge_base', 'chat_messages', 'session_states',
|
||||||
'system_prompt_nodes', 'tasks', 'exceptions_registry'
|
'system_prompt_nodes', 'tasks', 'exceptions_registry'
|
||||||
]
|
]
|
||||||
@@ -296,15 +299,8 @@ def print_rules():
|
|||||||
|
|
||||||
def dump_all_to_excel(out_filename="db_dump_full.xlsx"):
|
def dump_all_to_excel(out_filename="db_dump_full.xlsx"):
|
||||||
"""Дампит всю базу SQLite во многостраничный Excel."""
|
"""Дампит всю базу SQLite во многостраничный Excel."""
|
||||||
out_path = os.path.join(OUTPUT_DIR, out_filename)
|
print(f"\n[🔄] Создание полного дампа БД в файл: {out_filename} ...")
|
||||||
print(f"\n[🔄] Создание полного дампа БД в файл: {out_path} ...")
|
out_path = dump_database_to_excel(out_filename)
|
||||||
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"[✓] Дамп успешно сохранен: {out_path}\n")
|
print(f"[✓] Дамп успешно сохранен: {out_path}\n")
|
||||||
|
|
||||||
|
|
||||||
@@ -393,47 +389,12 @@ def print_chat_messages(session_id=None, limit=50):
|
|||||||
|
|
||||||
|
|
||||||
def purge_chat_context(session_id=None, purge_all=False):
|
def purge_chat_context(session_id=None, purge_all=False):
|
||||||
"""
|
"""Очистка контекста сообщений через репозиторий чата."""
|
||||||
Очистка контекста сообщений:
|
deleted_msgs = db_clear_all_chat_context(session_id=session_id, purge_all=purge_all)
|
||||||
- По умолчанию: удаляет эфемерные сообщения, осиротевшие превью и сбрасывает стейты сессий.
|
mode = "Полная" if purge_all else "Умная"
|
||||||
- purge_all=True (--all): полностью очищает всю таблицу chat_messages и сбрасывает сессии.
|
print(f"\n[✓] {mode} зачистка контекста выполнена! Удалено сообщений: {deleted_msgs}\n")
|
||||||
"""
|
|
||||||
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")
|
|
||||||
|
|
||||||
|
|
||||||
# ⭐️ Новые функции управления исключениями (Exceptions & Whitelist)
|
|
||||||
def print_exceptions():
|
def print_exceptions():
|
||||||
"""Выводит реестр исключений и белый список сотрудников из базы SQLite."""
|
"""Выводит реестр исключений и белый список сотрудников из базы SQLite."""
|
||||||
exc = get_all_exceptions_from_db()
|
exc = get_all_exceptions_from_db()
|
||||||
@@ -450,6 +411,26 @@ def print_exceptions():
|
|||||||
print("-" * 80)
|
print("-" * 80)
|
||||||
print("=" * 80 + "\n")
|
print("=" * 80 + "\n")
|
||||||
|
|
||||||
|
def print_building_presence(date_str: str, all_statuses: bool = False):
|
||||||
|
"""Выводит оперативный список сотрудников, находящихся в здании."""
|
||||||
|
records = get_building_presence(date_str, only_inside=not all_statuses)
|
||||||
|
|
||||||
|
title = f"КТО СЕЙЧАС В ЗДАНИИ [{date_str}]" if not all_statuses else f"ОПЕРАТИВНЫЙ СТАТУС СОТРУДНИКОВ [{date_str}]"
|
||||||
|
print("\n" + "=" * 80)
|
||||||
|
print(f"🏢 {title} (Всего: {len(records)})")
|
||||||
|
print("=" * 80)
|
||||||
|
|
||||||
|
if not records:
|
||||||
|
print(" Нет данных о проходах за указанную дату.")
|
||||||
|
else:
|
||||||
|
print(f"{'ФИО':<35} | {'Подразделение':<15} | {'Время':<10} | {'Статус':<8}")
|
||||||
|
print("-" * 80)
|
||||||
|
for r in records:
|
||||||
|
time_short = r['last_event_time'].split()[-1][:8] if ' ' in r['last_event_time'] else r['last_event_time'][:8]
|
||||||
|
print(f"{r['fio']:<35} | {r['department'][:15]:<15} | {time_short:<10} | {r['status']:<8}")
|
||||||
|
|
||||||
|
print("=" * 80 + "\n")
|
||||||
|
|
||||||
|
|
||||||
HELP_TEXT = """
|
HELP_TEXT = """
|
||||||
CLI-утилита инспекции и управления SQLite базой данных СКУД (scud_orion_ai.db)
|
CLI-утилита инспекции и управления SQLite базой данных СКУД (scud_orion_ai.db)
|
||||||
@@ -458,6 +439,7 @@ CLI-утилита инспекции и управления SQLite базой
|
|||||||
stats -- Общая статистика строк по всем таблицам БД
|
stats -- Общая статистика строк по всем таблицам БД
|
||||||
snapshots [ДД.ММ.ГГГГ] -- Посмотреть реестр снапшотов (опционально за конкретную дату)
|
snapshots [ДД.ММ.ГГГГ] -- Посмотреть реестр снапшотов (опционально за конкретную дату)
|
||||||
scud [ДД.ММ.ГГГГ] [--snapshot ID] [--export-xlsx NAME] -- Инспекция логов СКУД по дате/снапшоту и экспорт в Excel
|
scud [ДД.ММ.ГГГГ] [--snapshot ID] [--export-xlsx NAME] -- Инспекция логов СКУД по дате/снапшоту и экспорт в Excel
|
||||||
|
in_building [ДД.ММ.ГГГГ] [--all] -- Оперативный статус: кто сейчас в здании (или все статусы с флагом --all)
|
||||||
absences [ДД.ММ.ГГГГ] -- Посмотреть список официально отсутствующих из 1С:ЗУП
|
absences [ДД.ММ.ГГГГ] -- Посмотреть список официально отсутствующих из 1С:ЗУП
|
||||||
anomalies -- Посмотреть историю найденных аномалий СКУД ⟷ 1С
|
anomalies -- Посмотреть историю найденных аномалий СКУД ⟷ 1С
|
||||||
rules -- Посмотреть правила Базы Знаний ИИ из SQLite
|
rules -- Посмотреть правила Базы Знаний ИИ из SQLite
|
||||||
@@ -478,6 +460,8 @@ CLI-утилита инспекции и управления SQLite базой
|
|||||||
python scripts/db_cli.py stats
|
python scripts/db_cli.py stats
|
||||||
python scripts/db_cli.py snapshots 06.08.2026
|
python scripts/db_cli.py snapshots 06.08.2026
|
||||||
python scripts/db_cli.py scud 06.08.2026 --export-xlsx срез_четверг
|
python scripts/db_cli.py scud 06.08.2026 --export-xlsx срез_четверг
|
||||||
|
python scripts/db_cli.py in_building 07.09.2026
|
||||||
|
python scripts/db_cli.py in_building 07.09.2026 --all
|
||||||
python scripts/db_cli.py absences 07.08.2026
|
python scripts/db_cli.py absences 07.08.2026
|
||||||
python scripts/db_cli.py prompts
|
python scripts/db_cli.py prompts
|
||||||
python scripts/db_cli.py tools
|
python scripts/db_cli.py tools
|
||||||
@@ -509,11 +493,15 @@ def main():
|
|||||||
parser.add_argument('command', nargs='?', default=None, choices=[
|
parser.add_argument('command', nargs='?', default=None, choices=[
|
||||||
'stats', 'snapshots', 'scud', 'absences', 'anomalies',
|
'stats', 'snapshots', 'scud', 'absences', 'anomalies',
|
||||||
'rules', 'prompts', 'sessions', 'dump', 'snapshot',
|
'rules', 'prompts', 'sessions', 'dump', 'snapshot',
|
||||||
'tools', 'context', 'exceptions'
|
'tools', 'context', 'in_building', 'exceptions'
|
||||||
], help="Основная команда")
|
], help="Основная команда")
|
||||||
parser.add_argument('action', nargs='?', default=None, help="Действие ('del', 'purge', 'add', 'sync') или дата/сессия")
|
parser.add_argument('action', nargs='?', default=None, help="Действие ('del', 'purge', 'add', 'sync') или дата/сессия")
|
||||||
parser.add_argument('param', nargs='?', default=None, help="Параметр (дата, ID снапшота, session_id, имя файла)")
|
parser.add_argument('param', nargs='?', default=None, help="Параметр (дата, ID снапшота, session_id, имя файла)")
|
||||||
parser.add_argument('-c', '--category', type=str, default=None, choices=['departments', 'positions', 'fio', 'position_keywords', 'include_fio'], help="Категория исключения")
|
parser.add_argument('-c', '--category', type=str, default=None, choices=[
|
||||||
|
'departments', 'positions', 'fio', 'position_keywords',
|
||||||
|
'include_fio', 'turnstile_fio', 'turnstile_departments',
|
||||||
|
'fligel_fio', 'fligel_departments'
|
||||||
|
], help="Категория исключения")
|
||||||
parser.add_argument('-v', '--value', type=str, default=None, help="Значение исключения (ФИО, отдел, должность)")
|
parser.add_argument('-v', '--value', type=str, default=None, help="Значение исключения (ФИО, отдел, должность)")
|
||||||
parser.add_argument('-m', '--comment', type=str, default="", help="Комментарий к исключению")
|
parser.add_argument('-m', '--comment', type=str, default="", help="Комментарий к исключению")
|
||||||
parser.add_argument('--snapshot', type=str, default=None, help="ID конкретного снапшота для инспекции")
|
parser.add_argument('--snapshot', type=str, default=None, help="ID конкретного снапшота для инспекции")
|
||||||
@@ -549,6 +537,9 @@ def main():
|
|||||||
print_session_states()
|
print_session_states()
|
||||||
elif args.command == 'tools':
|
elif args.command == 'tools':
|
||||||
print_tool_actions()
|
print_tool_actions()
|
||||||
|
elif args.command in ('in_building', 'presence'):
|
||||||
|
target_date = args.action if args.action else datetime.now().strftime("%d.%m.%Y")
|
||||||
|
print_building_presence(target_date, all_statuses=args.all)
|
||||||
elif args.command == 'context':
|
elif args.command == 'context':
|
||||||
if args.action in ['purge', 'clear']:
|
if args.action in ['purge', 'clear']:
|
||||||
is_all = args.all or (args.param == '--all')
|
is_all = args.all or (args.param == '--all')
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
"""
|
"""
|
||||||
===============================================================================
|
===============================================================================
|
||||||
FILE: scripts/diagnostics/make_etl_snapshot.py
|
FILE: scripts/diagnostics/make_etl_snapshot.py
|
||||||
ROLE: Генерация компактного слепка ETL-конвейера, БД и сервисов СКУД.
|
ROLE: Компактная динамическая генерация слепка ETL-конвейера, сервисов и БД.
|
||||||
|
Исключает исторические манифесты docs, диагностический шум и пустые файлы.
|
||||||
===============================================================================
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -11,46 +12,93 @@ import os
|
|||||||
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||||
OUTPUT_FILE = os.path.join(ROOT_DIR, "etl_code_snapshot.md")
|
OUTPUT_FILE = os.path.join(ROOT_DIR, "etl_code_snapshot.md")
|
||||||
|
|
||||||
# Строгий целевой список файлов для ETL-слепка
|
# 1. Отдельные ключевые файлы в корне проекта
|
||||||
TARGET_FILES = [
|
ROOT_EXPLICIT_FILES = [
|
||||||
"config.py",
|
"config.py",
|
||||||
"exceptions.json",
|
"exceptions.json",
|
||||||
"main_etl.py",
|
"main_etl.py"
|
||||||
"run_cron_etl.sh",
|
|
||||||
"scripts/db_cli.py", # ⭐️ Гарантированно включен
|
|
||||||
"core/connection.py",
|
|
||||||
"core/database.py",
|
|
||||||
"core/schema.py",
|
|
||||||
"core/repositories/scud_repo.py",
|
|
||||||
"core/repositories/zup_repo.py",
|
|
||||||
"services/data_loader.py",
|
|
||||||
"services/scud_export.py",
|
|
||||||
"services/share_copier.py",
|
|
||||||
"services/excel_exporter.py",
|
|
||||||
"services/text_reporter.py",
|
|
||||||
"services/exceptions_repo.py",
|
|
||||||
"services/zup_extractor.py",
|
|
||||||
"services/ai_verifier.py",
|
|
||||||
"services/knowledge_base.py",
|
|
||||||
"services/knowledge/service.py",
|
|
||||||
"services/scud_etl/pipeline.py",
|
|
||||||
"services/scud_etl/merger.py",
|
|
||||||
"services/scud_etl/anomaly_detector.py",
|
|
||||||
"services/snapshots/service.py",
|
|
||||||
"services/tasks/repository.py",
|
|
||||||
"services/tasks/service.py"
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# 2. Директории для автоматического сканирования
|
||||||
|
INCLUDED_DIRS = [
|
||||||
|
"core",
|
||||||
|
"services",
|
||||||
|
"scripts",
|
||||||
|
"docs"
|
||||||
|
]
|
||||||
|
|
||||||
|
# 3. Разрешенные расширения файлов
|
||||||
|
ALLOWED_EXTENSIONS = {
|
||||||
|
".py": "py",
|
||||||
|
".sh": "bash",
|
||||||
|
".json": "json",
|
||||||
|
".md": "markdown"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 4. Папки, которые категорически игнорируются
|
||||||
|
IGNORE_DIRS = {
|
||||||
|
"venv", ".venv", ".git", "__pycache__", "data", "output", "logs",
|
||||||
|
"node_modules", "static", ".idea", ".vscode", "diagnostics"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 5. Файлы, исключаемые для экономии контекста (тяжелые исторические манифесты и временные дампы)
|
||||||
|
IGNORE_FILES = {
|
||||||
|
"etl_code_snapshot.md",
|
||||||
|
"web_api_code_snapshot.md",
|
||||||
|
"db_dump_full.xlsx",
|
||||||
|
# Исключаем исторические манифесты из docs/ (~60 КБ дублирующего текста)
|
||||||
|
"PROJECT BRAIN_ SCUD Orion AI & Context API (Master Manifesto v5.0).md",
|
||||||
|
"SCUD Orion AI — Полная энциклопедическая хроника, архитектурный паспорт и технический контекст (v4.0).md"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def collect_target_files():
|
||||||
|
"""Автоматически собирает список файлов проекта без шума и устаревших манифестов."""
|
||||||
|
target_files = []
|
||||||
|
|
||||||
|
# Добавляем ключевые файлы из корня
|
||||||
|
for fname in ROOT_EXPLICIT_FILES:
|
||||||
|
fpath = os.path.join(ROOT_DIR, fname)
|
||||||
|
if os.path.isfile(fpath):
|
||||||
|
target_files.append(fname)
|
||||||
|
|
||||||
|
# Рекурсивный обход разрешенных каталогов
|
||||||
|
for d_name in INCLUDED_DIRS:
|
||||||
|
base_dir = os.path.join(ROOT_DIR, d_name)
|
||||||
|
if not os.path.exists(base_dir):
|
||||||
|
continue
|
||||||
|
|
||||||
|
for root, dirs, files in os.walk(base_dir):
|
||||||
|
dirs[:] = [d for d in dirs if d not in IGNORE_DIRS and not d.startswith(".")]
|
||||||
|
|
||||||
|
for file in sorted(files):
|
||||||
|
if file in IGNORE_FILES or file.startswith("."):
|
||||||
|
continue
|
||||||
|
|
||||||
|
_, ext = os.path.splitext(file)
|
||||||
|
if ext.lower() in ALLOWED_EXTENSIONS:
|
||||||
|
full_path = os.path.join(root, file)
|
||||||
|
|
||||||
|
# Пропускаем пустые __init__.py (0 байт)
|
||||||
|
if file == "__init__.py" and os.path.getsize(full_path) == 0:
|
||||||
|
continue
|
||||||
|
|
||||||
|
rel_path = os.path.relpath(full_path, ROOT_DIR)
|
||||||
|
target_files.append(rel_path)
|
||||||
|
|
||||||
|
return sorted(target_files)
|
||||||
|
|
||||||
|
|
||||||
def create_etl_snapshot():
|
def create_etl_snapshot():
|
||||||
content = ["# 📦 ETL-СЛЕПОК ИСХОДНОГО КОДА (СКУД ⟷ 1С & DB CORE)\n"]
|
files_to_pack = collect_target_files()
|
||||||
|
content = ["# 📦 КОМПАКТНЫЙ ETL-СЛЕПОК ИСХОДНОГО КОДА (СКУД ⟷ 1С & DB CORE)\n"]
|
||||||
included_count = 0
|
included_count = 0
|
||||||
|
|
||||||
for rel_path in TARGET_FILES:
|
for rel_path in files_to_pack:
|
||||||
full_path = os.path.join(ROOT_DIR, rel_path)
|
full_path = os.path.join(ROOT_DIR, rel_path)
|
||||||
if os.path.exists(full_path):
|
ext = os.path.splitext(rel_path)[1].lower()
|
||||||
ext = os.path.splitext(rel_path)[1].replace(".", "")
|
lang = ALLOWED_EXTENSIONS.get(ext, "text")
|
||||||
lang = "py" if ext == "py" else ("json" if ext == "json" else "bash")
|
|
||||||
try:
|
try:
|
||||||
with open(full_path, "r", encoding="utf-8") as f:
|
with open(full_path, "r", encoding="utf-8") as f:
|
||||||
file_text = f.read()
|
file_text = f.read()
|
||||||
@@ -58,14 +106,12 @@ def create_etl_snapshot():
|
|||||||
included_count += 1
|
included_count += 1
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[⚠️] Ошибка чтения {rel_path}: {e}")
|
print(f"[⚠️] Ошибка чтения {rel_path}: {e}")
|
||||||
else:
|
|
||||||
print(f"[ℹ️] Пропущен отсутствующий файл: {rel_path}")
|
|
||||||
|
|
||||||
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
|
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
|
||||||
f.write("\n".join(content))
|
f.write("\n".join(content))
|
||||||
|
|
||||||
size_kb = os.path.getsize(OUTPUT_FILE) / 1024
|
size_kb = os.path.getsize(OUTPUT_FILE) / 1024
|
||||||
print(f"\n[✓] ETL-слепок создан: {OUTPUT_FILE}")
|
print(f"\n[✓] Компактный ETL-слепок успешно создан: {OUTPUT_FILE}")
|
||||||
print(f" Включено файлов: {included_count} | Размер: {size_kb:.1f} KB\n")
|
print(f" Включено файлов: {included_count} | Размер: {size_kb:.1f} KB\n")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,47 +2,76 @@
|
|||||||
"""
|
"""
|
||||||
===============================================================================
|
===============================================================================
|
||||||
FILE: scripts/diagnostics/make_web_snapshot.py
|
FILE: scripts/diagnostics/make_web_snapshot.py
|
||||||
ROLE: Генерация слепка Web API, LLM-движка и клиентских скриптов.
|
ROLE: Динамическая автоматическая генерация слепка Web API, фронтенда и LLM.
|
||||||
|
Исключает временный кэш, авто-обнаруживает новые модули sidebar и роутеры.
|
||||||
===============================================================================
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||||
|
WEB_API_DIR = os.path.join(ROOT_DIR, "modules", "web_api")
|
||||||
OUTPUT_FILE = os.path.join(ROOT_DIR, "web_api_code_snapshot.md")
|
OUTPUT_FILE = os.path.join(ROOT_DIR, "web_api_code_snapshot.md")
|
||||||
|
|
||||||
WEB_TARGET_FILES = [
|
ALLOWED_EXTENSIONS = {
|
||||||
"modules/web_api/main.py",
|
".py": "py",
|
||||||
"modules/web_api/routers/chat.py",
|
".js": "js",
|
||||||
"modules/web_api/routers/auth.py",
|
".html": "html",
|
||||||
"modules/web_api/routers/tasks.py",
|
".css": "css",
|
||||||
"modules/web_api/routers/exceptions.py",
|
".json": "json"
|
||||||
"modules/web_api/routers/admin.py",
|
}
|
||||||
"modules/web_api/routers/files.py",
|
|
||||||
"modules/web_api/llm/agent.py",
|
IGNORE_DIRS = {
|
||||||
"modules/web_api/llm/db_tools.py",
|
"__pycache__", ".git", "venv", ".venv", "uploads", "logs",
|
||||||
"modules/web_api/llm/schemas.py",
|
"node_modules", ".idea", ".vscode"
|
||||||
"modules/web_api/llm/core/context_manager.py",
|
}
|
||||||
"modules/web_api/llm/core/tool_injector.py",
|
|
||||||
"modules/web_api/llm/core/ollama_client.py",
|
IGNORE_FILES = {
|
||||||
"modules/web_api/llm/core/fast_path.py",
|
"web_api_code_snapshot.md",
|
||||||
"modules/web_api/static/js/app.js",
|
"favicon.ico"
|
||||||
"modules/web_api/static/js/auth.js",
|
}
|
||||||
"modules/web_api/static/js/tasks.js",
|
|
||||||
"modules/web_api/static/js/chat/core.js",
|
|
||||||
"modules/web_api/static/js/chat/task_widget.js"
|
def collect_web_files():
|
||||||
]
|
"""Рекурсивно собирает все исходники web_api без необходимости хардкода."""
|
||||||
|
target_files = []
|
||||||
|
|
||||||
|
if not os.path.exists(WEB_API_DIR):
|
||||||
|
print(f"[❌] Директория не найдена: {WEB_API_DIR}")
|
||||||
|
return []
|
||||||
|
|
||||||
|
for root, dirs, files in os.walk(WEB_API_DIR):
|
||||||
|
# Исключаем служебные папки
|
||||||
|
dirs[:] = [d for d in dirs if d not in IGNORE_DIRS and not d.startswith(".")]
|
||||||
|
|
||||||
|
for file in sorted(files):
|
||||||
|
if file in IGNORE_FILES or file.startswith("."):
|
||||||
|
continue
|
||||||
|
|
||||||
|
_, ext = os.path.splitext(file)
|
||||||
|
if ext.lower() in ALLOWED_EXTENSIONS:
|
||||||
|
full_path = os.path.join(root, file)
|
||||||
|
|
||||||
|
# Пропускаем пустые __init__.py (0 байт)
|
||||||
|
if file == "__init__.py" and os.path.getsize(full_path) == 0:
|
||||||
|
continue
|
||||||
|
|
||||||
|
rel_path = os.path.relpath(full_path, ROOT_DIR)
|
||||||
|
target_files.append(rel_path)
|
||||||
|
|
||||||
|
return sorted(target_files)
|
||||||
|
|
||||||
|
|
||||||
def create_web_snapshot():
|
def create_web_snapshot():
|
||||||
content = ["# 🌐 WEB API & LLM AGENT CODE SNAPSHOT\n"]
|
files_to_pack = collect_web_files()
|
||||||
|
content = ["# 🌐 WEB API & FRONTEND CODE SNAPSHOT (AUTO-DISCOVERY)\n"]
|
||||||
included_count = 0
|
included_count = 0
|
||||||
|
|
||||||
for rel_path in WEB_TARGET_FILES:
|
for rel_path in files_to_pack:
|
||||||
full_path = os.path.join(ROOT_DIR, rel_path)
|
full_path = os.path.join(ROOT_DIR, rel_path)
|
||||||
if os.path.exists(full_path):
|
ext = os.path.splitext(rel_path)[1].lower()
|
||||||
ext = os.path.splitext(rel_path)[1].replace(".", "")
|
lang = ALLOWED_EXTENSIONS.get(ext, "text")
|
||||||
lang = "js" if ext == "js" else ("py" if ext == "py" else "text")
|
|
||||||
try:
|
try:
|
||||||
with open(full_path, "r", encoding="utf-8") as f:
|
with open(full_path, "r", encoding="utf-8") as f:
|
||||||
file_text = f.read()
|
file_text = f.read()
|
||||||
@@ -55,7 +84,7 @@ def create_web_snapshot():
|
|||||||
f.write("\n".join(content))
|
f.write("\n".join(content))
|
||||||
|
|
||||||
size_kb = os.path.getsize(OUTPUT_FILE) / 1024
|
size_kb = os.path.getsize(OUTPUT_FILE) / 1024
|
||||||
print(f"\n[✓] Web API слепок создан: {OUTPUT_FILE}")
|
print(f"\n[✓] Web API + Фронтенд слепок успешно создан: {OUTPUT_FILE}")
|
||||||
print(f" Включено файлов: {included_count} | Размер: {size_kb:.1f} KB\n")
|
print(f" Включено файлов: {included_count} | Размер: {size_kb:.1f} KB\n")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+95
-13
@@ -7,6 +7,7 @@ ROLE: Надежная загрузка штата и отсутствий (MS S
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import warnings
|
import warnings
|
||||||
|
from datetime import datetime # <-- ДОБАВИТЬ ЭТУ СТРОКУ
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from config import (
|
from config import (
|
||||||
normalize_fio, clean_scud_fio_light, load_exceptions,
|
normalize_fio, clean_scud_fio_light, load_exceptions,
|
||||||
@@ -101,31 +102,112 @@ def load_absent_data(date_str):
|
|||||||
if df_absent is None:
|
if df_absent is None:
|
||||||
df_absent = pd.DataFrame(columns=['fio_clean', 'Вид_отсутствия'])
|
df_absent = pd.DataFrame(columns=['fio_clean', 'Вид_отсутствия'])
|
||||||
|
|
||||||
# Обогащение удаленщиками из static_reason_workers.csv
|
# Обогащение удаленщиками из static_reason_workers.csv с ротацией просроченных записей
|
||||||
try:
|
try:
|
||||||
static_path = os.path.join(DATA_DIR, "static_reason_workers.csv")
|
static_path = os.path.join(DATA_DIR, "static_reason_workers.csv")
|
||||||
if os.path.exists(static_path):
|
if os.path.exists(static_path):
|
||||||
df_static = pd.read_csv(static_path, encoding='utf-8')
|
df_static = pd.read_csv(static_path, dtype=str, on_bad_lines='skip').fillna("")
|
||||||
if 'fio' in df_static.columns and 'reason' in df_static.columns:
|
|
||||||
df_static['fio_clean'] = df_static['fio'].apply(normalize_fio)
|
for col in ['fio', 'reason', 'department', 'date_from', 'date_to']:
|
||||||
|
if col not in df_static.columns:
|
||||||
|
df_static[col] = ""
|
||||||
|
|
||||||
|
today_date = datetime.now().date()
|
||||||
|
|
||||||
|
# Разбор целевой даты отчета
|
||||||
|
clean_target_str = str(date_str).replace('_', '.')
|
||||||
|
try:
|
||||||
|
clean_target_date = datetime.strptime(clean_target_str, "%d.%m.%Y").date()
|
||||||
|
except ValueError:
|
||||||
|
clean_target_date = today_date
|
||||||
|
|
||||||
|
def parse_date_safe(d_val):
|
||||||
|
if not d_val or str(d_val).lower() in ['nan', 'none', '', 'nat']:
|
||||||
|
return None
|
||||||
|
s = str(d_val).strip().replace('_', '.')
|
||||||
|
for fmt in ("%d.%m.%Y", "%Y-%m-%d"):
|
||||||
|
try:
|
||||||
|
return datetime.strptime(s, fmt).date()
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
return None
|
||||||
|
|
||||||
|
active_for_file = []
|
||||||
|
new_rows_for_report = []
|
||||||
|
file_changed = False
|
||||||
|
|
||||||
existing_fios = set(df_absent['fio_clean'].dropna().tolist()) if not df_absent.empty else set()
|
existing_fios = set(df_absent['fio_clean'].dropna().tolist()) if not df_absent.empty else set()
|
||||||
|
|
||||||
new_rows = []
|
|
||||||
for _, s_row in df_static.iterrows():
|
for _, s_row in df_static.iterrows():
|
||||||
if s_row['fio_clean'] not in existing_fios:
|
fio_raw = str(s_row.get('fio', '')).strip()
|
||||||
new_rows.append({
|
if not fio_raw:
|
||||||
'fio_clean': s_row['fio_clean'],
|
continue
|
||||||
'Вид_отсутствия': s_row['reason']
|
|
||||||
|
fio_c = normalize_fio(fio_raw)
|
||||||
|
reason = str(s_row.get('reason', '')).strip() or "Дистанционная работа"
|
||||||
|
|
||||||
|
d_from = parse_date_safe(s_row.get('date_from'))
|
||||||
|
d_to = parse_date_safe(s_row.get('date_to'))
|
||||||
|
|
||||||
|
# 1. Физическая ротация просроченных: только если текущий реальный день (today) строго больше date_to
|
||||||
|
if d_to is not None and today_date > d_to:
|
||||||
|
print(f" [🧹] Удаленка истекла: {fio_raw} (до {d_to.strftime('%d.%m.%Y')}). Удалена из CSV.")
|
||||||
|
file_changed = True
|
||||||
|
continue
|
||||||
|
|
||||||
|
active_for_file.append(s_row.to_dict())
|
||||||
|
|
||||||
|
# 2. Проверка действия удаленки на дату формируемого отчета:
|
||||||
|
# Если date_from не указана — действует всегда до date_to
|
||||||
|
is_after_start = (d_from is None) or (clean_target_date >= d_from)
|
||||||
|
is_before_end = (d_to is None) or (clean_target_date <= d_to)
|
||||||
|
|
||||||
|
if is_after_start and is_before_end:
|
||||||
|
if fio_c not in existing_fios:
|
||||||
|
new_rows_for_report.append({
|
||||||
|
'fio_clean': fio_c,
|
||||||
|
'Вид_отсутствия': reason
|
||||||
})
|
})
|
||||||
if new_rows:
|
existing_fios.add(fio_c)
|
||||||
df_absent = pd.concat([df_absent, pd.DataFrame(new_rows)], ignore_index=True)
|
|
||||||
print(f" [✓] Реестр удаленщиков: добавлено {len(new_rows)} чел. из static_reason_workers.csv")
|
# Перезаписываем CSV только если реально были удалены просроченные сотрудники
|
||||||
|
if file_changed:
|
||||||
|
pd.DataFrame(active_for_file).to_csv(static_path, index=False, encoding='utf-8')
|
||||||
|
print(" [✓] Файл static_reason_workers.csv синхронизирован без просроченных записей.")
|
||||||
|
|
||||||
|
if new_rows_for_report:
|
||||||
|
df_absent = pd.concat([df_absent, pd.DataFrame(new_rows_for_report)], ignore_index=True)
|
||||||
|
print(f" [✓] Реестр удаленщиков: добавлено {len(new_rows_for_report)} чел. в отчет за {date_str}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f" [⚠️] Ошибка чтения static_reason_workers.csv: {e}")
|
print(f" [⚠️] Ошибка обработки static_reason_workers.csv: {e}")
|
||||||
|
|
||||||
|
# Обогащение реестрами "Мест. командир." и "Иное"
|
||||||
|
try:
|
||||||
|
from services.manual_absences_repo import get_active_manual_absences_for_date
|
||||||
|
manual_records = get_active_manual_absences_for_date(date_str)
|
||||||
|
if manual_records:
|
||||||
|
existing_fios = set(df_absent['fio_clean'].dropna().tolist()) if not df_absent.empty else set()
|
||||||
|
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 "Иное"
|
||||||
|
manual_rows.append({
|
||||||
|
'fio_clean': fc,
|
||||||
|
'Вид_отсутствия': label
|
||||||
|
})
|
||||||
|
existing_fios.add(fc)
|
||||||
|
if manual_rows:
|
||||||
|
df_absent = pd.concat([df_absent, pd.DataFrame(manual_rows)], ignore_index=True)
|
||||||
|
print(f" [✓] Реестры 'Мест. командир.' / 'Иное': добавлено {len(manual_rows)} чел. в отчет за {date_str}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f" [⚠️] Ошибка применения manual_absences: {e}")
|
||||||
|
|
||||||
return df_absent if not df_absent.empty else None
|
return df_absent if not df_absent.empty else None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def load_1c_data_smart(date_str, use_db=False):
|
def load_1c_data_smart(date_str, use_db=False):
|
||||||
df_staff = None
|
df_staff = None
|
||||||
df_absent = None
|
df_absent = None
|
||||||
|
|||||||
+14
-444
@@ -1,449 +1,19 @@
|
|||||||
import math
|
|
||||||
import os
|
|
||||||
import time
|
|
||||||
import openpyxl
|
|
||||||
import pandas as pd
|
|
||||||
from datetime import datetime, timedelta
|
|
||||||
from openpyxl import Workbook
|
|
||||||
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
|
|
||||||
from openpyxl.utils import get_column_letter
|
|
||||||
from config import REPORTS_DIR
|
|
||||||
|
|
||||||
MONTHS_RU_GENITIVE = {
|
|
||||||
1: "января", 2: "февраля", 3: "марта", 4: "апреля",
|
|
||||||
5: "мая", 6: "июня", 7: "июля", 8: "августа",
|
|
||||||
9: "сентября", 10: "октября", 11: "ноября", 12: "декабря"
|
|
||||||
}
|
|
||||||
|
|
||||||
MONTHS_RU_NOMINATIVE = {
|
|
||||||
1: "январь", 2: "февраль", 3: "март", 4: "апрель",
|
|
||||||
5: "май", 6: "июнь", 7: "июль", 8: "август",
|
|
||||||
9: "сентябрь", 10: "октябрь", 11: "ноябрь", 12: "декабрь"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def format_date_ru(date_str):
|
|
||||||
date_clean = str(date_str).replace('_', '.')
|
|
||||||
try:
|
|
||||||
dt = datetime.strptime(date_clean, "%d.%m.%Y")
|
|
||||||
return f"{dt.day} {MONTHS_RU_GENITIVE[dt.month]} {dt.year}"
|
|
||||||
except Exception:
|
|
||||||
return date_str
|
|
||||||
|
|
||||||
|
|
||||||
def get_dated_reports_dir(date_str):
|
|
||||||
date_clean = str(date_str).replace('_', '.')
|
|
||||||
try:
|
|
||||||
dt = datetime.strptime(date_clean, "%d.%m.%Y")
|
|
||||||
year_str = str(dt.year)
|
|
||||||
month_name = MONTHS_RU_NOMINATIVE[dt.month]
|
|
||||||
except Exception:
|
|
||||||
now = datetime.now()
|
|
||||||
year_str = str(now.year)
|
|
||||||
month_name = MONTHS_RU_NOMINATIVE[now.month]
|
|
||||||
|
|
||||||
target_dir = os.path.join(REPORTS_DIR, year_str, month_name)
|
|
||||||
os.makedirs(target_dir, exist_ok=True)
|
|
||||||
return target_dir
|
|
||||||
|
|
||||||
|
|
||||||
THIN_SIDE = Side(border_style="thin", color="D3D3D3")
|
|
||||||
THIN_BORDER = Border(left=THIN_SIDE, right=THIN_SIDE, top=THIN_SIDE, bottom=THIN_SIDE)
|
|
||||||
|
|
||||||
FILL_HEADER = PatternFill(start_color="D9E1F2", end_color="D9E1F2", fill_type="solid")
|
|
||||||
FILL_TOTAL_LIST = PatternFill(start_color="F2F2F2", end_color="F2F2F2", fill_type="solid")
|
|
||||||
FILL_UNEXPLAINED = PatternFill(start_color="FCE4D6", end_color="FCE4D6", fill_type="solid")
|
|
||||||
FILL_PRESENT = PatternFill(start_color="E2EFDA", end_color="E2EFDA", fill_type="solid")
|
|
||||||
FILL_REMOTE = PatternFill(start_color="E8F8F5", end_color="E8F8F5", fill_type="solid")
|
|
||||||
FILL_ANOMALY = PatternFill(start_color="FCE4D6", end_color="FCE4D6", fill_type="solid")
|
|
||||||
FILL_NO_PASS = PatternFill(start_color="E1F5FE", end_color="E1F5FE", fill_type="solid")
|
|
||||||
FILL_EXCEPTIONS = PatternFill(start_color="EFEBE9", end_color="EFEBE9", fill_type="solid")
|
|
||||||
|
|
||||||
CATEGORY_PASTEL_COLORS = ["FFF2CC", "E1D5E7", "E1F5FE", "FFF0F5", "FCF3CF"]
|
|
||||||
YELLOW_FILL = PatternFill(start_color="FFF2CC", end_color="FFF2CC", fill_type="solid")
|
|
||||||
LIGHT_RED_FILL = PatternFill(start_color="FCE4D6", end_color="FCE4D6", fill_type="solid")
|
|
||||||
GREEN_FILL = PatternFill(start_color="E2EFDA", end_color="E2EFDA", fill_type="solid")
|
|
||||||
|
|
||||||
|
|
||||||
def calculate_autoclose_time(time_in_str: str) -> tuple[str, str, str]:
|
|
||||||
"""
|
"""
|
||||||
⭐️ Правило 8.5ч: при наличии входа и отсутствии выхода
|
===============================================================================
|
||||||
рассчитывает время выхода (Вход + 8ч 30мин) с нормой 8:00 и отклонением 0:00.
|
FILE: services/excel_exporter.py
|
||||||
|
ROLE: Единый фасад генераторов Excel-отчетов (обратная совместимость).
|
||||||
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
parts = time_in_str.strip().split(':')
|
|
||||||
hh = int(parts[0])
|
|
||||||
mm = int(parts[1]) if len(parts) > 1 else 0
|
|
||||||
ss = int(parts[2]) if len(parts) > 2 else 0
|
|
||||||
|
|
||||||
dt_in = datetime(2000, 1, 1, hh, mm, ss)
|
from services.reports.styles import (
|
||||||
dt_out = dt_in + timedelta(hours=8, minutes=30)
|
format_date_ru,
|
||||||
return dt_out.strftime("%H:%M:%S"), "08:30", "0:00"
|
get_dated_reports_dir,
|
||||||
except Exception:
|
safe_close_workbook,
|
||||||
return "17:00:00", "08:30", "0:00"
|
create_xlsx_format
|
||||||
|
|
||||||
|
|
||||||
def calculate_deviation(time_in_building_str, reason="", norm_hours=8, lunch_minutes=30):
|
|
||||||
if pd.notna(reason) and isinstance(reason, str) and reason.strip() != "":
|
|
||||||
return "0:00"
|
|
||||||
|
|
||||||
if not isinstance(time_in_building_str, str) or time_in_building_str in ['00:00', '0', '', 'None', 'nan', 'NaN']:
|
|
||||||
return f"-{norm_hours}:00"
|
|
||||||
|
|
||||||
try:
|
|
||||||
parts = time_in_building_str.strip().split(':')
|
|
||||||
hh = int(parts[0])
|
|
||||||
mm = int(parts[1]) if len(parts) > 1 else 0
|
|
||||||
total_in_building_minutes = hh * 60 + mm
|
|
||||||
|
|
||||||
if total_in_building_minutes == 0:
|
|
||||||
return f"-{norm_hours}:00"
|
|
||||||
|
|
||||||
work_minutes = max(0, total_in_building_minutes - lunch_minutes)
|
|
||||||
norm_minutes = norm_hours * 60
|
|
||||||
diff = work_minutes - norm_minutes
|
|
||||||
|
|
||||||
if diff == 0:
|
|
||||||
return "0:00"
|
|
||||||
|
|
||||||
sign = "-" if diff < 0 else ""
|
|
||||||
abs_diff = abs(diff)
|
|
||||||
res_hh = abs_diff // 60
|
|
||||||
res_mm = abs_diff % 60
|
|
||||||
|
|
||||||
return f"{sign}{res_hh}:{res_mm:02d}"
|
|
||||||
except Exception:
|
|
||||||
return f"-{norm_hours}:00"
|
|
||||||
|
|
||||||
|
|
||||||
def apply_borders_to_cell(cell, border=THIN_BORDER):
|
|
||||||
cell.border = border
|
|
||||||
|
|
||||||
|
|
||||||
def format_row_cells(ws, r_num, fill_obj, is_bold=False, align_b="right", bold_font=None, wrap_b=False):
|
|
||||||
cell_a = ws.cell(row=r_num, column=1)
|
|
||||||
cell_b = ws.cell(row=r_num, column=2)
|
|
||||||
if fill_obj:
|
|
||||||
cell_a.fill = fill_obj
|
|
||||||
cell_b.fill = fill_obj
|
|
||||||
apply_borders_to_cell(cell_a)
|
|
||||||
apply_borders_to_cell(cell_b)
|
|
||||||
if is_bold and bold_font:
|
|
||||||
cell_a.font = bold_font
|
|
||||||
cell_b.font = bold_font
|
|
||||||
if align_b:
|
|
||||||
cell_b.alignment = Alignment(horizontal=align_b, vertical="center", wrap_text=wrap_b)
|
|
||||||
|
|
||||||
|
|
||||||
# --- 1. СВОДКА НА СЕГОДНЯ ---
|
|
||||||
def generate_summary_excel(merged_df, date_str="21.08.2026", filename=None):
|
|
||||||
date_clean = str(date_str).replace('_', '.')
|
|
||||||
if not filename:
|
|
||||||
filename = f"{format_date_ru(date_clean)} сводка.xlsx"
|
|
||||||
|
|
||||||
target_dir = get_dated_reports_dir(date_clean)
|
|
||||||
output_path = os.path.join(target_dir, filename)
|
|
||||||
wb = Workbook()
|
|
||||||
ws = wb.active
|
|
||||||
ws.title = "Лист_1"
|
|
||||||
|
|
||||||
ws.sheet_properties.outlinePr.summaryBelow = False
|
|
||||||
ws.sheet_properties.outlinePr.summaryRight = False
|
|
||||||
ws.sheet_properties.outlinePr.showOutlineSymbols = True
|
|
||||||
ws.sheet_view.showOutlineSymbols = True
|
|
||||||
|
|
||||||
bold_font = Font(name="Calibri", size=11, bold=True)
|
|
||||||
|
|
||||||
ws.cell(row=1, column=1, value="Сводка на")
|
|
||||||
ws.cell(row=1, column=2, value=date_clean)
|
|
||||||
format_row_cells(ws, 1, FILL_HEADER, is_bold=True, bold_font=bold_font)
|
|
||||||
|
|
||||||
apply_borders_to_cell(ws.cell(row=2, column=1))
|
|
||||||
apply_borders_to_cell(ws.cell(row=2, column=2))
|
|
||||||
|
|
||||||
ws.cell(row=3, column=1, value="По списку")
|
|
||||||
ws.cell(row=3, column=2, value=len(merged_df))
|
|
||||||
format_row_cells(ws, 3, FILL_TOTAL_LIST, is_bold=True, bold_font=bold_font)
|
|
||||||
|
|
||||||
current_row = 4
|
|
||||||
|
|
||||||
is_no_pass = merged_df['no_scud_pass'] == True if 'no_scud_pass' in merged_df.columns else False
|
|
||||||
is_exc = merged_df.get('is_excluded', False) == True
|
|
||||||
|
|
||||||
unexplained = merged_df[
|
|
||||||
(merged_df['Пришел'] == False) &
|
|
||||||
(merged_df['Вид_отсутствия'].isna() | (merged_df['Вид_отсутствия'].astype(str).str.strip() == '')) &
|
|
||||||
(~is_no_pass) &
|
|
||||||
(~is_exc)
|
|
||||||
]
|
|
||||||
ws.cell(row=current_row, column=1, value="неизвестно")
|
|
||||||
ws.cell(row=current_row, column=2, value=len(unexplained))
|
|
||||||
format_row_cells(ws, current_row, FILL_UNEXPLAINED, is_bold=True, bold_font=bold_font)
|
|
||||||
current_row += 1
|
|
||||||
|
|
||||||
for fio in sorted(unexplained['Сотрудник'].dropna().unique()):
|
|
||||||
ws.cell(row=current_row, column=1, value=fio)
|
|
||||||
format_row_cells(ws, current_row, FILL_UNEXPLAINED, is_bold=False)
|
|
||||||
ws.row_dimensions[current_row].outlineLevel = 1
|
|
||||||
ws.row_dimensions[current_row].hidden = False
|
|
||||||
current_row += 1
|
|
||||||
|
|
||||||
no_pass_df = merged_df[is_no_pass] if 'no_scud_pass' in merged_df.columns else pd.DataFrame()
|
|
||||||
ws.cell(row=current_row, column=1, value="Нет пропуска")
|
|
||||||
ws.cell(row=current_row, column=2, value=len(no_pass_df))
|
|
||||||
format_row_cells(ws, current_row, FILL_NO_PASS, is_bold=True, bold_font=bold_font)
|
|
||||||
current_row += 1
|
|
||||||
|
|
||||||
if not no_pass_df.empty:
|
|
||||||
for fio in sorted(no_pass_df['Сотрудник'].dropna().unique()):
|
|
||||||
ws.cell(row=current_row, column=1, value=fio)
|
|
||||||
format_row_cells(ws, current_row, FILL_NO_PASS, is_bold=False)
|
|
||||||
ws.row_dimensions[current_row].outlineLevel = 1
|
|
||||||
ws.row_dimensions[current_row].hidden = False
|
|
||||||
current_row += 1
|
|
||||||
|
|
||||||
reason_clean = merged_df['Вид_отсутствия'].astype(str).str.lower()
|
|
||||||
is_remote_reason = reason_clean.str.contains('удален|дистанцион', regex=True, na=False)
|
|
||||||
|
|
||||||
absent_only = merged_df[
|
|
||||||
(merged_df['Пришел'] == False) &
|
|
||||||
(merged_df['Вид_отсутствия'].notna()) &
|
|
||||||
(~merged_df['Вид_отсутствия'].astype(str).str.startswith('Исключение')) &
|
|
||||||
(~is_remote_reason) &
|
|
||||||
(~is_exc)
|
|
||||||
]
|
|
||||||
absent_groups = absent_only.groupby('Вид_отсутствия')
|
|
||||||
|
|
||||||
for idx_cat, (cat_name, group) in enumerate(absent_groups):
|
|
||||||
hex_color = CATEGORY_PASTEL_COLORS[idx_cat % len(CATEGORY_PASTEL_COLORS)]
|
|
||||||
cat_fill = PatternFill(start_color=hex_color, end_color=hex_color, fill_type="solid")
|
|
||||||
|
|
||||||
ws.cell(row=current_row, column=1, value=cat_name)
|
|
||||||
ws.cell(row=current_row, column=2, value=len(group))
|
|
||||||
format_row_cells(ws, current_row, cat_fill, is_bold=True, bold_font=bold_font)
|
|
||||||
current_row += 1
|
|
||||||
|
|
||||||
for fio in sorted(group['Сотрудник'].dropna().unique()):
|
|
||||||
ws.cell(row=current_row, column=1, value=fio)
|
|
||||||
format_row_cells(ws, current_row, cat_fill, is_bold=False)
|
|
||||||
ws.row_dimensions[current_row].outlineLevel = 1
|
|
||||||
ws.row_dimensions[current_row].hidden = True
|
|
||||||
current_row += 1
|
|
||||||
|
|
||||||
present = merged_df[(merged_df['Пришел'] == True) & (~is_exc)]
|
|
||||||
ws.cell(row=current_row, column=1, value="Итого на работе")
|
|
||||||
ws.cell(row=current_row, column=2, value=len(present))
|
|
||||||
format_row_cells(ws, current_row, FILL_PRESENT, is_bold=True, bold_font=bold_font)
|
|
||||||
current_row += 1
|
|
||||||
|
|
||||||
if not present.empty:
|
|
||||||
for fio in sorted(present['Сотрудник'].dropna().unique()):
|
|
||||||
ws.cell(row=current_row, column=1, value=fio)
|
|
||||||
format_row_cells(ws, current_row, FILL_PRESENT, is_bold=False)
|
|
||||||
ws.row_dimensions[current_row].outlineLevel = 1
|
|
||||||
ws.row_dimensions[current_row].hidden = True
|
|
||||||
current_row += 1
|
|
||||||
|
|
||||||
remote_home = merged_df[(merged_df['Пришел'] == False) & is_remote_reason & (~is_exc)]
|
|
||||||
ws.cell(row=current_row, column=1, value="В том числе на удаленной работе")
|
|
||||||
ws.cell(row=current_row, column=2, value=len(remote_home))
|
|
||||||
format_row_cells(ws, current_row, FILL_REMOTE, is_bold=True, bold_font=bold_font)
|
|
||||||
current_row += 1
|
|
||||||
|
|
||||||
if not remote_home.empty:
|
|
||||||
for fio in sorted(remote_home['Сотрудник'].dropna().unique()):
|
|
||||||
ws.cell(row=current_row, column=1, value=fio)
|
|
||||||
format_row_cells(ws, current_row, FILL_REMOTE, is_bold=False)
|
|
||||||
ws.row_dimensions[current_row].outlineLevel = 1
|
|
||||||
ws.row_dimensions[current_row].hidden = True
|
|
||||||
current_row += 1
|
|
||||||
|
|
||||||
anomalies = merged_df[
|
|
||||||
(~is_exc) & (
|
|
||||||
((merged_df['Пришел'] == True) & (merged_df['Вид_отсутствия'].notna()) &
|
|
||||||
(~merged_df['Вид_отсутствия'].astype(str).str.startswith('Исключение')) &
|
|
||||||
(~is_remote_reason) &
|
|
||||||
(~reason_clean.str.contains('командировк', na=False))) |
|
|
||||||
(merged_df.get('anomaly_flag', 'NONE') == 'ANOMALY_NO_IN_HAS_ACTIVITY')
|
|
||||||
)
|
)
|
||||||
]
|
from services.reports.calculators import calculate_deviation
|
||||||
|
|
||||||
ws.cell(row=current_row, column=1, value="Аномалии СКУД и 1С")
|
# Реэкспорт функций генерации
|
||||||
ws.cell(row=current_row, column=2, value=len(anomalies))
|
from services.reports.svodka_builder import generate_summary_excel
|
||||||
format_row_cells(ws, current_row, FILL_ANOMALY, is_bold=True, bold_font=bold_font)
|
from services.reports.otchet_builder import generate_detailed_excel
|
||||||
current_row += 1
|
from services.reports.raw_scud_builder import export_raw_scud
|
||||||
|
|
||||||
chars_per_line_b = 30
|
|
||||||
if not anomalies.empty:
|
|
||||||
for _, row in anomalies.iterrows():
|
|
||||||
fio = row.get('Сотрудник', '')
|
|
||||||
reason = row.get('Вид_отсутствия', '')
|
|
||||||
anom_flag = row.get('anomaly_flag', 'NONE')
|
|
||||||
|
|
||||||
if anom_flag == 'ANOMALY_NO_IN_HAS_ACTIVITY':
|
|
||||||
first_act = row.get('Первая_активность', '—')
|
|
||||||
reason_text = f"🚨 АНОМАЛИЯ СКУД: Нет входа (первая активность: {first_act})"
|
|
||||||
else:
|
|
||||||
reason_text = f"В 1С: {reason}"
|
|
||||||
|
|
||||||
cell_a = ws.cell(row=current_row, column=1, value=f"{fio}")
|
|
||||||
cell_b = ws.cell(row=current_row, column=2, value=reason_text)
|
|
||||||
cell_a.fill = FILL_ANOMALY
|
|
||||||
cell_b.fill = FILL_ANOMALY
|
|
||||||
apply_borders_to_cell(cell_a)
|
|
||||||
apply_borders_to_cell(cell_b)
|
|
||||||
cell_b.alignment = Alignment(horizontal="left", vertical="center", wrap_text=True)
|
|
||||||
cell_a.alignment = Alignment(horizontal="left", vertical="center")
|
|
||||||
|
|
||||||
lines_count = math.ceil(len(reason_text) / chars_per_line_b) if len(reason_text) > chars_per_line_b else 1
|
|
||||||
ws.row_dimensions[current_row].height = max(lines_count * 18, 20)
|
|
||||||
ws.row_dimensions[current_row].outlineLevel = 1
|
|
||||||
ws.row_dimensions[current_row].hidden = True
|
|
||||||
current_row += 1
|
|
||||||
|
|
||||||
exceptions_df = merged_df[is_exc]
|
|
||||||
ws.cell(row=current_row, column=1, value="Исключения")
|
|
||||||
ws.cell(row=current_row, column=2, value=len(exceptions_df))
|
|
||||||
format_row_cells(ws, current_row, FILL_EXCEPTIONS, is_bold=True, bold_font=bold_font)
|
|
||||||
current_row += 1
|
|
||||||
|
|
||||||
if not exceptions_df.empty:
|
|
||||||
for fio in sorted(exceptions_df['Сотрудник'].dropna().unique()):
|
|
||||||
ws.cell(row=current_row, column=1, value=fio)
|
|
||||||
format_row_cells(ws, current_row, FILL_EXCEPTIONS, is_bold=False)
|
|
||||||
ws.row_dimensions[current_row].outlineLevel = 1
|
|
||||||
ws.row_dimensions[current_row].hidden = True
|
|
||||||
current_row += 1
|
|
||||||
|
|
||||||
ws.column_dimensions['A'].width = 45.0
|
|
||||||
ws.column_dimensions['B'].width = 38.0
|
|
||||||
|
|
||||||
try:
|
|
||||||
wb.save(output_path)
|
|
||||||
print(f"[✓] Ежедневная сводка сохранена: {output_path}")
|
|
||||||
except (PermissionError, OSError):
|
|
||||||
alt_filename = filename.replace(".xlsx", f"_{int(time.time())}.xlsx")
|
|
||||||
alt_path = os.path.join(target_dir, alt_filename)
|
|
||||||
wb.save(alt_path)
|
|
||||||
print(f"[⚠️] Файл открыт в Excel! Сохранено как: {alt_path}")
|
|
||||||
|
|
||||||
|
|
||||||
# --- 2. ДЕТАЛЬНЫЙ ОТЧЕТ ЗА ВЧЕРА (С ПРАВИЛОМ 8.5ч) ---
|
|
||||||
def generate_detailed_excel(merged_df, date_str="20.08.2026", filename=None):
|
|
||||||
date_clean = str(date_str).replace('_', '.')
|
|
||||||
if not filename:
|
|
||||||
filename = f"{format_date_ru(date_clean)} отчет.xlsx"
|
|
||||||
|
|
||||||
if merged_df is not None and not merged_df.empty:
|
|
||||||
df_export = merged_df[merged_df.get('is_excluded', False) == False].copy()
|
|
||||||
else:
|
|
||||||
df_export = pd.DataFrame()
|
|
||||||
|
|
||||||
target_dir = get_dated_reports_dir(date_clean)
|
|
||||||
output_path = os.path.join(target_dir, filename)
|
|
||||||
wb = Workbook()
|
|
||||||
ws = wb.active
|
|
||||||
ws.title = "Детальный_отчет"
|
|
||||||
|
|
||||||
ws["B2"] = "Дата:"
|
|
||||||
ws["D2"] = date_clean
|
|
||||||
ws["B2"].font = Font(name="Arial", size=10, bold=True)
|
|
||||||
ws["D2"].font = Font(name="Arial", size=10, bold=True)
|
|
||||||
|
|
||||||
headers = [
|
|
||||||
"№", "ФИО", "Подразделение", "время входа", "первая активность", "время выхода",
|
|
||||||
"находился в здании", "причина отсутствия", "норма", "отклонение от нормы"
|
|
||||||
]
|
|
||||||
ws.append([])
|
|
||||||
ws.append(headers)
|
|
||||||
|
|
||||||
header_fill = PatternFill(start_color="D9E1F2", end_color="D9E1F2", fill_type="solid")
|
|
||||||
for col_idx in range(1, len(headers) + 1):
|
|
||||||
cell = ws.cell(row=4, column=col_idx)
|
|
||||||
cell.fill = header_fill
|
|
||||||
cell.font = Font(name="Arial", size=10, bold=True)
|
|
||||||
cell.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
|
|
||||||
apply_borders_to_cell(cell)
|
|
||||||
|
|
||||||
start_col = 'Начало дня' if 'Начало дня' in df_export.columns else 'Начало_дня'
|
|
||||||
end_col = 'Конец дня' if 'Конец дня' in df_export.columns else 'Конец_дня'
|
|
||||||
hours_col = 'Часы' if 'Часы' in df_export.columns else 'Находился_в_здании'
|
|
||||||
|
|
||||||
chars_per_line_h = 24
|
|
||||||
|
|
||||||
for idx, row in df_export.reset_index(drop=True).iterrows():
|
|
||||||
is_present = row.get('Пришел', False)
|
|
||||||
absence_reason = row.get('Вид_отсутствия', '')
|
|
||||||
has_reason = pd.notna(absence_reason) and str(absence_reason).strip() != ''
|
|
||||||
|
|
||||||
in_val = str(row.get(start_col, 'Нет входа')).strip()
|
|
||||||
out_val = str(row.get(end_col, 'Нет выхода')).strip()
|
|
||||||
in_building_str = str(row.get(hours_col, '00:00'))
|
|
||||||
first_act_val = str(row.get('Первая_активность', '—')).strip()
|
|
||||||
has_first_act = first_act_val not in ['—', '', 'None', 'nan']
|
|
||||||
|
|
||||||
# ⭐️ ПРАВИЛО 8.5ч: если есть вход, но нет выхода (и нет официального документа отсутствия)
|
|
||||||
if in_val not in ['Нет входа', '—', '', 'nan', 'None'] and out_val in ['Нет выхода', '—', '', 'nan', 'None'] and not has_reason:
|
|
||||||
out_val, in_building_str, deviation_val = calculate_autoclose_time(in_val)
|
|
||||||
else:
|
|
||||||
deviation_val = calculate_deviation(in_building_str, reason=absence_reason if has_reason else "", norm_hours=8, lunch_minutes=30)
|
|
||||||
|
|
||||||
dept_scud_val = row.get('department_scud', row.get('department', row.get('Подразделение', '')))
|
|
||||||
|
|
||||||
ws.append([
|
|
||||||
idx + 1,
|
|
||||||
row.get('Сотрудник', ''),
|
|
||||||
dept_scud_val,
|
|
||||||
in_val,
|
|
||||||
first_act_val,
|
|
||||||
out_val,
|
|
||||||
in_building_str,
|
|
||||||
absence_reason if has_reason else '',
|
|
||||||
8,
|
|
||||||
deviation_val
|
|
||||||
])
|
|
||||||
|
|
||||||
row_num = 5 + idx
|
|
||||||
if is_present and has_reason:
|
|
||||||
row_fill = GREEN_FILL
|
|
||||||
elif not is_present and has_reason:
|
|
||||||
row_fill = YELLOW_FILL
|
|
||||||
elif not is_present and not has_reason and not has_first_act:
|
|
||||||
row_fill = LIGHT_RED_FILL
|
|
||||||
else:
|
|
||||||
row_fill = None
|
|
||||||
|
|
||||||
val_h_str = str(absence_reason) if has_reason else ""
|
|
||||||
lines_count = math.ceil(len(val_h_str) / chars_per_line_h) if len(val_h_str) > chars_per_line_h else 1
|
|
||||||
ws.row_dimensions[row_num].height = max(lines_count * 18, 20)
|
|
||||||
|
|
||||||
for col_idx in range(1, len(headers) + 1):
|
|
||||||
cell = ws.cell(row=row_num, column=col_idx)
|
|
||||||
apply_borders_to_cell(cell)
|
|
||||||
if row_fill:
|
|
||||||
cell.fill = row_fill
|
|
||||||
if col_idx == 8:
|
|
||||||
cell.alignment = Alignment(horizontal="left", vertical="center", wrap_text=True)
|
|
||||||
elif col_idx in [1, 4, 5, 6, 7, 9, 10]:
|
|
||||||
cell.alignment = Alignment(horizontal="center", vertical="center")
|
|
||||||
else:
|
|
||||||
cell.alignment = Alignment(horizontal="left", vertical="center")
|
|
||||||
|
|
||||||
for col in ws.columns:
|
|
||||||
col_letter = get_column_letter(col[0].column)
|
|
||||||
max_len = max((len(str(cell.value or '')) for cell in col), default=8)
|
|
||||||
ws.column_dimensions[col_letter].width = min(max(max_len + 2, 8), 35)
|
|
||||||
|
|
||||||
try:
|
|
||||||
wb.save(output_path)
|
|
||||||
print(f"[✓] Детальный отчет сохранен: {output_path}")
|
|
||||||
except (PermissionError, OSError):
|
|
||||||
alt_filename = filename.replace(".xlsx", f"_{int(time.time())}.xlsx")
|
|
||||||
alt_path = os.path.join(target_dir, alt_filename)
|
|
||||||
wb.save(alt_path)
|
|
||||||
print(f"[⚠️] Файл открыт в Excel! Сохранено как: {alt_path}")
|
|
||||||
|
|
||||||
|
|
||||||
def export_raw_scud(df_scud, filename="СКУД_Сырые_данные.xlsx"):
|
|
||||||
output_path = os.path.join(REPORTS_DIR, filename)
|
|
||||||
df_scud.to_excel(output_path, index=False)
|
|
||||||
@@ -29,7 +29,17 @@ def init_exceptions_table():
|
|||||||
|
|
||||||
def get_all_exceptions_from_db() -> Dict[str, List[str]]:
|
def get_all_exceptions_from_db() -> Dict[str, List[str]]:
|
||||||
init_exceptions_table()
|
init_exceptions_table()
|
||||||
cfg = {"departments": [], "positions": [], "fio": [], "position_keywords": [], "include_fio": []}
|
cfg = {
|
||||||
|
"departments": [],
|
||||||
|
"positions": [],
|
||||||
|
"fio": [],
|
||||||
|
"position_keywords": [],
|
||||||
|
"include_fio": [],
|
||||||
|
"turnstile_fio": [],
|
||||||
|
"turnstile_departments": [],
|
||||||
|
"fligel_fio": [], # ⭐️ Сотрудники с доступом через Флигель (DoorIndex = 23)
|
||||||
|
"fligel_departments": [] # ⭐️ Отделы с доступом через Флигель
|
||||||
|
}
|
||||||
|
|
||||||
with get_connection() as conn:
|
with get_connection() as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
@@ -37,7 +47,6 @@ def get_all_exceptions_from_db() -> Dict[str, List[str]]:
|
|||||||
rows = cursor.fetchall()
|
rows = cursor.fetchall()
|
||||||
|
|
||||||
if not rows and os.path.exists(EXCEPTIONS_PATH):
|
if not rows and os.path.exists(EXCEPTIONS_PATH):
|
||||||
# Первичная миграция из JSON в SQLite
|
|
||||||
sync_json_to_db()
|
sync_json_to_db()
|
||||||
return get_all_exceptions_from_db()
|
return get_all_exceptions_from_db()
|
||||||
|
|
||||||
@@ -49,7 +58,8 @@ def get_all_exceptions_from_db() -> Dict[str, List[str]]:
|
|||||||
|
|
||||||
def add_exception_to_db(category: str, value: str, comment: str = "") -> bool:
|
def add_exception_to_db(category: str, value: str, comment: str = "") -> bool:
|
||||||
init_exceptions_table()
|
init_exceptions_table()
|
||||||
val_clean = normalize_fio(value) if category in ["fio", "include_fio"] else value.strip()
|
# Нормализуем ФИО в том числе для реестра флигеля
|
||||||
|
val_clean = normalize_fio(value) if category in ["fio", "include_fio", "turnstile_fio", "fligel_fio"] else value.strip()
|
||||||
if not val_clean:
|
if not val_clean:
|
||||||
return False
|
return False
|
||||||
with get_connection() as conn:
|
with get_connection() as conn:
|
||||||
|
|||||||
@@ -0,0 +1,222 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: services/manual_absences_repo.py
|
||||||
|
ROLE: Репозиторий ручных реестров ("Мест. командир.", "Иное") и поиск по штату 1С.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import csv
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import List, Dict, Any, Optional
|
||||||
|
from core.connection import get_connection
|
||||||
|
from config import normalize_fio, DATA_DIR
|
||||||
|
|
||||||
|
REASONS_CSV_PATH = os.path.join(DATA_DIR, "static_reason_absence.csv")
|
||||||
|
|
||||||
|
|
||||||
|
def init_manual_absences_table() -> None:
|
||||||
|
with get_connection() as conn:
|
||||||
|
conn.execute("""
|
||||||
|
CREATE TABLE IF NOT EXISTS manual_absences (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
absence_type TEXT NOT NULL, -- 'LOCAL_TRIP' или 'OTHER'
|
||||||
|
fio TEXT NOT NULL,
|
||||||
|
fio_clean TEXT NOT NULL,
|
||||||
|
department TEXT DEFAULT '',
|
||||||
|
position TEXT DEFAULT '',
|
||||||
|
date_start TEXT, -- 'YYYY-MM-DD'
|
||||||
|
date_end TEXT, -- 'YYYY-MM-DD'
|
||||||
|
reason TEXT NOT NULL,
|
||||||
|
comment TEXT DEFAULT '',
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
""")
|
||||||
|
conn.execute("CREATE INDEX IF NOT EXISTS idx_manual_abs_dates ON manual_absences(date_start, date_end);")
|
||||||
|
conn.execute("CREATE INDEX IF NOT EXISTS idx_manual_abs_fio ON manual_absences(fio_clean);")
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def get_static_reasons() -> List[str]:
|
||||||
|
"""Возвращает список причин из data/static_reason_absence.csv."""
|
||||||
|
if not os.path.exists(REASONS_CSV_PATH):
|
||||||
|
# Если файл еще не создан, создаем базовый набор причин
|
||||||
|
os.makedirs(os.path.dirname(REASONS_CSV_PATH), exist_ok=True)
|
||||||
|
default_reasons = ["По семейным обстоятельствам", "Медосмотр", "Сдача крови", "Учебный отпуск", "Административный отпуск"]
|
||||||
|
with open(REASONS_CSV_PATH, "w", encoding="utf-8", newline="") as f:
|
||||||
|
writer = csv.writer(f)
|
||||||
|
writer.writerow(["reason"])
|
||||||
|
for r in default_reasons:
|
||||||
|
writer.writerow([r])
|
||||||
|
return default_reasons
|
||||||
|
|
||||||
|
reasons = []
|
||||||
|
try:
|
||||||
|
with open(REASONS_CSV_PATH, "r", encoding="utf-8") as f:
|
||||||
|
reader = csv.reader(f)
|
||||||
|
for row in reader:
|
||||||
|
if row and row[0].strip() and row[0].strip().lower() != "reason":
|
||||||
|
reasons.append(row[0].strip())
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return reasons
|
||||||
|
|
||||||
|
|
||||||
|
def search_staff_suggestions(query: str, limit: int = 15) -> List[Dict[str, str]]:
|
||||||
|
"""Живой поиск сотрудников по zup_staff для автокомплита."""
|
||||||
|
q = (query or "").strip()
|
||||||
|
if not q or len(q) < 2:
|
||||||
|
return []
|
||||||
|
|
||||||
|
# Приводим к разным регистрам для гарантированного поиска кириллицы в SQLite
|
||||||
|
q_lower = q.lower()
|
||||||
|
q_title = q.capitalize()
|
||||||
|
|
||||||
|
with get_connection(row_factory=True) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
# 1. Находим действительно самый свежий срез штата (по created_at или по структуре даты ГГГГ-ММ-ДД)
|
||||||
|
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()
|
||||||
|
latest_date = row[0] if row else None
|
||||||
|
|
||||||
|
if not latest_date:
|
||||||
|
return []
|
||||||
|
|
||||||
|
# 2. Поиск с сортировкой: сначала те, у кого фамилия НАЧИНАЕТСЯ с запроса
|
||||||
|
sql = """
|
||||||
|
SELECT DISTINCT fio, fio_clean, department, position
|
||||||
|
FROM zup_staff
|
||||||
|
WHERE snapshot_date = ?
|
||||||
|
AND (
|
||||||
|
fio LIKE ? OR fio LIKE ? OR fio_clean LIKE ? OR fio_clean LIKE ?
|
||||||
|
OR fio LIKE ? OR fio_clean LIKE ?
|
||||||
|
)
|
||||||
|
ORDER BY
|
||||||
|
CASE
|
||||||
|
WHEN fio LIKE ? OR fio_clean LIKE ? THEN 0
|
||||||
|
ELSE 1
|
||||||
|
END,
|
||||||
|
fio ASC
|
||||||
|
LIMIT ?
|
||||||
|
"""
|
||||||
|
prefix_pattern_title = f"{q_title}%"
|
||||||
|
prefix_pattern_lower = f"{q_lower}%"
|
||||||
|
any_pattern_title = f"%{q_title}%"
|
||||||
|
any_pattern_lower = f"%{q_lower}%"
|
||||||
|
|
||||||
|
cursor.execute(sql, (
|
||||||
|
latest_date,
|
||||||
|
prefix_pattern_title, prefix_pattern_lower, prefix_pattern_title, prefix_pattern_lower,
|
||||||
|
any_pattern_title, any_pattern_lower,
|
||||||
|
prefix_pattern_title, prefix_pattern_title,
|
||||||
|
limit
|
||||||
|
))
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"fio": r["fio"],
|
||||||
|
"fio_clean": r["fio_clean"],
|
||||||
|
"department": r["department"] or "—",
|
||||||
|
"position": r["position"] or "—"
|
||||||
|
}
|
||||||
|
for r in rows
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def add_manual_absence(
|
||||||
|
absence_type: str,
|
||||||
|
fio: str,
|
||||||
|
reason: str,
|
||||||
|
department: str = "",
|
||||||
|
position: str = "",
|
||||||
|
date_start: Optional[str] = None,
|
||||||
|
date_end: Optional[str] = None,
|
||||||
|
comment: str = ""
|
||||||
|
) -> int:
|
||||||
|
init_manual_absences_table()
|
||||||
|
clean_fio = normalize_fio(fio)
|
||||||
|
if not clean_fio:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
today_str = datetime.now().strftime("%Y-%m-%d")
|
||||||
|
d_start = date_start.strip() if date_start and date_start.strip() else today_str
|
||||||
|
d_end = date_end.strip() if date_end and date_end.strip() else today_str
|
||||||
|
|
||||||
|
with get_connection() as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("""
|
||||||
|
INSERT INTO manual_absences (
|
||||||
|
absence_type, fio, fio_clean, department, position,
|
||||||
|
date_start, date_end, reason, comment
|
||||||
|
)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""", (absence_type.upper(), fio.strip(), clean_fio, department.strip(), position.strip(), d_start, d_end, reason.strip(), comment.strip()))
|
||||||
|
conn.commit()
|
||||||
|
return cursor.lastrowid
|
||||||
|
|
||||||
|
|
||||||
|
def delete_manual_absence(item_id: int) -> bool:
|
||||||
|
init_manual_absences_table()
|
||||||
|
with get_connection() as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("DELETE FROM manual_absences WHERE id = ?", (item_id,))
|
||||||
|
conn.commit()
|
||||||
|
return cursor.rowcount > 0
|
||||||
|
|
||||||
|
|
||||||
|
def get_manual_absences_list(absence_type: Optional[str] = None) -> List[Dict[str, Any]]:
|
||||||
|
today_str = datetime.now().strftime("%Y-%m-%d")
|
||||||
|
# ⭐️ Обязательно row_factory=True для преобразования строк SQLite в dict
|
||||||
|
with get_connection(row_factory=True) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
# 1. Автоочистка просроченных командировок и отсутствий
|
||||||
|
cursor.execute("""
|
||||||
|
DELETE FROM manual_absences
|
||||||
|
WHERE date_end IS NOT NULL
|
||||||
|
AND date_end != ''
|
||||||
|
AND date_end < ?
|
||||||
|
""", (today_str,))
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
# 2. Выборка активных записей
|
||||||
|
query = "SELECT id, absence_type, fio, department, position, reason, date_start, date_end, comment FROM manual_absences"
|
||||||
|
params = []
|
||||||
|
if absence_type:
|
||||||
|
query += " WHERE absence_type = ?"
|
||||||
|
params.append(absence_type)
|
||||||
|
query += " ORDER BY id DESC"
|
||||||
|
|
||||||
|
cursor.execute(query, params)
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
return [dict(r) for r in rows]
|
||||||
|
|
||||||
|
|
||||||
|
def get_active_manual_absences_for_date(date_str: str) -> List[Dict[str, Any]]:
|
||||||
|
"""Выбирает записи, активные на дату отчета (формат даты ДД.ММ.ГГГГ)."""
|
||||||
|
init_manual_absences_table()
|
||||||
|
try:
|
||||||
|
dt_target = datetime.strptime(date_str.replace('_', '.'), "%d.%m.%Y").strftime("%Y-%m-%d")
|
||||||
|
except Exception:
|
||||||
|
dt_target = datetime.now().strftime("%Y-%m-%d")
|
||||||
|
|
||||||
|
with get_connection(row_factory=True) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT id, absence_type, fio, fio_clean, department, position, date_start, date_end, reason
|
||||||
|
FROM manual_absences
|
||||||
|
WHERE (date_start IS NULL OR date_start <= ?)
|
||||||
|
AND (date_end IS NULL OR date_end >= ?)
|
||||||
|
""", (dt_target, dt_target))
|
||||||
|
return [dict(r) for r in cursor.fetchall()]
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: services/office/document_extractor.py
|
||||||
|
PROJECT: SCUD Orion AI (Office Domain)
|
||||||
|
ROLE: Постраничное извлечение текста и OCR сканов документов через Vision LLM.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import base64
|
||||||
|
import logging
|
||||||
|
from typing import List, Dict, Any, Optional
|
||||||
|
|
||||||
|
import fitz # PyMuPDF
|
||||||
|
from modules.web_api.llm.core.ollama_client import call_ollama_chat
|
||||||
|
|
||||||
|
logger = logging.getLogger("OFFICE_EXTRACTOR")
|
||||||
|
|
||||||
|
|
||||||
|
def ocr_image_b64(image_b64: str, page_num: int = 1) -> str:
|
||||||
|
"""
|
||||||
|
Распознает текст с одного растрового изображения через Qwen 2.5 VL.
|
||||||
|
"""
|
||||||
|
system_prompt = (
|
||||||
|
"Ты — высокоточный профессиональный модуль OCR для канцелярии и документооборота.\n"
|
||||||
|
"Твоя задача — точно переписать весь текст с предоставленного изображения документа.\n"
|
||||||
|
"ПРАВИЛА:\n"
|
||||||
|
"1. Переписывай текст дословно, сохраняя структуру, заголовки, списки, нумерацию и таблицы.\n"
|
||||||
|
"2. Запрещено добавлять вводные слова, приветствия, комментарии ('Спасибо за обращение', 'Вот текст' и т.д.).\n"
|
||||||
|
"3. Выводи СТРОГО чистый распознанный текст документа."
|
||||||
|
)
|
||||||
|
user_message = {
|
||||||
|
"role": "user",
|
||||||
|
"content": f"Распознай весь печатный и рукописный текст страницы №{page_num} без пропусков.",
|
||||||
|
"images": [image_b64]
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
res = call_ollama_chat(
|
||||||
|
messages=[{"role": "system", "content": system_prompt}, user_message],
|
||||||
|
is_vision=True,
|
||||||
|
timeout=300 # 5 минут на тяжелые страницы
|
||||||
|
)
|
||||||
|
return (res.get("content") or "").strip()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[Office OCR] Ошибка распознавания страницы {page_num}: {e}")
|
||||||
|
return f"[Ошибка распознавания страницы {page_num}: {e}]"
|
||||||
|
|
||||||
|
|
||||||
|
def process_pdf_full(file_path: str, max_pages: Optional[int] = None) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
Обрабатывает PDF целиком:
|
||||||
|
- Если есть качественный цифровой текст — мгновенно извлекает его со всех страниц.
|
||||||
|
- Если страница является сканом/картинкой — рендерит ее в 200 DPI и прогоняет через Vision OCR.
|
||||||
|
"""
|
||||||
|
results = []
|
||||||
|
doc = fitz.open(file_path)
|
||||||
|
total_pages = len(doc)
|
||||||
|
limit = min(total_pages, max_pages) if max_pages else total_pages
|
||||||
|
|
||||||
|
logger.info(f"[Office] Начало обработки PDF: {os.path.basename(file_path)} (всего страниц: {total_pages})")
|
||||||
|
|
||||||
|
for idx in range(limit):
|
||||||
|
page_num = idx + 1
|
||||||
|
page = doc[idx]
|
||||||
|
extracted_text = (page.get_text("text") or "").strip()
|
||||||
|
|
||||||
|
# Если на странице есть хороший машинный текст (не скан)
|
||||||
|
if len(extracted_text) > 80:
|
||||||
|
logger.info(f"[Office] Страница {page_num}/{limit}: извлечен цифровой текст ({len(extracted_text)} симв.)")
|
||||||
|
results.append({
|
||||||
|
"page": page_num,
|
||||||
|
"method": "DIGITAL_TEXT",
|
||||||
|
"text": extracted_text
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
# Чистый скан — рендерим страницу в PNG и передаем в Vision LLM
|
||||||
|
logger.info(f"[Office] Страница {page_num}/{limit}: распознавание скана через Ollama Vision...")
|
||||||
|
pix = page.get_pixmap(dpi=200)
|
||||||
|
img_b64 = base64.b64encode(pix.tobytes("png")).decode("utf-8")
|
||||||
|
|
||||||
|
ocr_text = ocr_image_b64(img_b64, page_num=page_num)
|
||||||
|
results.append({
|
||||||
|
"page": page_num,
|
||||||
|
"method": "VISION_OCR",
|
||||||
|
"text": ocr_text
|
||||||
|
})
|
||||||
|
|
||||||
|
doc.close()
|
||||||
|
return results
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: services/office/service.py
|
||||||
|
PROJECT: SCUD Orion AI (Office Domain)
|
||||||
|
ROLE: Единый фасад офисного модуля для вызова из API и фоновых задач.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import uuid
|
||||||
|
import logging
|
||||||
|
from typing import Dict, Any
|
||||||
|
|
||||||
|
from config import BASE_DIR
|
||||||
|
from .document_extractor import process_pdf_full
|
||||||
|
from .word_builder import build_docx_from_ocr
|
||||||
|
|
||||||
|
logger = logging.getLogger("OFFICE_SERVICE")
|
||||||
|
|
||||||
|
OFFICE_OUTPUT_DIR = os.path.join(BASE_DIR, "output", "web", "office")
|
||||||
|
os.makedirs(OFFICE_OUTPUT_DIR, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
|
def convert_pdf_to_word_service(file_path: str, original_filename: str) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Полный цикл: Постраничный OCR скана PDF -> Сборка документа DOCX -> Ссылка на скачивание.
|
||||||
|
"""
|
||||||
|
session_id = str(uuid.uuid4())[:8]
|
||||||
|
base_name = os.path.splitext(os.path.basename(original_filename))[0]
|
||||||
|
out_docx_name = f"{base_name}_распознан.docx"
|
||||||
|
|
||||||
|
target_dir = os.path.join(OFFICE_OUTPUT_DIR, session_id)
|
||||||
|
os.makedirs(target_dir, exist_ok=True)
|
||||||
|
out_docx_path = os.path.join(target_dir, out_docx_name)
|
||||||
|
|
||||||
|
# 1. Постраничный парсинг всех страниц
|
||||||
|
pages = process_pdf_full(file_path)
|
||||||
|
|
||||||
|
# 2. Сборка Word-документа
|
||||||
|
build_docx_from_ocr(pages, out_docx_path, doc_title=base_name)
|
||||||
|
|
||||||
|
# 3. Краткое превью для окна чата
|
||||||
|
preview_sample = pages[0]["text"][:600] if pages else "Документ распознан."
|
||||||
|
|
||||||
|
download_url = f"/api/v1/files/download/office/{session_id}/{out_docx_name}"
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "success",
|
||||||
|
"total_pages": len(pages),
|
||||||
|
"filename": out_docx_name,
|
||||||
|
"filepath": out_docx_path,
|
||||||
|
"download_url": download_url,
|
||||||
|
"preview_text": preview_sample,
|
||||||
|
"message": f"Документ успешно распознан целиком ({len(pages)} стр.) и собран в MS Word."
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: services/office/word_builder.py
|
||||||
|
PROJECT: SCUD Orion AI (Office Domain)
|
||||||
|
ROLE: Сборка форматированного документа MS Word (.docx) из распознанного текста.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
from typing import List, Dict, Any
|
||||||
|
from docx import Document
|
||||||
|
from docx.shared import Pt, Inches
|
||||||
|
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
||||||
|
|
||||||
|
|
||||||
|
def build_docx_from_ocr(pages_data: List[Dict[str, Any]], output_filepath: str, doc_title: str = "Распознанный документ") -> str:
|
||||||
|
"""
|
||||||
|
Создает файл .docx по ГОСТ-стандартам делопроизводства:
|
||||||
|
- Шрифт Times New Roman 12-14pt.
|
||||||
|
- Межстрочный интервал 1.15.
|
||||||
|
- Разделители страниц и колонтитулы.
|
||||||
|
"""
|
||||||
|
doc = Document()
|
||||||
|
|
||||||
|
sections = doc.sections
|
||||||
|
for section in sections:
|
||||||
|
section.top_margin = Inches(0.79)
|
||||||
|
section.bottom_margin = Inches(0.79)
|
||||||
|
section.left_margin = Inches(0.79)
|
||||||
|
section.right_margin = Inches(0.59)
|
||||||
|
|
||||||
|
for p_idx, page in enumerate(pages_data):
|
||||||
|
page_num = page.get("page", p_idx + 1)
|
||||||
|
text_content = page.get("text", "")
|
||||||
|
|
||||||
|
if p_idx > 0:
|
||||||
|
doc.add_page_break()
|
||||||
|
|
||||||
|
lines = text_content.splitlines()
|
||||||
|
for line in lines:
|
||||||
|
line_str = line.strip()
|
||||||
|
if not line_str:
|
||||||
|
continue
|
||||||
|
|
||||||
|
p = doc.add_paragraph()
|
||||||
|
p.paragraph_format.space_after = Pt(3)
|
||||||
|
p.paragraph_format.line_spacing = 1.15
|
||||||
|
|
||||||
|
if any(h in line_str.upper() for h in ["ПРЕДСЕДАТЕЛЬСТВОВАЛ", "ПРОТОКОЛ", "ПОВЕСТКА", "РЕШИЛИ:", "ОТМЕТИЛИ:"]):
|
||||||
|
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||||
|
run = p.add_run(line_str)
|
||||||
|
run.font.name = "Times New Roman"
|
||||||
|
run.font.size = Pt(13)
|
||||||
|
run.font.bold = True
|
||||||
|
else:
|
||||||
|
p.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
|
||||||
|
run = p.add_run(line_str)
|
||||||
|
run.font.name = "Times New Roman"
|
||||||
|
run.font.size = Pt(12)
|
||||||
|
|
||||||
|
os.makedirs(os.path.dirname(output_filepath), exist_ok=True)
|
||||||
|
doc.save(output_filepath)
|
||||||
|
return output_filepath
|
||||||
@@ -0,0 +1,256 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: services/presence_service.py
|
||||||
|
ROLE: Сервис оперативного мониторинга («Кто в здании прямо сейчас»).
|
||||||
|
1. Строгое соответствие общему штату 1С (286 чел.).
|
||||||
|
2. Служебный персонал (уборщики, контролеры) маркируется как EXCLUDED
|
||||||
|
и не искажает вкладки "Не пришли" и "В здании".
|
||||||
|
3. Сотрудники флигеля/двора (Чупряев, Пухаренко, БЛ) при выходе через
|
||||||
|
турникет во двор остаются со статусом "В здании (Флигель/Двор)".
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Dict, Any, List, Optional
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
from core.connection import get_connection
|
||||||
|
from config import DATE_TODAY, normalize_fio
|
||||||
|
from services.exceptions_repo import get_all_exceptions_from_db
|
||||||
|
from services.data_loader import load_1c_data_smart
|
||||||
|
from services.scud_export import run_export
|
||||||
|
|
||||||
|
logger = logging.getLogger("PRESENCE_SERVICE")
|
||||||
|
|
||||||
|
|
||||||
|
def get_latest_zup_staff() -> Optional[pd.DataFrame]:
|
||||||
|
"""Загружает самый свежий доступный срез штата из zup_staff."""
|
||||||
|
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 not row:
|
||||||
|
return None
|
||||||
|
|
||||||
|
latest_date = row[0]
|
||||||
|
df = pd.read_sql_query(
|
||||||
|
"SELECT fio as 'ФИО', fio_clean, department as 'Подразделение', position as 'Должность' FROM zup_staff WHERE snapshot_date = ?",
|
||||||
|
conn, params=(latest_date,)
|
||||||
|
)
|
||||||
|
return df if not df.empty else None
|
||||||
|
|
||||||
|
|
||||||
|
def get_live_presence(date_str: Optional[str] = None, force_refresh: bool = False) -> Dict[str, Any]:
|
||||||
|
clean_date = (date_str or DATE_TODAY).replace('_', '.')
|
||||||
|
data_source = "LOCAL_SQLITE"
|
||||||
|
|
||||||
|
# 1. Принудительный опрос MS SQL при запросе
|
||||||
|
if force_refresh:
|
||||||
|
logger.info(f"[Presence] Прямой опрос MS SQL Орион за {clean_date}...")
|
||||||
|
try:
|
||||||
|
run_export(input_date=clean_date, save_xlsx=False, debug=False)
|
||||||
|
data_source = "LIVE_MSSQL"
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[Presence] Ошибка при обращении к MS SQL: {e}")
|
||||||
|
|
||||||
|
# 2. Выборка последних событий по каждому сотруднику за сегодня
|
||||||
|
events_raw = []
|
||||||
|
with get_connection(row_factory=True) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
query = """
|
||||||
|
WITH RankedEvents AS (
|
||||||
|
SELECT
|
||||||
|
hoz_organ,
|
||||||
|
fio,
|
||||||
|
fio_clean,
|
||||||
|
department,
|
||||||
|
time_val,
|
||||||
|
direction,
|
||||||
|
ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY fio_clean
|
||||||
|
ORDER BY time_val DESC, id DESC
|
||||||
|
) as rn
|
||||||
|
FROM scud_events_raw
|
||||||
|
WHERE log_date = ?
|
||||||
|
)
|
||||||
|
SELECT hoz_organ, fio, fio_clean, department, time_val, direction
|
||||||
|
FROM RankedEvents
|
||||||
|
WHERE rn = 1;
|
||||||
|
"""
|
||||||
|
cursor.execute(query, (clean_date,))
|
||||||
|
events_raw = [dict(r) for r in cursor.fetchall()]
|
||||||
|
|
||||||
|
last_events_map = {r['fio_clean']: r for r in events_raw}
|
||||||
|
|
||||||
|
# 3. Штат 1С и кадровые отклонения
|
||||||
|
df_staff, df_abs = load_1c_data_smart(clean_date, use_db=True)
|
||||||
|
if df_staff is None or df_staff.empty:
|
||||||
|
df_staff = get_latest_zup_staff()
|
||||||
|
|
||||||
|
absences_map = {}
|
||||||
|
if df_abs is not None and not df_abs.empty:
|
||||||
|
for _, row in df_abs.iterrows():
|
||||||
|
fc = row.get('fio_clean')
|
||||||
|
reason = str(row.get('Вид_отсутствия', '')).strip()
|
||||||
|
if fc and reason:
|
||||||
|
absences_map[fc] = reason
|
||||||
|
|
||||||
|
# 4. Исключения и реестр флигеля из базы данных
|
||||||
|
try:
|
||||||
|
exceptions_cfg = get_all_exceptions_from_db()
|
||||||
|
except Exception:
|
||||||
|
exceptions_cfg = {}
|
||||||
|
|
||||||
|
exc_fios = set([normalize_fio(f) for f in exceptions_cfg.get("fio", []) if f])
|
||||||
|
exc_depts = set([str(d).strip().lower() for d in exceptions_cfg.get("departments", []) if d])
|
||||||
|
exc_positions = set([str(p).strip().lower() for p in exceptions_cfg.get("positions", []) if p])
|
||||||
|
|
||||||
|
# Реестр флигеля / двора (ФИО и отделы)
|
||||||
|
fligel_fios = set([normalize_fio(f) for f in exceptions_cfg.get("fligel_fio", []) if f])
|
||||||
|
fligel_depts = set([str(d).strip().lower() for d in exceptions_cfg.get("fligel_departments", []) if d])
|
||||||
|
# Авто-добавление ключевых сотрудников и подразделений лаборатории БЛ
|
||||||
|
fligel_depts.update(["бл", "бетонная лаборатория", "испытательная геотехническая лаборатория"])
|
||||||
|
fligel_fios.update([normalize_fio("Чупряев Антон Михайлович"), normalize_fio("Пухаренко Ольга Юрьевна")])
|
||||||
|
|
||||||
|
# Ключевые слова технического персонала (клининг, контролеры КПП)
|
||||||
|
default_exc_keywords = ["уборщ", "дворник", "контролер", "внутреннего контроля", "клининг"]
|
||||||
|
|
||||||
|
def check_is_excluded(fio_c: str, dept: str, pos: str) -> bool:
|
||||||
|
if fio_c in exc_fios:
|
||||||
|
return True
|
||||||
|
d_lower = str(dept).strip().lower()
|
||||||
|
if d_lower in exc_depts or any(k in d_lower for k in ["контрол", "клининг"]):
|
||||||
|
return True
|
||||||
|
p_lower = str(pos).strip().lower()
|
||||||
|
if p_lower in exc_positions or any(k in p_lower for k in default_exc_keywords):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def check_is_fligel(fio_c: str, dept: str) -> bool:
|
||||||
|
if fio_c in fligel_fios:
|
||||||
|
return True
|
||||||
|
d_lower = str(dept).strip().lower()
|
||||||
|
return d_lower in fligel_depts or any(k in d_lower for k in ["бетонная лаб", "геотехническая лаб"])
|
||||||
|
|
||||||
|
# 5. Формирование полного списка сотрудников
|
||||||
|
staff_items = []
|
||||||
|
seen_fios = set()
|
||||||
|
|
||||||
|
# 5.1. Обработка всех сотрудников из официального штата 1С
|
||||||
|
if df_staff is not None and not df_staff.empty:
|
||||||
|
for _, s_row in df_staff.iterrows():
|
||||||
|
fio_raw = s_row.get('ФИО', '')
|
||||||
|
fio_c = s_row.get('fio_clean', normalize_fio(fio_raw))
|
||||||
|
if not fio_c or fio_c in seen_fios:
|
||||||
|
continue
|
||||||
|
|
||||||
|
seen_fios.add(fio_c)
|
||||||
|
dept_1c = str(s_row.get('Подразделение', '—')).strip()
|
||||||
|
pos = str(s_row.get('Должность', '—')).strip()
|
||||||
|
|
||||||
|
is_excluded = check_is_excluded(fio_c, dept_1c, pos)
|
||||||
|
is_fligel = check_is_fligel(fio_c, dept_1c)
|
||||||
|
|
||||||
|
absence_reason = absences_map.get(fio_c, "")
|
||||||
|
event = last_events_map.get(fio_c)
|
||||||
|
has_events_today = event is not None
|
||||||
|
last_time = event['time_val'].split()[-1][:5] if event and ' ' in event['time_val'] else (event['time_val'][:5] if event else '—')
|
||||||
|
direction = str(event.get('direction', '')).upper() if event else ""
|
||||||
|
|
||||||
|
dept_val = str(event['department']).strip() if (event and event.get('department') and str(event['department']).strip() not in ['—', 'Без подразделения', '']) else dept_1c
|
||||||
|
|
||||||
|
is_remote = "удален" in absence_reason.lower() or "дистанцион" in absence_reason.lower()
|
||||||
|
is_trip_or_leave = bool(absence_reason) and not is_remote
|
||||||
|
|
||||||
|
# ⭐️ ЛОГИКА ОПРЕДЕЛЕНИЯ СТАТУСА:
|
||||||
|
if is_excluded:
|
||||||
|
# Сотрудник входит в штат 1С, но имеет служебный статус исключения
|
||||||
|
status = "EXCLUDED"
|
||||||
|
status_label = "Исключение (Служебный)"
|
||||||
|
elif is_fligel and has_events_today:
|
||||||
|
# ⭐️ Сотрудник флигеля/двора: выход через турникет = внутридневной выход во двор на рабочее место
|
||||||
|
status = "INSIDE"
|
||||||
|
status_label = "В здании (Флигель/Двор)"
|
||||||
|
elif direction == "OUT":
|
||||||
|
status = "OUTSIDE"
|
||||||
|
status_label = "Вышел"
|
||||||
|
elif direction == "IN" or has_events_today:
|
||||||
|
status = "INSIDE"
|
||||||
|
status_label = "В здании"
|
||||||
|
elif is_remote:
|
||||||
|
status = "REMOTE"
|
||||||
|
status_label = "Удаленная работа"
|
||||||
|
elif is_trip_or_leave:
|
||||||
|
status = "OFFICIAL_ABSENCE"
|
||||||
|
status_label = absence_reason
|
||||||
|
else:
|
||||||
|
status = "NOT_ENTERED"
|
||||||
|
status_label = "Не пришел"
|
||||||
|
|
||||||
|
staff_items.append({
|
||||||
|
"fio": fio_raw,
|
||||||
|
"fio_clean": fio_c,
|
||||||
|
"department": dept_val,
|
||||||
|
"position": pos,
|
||||||
|
"status": status,
|
||||||
|
"status_label": status_label,
|
||||||
|
"last_time": last_time,
|
||||||
|
"last_direction": direction or "NONE",
|
||||||
|
"absence_reason": absence_reason,
|
||||||
|
"is_excluded": is_excluded,
|
||||||
|
"is_fligel": is_fligel
|
||||||
|
})
|
||||||
|
|
||||||
|
# Сортировка: В здании -> Вышли -> Удаленка -> Отсутствуют -> Не пришли -> Исключения
|
||||||
|
status_order = {
|
||||||
|
"INSIDE": 0,
|
||||||
|
"OUTSIDE": 1,
|
||||||
|
"REMOTE": 2,
|
||||||
|
"OFFICIAL_ABSENCE": 3,
|
||||||
|
"NOT_ENTERED": 4,
|
||||||
|
"EXCLUDED": 5
|
||||||
|
}
|
||||||
|
staff_items.sort(key=lambda x: (status_order.get(x["status"], 6), x["fio"].lower()))
|
||||||
|
|
||||||
|
# 6. Метрики
|
||||||
|
total_staff = len(staff_items)
|
||||||
|
inside_count = sum(1 for x in staff_items if x["status"] == "INSIDE")
|
||||||
|
outside_count = sum(1 for x in staff_items if x["status"] == "OUTSIDE")
|
||||||
|
remote_count = sum(1 for x in staff_items if x["status"] == "REMOTE")
|
||||||
|
absence_count = sum(1 for x in staff_items if x["status"] == "OFFICIAL_ABSENCE")
|
||||||
|
not_entered_count = sum(1 for x in staff_items if x["status"] == "NOT_ENTERED")
|
||||||
|
excluded_count = sum(1 for x in staff_items if x["status"] == "EXCLUDED")
|
||||||
|
|
||||||
|
latest_event_time = "—"
|
||||||
|
if events_raw:
|
||||||
|
times = [r['time_val'] for r in events_raw if r.get('time_val')]
|
||||||
|
if times:
|
||||||
|
max_t = max(times)
|
||||||
|
latest_event_time = max_t.split()[-1][:5] if ' ' in max_t else max_t[:5]
|
||||||
|
|
||||||
|
return {
|
||||||
|
"date": clean_date,
|
||||||
|
"data_source": data_source,
|
||||||
|
"latest_event_time": latest_event_time,
|
||||||
|
"timestamp": datetime.now().strftime("%H:%M:%S"),
|
||||||
|
"metrics": {
|
||||||
|
"total_staff": total_staff,
|
||||||
|
"inside": inside_count,
|
||||||
|
"outside": outside_count,
|
||||||
|
"remote": remote_count,
|
||||||
|
"official_absence": absence_count,
|
||||||
|
"not_entered": not_entered_count,
|
||||||
|
"excluded": excluded_count
|
||||||
|
},
|
||||||
|
"records": staff_items
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: services/reports/calculators.py
|
||||||
|
ROLE: Расчет баланса рабочего времени, обеденного перерыва и отклонений от нормы.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_deviation(time_in_building_str, reason="", norm_hours=8, lunch_minutes=30):
|
||||||
|
"""
|
||||||
|
Расчет отклонения от нормы.
|
||||||
|
Для удаленщиков при наличии физического времени в здании вычисляется реальное отклонение.
|
||||||
|
"""
|
||||||
|
reason_clean = str(reason).strip().lower() if pd.notna(reason) else ""
|
||||||
|
is_remote = "удален" in reason_clean or "дистанцион" in reason_clean
|
||||||
|
|
||||||
|
has_building_time = isinstance(time_in_building_str, str) and time_in_building_str not in ['00:00', '0', '', 'None', 'nan', 'NaN']
|
||||||
|
|
||||||
|
# Если есть уважительная причина (больничный, отпуск, командировка и т.д.) не удаленка
|
||||||
|
if reason_clean != "" and not is_remote:
|
||||||
|
return "0:00"
|
||||||
|
|
||||||
|
# Если удаленщик работал исключительно из дома (00:00 в здании)
|
||||||
|
if is_remote and not has_building_time:
|
||||||
|
return "0:00"
|
||||||
|
|
||||||
|
# Если сотрудника не было в здании и нет уважительной причины
|
||||||
|
if not has_building_time:
|
||||||
|
return f"-{norm_hours}:00"
|
||||||
|
|
||||||
|
try:
|
||||||
|
parts = time_in_building_str.strip().split(':')
|
||||||
|
hh = int(parts[0])
|
||||||
|
mm = int(parts[1]) if len(parts) > 1 else 0
|
||||||
|
total_in_building_minutes = hh * 60 + mm
|
||||||
|
|
||||||
|
if total_in_building_minutes == 0:
|
||||||
|
return "0:00" if is_remote else f"-{norm_hours}:00"
|
||||||
|
|
||||||
|
work_minutes = max(0, total_in_building_minutes - lunch_minutes)
|
||||||
|
norm_minutes = norm_hours * 60
|
||||||
|
diff = work_minutes - norm_minutes
|
||||||
|
|
||||||
|
if diff == 0:
|
||||||
|
return "0:00"
|
||||||
|
|
||||||
|
sign = "-" if diff < 0 else ""
|
||||||
|
abs_diff = abs(diff)
|
||||||
|
res_hh = abs_diff // 60
|
||||||
|
res_mm = abs_diff % 60
|
||||||
|
|
||||||
|
return f"{sign}{res_hh}:{res_mm:02d}"
|
||||||
|
except Exception:
|
||||||
|
return "0:00" if is_remote else f"-{norm_hours}:00"
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: services/reports/otchet_builder.py
|
||||||
|
ROLE: Генератор книги Детального суточного отчета со сверкой 1С:ЗУП.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import math
|
||||||
|
import pandas as pd
|
||||||
|
import xlsxwriter
|
||||||
|
from services.reports.styles import get_dated_reports_dir, format_date_ru, safe_close_workbook, create_xlsx_format
|
||||||
|
from services.reports.calculators import calculate_deviation
|
||||||
|
|
||||||
|
|
||||||
|
def generate_detailed_excel(merged_df, date_str="20.08.2026", filename=None):
|
||||||
|
date_clean = str(date_str).replace('_', '.')
|
||||||
|
if not filename:
|
||||||
|
filename = f"{format_date_ru(date_clean)} отчет.xlsx"
|
||||||
|
|
||||||
|
is_exc = merged_df.get('is_excluded', False) == True
|
||||||
|
is_not_hired = merged_df.get('not_hired_yet', False) == True
|
||||||
|
|
||||||
|
# Исключаем из отчета за вчера сотрудников-исключений И тех, кто еще не принят на работу
|
||||||
|
df_export = merged_df[(~is_exc) & (~is_not_hired)].copy() if merged_df is not None and not merged_df.empty else pd.DataFrame()
|
||||||
|
|
||||||
|
target_dir = get_dated_reports_dir(date_clean)
|
||||||
|
output_path = os.path.join(target_dir, filename)
|
||||||
|
|
||||||
|
# Включаем опцию защиты nan_inf_to_errors
|
||||||
|
wb = xlsxwriter.Workbook(output_path, {'nan_inf_to_errors': True})
|
||||||
|
ws = wb.add_worksheet("Детальный_отчет")
|
||||||
|
|
||||||
|
fmt_date_lbl = wb.add_format({'font_name': 'Arial', 'font_size': 10, 'bold': True})
|
||||||
|
ws.write(1, 1, "Дата:", fmt_date_lbl)
|
||||||
|
ws.write(1, 3, date_clean, fmt_date_lbl)
|
||||||
|
|
||||||
|
headers = [
|
||||||
|
"№", "ФИО", "Подразделение", "время входа", "первая активность", "время выхода",
|
||||||
|
"находился в здании", "причина отсутствия", "норма", "отклонение от нормы"
|
||||||
|
]
|
||||||
|
fmt_hdr = create_xlsx_format(wb, font_name='Arial', font_size=10, bg_color='#D9E1F2', bold=True, align="center", wrap=True)
|
||||||
|
ws.set_row(3, 26)
|
||||||
|
for col_idx, h_text in enumerate(headers):
|
||||||
|
ws.write(3, col_idx, h_text, fmt_hdr)
|
||||||
|
|
||||||
|
start_col = 'Начало дня' if 'Начало дня' in df_export.columns else 'Начало_дня'
|
||||||
|
end_col = 'Конец дня' if 'Конец дня' in df_export.columns else 'Конец_дня'
|
||||||
|
hours_col = 'Часы' if 'Часы' in df_export.columns else 'Находился_в_здании'
|
||||||
|
chars_per_line_h = 24
|
||||||
|
|
||||||
|
for idx, row in df_export.reset_index(drop=True).iterrows():
|
||||||
|
row_num = 4 + idx
|
||||||
|
is_present = bool(row.get('Пришел', False))
|
||||||
|
absence_reason = row.get('Вид_отсутствия', '')
|
||||||
|
has_reason = pd.notna(absence_reason) and str(absence_reason).strip() not in ['', 'nan', 'None']
|
||||||
|
|
||||||
|
in_val = str(row.get(start_col, 'Нет входа')).strip()
|
||||||
|
if in_val.lower() in ['nan', 'none']: in_val = 'Нет входа'
|
||||||
|
|
||||||
|
out_val = str(row.get(end_col, 'Нет выхода')).strip()
|
||||||
|
if out_val.lower() in ['nan', 'none']: out_val = 'Нет выхода'
|
||||||
|
|
||||||
|
in_building_str = str(row.get(hours_col, '00:00')).strip()
|
||||||
|
if in_building_str.lower() in ['nan', 'none']: in_building_str = '00:00'
|
||||||
|
|
||||||
|
first_act_val = str(row.get('Первая_активность', '—')).strip()
|
||||||
|
if first_act_val.lower() in ['nan', 'none']: first_act_val = '—'
|
||||||
|
has_first_act = first_act_val not in ['—', '', 'None', 'nan']
|
||||||
|
|
||||||
|
deviation_val = calculate_deviation(in_building_str, reason=absence_reason if has_reason else "", norm_hours=8, lunch_minutes=30)
|
||||||
|
|
||||||
|
dept_raw = row.get('department_scud', row.get('department', row.get('Подразделение', '')))
|
||||||
|
dept_scud_val = str(dept_raw).strip() if pd.notna(dept_raw) else '—'
|
||||||
|
if dept_scud_val.lower() in ['nan', 'none']: dept_scud_val = '—'
|
||||||
|
|
||||||
|
fio_raw = row.get('Сотрудник', '')
|
||||||
|
fio_val = str(fio_raw).strip() if pd.notna(fio_raw) else ''
|
||||||
|
if fio_val.lower() in ['nan', 'none']: fio_val = ''
|
||||||
|
|
||||||
|
row_color = None
|
||||||
|
if is_present and has_reason:
|
||||||
|
row_color = '#E2EFDA'
|
||||||
|
elif not is_present and has_reason:
|
||||||
|
row_color = '#FFF2CC'
|
||||||
|
elif not is_present and not has_reason and not has_first_act:
|
||||||
|
row_color = '#FCE4D6'
|
||||||
|
|
||||||
|
val_h_str = str(absence_reason).strip() if has_reason else ""
|
||||||
|
if val_h_str.lower() in ['nan', 'none']: val_h_str = ""
|
||||||
|
|
||||||
|
lines_count = math.ceil(len(val_h_str) / chars_per_line_h) if len(val_h_str) > chars_per_line_h else 1
|
||||||
|
ws.set_row(row_num, max(lines_count * 18, 20))
|
||||||
|
|
||||||
|
values = [
|
||||||
|
(idx + 1, 'center', False),
|
||||||
|
(fio_val, 'left', False),
|
||||||
|
(dept_scud_val, 'center', False),
|
||||||
|
(in_val, 'center', False),
|
||||||
|
(first_act_val, 'center', False),
|
||||||
|
(out_val, 'center', False),
|
||||||
|
(in_building_str, 'center', False),
|
||||||
|
(val_h_str, 'left', True),
|
||||||
|
(8, 'center', False),
|
||||||
|
(deviation_val, 'center', False)
|
||||||
|
]
|
||||||
|
|
||||||
|
for col_idx, (val, align_type, is_wrap) in enumerate(values):
|
||||||
|
# Санитизация для исключения передачи float('nan') в _write_number
|
||||||
|
if pd.isna(val) or val is None or str(val).strip().lower() == 'nan':
|
||||||
|
val = ""
|
||||||
|
fmt = create_xlsx_format(wb, font_name='Arial', font_size=10, bg_color=row_color, align=align_type, wrap=is_wrap)
|
||||||
|
ws.write(row_num, col_idx, val, fmt)
|
||||||
|
|
||||||
|
col_widths = {0: 4, 1: 33, 2: 13, 3: 11, 4: 11, 5: 11, 6: 12, 7: 24, 8: 6, 9: 11}
|
||||||
|
for col_idx, width in col_widths.items():
|
||||||
|
ws.set_column(col_idx, col_idx, width)
|
||||||
|
|
||||||
|
safe_close_workbook(wb, output_path, target_dir, filename)
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: services/reports/raw_scud_builder.py
|
||||||
|
ROLE: Генерация Excel-файла сырых данных СКУД.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import pandas as pd
|
||||||
|
import xlsxwriter
|
||||||
|
from config import REPORTS_DIR
|
||||||
|
from services.reports.styles import safe_close_workbook, create_xlsx_format
|
||||||
|
|
||||||
|
|
||||||
|
def export_raw_scud(df_scud, filename="СКУД_Сырые_данные.xlsx"):
|
||||||
|
output_path = os.path.join(REPORTS_DIR, filename)
|
||||||
|
target_dir = os.path.dirname(output_path)
|
||||||
|
wb = xlsxwriter.Workbook(output_path)
|
||||||
|
ws = wb.add_worksheet("Сырые_данные")
|
||||||
|
|
||||||
|
fmt_hdr = create_xlsx_format(wb, bold=True, bg_color='#D9E1F2', align='center')
|
||||||
|
fmt_cell = create_xlsx_format(wb, align='left')
|
||||||
|
|
||||||
|
headers = list(df_scud.columns)
|
||||||
|
ws.set_row(0, 28)
|
||||||
|
for col_idx, header in enumerate(headers):
|
||||||
|
ws.write(0, col_idx, str(header), fmt_hdr)
|
||||||
|
|
||||||
|
col_widths = [len(str(h)) for h in headers]
|
||||||
|
|
||||||
|
for row_idx, row_values in enumerate(df_scud.values, start=1):
|
||||||
|
ws.set_row(row_idx, 19)
|
||||||
|
for col_idx, val in enumerate(row_values):
|
||||||
|
val_str = "" if (pd.isna(val) or val is None) else ("Да" if isinstance(val, bool) and val else ("Нет" if isinstance(val, bool) else str(val)))
|
||||||
|
ws.write(row_idx, col_idx, val_str, fmt_cell)
|
||||||
|
if len(val_str) > col_widths[col_idx]:
|
||||||
|
col_widths[col_idx] = len(val_str)
|
||||||
|
|
||||||
|
for col_idx, width in enumerate(col_widths):
|
||||||
|
ws.set_column(col_idx, col_idx, min(max(width + 3, 10), 45))
|
||||||
|
|
||||||
|
safe_close_workbook(wb, output_path, target_dir, filename)
|
||||||
@@ -0,0 +1,147 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: services/reports/simplified_builder.py
|
||||||
|
ROLE: Генератор книги "Упрощенный отчет за ДД.ММ.ГГГГг..xlsx"
|
||||||
|
на основе агрегированных данных СКУД и 1С.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import openpyxl
|
||||||
|
from openpyxl.styles import Font, Alignment, Border, Side
|
||||||
|
from datetime import datetime, time
|
||||||
|
import pandas as pd
|
||||||
|
from config import REPORTS_DIR
|
||||||
|
from services.reports.styles import get_dated_reports_dir
|
||||||
|
|
||||||
|
|
||||||
|
def format_fio_initials(full_fio: str) -> str:
|
||||||
|
"""Преобразует 'Иванов Иван Иванович' в 'Иванов И.И.'"""
|
||||||
|
if not full_fio or pd.isna(full_fio):
|
||||||
|
return ""
|
||||||
|
parts = str(full_fio).strip().split()
|
||||||
|
if len(parts) >= 3:
|
||||||
|
return f"{parts[0]} {parts[1][0]}.{parts[2][0]}."
|
||||||
|
elif len(parts) == 2:
|
||||||
|
return f"{parts[0]} {parts[1][0]}."
|
||||||
|
return str(full_fio).strip()
|
||||||
|
|
||||||
|
|
||||||
|
def parse_time_cell(val, default_empty="Нет входа (0:00)"):
|
||||||
|
"""Преобразует строку времени СКУД в time(hh, mm) либо оставляет текстовую заглушку."""
|
||||||
|
if not val or pd.isna(val):
|
||||||
|
return default_empty
|
||||||
|
s = str(val).strip()
|
||||||
|
if s in ["Нет входа", "—", "", "nan", "None", "00:00:00", "00:00"]:
|
||||||
|
return default_empty
|
||||||
|
if s in ["Нет выхода"]:
|
||||||
|
return "Нет выхода (23:59)"
|
||||||
|
|
||||||
|
# Если уже пришёл time
|
||||||
|
if isinstance(val, time):
|
||||||
|
return val
|
||||||
|
|
||||||
|
try:
|
||||||
|
parts = s.split(":")
|
||||||
|
h = int(parts[0])
|
||||||
|
m = int(parts[1]) if len(parts) > 1 else 0
|
||||||
|
return time(h, m)
|
||||||
|
except Exception:
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
def generate_simplified_excel(df_merged: pd.DataFrame, date_str: str, filename: str = None) -> str:
|
||||||
|
"""
|
||||||
|
Генерирует Excel-файл упрощенного отчета точно по образцу Ориона.
|
||||||
|
Структура:
|
||||||
|
A: № | B: Подразделение | C: Сотрудник | D: Должность | E: Начало дня | F: Конец дня
|
||||||
|
"""
|
||||||
|
clean_date = str(date_str).replace('_', '.')
|
||||||
|
if not filename:
|
||||||
|
filename = f"Упрощенный отчет за {clean_date}г..xlsx"
|
||||||
|
|
||||||
|
target_dir = get_dated_reports_dir(clean_date)
|
||||||
|
output_path = os.path.join(target_dir, filename)
|
||||||
|
|
||||||
|
# Исключаем тех, кто не принят на работу и исключения
|
||||||
|
is_exc = df_merged.get('is_excluded', False) == True
|
||||||
|
is_not_hired = df_merged.get('not_hired_yet', False) == True
|
||||||
|
df_src = df_merged[(~is_exc) & (~is_not_hired)].copy()
|
||||||
|
|
||||||
|
# Подготовка данных
|
||||||
|
rows_to_sort = []
|
||||||
|
for _, r in df_src.iterrows():
|
||||||
|
fio_raw = r.get('Сотрудник', r.get('ФИО', ''))
|
||||||
|
dept = str(r.get('Подразделение', '—')).strip()
|
||||||
|
pos = str(r.get('Должность', '—')).strip()
|
||||||
|
if pos.lower() in ['nan', 'none', '']: pos = '—'
|
||||||
|
if dept.lower() in ['nan', 'none', '']: dept = '—'
|
||||||
|
|
||||||
|
fio_short = format_fio_initials(fio_raw)
|
||||||
|
t_in = parse_time_cell(r.get('Начало_дня'), default_empty="Нет входа (0:00)")
|
||||||
|
t_out = parse_time_cell(r.get('Конец_дня'), default_empty="Нет выхода (23:59)")
|
||||||
|
|
||||||
|
rows_to_sort.append({
|
||||||
|
'dept': dept,
|
||||||
|
'fio_short': fio_short,
|
||||||
|
'pos': pos,
|
||||||
|
't_in': t_in,
|
||||||
|
't_out': t_out
|
||||||
|
})
|
||||||
|
|
||||||
|
# Сортировка: Подразделение (А-Я), затем Сотрудник (А-Я)
|
||||||
|
rows_sorted = sorted(rows_to_sort, key=lambda x: (x['dept'].lower(), x['fio_short'].lower()))
|
||||||
|
|
||||||
|
# Создание книги openpyxl для точного соблюдения структуры образца
|
||||||
|
wb = openpyxl.Workbook()
|
||||||
|
ws = wb.active
|
||||||
|
ws.title = "Лист1"
|
||||||
|
|
||||||
|
# Стили по образцу
|
||||||
|
font_main = Font(name="Calibri", size=11, bold=False)
|
||||||
|
align_center = Alignment(horizontal="center", vertical="center")
|
||||||
|
border_thin = Border(
|
||||||
|
left=Side(style="thin"),
|
||||||
|
right=Side(style="thin"),
|
||||||
|
top=Side(style="thin"),
|
||||||
|
bottom=Side(style="thin")
|
||||||
|
)
|
||||||
|
|
||||||
|
# Заголовок
|
||||||
|
ws["B1"] = f"Упрощенный отчет: c {clean_date} по {clean_date}"
|
||||||
|
ws["B1"].font = font_main
|
||||||
|
|
||||||
|
# Шапка таблицы (Строка 3)
|
||||||
|
headers = [None, "Подразделение", "Сотрудник", "Должность", "Начало дня", "Конец дня"]
|
||||||
|
for col_idx, h_text in enumerate(headers, start=1):
|
||||||
|
cell = ws.cell(row=3, column=col_idx, value=h_text)
|
||||||
|
cell.font = font_main
|
||||||
|
cell.alignment = align_center
|
||||||
|
cell.border = border_thin
|
||||||
|
|
||||||
|
# Заполнение строк данных (с 4 строки)
|
||||||
|
for idx, item in enumerate(rows_sorted, start=1):
|
||||||
|
row_num = 3 + idx
|
||||||
|
vals = [idx, item['dept'], item['fio_short'], item['pos'], item['t_in'], item['t_out']]
|
||||||
|
|
||||||
|
for col_idx, val in enumerate(vals, start=1):
|
||||||
|
cell = ws.cell(row=row_num, column=col_idx, value=val)
|
||||||
|
cell.font = font_main
|
||||||
|
cell.alignment = align_center
|
||||||
|
cell.border = border_thin
|
||||||
|
|
||||||
|
# Числовой формат времени h:mm
|
||||||
|
if isinstance(val, time):
|
||||||
|
cell.number_format = "h:mm"
|
||||||
|
|
||||||
|
# Настройка ширины колонок по образцу
|
||||||
|
widths = {'A': 13.0, 'B': 25.0, 'C': 27.0, 'D': 80.0, 'E': 21.5, 'F': 23.3}
|
||||||
|
for col_letter, w in widths.items():
|
||||||
|
ws.column_dimensions[col_letter].width = w
|
||||||
|
|
||||||
|
# Создание пустых Лист2 и Лист3 как в оригинальном шаблоне
|
||||||
|
wb.create_sheet("Лист2")
|
||||||
|
wb.create_sheet("Лист3")
|
||||||
|
|
||||||
|
wb.save(output_path)
|
||||||
|
return output_path
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: services/reports/styles.py
|
||||||
|
ROLE: Стили, палитры цветов, форматирование дат и защита от блокировок Excel.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
from datetime import datetime
|
||||||
|
from xlsxwriter.exceptions import FileCreateError
|
||||||
|
from config import REPORTS_DIR
|
||||||
|
|
||||||
|
MONTHS_RU_GENITIVE = {
|
||||||
|
1: "января", 2: "февраля", 3: "марта", 4: "апреля",
|
||||||
|
5: "мая", 6: "июня", 7: "июля", 8: "августа",
|
||||||
|
9: "сентября", 10: "октября", 11: "ноября", 12: "декабря"
|
||||||
|
}
|
||||||
|
|
||||||
|
MONTHS_RU_NOMINATIVE = {
|
||||||
|
1: "январь", 2: "февраль", 3: "март", 4: "апрель",
|
||||||
|
5: "май", 6: "июнь", 7: "июль", 8: "август",
|
||||||
|
9: "сентябрь", 10: "октябрь", 11: "ноябрь", 12: "декабрь"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def format_date_ru(date_str):
|
||||||
|
date_clean = str(date_str).replace('_', '.')
|
||||||
|
try:
|
||||||
|
dt = datetime.strptime(date_clean, "%d.%m.%Y")
|
||||||
|
return f"{dt.day} {MONTHS_RU_GENITIVE[dt.month]} {dt.year}"
|
||||||
|
except Exception:
|
||||||
|
return date_str
|
||||||
|
|
||||||
|
|
||||||
|
def get_dated_reports_dir(date_str):
|
||||||
|
date_clean = str(date_str).replace('_', '.')
|
||||||
|
try:
|
||||||
|
dt = datetime.strptime(date_clean, "%d.%m.%Y")
|
||||||
|
year_str = str(dt.year)
|
||||||
|
month_name = MONTHS_RU_NOMINATIVE[dt.month]
|
||||||
|
except Exception:
|
||||||
|
now = datetime.now()
|
||||||
|
year_str = str(now.year)
|
||||||
|
month_name = MONTHS_RU_NOMINATIVE[now.month]
|
||||||
|
|
||||||
|
target_dir = os.path.join(REPORTS_DIR, year_str, month_name)
|
||||||
|
os.makedirs(target_dir, exist_ok=True)
|
||||||
|
return target_dir
|
||||||
|
|
||||||
|
|
||||||
|
def safe_close_workbook(wb, output_path, target_dir, filename):
|
||||||
|
try:
|
||||||
|
wb.close()
|
||||||
|
return output_path
|
||||||
|
except (FileCreateError, OSError, PermissionError):
|
||||||
|
alt_filename = filename.replace(".xlsx", f"_{int(time.time())}.xlsx")
|
||||||
|
alt_path = os.path.join(target_dir, alt_filename)
|
||||||
|
try:
|
||||||
|
wb.filename = alt_path
|
||||||
|
wb._store_workbook()
|
||||||
|
return alt_path
|
||||||
|
except Exception:
|
||||||
|
return output_path
|
||||||
|
|
||||||
|
|
||||||
|
def create_xlsx_format(workbook, font_name="Calibri", font_size=11, bg_color=None, bold=False, align="left", wrap=False):
|
||||||
|
fmt_dict = {
|
||||||
|
'font_name': font_name,
|
||||||
|
'font_size': font_size,
|
||||||
|
'bold': bold,
|
||||||
|
'align': align,
|
||||||
|
'valign': 'vcenter',
|
||||||
|
'border': 1,
|
||||||
|
'border_color': '#D3D3D3',
|
||||||
|
'text_wrap': wrap
|
||||||
|
}
|
||||||
|
if bg_color:
|
||||||
|
fmt_dict['bg_color'] = bg_color
|
||||||
|
return workbook.add_format(fmt_dict)
|
||||||
@@ -0,0 +1,219 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: services/reports/svodka_builder.py
|
||||||
|
ROLE: Генератор книги Ежедневной сводки (иерархические группировки XlsxWriter).
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import math
|
||||||
|
import pandas as pd
|
||||||
|
import xlsxwriter
|
||||||
|
from services.reports.styles import get_dated_reports_dir, format_date_ru, safe_close_workbook, create_xlsx_format
|
||||||
|
|
||||||
|
|
||||||
|
def generate_summary_excel(merged_df, date_str="21.08.2026", filename=None):
|
||||||
|
date_clean = str(date_str).replace('_', '.')
|
||||||
|
if not filename:
|
||||||
|
filename = f"{format_date_ru(date_clean)} сводка.xlsx"
|
||||||
|
|
||||||
|
target_dir = get_dated_reports_dir(date_clean)
|
||||||
|
output_path = os.path.join(target_dir, filename)
|
||||||
|
|
||||||
|
wb = xlsxwriter.Workbook(output_path, {'nan_inf_to_errors': True})
|
||||||
|
ws = wb.add_worksheet("Лист_1")
|
||||||
|
ws.outline_settings(visible=True, symbols_below=False, symbols_right=False, auto_style=False)
|
||||||
|
|
||||||
|
fmt_hdr_l = create_xlsx_format(wb, bg_color='#D9E1F2', bold=True, align="left")
|
||||||
|
fmt_hdr_r = create_xlsx_format(wb, bg_color='#D9E1F2', bold=True, align="right")
|
||||||
|
fmt_tot_l = create_xlsx_format(wb, bg_color='#F2F2F2', bold=True, align="left")
|
||||||
|
fmt_tot_r = create_xlsx_format(wb, bg_color='#F2F2F2', bold=True, align="right")
|
||||||
|
fmt_empty = create_xlsx_format(wb)
|
||||||
|
|
||||||
|
ws.set_row(0, 20)
|
||||||
|
ws.write(0, 0, "Сводка на", fmt_hdr_l)
|
||||||
|
ws.write(0, 1, date_clean, fmt_hdr_r)
|
||||||
|
|
||||||
|
ws.set_row(1, 20)
|
||||||
|
ws.write(1, 0, "", fmt_empty)
|
||||||
|
ws.write(1, 1, "", fmt_empty)
|
||||||
|
|
||||||
|
is_not_hired = merged_df.get('not_hired_yet', False) == True
|
||||||
|
is_no_pass = merged_df.get('no_scud_pass', False) == True
|
||||||
|
is_exc = merged_df.get('is_excluded', False) == True
|
||||||
|
|
||||||
|
# "По списку" строго по официальному штату 1С
|
||||||
|
staff_total = len(merged_df[~is_not_hired])
|
||||||
|
|
||||||
|
ws.set_row(2, 20)
|
||||||
|
ws.write(2, 0, "По списку", fmt_tot_l)
|
||||||
|
ws.write(2, 1, staff_total, fmt_tot_r)
|
||||||
|
|
||||||
|
current_row = 3
|
||||||
|
|
||||||
|
# 1. Неизвестно
|
||||||
|
unexplained = merged_df[
|
||||||
|
(~is_not_hired) &
|
||||||
|
(merged_df['Пришел'] == False) &
|
||||||
|
(merged_df['Вид_отсутствия'].isna() | (merged_df['Вид_отсутствия'].astype(str).str.strip().isin(['', 'nan', 'None']))) &
|
||||||
|
(~is_no_pass) & (~is_exc)
|
||||||
|
]
|
||||||
|
fmt_unexp_hl = create_xlsx_format(wb, bg_color='#FCE4D6', bold=True, align="left")
|
||||||
|
fmt_unexp_hr = create_xlsx_format(wb, bg_color='#FCE4D6', bold=True, align="right")
|
||||||
|
fmt_unexp_rl = create_xlsx_format(wb, bg_color='#FCE4D6', bold=False, align="left")
|
||||||
|
fmt_unexp_rr = create_xlsx_format(wb, bg_color='#FCE4D6', bold=False, align="right")
|
||||||
|
|
||||||
|
ws.set_row(current_row, 20)
|
||||||
|
ws.write(current_row, 0, "неизвестно", fmt_unexp_hl)
|
||||||
|
ws.write(current_row, 1, len(unexplained), fmt_unexp_hr)
|
||||||
|
current_row += 1
|
||||||
|
|
||||||
|
for fio in sorted(unexplained['Сотрудник'].dropna().unique()):
|
||||||
|
ws.set_row(current_row, 20, None, {'level': 1, 'hidden': False})
|
||||||
|
ws.write(current_row, 0, str(fio), fmt_unexp_rl)
|
||||||
|
ws.write(current_row, 1, "", fmt_unexp_rr)
|
||||||
|
current_row += 1
|
||||||
|
|
||||||
|
# 2. Нет пропуска (в штате 1С есть, но карты СКУД нет)
|
||||||
|
no_pass_df = merged_df[is_no_pass & (~is_exc) & (~is_not_hired)]
|
||||||
|
fmt_np_hl = create_xlsx_format(wb, bg_color='#E1F5FE', bold=True, align="left")
|
||||||
|
fmt_np_hr = create_xlsx_format(wb, bg_color='#E1F5FE', bold=True, align="right")
|
||||||
|
fmt_np_rl = create_xlsx_format(wb, bg_color='#E1F5FE', bold=False, align="left")
|
||||||
|
fmt_np_rr = create_xlsx_format(wb, bg_color='#E1F5FE', bold=False, align="right")
|
||||||
|
|
||||||
|
ws.set_row(current_row, 20)
|
||||||
|
ws.write(current_row, 0, "Нет в СКУД", fmt_np_hl)
|
||||||
|
ws.write(current_row, 1, len(no_pass_df), fmt_np_hr)
|
||||||
|
current_row += 1
|
||||||
|
|
||||||
|
if not no_pass_df.empty:
|
||||||
|
for fio in sorted(no_pass_df['Сотрудник'].dropna().unique()):
|
||||||
|
ws.set_row(current_row, 20, None, {'level': 1, 'hidden': False})
|
||||||
|
ws.write(current_row, 0, str(fio), fmt_np_rl)
|
||||||
|
ws.write(current_row, 1, "", fmt_np_rr)
|
||||||
|
current_row += 1
|
||||||
|
|
||||||
|
# 3. Не приняты на работу (в СКУД есть, но в 1С приказа еще нет)
|
||||||
|
not_hired_df = merged_df[is_not_hired & (~is_exc)]
|
||||||
|
fmt_nh_hl = create_xlsx_format(wb, bg_color='#FFF0F5', bold=True, align="left")
|
||||||
|
fmt_nh_hr = create_xlsx_format(wb, bg_color='#FFF0F5', bold=True, align="right")
|
||||||
|
fmt_nh_rl = create_xlsx_format(wb, bg_color='#FFF0F5', bold=False, align="left")
|
||||||
|
fmt_nh_rr = create_xlsx_format(wb, bg_color='#FFF0F5', bold=False, align="right")
|
||||||
|
|
||||||
|
ws.set_row(current_row, 20)
|
||||||
|
ws.write(current_row, 0, "Нет в ЗУП", fmt_nh_hl)
|
||||||
|
ws.write(current_row, 1, len(not_hired_df), fmt_nh_hr)
|
||||||
|
current_row += 1
|
||||||
|
|
||||||
|
if not not_hired_df.empty:
|
||||||
|
for fio in sorted(not_hired_df['Сотрудник'].dropna().unique()):
|
||||||
|
ws.set_row(current_row, 20, None, {'level': 1, 'hidden': False})
|
||||||
|
ws.write(current_row, 0, str(fio), fmt_nh_rl)
|
||||||
|
ws.write(current_row, 1, "", fmt_nh_rr)
|
||||||
|
current_row += 1
|
||||||
|
|
||||||
|
# 4. Официальные отсутствия
|
||||||
|
reason_clean = merged_df['Вид_отсутствия'].astype(str).str.lower()
|
||||||
|
is_remote_reason = reason_clean.str.contains('удален|дистанцион', regex=True, na=False)
|
||||||
|
|
||||||
|
absent_only = merged_df[
|
||||||
|
(~is_not_hired) &
|
||||||
|
(merged_df['Пришел'] == False) &
|
||||||
|
(merged_df['Вид_отсутствия'].notna()) &
|
||||||
|
(~merged_df['Вид_отсутствия'].astype(str).str.startswith('Исключение')) &
|
||||||
|
(~is_remote_reason)
|
||||||
|
]
|
||||||
|
absent_groups = absent_only.groupby('Вид_отсутствия')
|
||||||
|
pastels = ['#FFF2CC', '#E1D5E7', '#E1F5FE', '#FFF0F5', '#FCF3CF']
|
||||||
|
|
||||||
|
for idx_cat, (cat_name, group) in enumerate(absent_groups):
|
||||||
|
hex_c = pastels[idx_cat % len(pastels)]
|
||||||
|
fmt_cat_hl = create_xlsx_format(wb, bg_color=hex_c, bold=True, align="left")
|
||||||
|
fmt_cat_hr = create_xlsx_format(wb, bg_color=hex_c, bold=True, align="right")
|
||||||
|
fmt_cat_rl = create_xlsx_format(wb, bg_color=hex_c, bold=False, align="left")
|
||||||
|
fmt_cat_rr = create_xlsx_format(wb, bg_color=hex_c, bold=False, align="right")
|
||||||
|
|
||||||
|
ws.set_row(current_row, 20)
|
||||||
|
ws.write(current_row, 0, str(cat_name), fmt_cat_hl)
|
||||||
|
ws.write(current_row, 1, len(group), fmt_cat_hr)
|
||||||
|
current_row += 1
|
||||||
|
|
||||||
|
is_other_category = (str(cat_name).strip().lower() == "иное")
|
||||||
|
for _, row in group.sort_values(by='Сотрудник').iterrows():
|
||||||
|
fio = row.get('Сотрудник', '')
|
||||||
|
detail_val = row.get('detailed_reason', row.get('comment', '')) if is_other_category else ""
|
||||||
|
if pd.isna(detail_val):
|
||||||
|
detail_val = ""
|
||||||
|
ws.set_row(current_row, 20, None, {'level': 1, 'hidden': True, 'collapsed': True})
|
||||||
|
ws.write(current_row, 0, str(fio), fmt_cat_rl)
|
||||||
|
ws.write(current_row, 1, str(detail_val), fmt_cat_rr)
|
||||||
|
current_row += 1
|
||||||
|
|
||||||
|
# 5. Итого на работе (только принятые)
|
||||||
|
exc_without_doc = merged_df[(~is_not_hired) & is_exc & (merged_df['Вид_отсутствия'].isna() | (merged_df['Вид_отсутствия'].astype(str).str.strip().isin(['', 'nan', 'Исключение'])))]
|
||||||
|
present_scud = merged_df[(~is_not_hired) & (merged_df['Пришел'] == True) & (~is_exc)]
|
||||||
|
total_present_count = len(present_scud) + len(exc_without_doc)
|
||||||
|
|
||||||
|
fmt_pres_hl = create_xlsx_format(wb, bg_color='#E2EFDA', bold=True, align="left")
|
||||||
|
fmt_pres_hr = create_xlsx_format(wb, bg_color='#E2EFDA', bold=True, align="right")
|
||||||
|
|
||||||
|
ws.set_row(current_row, 20)
|
||||||
|
ws.write(current_row, 0, "Итого на работе", fmt_pres_hl)
|
||||||
|
ws.write(current_row, 1, total_present_count, fmt_pres_hr)
|
||||||
|
current_row += 1
|
||||||
|
|
||||||
|
# 6. Удаленная работа
|
||||||
|
remote_home = merged_df[(~is_not_hired) & (merged_df['Пришел'] == False) & is_remote_reason & (~is_exc)]
|
||||||
|
fmt_rem_hl = create_xlsx_format(wb, bg_color='#E8F8F5', bold=True, align="left")
|
||||||
|
fmt_rem_hr = create_xlsx_format(wb, bg_color='#E8F8F5', bold=True, align="right")
|
||||||
|
fmt_rem_rl = create_xlsx_format(wb, bg_color='#E8F8F5', bold=False, align="left")
|
||||||
|
fmt_rem_rr = create_xlsx_format(wb, bg_color='#E8F8F5', bold=False, align="right")
|
||||||
|
|
||||||
|
ws.set_row(current_row, 20)
|
||||||
|
ws.write(current_row, 0, "В том числе на удаленной работе", fmt_rem_hl)
|
||||||
|
ws.write(current_row, 1, len(remote_home), fmt_rem_hr)
|
||||||
|
current_row += 1
|
||||||
|
|
||||||
|
if not remote_home.empty:
|
||||||
|
for fio in sorted(remote_home['Сотрудник'].dropna().unique()):
|
||||||
|
ws.set_row(current_row, 20, None, {'level': 1, 'hidden': True, 'collapsed': True})
|
||||||
|
ws.write(current_row, 0, str(fio), fmt_rem_rl)
|
||||||
|
ws.write(current_row, 1, "", fmt_rem_rr)
|
||||||
|
current_row += 1
|
||||||
|
|
||||||
|
# 7. Аномалии СКУД и 1С
|
||||||
|
anomalies = merged_df[
|
||||||
|
(~is_not_hired) & (~is_exc) & (
|
||||||
|
((merged_df['Пришел'] == True) & (merged_df['Вид_отсутствия'].notna()) &
|
||||||
|
(~merged_df['Вид_отсутствия'].astype(str).str.startswith('Исключение')) &
|
||||||
|
(~is_remote_reason) &
|
||||||
|
(~reason_clean.str.contains('командировк', na=False))) |
|
||||||
|
(merged_df.get('anomaly_flag', 'NONE') == 'ANOMALY_NO_IN_HAS_ACTIVITY')
|
||||||
|
)
|
||||||
|
]
|
||||||
|
fmt_anom_hl = create_xlsx_format(wb, bg_color='#FCE4D6', bold=True, align="left")
|
||||||
|
fmt_anom_hr = create_xlsx_format(wb, bg_color='#FCE4D6', bold=True, align="right")
|
||||||
|
fmt_anom_rl = create_xlsx_format(wb, bg_color='#FCE4D6', bold=False, align="left")
|
||||||
|
fmt_anom_rr = create_xlsx_format(wb, bg_color='#FCE4D6', bold=False, align="left", wrap=True)
|
||||||
|
|
||||||
|
ws.set_row(current_row, 20)
|
||||||
|
ws.write(current_row, 0, "Аномалии СКУД и 1С", fmt_anom_hl)
|
||||||
|
ws.write(current_row, 1, len(anomalies), fmt_anom_hr)
|
||||||
|
current_row += 1
|
||||||
|
|
||||||
|
chars_per_line_b = 30
|
||||||
|
if not anomalies.empty:
|
||||||
|
for _, row in anomalies.iterrows():
|
||||||
|
fio = row.get('Сотрудник', '')
|
||||||
|
anom_flag = row.get('anomaly_flag', 'NONE')
|
||||||
|
reason_text = f"🚨 АНОМАЛИЯ СКУД: Нет входа (первая активность: {row.get('Первая_активность', '—')})" if anom_flag == 'ANOMALY_NO_IN_HAS_ACTIVITY' else f"В 1С: {row.get('Вид_отсутствия', '')}"
|
||||||
|
|
||||||
|
lines_count = math.ceil(len(reason_text) / chars_per_line_b) if len(reason_text) > chars_per_line_b else 1
|
||||||
|
ws.set_row(current_row, max(lines_count * 18, 20), None, {'level': 1, 'hidden': True, 'collapsed': True})
|
||||||
|
ws.write(current_row, 0, str(fio), fmt_anom_rl)
|
||||||
|
ws.write(current_row, 1, str(reason_text), fmt_anom_rr)
|
||||||
|
current_row += 1
|
||||||
|
|
||||||
|
ws.set_column(0, 0, 45)
|
||||||
|
ws.set_column(1, 1, 38)
|
||||||
|
safe_close_workbook(wb, output_path, target_dir, filename)
|
||||||
+140
-38
@@ -2,6 +2,7 @@
|
|||||||
===============================================================================
|
===============================================================================
|
||||||
FILE: services/scud_etl/merger.py
|
FILE: services/scud_etl/merger.py
|
||||||
ROLE: Агрегация реестров, выбор совместителей 1С по отделу СКУД и авто-связки.
|
ROLE: Агрегация реестров, выбор совместителей 1С по отделу СКУД и авто-связки.
|
||||||
|
Распределение исключений в общий рабочий пул при отсутствии справок.
|
||||||
===============================================================================
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -17,10 +18,8 @@ logger = logging.getLogger("SCUD_MERGER")
|
|||||||
|
|
||||||
|
|
||||||
def load_identity_mappings() -> Dict[str, str]:
|
def load_identity_mappings() -> Dict[str, str]:
|
||||||
"""Загружает подтвержденные сопоставления ФИО (СКУД -> 1С:ЗУП) из SQLite."""
|
|
||||||
with get_connection() as conn:
|
with get_connection() as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
# Автоматическая инициализация таблицы при первом обращении
|
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
CREATE TABLE IF NOT EXISTS person_identity_mapping (
|
CREATE TABLE IF NOT EXISTS person_identity_mapping (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
@@ -44,7 +43,6 @@ def load_identity_mappings() -> Dict[str, str]:
|
|||||||
|
|
||||||
|
|
||||||
def aggregate_scud_by_person(df_scud: pd.DataFrame) -> pd.DataFrame:
|
def aggregate_scud_by_person(df_scud: pd.DataFrame) -> pd.DataFrame:
|
||||||
"""Схлопывает дубликаты пропусков одного человека в СКУД."""
|
|
||||||
if df_scud is None or df_scud.empty:
|
if df_scud is None or df_scud.empty:
|
||||||
return df_scud
|
return df_scud
|
||||||
|
|
||||||
@@ -53,7 +51,6 @@ def aggregate_scud_by_person(df_scud: pd.DataFrame) -> pd.DataFrame:
|
|||||||
fio_col = 'Сотрудник' if 'Сотрудник' in df.columns else 'ФИО'
|
fio_col = 'Сотрудник' if 'Сотрудник' in df.columns else 'ФИО'
|
||||||
df['fio_clean'] = df[fio_col].apply(normalize_fio)
|
df['fio_clean'] = df[fio_col].apply(normalize_fio)
|
||||||
|
|
||||||
# Применяем сохраненный кэш связок ФИО
|
|
||||||
mapping_dict = load_identity_mappings()
|
mapping_dict = load_identity_mappings()
|
||||||
if mapping_dict:
|
if mapping_dict:
|
||||||
df['fio_clean'] = df['fio_clean'].apply(lambda f: mapping_dict.get(f, f))
|
df['fio_clean'] = df['fio_clean'].apply(lambda f: mapping_dict.get(f, f))
|
||||||
@@ -95,10 +92,6 @@ def aggregate_scud_by_person(df_scud: pd.DataFrame) -> pd.DataFrame:
|
|||||||
|
|
||||||
|
|
||||||
def select_best_zup_position(df_staff_1c: pd.DataFrame, df_scud_agg: pd.DataFrame) -> pd.DataFrame:
|
def select_best_zup_position(df_staff_1c: pd.DataFrame, df_scud_agg: pd.DataFrame) -> pd.DataFrame:
|
||||||
"""
|
|
||||||
⭐️ Для совместителей с несколькими должностями в 1С:ЗУП
|
|
||||||
выбирает ту ставку, которая соответствует отделу физического нахождения по СКУД.
|
|
||||||
"""
|
|
||||||
if df_staff_1c is None or df_staff_1c.empty:
|
if df_staff_1c is None or df_staff_1c.empty:
|
||||||
return pd.DataFrame()
|
return pd.DataFrame()
|
||||||
|
|
||||||
@@ -145,7 +138,8 @@ def merge_scud_and_1c(
|
|||||||
return pd.DataFrame(columns=[
|
return pd.DataFrame(columns=[
|
||||||
'Сотрудник', 'fio_clean', 'Подразделение', 'Должность',
|
'Сотрудник', 'fio_clean', 'Подразделение', 'Должность',
|
||||||
'Начало_дня', 'Первая_активность', 'Конец_дня', 'Находился_в_здании',
|
'Начало_дня', 'Первая_активность', 'Конец_дня', 'Находился_в_здании',
|
||||||
'Пришел', 'anomaly_flag', 'причина отсутствия', 'Вид_отсутствия', 'is_excluded'
|
'Пришел', 'anomaly_flag', 'причина отсутствия', 'Вид_отсутствия',
|
||||||
|
'is_excluded', 'not_hired_yet', 'no_scud_pass'
|
||||||
])
|
])
|
||||||
|
|
||||||
synonyms = get_department_synonyms_dict()
|
synonyms = get_department_synonyms_dict()
|
||||||
@@ -154,15 +148,63 @@ def merge_scud_and_1c(
|
|||||||
df_scud_agg = aggregate_scud_by_person(df_scud)
|
df_scud_agg = aggregate_scud_by_person(df_scud)
|
||||||
df_staff_agg = select_best_zup_position(df_staff_1c, df_scud_agg)
|
df_staff_agg = select_best_zup_position(df_staff_1c, df_scud_agg)
|
||||||
|
|
||||||
df_res = df_scud_agg.copy() if df_scud_agg is not None and not df_scud_agg.empty else df_staff_agg.copy()
|
staff_fios = set(df_staff_agg['fio_clean'].dropna().tolist()) if df_staff_agg is not None and not df_staff_agg.empty else set()
|
||||||
|
scud_dict = df_scud_agg.set_index('fio_clean').to_dict('index') if df_scud_agg is not None and not df_scud_agg.empty else {}
|
||||||
|
|
||||||
if "Сотрудник" in df_res.columns:
|
merged_rows = []
|
||||||
df_res["fio_clean"] = df_res["Сотрудник"].apply(normalize_fio)
|
|
||||||
elif "ФИО" in df_res.columns:
|
# 1. Формируем строки по официальному штату 1С
|
||||||
|
if df_staff_agg is not None and not df_staff_agg.empty:
|
||||||
|
for _, s_row in df_staff_agg.iterrows():
|
||||||
|
fio_c = s_row.get('fio_clean', '')
|
||||||
|
r = dict(s_row)
|
||||||
|
if 'Сотрудник' not in r or pd.isna(r['Сотрудник']) or str(r['Сотрудник']).strip() == '':
|
||||||
|
r['Сотрудник'] = r.get('ФИО', fio_c)
|
||||||
|
|
||||||
|
if fio_c in scud_dict:
|
||||||
|
# Сотрудник есть в СКУД — берем короткую аббревиатуру отдела из СКУД
|
||||||
|
scud_data = scud_dict[fio_c]
|
||||||
|
dept_scud = str(scud_data.get('Подразделение', '')).strip()
|
||||||
|
if dept_scud and dept_scud.lower() not in ['nan', 'none', '—', 'без подразделения']:
|
||||||
|
r['Подразделение'] = dept_scud
|
||||||
|
|
||||||
|
r['Начало_дня'] = scud_data.get('Начало_дня', 'Нет входа')
|
||||||
|
r['Первая_активность'] = scud_data.get('Первая_активность', '—')
|
||||||
|
r['Конец_дня'] = scud_data.get('Конец_дня', 'Нет выхода')
|
||||||
|
r['Находился_в_здании'] = scud_data.get('Находился_в_здании', '00:00')
|
||||||
|
r['Пришел'] = bool(scud_data.get('Пришел', False))
|
||||||
|
r['anomaly_flag'] = scud_data.get('anomaly_flag', 'NONE')
|
||||||
|
r['no_scud_pass'] = False
|
||||||
|
r['not_hired_yet'] = False
|
||||||
|
else:
|
||||||
|
# Сотрудника нет в СКУД (Нет пропуска)
|
||||||
|
r['Начало_дня'] = 'Нет входа'
|
||||||
|
r['Первая_активность'] = '—'
|
||||||
|
r['Конец_дня'] = 'Нет выхода'
|
||||||
|
r['Находился_в_здании'] = '00:00'
|
||||||
|
r['Пришел'] = False
|
||||||
|
r['anomaly_flag'] = 'NONE'
|
||||||
|
r['no_scud_pass'] = True
|
||||||
|
r['not_hired_yet'] = False
|
||||||
|
|
||||||
|
merged_rows.append(r)
|
||||||
|
|
||||||
|
# 2. Сотрудники из СКУД, которых еще нет в 1С (Не приняты на работу)
|
||||||
|
if df_scud_agg is not None and not df_scud_agg.empty:
|
||||||
|
for _, scud_row in df_scud_agg.iterrows():
|
||||||
|
fio_c = scud_row.get('fio_clean', '')
|
||||||
|
if fio_c not in staff_fios:
|
||||||
|
r = dict(scud_row)
|
||||||
|
r['not_hired_yet'] = True
|
||||||
|
r['no_scud_pass'] = False
|
||||||
|
if 'Должность' not in r or pd.isna(r['Должность']):
|
||||||
|
r['Должность'] = '—'
|
||||||
|
merged_rows.append(r)
|
||||||
|
|
||||||
|
df_res = pd.DataFrame(merged_rows)
|
||||||
|
|
||||||
|
if "Сотрудник" not in df_res.columns and "ФИО" in df_res.columns:
|
||||||
df_res["Сотрудник"] = df_res["ФИО"]
|
df_res["Сотрудник"] = df_res["ФИО"]
|
||||||
df_res["fio_clean"] = df_res["ФИО"].apply(normalize_fio)
|
|
||||||
elif "fio_clean" not in df_res.columns:
|
|
||||||
df_res["fio_clean"] = ""
|
|
||||||
|
|
||||||
for col, default_val in [
|
for col, default_val in [
|
||||||
('Начало_дня', 'Нет входа'),
|
('Начало_дня', 'Нет входа'),
|
||||||
@@ -170,20 +212,46 @@ def merge_scud_and_1c(
|
|||||||
('Конец_дня', 'Нет выхода'),
|
('Конец_дня', 'Нет выхода'),
|
||||||
('Находился_в_здании', '00:00'),
|
('Находился_в_здании', '00:00'),
|
||||||
('Пришел', False),
|
('Пришел', False),
|
||||||
('anomaly_flag', 'NONE')
|
('anomaly_flag', 'NONE'),
|
||||||
|
('not_hired_yet', False),
|
||||||
|
('no_scud_pass', False)
|
||||||
]:
|
]:
|
||||||
if col not in df_res.columns:
|
if col not in df_res.columns:
|
||||||
df_res[col] = default_val
|
df_res[col] = default_val
|
||||||
|
|
||||||
|
# Словарь синонимов и принудительное сокращение длинных отделов 1С до аббревиатур
|
||||||
reverse_synonyms = {v.lower(): k.upper() for k, v in synonyms.items()}
|
reverse_synonyms = {v.lower(): k.upper() for k, v in synonyms.items()}
|
||||||
direct_synonyms = {k.lower(): k.upper() for k in synonyms.keys()}
|
direct_synonyms = {k.lower(): k.upper() for k in synonyms.keys()}
|
||||||
all_dept_map = {**reverse_synonyms, **direct_synonyms, "отдел внутреннего контроля": "ОВК", "отдел вневедомственного контроля": "ОВК"}
|
all_dept_map = {
|
||||||
|
**reverse_synonyms,
|
||||||
|
**direct_synonyms,
|
||||||
|
"отдел внутреннего контроля": "ОВК",
|
||||||
|
"отдел вневедомственного контроля": "ОВК",
|
||||||
|
"отдел авторского надзора и технического аудита": "ОАН",
|
||||||
|
"отдел инженерных изысканий": "ОИЗ",
|
||||||
|
"правовое управление": "ПУ",
|
||||||
|
"макетная мастерская": "ММ",
|
||||||
|
"отдел автоматизации": "ОА",
|
||||||
|
"испытательная геотехническая лаборатория лабораторного центра": "ИГТЛЛ",
|
||||||
|
"отдел экономики, смет и организации строительства": "ОЭС",
|
||||||
|
"отдел электротехники, связи и пожарной автоматики": "ОЭСС",
|
||||||
|
"бетонная лаборатория": "БЛ",
|
||||||
|
"управление главных инженеров проектов №1": "УГИП №1",
|
||||||
|
"управление главных инженеров проектов №2": "УГИП №2",
|
||||||
|
"строительный отдел": "СО",
|
||||||
|
"гидротехническая экспедиция": "ГЭ",
|
||||||
|
"планово-экономический отдел": "ПЭО",
|
||||||
|
"конструкторский отдел": "КО",
|
||||||
|
"отдел тепловодоснабжения и канализации": "ОТВК",
|
||||||
|
"электротехнический отдел": "ЭТО"
|
||||||
|
}
|
||||||
|
|
||||||
if "Подразделение" in df_res.columns:
|
if "Подразделение" in df_res.columns:
|
||||||
df_res["Подразделение"] = df_res["Подразделение"].apply(
|
df_res["Подразделение"] = df_res["Подразделение"].apply(
|
||||||
lambda d: all_dept_map.get(str(d).strip().lower(), str(d).strip())
|
lambda d: all_dept_map.get(str(d).strip().lower(), str(d).strip()) if pd.notna(d) else "—"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Привязка кадровых документов 1С
|
||||||
absences_map = {}
|
absences_map = {}
|
||||||
if df_absences_1c is not None and not df_absences_1c.empty:
|
if df_absences_1c is not None and not df_absences_1c.empty:
|
||||||
fio_col = next((c for c in ["fio_clean", "ФИО", "Сотрудник"] if c in df_absences_1c.columns), None)
|
fio_col = next((c for c in ["fio_clean", "ФИО", "Сотрудник"] if c in df_absences_1c.columns), None)
|
||||||
@@ -193,12 +261,25 @@ def merge_scud_and_1c(
|
|||||||
for _, row in df_absences_1c.iterrows():
|
for _, row in df_absences_1c.iterrows():
|
||||||
fio = normalize_fio(str(row[fio_col]))
|
fio = normalize_fio(str(row[fio_col]))
|
||||||
reason = str(row[reason_col]).strip()
|
reason = str(row[reason_col]).strip()
|
||||||
if reason and reason.lower() != "nan":
|
if reason and reason.lower() not in ["nan", "none"]:
|
||||||
absences_map[fio] = reason
|
absences_map[fio] = reason
|
||||||
|
|
||||||
|
manual_reasons_map = {}
|
||||||
|
try:
|
||||||
|
from services.manual_absences_repo import get_active_manual_absences_for_date
|
||||||
|
target_date_val = df_res.get('Дата', pd.Series()).iloc[0] if 'Дата' in df_res.columns and not df_res.empty else None
|
||||||
|
if target_date_val:
|
||||||
|
m_records = get_active_manual_absences_for_date(str(target_date_val))
|
||||||
|
for mr in m_records:
|
||||||
|
manual_reasons_map[mr['fio_clean']] = mr['reason']
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
df_res["detailed_reason"] = df_res["fio_clean"].map(manual_reasons_map).fillna("")
|
||||||
df_res["причина отсутствия"] = df_res["fio_clean"].map(absences_map)
|
df_res["причина отсутствия"] = df_res["fio_clean"].map(absences_map)
|
||||||
df_res["Вид_отсутствия"] = df_res["причина отсутствия"]
|
df_res["Вид_отсутствия"] = df_res["причина отсутствия"]
|
||||||
|
|
||||||
|
# Исключения и белый список
|
||||||
exc_fios = [normalize_fio(f) for f in exceptions_cfg.get("fio", []) if f]
|
exc_fios = [normalize_fio(f) for f in exceptions_cfg.get("fio", []) if f]
|
||||||
exc_depts = [d.strip().upper() for d in exceptions_cfg.get("departments", []) if d]
|
exc_depts = [d.strip().upper() for d in exceptions_cfg.get("departments", []) if d]
|
||||||
exc_pos = [p.strip().lower() for p in exceptions_cfg.get("positions", []) if p]
|
exc_pos = [p.strip().lower() for p in exceptions_cfg.get("positions", []) if p]
|
||||||
@@ -210,15 +291,17 @@ def merge_scud_and_1c(
|
|||||||
fio = row.get("fio_clean", "")
|
fio = row.get("fio_clean", "")
|
||||||
has_official_absence = pd.notna(row.get("Вид_отсутствия")) and str(row.get("Вид_отсутствия")).strip() not in ["", "nan", "None", "Исключение"]
|
has_official_absence = pd.notna(row.get("Вид_отсутствия")) and str(row.get("Вид_отсутствия")).strip() not in ["", "nan", "None", "Исключение"]
|
||||||
|
|
||||||
if fio in whitelist_fios or has_official_absence:
|
if fio in whitelist_fios:
|
||||||
df_res.at[idx, "is_excluded"] = False
|
df_res.at[idx, "is_excluded"] = False
|
||||||
continue
|
continue
|
||||||
|
|
||||||
dep = str(row.get("Подразделение", "")).upper()
|
dep = str(row.get("Подразделение", "")).upper()
|
||||||
pos = str(row.get("Должность", "")).lower()
|
pos = str(row.get("Должность", "")).lower()
|
||||||
|
|
||||||
if fio in exc_fios or dep in exc_depts or any(d in dep for d in exc_depts) or pos in exc_pos or any(k in pos for k in pos_kw):
|
is_match_exc = (fio in exc_fios or dep in exc_depts or any(d in dep for d in exc_depts) or pos in exc_pos or any(k in pos for k in pos_kw))
|
||||||
df_res.at[idx, "is_excluded"] = True
|
|
||||||
|
if is_match_exc:
|
||||||
|
df_res.at[idx, "is_excluded"] = not has_official_absence
|
||||||
|
|
||||||
mask_exc = (df_res["is_excluded"] == True) & (df_res["Вид_отсутствия"].isna() | (df_res["Вид_отсутствия"] == ""))
|
mask_exc = (df_res["is_excluded"] == True) & (df_res["Вид_отсутствия"].isna() | (df_res["Вид_отсутствия"] == ""))
|
||||||
df_res.loc[mask_exc, "Вид_отсутствия"] = "Исключение"
|
df_res.loc[mask_exc, "Вид_отсутствия"] = "Исключение"
|
||||||
@@ -228,35 +311,54 @@ def merge_scud_and_1c(
|
|||||||
|
|
||||||
|
|
||||||
def calculate_summary_metrics(df_merged: pd.DataFrame) -> Dict[str, Any]:
|
def calculate_summary_metrics(df_merged: pd.DataFrame) -> Dict[str, Any]:
|
||||||
active_df = df_merged[df_merged.get("is_excluded", False) == False].copy()
|
# Создаем независимую копию среза штата с собственным непрерывным индексом
|
||||||
total_staff = len(active_df)
|
df_staff_only = df_merged[~df_merged.get('not_hired_yet', False)].copy().reset_index(drop=True)
|
||||||
|
total_staff = len(df_staff_only)
|
||||||
|
|
||||||
came_to_office_mask = active_df["Начало_дня"].astype(str).str.strip().ne("Нет входа")
|
is_exc_staff = df_staff_only.get('is_excluded', False) == True
|
||||||
working_in_office = active_df[came_to_office_mask]
|
is_no_pass_staff = df_staff_only.get('no_scud_pass', False) == True
|
||||||
working_in_office_count = len(working_in_office)
|
|
||||||
|
|
||||||
df_not_came = active_df[~came_to_office_mask]
|
came_to_office_mask = (df_staff_only["Начало_дня"].astype(str).str.strip().ne("Нет входа")) & (~is_exc_staff)
|
||||||
|
exc_without_doc_mask = (is_exc_staff) & (
|
||||||
|
df_staff_only["Вид_отсутствия"].isna() |
|
||||||
|
df_staff_only["Вид_отсутствия"].astype(str).str.strip().isin(["", "nan", "Исключение"])
|
||||||
|
)
|
||||||
|
|
||||||
reason_series = df_not_came["причина отсутствия"].astype(str).str.lower()
|
working_in_office_count = int((came_to_office_mask | exc_without_doc_mask).sum())
|
||||||
|
|
||||||
|
df_not_working = df_staff_only[~came_to_office_mask & ~exc_without_doc_mask].copy().reset_index(drop=True)
|
||||||
|
|
||||||
|
reason_series = df_not_working["причина отсутствия"].astype(str).str.lower()
|
||||||
is_remote_mask = reason_series.str.contains("удален|дистанцион", regex=True, na=False)
|
is_remote_mask = reason_series.str.contains("удален|дистанцион", regex=True, na=False)
|
||||||
remote_home = df_not_came[is_remote_mask]
|
remote_home_count = int(is_remote_mask.sum())
|
||||||
remote_home_count = len(remote_home)
|
|
||||||
|
|
||||||
df_remaining_absent = df_not_came[~is_remote_mask]
|
df_remaining_absent = df_not_working[~is_remote_mask].copy().reset_index(drop=True)
|
||||||
has_doc_mask = df_remaining_absent["причина отсутствия"].notna() & \
|
has_doc_mask = (
|
||||||
df_remaining_absent["причина отсутствия"].ne("") & \
|
df_remaining_absent["причина отсутствия"].notna() &
|
||||||
df_remaining_absent["причина отсутствия"].ne("nan") & \
|
df_remaining_absent["причина отсутствия"].ne("") &
|
||||||
|
df_remaining_absent["причина отсутствия"].ne("nan") &
|
||||||
(~df_remaining_absent["причина отсутствия"].astype(str).str.startswith("Исключение"))
|
(~df_remaining_absent["причина отсутствия"].astype(str).str.startswith("Исключение"))
|
||||||
official_absent_count = len(df_remaining_absent[has_doc_mask])
|
)
|
||||||
|
official_absent_count = int(has_doc_mask.sum())
|
||||||
|
|
||||||
unknown = df_remaining_absent[~has_doc_mask]
|
# Неизвестно: среди тех, у кого нет официального документа и кто имеет пропуск
|
||||||
|
is_no_pass_remaining = df_remaining_absent.get('no_scud_pass', False) == True
|
||||||
|
unknown = df_remaining_absent[~has_doc_mask & ~is_no_pass_remaining]
|
||||||
unknown_count = len(unknown)
|
unknown_count = len(unknown)
|
||||||
|
|
||||||
|
no_pass_count = int((is_no_pass_staff & ~is_exc_staff).sum())
|
||||||
|
|
||||||
|
is_not_hired_all = df_merged.get('not_hired_yet', False) == True
|
||||||
|
is_exc_all = df_merged.get('is_excluded', False) == True
|
||||||
|
not_hired_count = int((is_not_hired_all & ~is_exc_all).sum())
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"total_staff": total_staff,
|
"total_staff": total_staff,
|
||||||
"working_in_office_count": working_in_office_count,
|
"working_in_office_count": working_in_office_count,
|
||||||
"remote_home_count": remote_home_count,
|
"remote_home_count": remote_home_count,
|
||||||
"official_absent_count": official_absent_count,
|
"official_absent_count": official_absent_count,
|
||||||
|
"no_pass_count": no_pass_count,
|
||||||
|
"not_hired_count": not_hired_count,
|
||||||
"unknown_count": unknown_count,
|
"unknown_count": unknown_count,
|
||||||
"unknown_list": unknown[["fio_clean", "Подразделение", "Должность"]].to_dict(orient="records") if not unknown.empty else []
|
"unknown_list": unknown[["fio_clean", "Подразделение", "Должность"]].to_dict(orient="records") if not unknown.empty else []
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
===============================================================================
|
===============================================================================
|
||||||
FILE: services/scud_etl/pipeline.py
|
FILE: services/scud_etl/pipeline.py
|
||||||
|
ROLE: Загрузка наилучших срезов СКУД и штата/отсутствий 1С с умным fallback-ом.
|
||||||
===============================================================================
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -12,7 +13,7 @@ import pandas as pd
|
|||||||
from core.connection import get_connection
|
from core.connection import get_connection
|
||||||
from core.database import load_scud_from_db_by_snapshot
|
from core.database import load_scud_from_db_by_snapshot
|
||||||
from config import DATA_DIR
|
from config import DATA_DIR
|
||||||
from services.data_loader import load_staff_data, load_absent_data
|
from services.data_loader import load_1c_data_smart, load_staff_data, load_absent_data
|
||||||
|
|
||||||
logger = logging.getLogger("SCUD_PIPELINE")
|
logger = logging.getLogger("SCUD_PIPELINE")
|
||||||
|
|
||||||
@@ -57,8 +58,42 @@ def load_best_snapshot_for_date(date_str: str, prefer_final_y: bool = False) ->
|
|||||||
|
|
||||||
def load_1c_files_for_date(date_str: str) -> Tuple[Optional[pd.DataFrame], Optional[pd.DataFrame]]:
|
def load_1c_files_for_date(date_str: str) -> Tuple[Optional[pd.DataFrame], Optional[pd.DataFrame]]:
|
||||||
"""
|
"""
|
||||||
Загружает реестры штата и отсутствий 1С на указанную дату через data_loader.
|
Загружает штат и отсутствия с каскадным fallback:
|
||||||
|
1. Синхронизирует свежие файлы с сетевой шары.
|
||||||
|
2. Загружает штат и отсутствия через load_1c_data_smart(..., use_db=True).
|
||||||
|
3. Если штат пуст — берет последний доступный срез штата из zup_staff в SQLite.
|
||||||
"""
|
"""
|
||||||
df_staff = load_staff_data(date_str)
|
clean_date = date_str.replace('_', '.')
|
||||||
df_abs = load_absent_data(date_str)
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
# 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)} чел.)")
|
||||||
|
|
||||||
return df_staff, df_abs
|
return df_staff, df_abs
|
||||||
@@ -1,40 +1,52 @@
|
|||||||
"""
|
"""
|
||||||
===============================================================================
|
===============================================================================
|
||||||
FILE: services/scud_etl/sql_queries.py
|
FILE: services/scud_etl/sql_queries.py
|
||||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
ROLE: Изолированные SQL-шаблоны для MS SQL Server (СКУД Орион Pro).
|
||||||
MODULE: services / scud_etl
|
|
||||||
ROLE: Хранилище сырых SQL-шаблонов для выгрузки из MS SQL Server (СКУД Орион Pro).
|
|
||||||
|
|
||||||
AI-CONTEXT-ANCHORS:
|
|
||||||
- ANCHOR[SQL_SCUD_EXPORT_TEMPLATE]: T-SQL запрос с расчетом первой активности и длительности.
|
|
||||||
===============================================================================
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# ANCHOR[SQL_SCUD_EXPORT_TEMPLATE]
|
# 1. Основной срез СКУД с учетом турникетов и флигеля
|
||||||
SCUD_EXPORT_QUERY_TEMPLATE = r"""
|
SQL_QUERY_TEMPLATE = r"""
|
||||||
DECLARE @InputDate DATE = '{target_date}';
|
DECLARE @InputDate DATE = '{target_date}';
|
||||||
DECLARE @TargetDate DATE = @InputDate;
|
DECLARE @TargetDate DATE = @InputDate;
|
||||||
|
|
||||||
DECLARE @StartDate DATETIME = CAST(@TargetDate AS DATETIME);
|
DECLARE @StartDate DATETIME = CAST(@TargetDate AS DATETIME);
|
||||||
DECLARE @EndDate DATETIME = DATEADD(SECOND, -1, DATEADD(DAY, 1, @StartDate));
|
DECLARE @EndDate DATETIME = {end_datetime_sql};
|
||||||
|
|
||||||
WITH DailyLogs AS (
|
WITH PercoPassages AS (
|
||||||
|
-- Физические факты прохода (Event = 32)
|
||||||
SELECT
|
SELECT
|
||||||
log.HozOrgan AS EmployeeID,
|
log.HozOrgan AS EmployeeID,
|
||||||
log.TimeVal,
|
log.TimeVal,
|
||||||
log.Event,
|
|
||||||
log.Mode,
|
|
||||||
CASE
|
CASE
|
||||||
WHEN log.Mode = 2 OR log.Event IN (29, 27, 33) THEN 'OUT'
|
WHEN log.Mode = 1 THEN 'IN'
|
||||||
WHEN log.Mode = 1 OR log.Event IN (28, 26, 32) THEN 'IN'
|
WHEN log.Mode = 2 THEN 'OUT'
|
||||||
ELSE 'OTHER'
|
ELSE 'OTHER'
|
||||||
END AS Direction,
|
END AS Direction
|
||||||
ROW_NUMBER() OVER (PARTITION BY log.HozOrgan ORDER BY log.TimeVal DESC) AS RowNumDesc
|
|
||||||
FROM pLogData log WITH (NOLOCK)
|
FROM pLogData log WITH (NOLOCK)
|
||||||
|
INNER JOIN pList p WITH (NOLOCK) ON log.HozOrgan = p.ID
|
||||||
|
LEFT JOIN PDivision div WITH (NOLOCK) ON p.Section = div.ID
|
||||||
WHERE log.TimeVal BETWEEN @StartDate AND @EndDate
|
WHERE log.TimeVal BETWEEN @StartDate AND @EndDate
|
||||||
AND log.HozOrgan IS NOT NULL
|
AND log.HozOrgan IS NOT NULL
|
||||||
AND log.HozOrgan > 0
|
AND log.HozOrgan > 0
|
||||||
AND log.Event IN (26, 27, 28, 29, 32, 33, 54, 55, 64, 65)
|
AND log.Event = 32
|
||||||
|
AND log.Mode IN (1, 2)
|
||||||
|
AND (
|
||||||
|
-- Контур 1: Левый турникет открыт для всех
|
||||||
|
log.DoorIndex = 1
|
||||||
|
OR
|
||||||
|
-- Контур 2: Правый турникет разрешен только для реестра двора
|
||||||
|
(
|
||||||
|
log.DoorIndex = 2
|
||||||
|
AND ({turnstile_filter_sql})
|
||||||
|
)
|
||||||
|
OR
|
||||||
|
-- Контур 3: Флигель 1 эт. разрешен только для реестра флигеля
|
||||||
|
(
|
||||||
|
log.DoorIndex = 23
|
||||||
|
AND ({fligel_filter_sql})
|
||||||
|
)
|
||||||
|
)
|
||||||
),
|
),
|
||||||
Passages AS (
|
Passages AS (
|
||||||
SELECT
|
SELECT
|
||||||
@@ -42,10 +54,21 @@ Passages AS (
|
|||||||
MIN(TimeVal) AS FirstRawEvent,
|
MIN(TimeVal) AS FirstRawEvent,
|
||||||
MAX(TimeVal) AS LastRawEvent,
|
MAX(TimeVal) AS LastRawEvent,
|
||||||
MIN(CASE WHEN Direction = 'IN' THEN TimeVal END) AS FirstIn,
|
MIN(CASE WHEN Direction = 'IN' THEN TimeVal END) AS FirstIn,
|
||||||
MAX(CASE WHEN Direction = 'OUT' THEN TimeVal END) AS LastOut,
|
MAX(CASE WHEN Direction = 'OUT' THEN TimeVal END) AS FinalOut
|
||||||
MAX(CASE WHEN RowNumDesc = 1 THEN Direction END) AS LastEventType
|
FROM PercoPassages
|
||||||
FROM DailyLogs
|
|
||||||
GROUP BY EmployeeID
|
GROUP BY EmployeeID
|
||||||
|
),
|
||||||
|
EvaluatedPassages AS (
|
||||||
|
SELECT
|
||||||
|
p.*,
|
||||||
|
CASE
|
||||||
|
WHEN p.FinalOut IS NOT NULL
|
||||||
|
AND p.FirstIn IS NOT NULL
|
||||||
|
AND p.FinalOut > DATEADD(MINUTE, 5, p.FirstIn)
|
||||||
|
THEN p.FinalOut
|
||||||
|
ELSE NULL
|
||||||
|
END AS FilteredLastOut
|
||||||
|
FROM Passages p
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
N'ЛЕНМОРНИИПРОЕКТ' AS [Фирма],
|
N'ЛЕНМОРНИИПРОЕКТ' AS [Фирма],
|
||||||
@@ -67,12 +90,8 @@ SELECT
|
|||||||
ELSE N'—'
|
ELSE N'—'
|
||||||
END AS [Первая_активность],
|
END AS [Первая_активность],
|
||||||
CASE
|
CASE
|
||||||
WHEN @TargetDate = CAST(GETDATE() AS DATE) AND (pass.LastEventType = 'IN' OR pass.LastOut IS NULL OR pass.LastOut <= pass.FirstIn)
|
WHEN pass.FilteredLastOut IS NOT NULL
|
||||||
THEN N'Нет выхода'
|
THEN CAST(CONVERT(VARCHAR(8), pass.FilteredLastOut, 108) AS NVARCHAR(20))
|
||||||
WHEN pass.LastOut IS NOT NULL AND pass.LastOut > pass.FirstIn
|
|
||||||
THEN CAST(CONVERT(VARCHAR(8), pass.LastOut, 108) AS NVARCHAR(20))
|
|
||||||
WHEN @TargetDate < CAST(GETDATE() AS DATE) AND pass.LastRawEvent IS NOT NULL AND pass.LastRawEvent > ISNULL(pass.FirstIn, pass.FirstRawEvent)
|
|
||||||
THEN CAST(CONVERT(VARCHAR(8), pass.LastRawEvent, 108) AS NVARCHAR(20))
|
|
||||||
ELSE N'Нет выхода'
|
ELSE N'Нет выхода'
|
||||||
END AS [Конец_дня],
|
END AS [Конец_дня],
|
||||||
CASE
|
CASE
|
||||||
@@ -80,14 +99,14 @@ SELECT
|
|||||||
RIGHT('0' + CAST(DATEDIFF(MINUTE,
|
RIGHT('0' + CAST(DATEDIFF(MINUTE,
|
||||||
ISNULL(pass.FirstIn, pass.FirstRawEvent),
|
ISNULL(pass.FirstIn, pass.FirstRawEvent),
|
||||||
CASE
|
CASE
|
||||||
WHEN @TargetDate = CAST(GETDATE() AS DATE) AND (pass.LastEventType = 'IN' OR pass.LastOut IS NULL OR pass.LastOut <= pass.FirstIn) THEN GETDATE()
|
WHEN pass.FilteredLastOut IS NOT NULL THEN pass.FilteredLastOut
|
||||||
ELSE ISNULL(pass.LastOut, pass.LastRawEvent)
|
ELSE @EndDate
|
||||||
END) / 60 AS VARCHAR), 2) + ':' +
|
END) / 60 AS VARCHAR), 2) + ':' +
|
||||||
RIGHT('0' + CAST(DATEDIFF(MINUTE,
|
RIGHT('0' + CAST(DATEDIFF(MINUTE,
|
||||||
ISNULL(pass.FirstIn, pass.FirstRawEvent),
|
ISNULL(pass.FirstIn, pass.FirstRawEvent),
|
||||||
CASE
|
CASE
|
||||||
WHEN @TargetDate = CAST(GETDATE() AS DATE) AND (pass.LastEventType = 'IN' OR pass.LastOut IS NULL OR pass.LastOut <= pass.FirstIn) THEN GETDATE()
|
WHEN pass.FilteredLastOut IS NOT NULL THEN pass.FilteredLastOut
|
||||||
ELSE ISNULL(pass.LastOut, pass.LastRawEvent)
|
ELSE @EndDate
|
||||||
END) % 60 AS VARCHAR), 2)
|
END) % 60 AS VARCHAR), 2)
|
||||||
ELSE N'00:00'
|
ELSE N'00:00'
|
||||||
END AS [Находился_в_здании],
|
END AS [Находился_в_здании],
|
||||||
@@ -98,7 +117,7 @@ SELECT
|
|||||||
FROM pList p WITH (NOLOCK)
|
FROM pList p WITH (NOLOCK)
|
||||||
LEFT JOIN PDivision div WITH (NOLOCK) ON p.Section = div.ID
|
LEFT JOIN PDivision div WITH (NOLOCK) ON p.Section = div.ID
|
||||||
LEFT JOIN PPost post WITH (NOLOCK) ON p.Post = post.ID
|
LEFT JOIN PPost post WITH (NOLOCK) ON p.Post = post.ID
|
||||||
LEFT JOIN Passages pass ON p.ID = pass.EmployeeID
|
LEFT JOIN EvaluatedPassages pass ON p.ID = pass.EmployeeID
|
||||||
WHERE
|
WHERE
|
||||||
ISNULL(p.StatusRecord, 0) = 0
|
ISNULL(p.StatusRecord, 0) = 0
|
||||||
AND p.DateTimeInArchive IS NULL
|
AND p.DateTimeInArchive IS NULL
|
||||||
@@ -114,3 +133,54 @@ WHERE
|
|||||||
AND ISNULL(CAST(post.Name AS NVARCHAR(255)), N'') NOT LIKE N'Практикант%'
|
AND ISNULL(CAST(post.Name AS NVARCHAR(255)), N'') NOT LIKE N'Практикант%'
|
||||||
ORDER BY p.Name ASC;
|
ORDER BY p.Name ASC;
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# 2. Сырые события физических проходов турникетов
|
||||||
|
SQL_RAW_EVENTS_QUERY = r"""
|
||||||
|
DECLARE @InputDate DATE = '{target_date}';
|
||||||
|
DECLARE @StartDate DATETIME = CAST(@InputDate AS DATETIME);
|
||||||
|
DECLARE @EndDate DATETIME = {end_datetime_sql};
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
log.TimeVal,
|
||||||
|
log.HozOrgan,
|
||||||
|
LTRIM(RTRIM(
|
||||||
|
ISNULL(CAST(p.Name AS NVARCHAR(255)), N'') +
|
||||||
|
CASE WHEN p.FirstName IS NOT NULL AND CAST(p.FirstName AS NVARCHAR(255)) <> ''
|
||||||
|
THEN N' ' + CAST(p.FirstName AS NVARCHAR(255)) ELSE N'' END +
|
||||||
|
CASE WHEN p.MidName IS NOT NULL AND CAST(p.MidName AS NVARCHAR(255)) <> ''
|
||||||
|
THEN N' ' + CAST(p.MidName AS NVARCHAR(255)) ELSE N'' END
|
||||||
|
)) AS [Сотрудник],
|
||||||
|
ISNULL(CAST(div.Name AS NVARCHAR(255)), N'Без подразделения') AS [Подразделение],
|
||||||
|
log.Event,
|
||||||
|
log.Mode,
|
||||||
|
log.DoorIndex,
|
||||||
|
CASE
|
||||||
|
WHEN log.Mode = 2 THEN 'OUT'
|
||||||
|
WHEN log.Mode = 1 THEN 'IN'
|
||||||
|
WHEN log.Event IN (2, 27, 29, 33, 55, 65) THEN 'OUT'
|
||||||
|
WHEN log.Event IN (1, 21, 26, 54, 64) THEN 'IN'
|
||||||
|
ELSE 'OTHER'
|
||||||
|
END AS Direction
|
||||||
|
FROM pLogData log WITH (NOLOCK)
|
||||||
|
INNER JOIN pList p WITH (NOLOCK) ON log.HozOrgan = p.ID
|
||||||
|
LEFT JOIN PDivision div WITH (NOLOCK) ON p.Section = div.ID
|
||||||
|
LEFT JOIN PPost post WITH (NOLOCK) ON p.Post = post.ID
|
||||||
|
WHERE log.TimeVal BETWEEN @StartDate AND @EndDate
|
||||||
|
AND log.HozOrgan IS NOT NULL
|
||||||
|
AND log.HozOrgan > 0
|
||||||
|
AND log.Event IN (28, 32)
|
||||||
|
-- ⭐️ Фильтрация сотрудников Ленморниипроект (исключение арендаторов и гостей)
|
||||||
|
AND ISNULL(p.StatusRecord, 0) = 0
|
||||||
|
AND p.DateTimeInArchive IS NULL
|
||||||
|
AND ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') NOT LIKE N'Аренд%'
|
||||||
|
AND ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') NOT IN (N'Без подразделения', N'')
|
||||||
|
AND p.Name NOT LIKE N'бр.%'
|
||||||
|
AND p.Name NOT LIKE N'Гость%'
|
||||||
|
AND ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') NOT IN (N'БГИ', N'КНР')
|
||||||
|
AND ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') NOT LIKE N'Рабоч%'
|
||||||
|
AND ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') NOT LIKE N'Врем%'
|
||||||
|
AND ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') NOT LIKE N'Практика%'
|
||||||
|
AND ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') NOT LIKE N'тест%'
|
||||||
|
AND ISNULL(CAST(post.Name AS NVARCHAR(255)), N'') NOT LIKE N'Практикант%'
|
||||||
|
ORDER BY log.TimeVal ASC;
|
||||||
|
"""
|
||||||
@@ -12,7 +12,7 @@ import pandas as pd
|
|||||||
|
|
||||||
from config import DATE_TODAY
|
from config import DATE_TODAY
|
||||||
from core.database import load_scud_from_db_by_snapshot
|
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.merger import merge_scud_and_1c, calculate_summary_metrics
|
||||||
from services.scud_etl.anomaly_detector import detect_registry_anomalies
|
from services.scud_etl.anomaly_detector import detect_registry_anomalies
|
||||||
from services.snapshots.finder import find_or_create_snapshot_for_time
|
from services.snapshots.finder import find_or_create_snapshot_for_time
|
||||||
@@ -27,37 +27,46 @@ def generate_svodka_service(
|
|||||||
snapshot_id: Optional[str] = None
|
snapshot_id: Optional[str] = None
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Формирует оперативную сводку на указанную дату / время:
|
Формирует оперативную сводку:
|
||||||
- target_date: дата сводки (по умолчанию сегодня).
|
- По умолчанию берет ПОСЛЕДНИЙ готовый снапшот из базы SQLite (без долгого опроса MS SQL).
|
||||||
- target_time: время среза (например '14:30').
|
|
||||||
- snapshot_id: точный ID среза.
|
|
||||||
"""
|
"""
|
||||||
date_clean = str(target_date or DATE_TODAY).replace('_', '.')
|
date_clean = str(target_date or DATE_TODAY).replace('_', '.')
|
||||||
applied_note = ""
|
applied_note = ""
|
||||||
|
|
||||||
# Если передано время, но не указан конкретный snapshot_id — ищем ближайший или запрашиваем экспорт
|
# 1. Загрузка среза СКУД строго из готовых в базе
|
||||||
if target_time and not snapshot_id:
|
if snapshot_id:
|
||||||
found_id, note = find_or_create_snapshot_for_time(date_clean, target_time, allow_ondemand_export=True)
|
|
||||||
snapshot_id = found_id
|
|
||||||
applied_note = note
|
|
||||||
if note:
|
|
||||||
logger.info(note)
|
|
||||||
|
|
||||||
df_scud = load_scud_from_db_by_snapshot(date_clean, snapshot_param=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
|
||||||
|
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)
|
||||||
|
|
||||||
if df_scud is None or df_scud.empty:
|
if df_scud is None or df_scud.empty:
|
||||||
return {
|
return {
|
||||||
"status": "error",
|
"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)
|
df_staff, df_abs = load_1c_files_for_date(date_clean)
|
||||||
|
|
||||||
|
# 3. Слияние и расчет
|
||||||
df_merged = merge_scud_and_1c(df_scud, df_staff, df_abs)
|
df_merged = merge_scud_and_1c(df_scud, df_staff, df_abs)
|
||||||
metrics = calculate_summary_metrics(df_merged)
|
metrics = calculate_summary_metrics(df_merged)
|
||||||
anomalies = detect_registry_anomalies(df_merged, df_raw_scud=df_scud)
|
anomalies = detect_registry_anomalies(df_merged, df_raw_scud=df_scud)
|
||||||
|
|
||||||
# Добавляем суффикс времени в имя файла, если сводка строилась на точный срез
|
# 4. Формирование книги Excel
|
||||||
time_suffix = f" на {target_time.replace(':', '-')}" if target_time else ""
|
time_suffix = f" на {actual_snap_time}" if actual_snap_time else ""
|
||||||
filename = f"{format_date_ru(date_clean)} сводка{time_suffix}.xlsx"
|
filename = f"{format_date_ru(date_clean)} сводка{time_suffix}.xlsx"
|
||||||
generate_summary_excel(df_merged, date_str=date_clean, filename=filename)
|
generate_summary_excel(df_merged, date_str=date_clean, filename=filename)
|
||||||
|
|
||||||
@@ -68,13 +77,13 @@ def generate_svodka_service(
|
|||||||
"status": "success",
|
"status": "success",
|
||||||
"report_type": "SVODKA",
|
"report_type": "SVODKA",
|
||||||
"date": date_clean,
|
"date": date_clean,
|
||||||
"target_time": target_time,
|
"target_time": actual_snap_time,
|
||||||
"snapshot_id": snapshot_id or "AUTO_LATEST",
|
"snapshot_id": snapshot_id or "LATEST_READY",
|
||||||
"filename": filename,
|
"filename": filename,
|
||||||
"filepath": full_filepath,
|
"filepath": full_filepath,
|
||||||
"download_url": f"/api/v1/files/download/reports/{os.path.basename(full_filepath)}",
|
"download_url": f"/api/v1/files/download/reports/{os.path.basename(full_filepath)}",
|
||||||
"metrics": metrics,
|
"metrics": metrics,
|
||||||
"anomalies_count": len(anomalies),
|
"anomalies_count": len(anomalies),
|
||||||
"note": applied_note,
|
"note": applied_note,
|
||||||
"message": f"Ежедневная сводка на {date_clean} {target_time or ''} успешно сформирована."
|
"message": f"Ежедневная сводка на {date_clean} успешно сформирована."
|
||||||
}
|
}
|
||||||
+283
-81
@@ -1,6 +1,11 @@
|
|||||||
"""
|
"""
|
||||||
Модуль автоматического экспорта данных СКУД (Orion) из MS SQL Server в SQLite и Excel.
|
===============================================================================
|
||||||
Добавлена фиксация Первой Активности (без учета направления) и среза по времени.
|
FILE: services/scud_export.py
|
||||||
|
ROLE: Прямой экспорт данных СКУД Орион (MS SQL) в SQLite и чистый Excel (XlsxWriter).
|
||||||
|
Корректная фильтрация транзитных проходов турникетов парковки и двора.
|
||||||
|
Учет только левого PERCo (DoorIndex = 1), правого PERCo (DoorIndex = 2)
|
||||||
|
и входа через Флигель (DoorIndex = 23).
|
||||||
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
@@ -10,12 +15,19 @@ import sys
|
|||||||
import warnings
|
import warnings
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
ROOT_DIR = os.path.abspath(os.path.join(CURRENT_DIR, ".."))
|
||||||
|
if ROOT_DIR not in sys.path:
|
||||||
|
sys.path.insert(0, ROOT_DIR)
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import pyodbc
|
import pyodbc
|
||||||
from openpyxl.utils import get_column_letter
|
import xlsxwriter
|
||||||
|
|
||||||
|
from services.scud_etl.sql_queries import SQL_QUERY_TEMPLATE, SQL_RAW_EVENTS_QUERY
|
||||||
from config import SCUD_DIR, clean_scud_fio_light, load_exceptions
|
from config import SCUD_DIR, clean_scud_fio_light, load_exceptions
|
||||||
from core.database import save_scud_to_db, has_scud_logs_for_date, has_yesterday_final_snapshot
|
from core.database import save_scud_to_db, has_scud_logs_for_date, has_yesterday_final_snapshot
|
||||||
|
from core.repositories.scud_repo import save_raw_events_to_db
|
||||||
|
|
||||||
warnings.filterwarnings("ignore", message="pandas only supports SQLAlchemy connectable")
|
warnings.filterwarnings("ignore", message="pandas only supports SQLAlchemy connectable")
|
||||||
|
|
||||||
@@ -31,12 +43,17 @@ logger.setLevel(logging.INFO)
|
|||||||
logger.propagate = False
|
logger.propagate = False
|
||||||
|
|
||||||
if not logger.handlers:
|
if not logger.handlers:
|
||||||
|
try:
|
||||||
_file_handler = logging.FileHandler(LOG_FILE, encoding="utf-8")
|
_file_handler = logging.FileHandler(LOG_FILE, encoding="utf-8")
|
||||||
_console_handler = logging.StreamHandler(sys.stdout)
|
|
||||||
_formatter = logging.Formatter("[%(asctime)s] [%(levelname)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
|
_formatter = logging.Formatter("[%(asctime)s] [%(levelname)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
|
||||||
_file_handler.setFormatter(_formatter)
|
_file_handler.setFormatter(_formatter)
|
||||||
_console_handler.setFormatter(_formatter)
|
|
||||||
logger.addHandler(_file_handler)
|
logger.addHandler(_file_handler)
|
||||||
|
except (PermissionError, OSError) as e:
|
||||||
|
sys.stderr.write(f"Предупреждение: невозможно создать лог-файл {LOG_FILE}: {e}\n")
|
||||||
|
|
||||||
|
_console_handler = logging.StreamHandler(sys.stdout)
|
||||||
|
_formatter = logging.Formatter("[%(asctime)s] [%(levelname)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
|
||||||
|
_console_handler.setFormatter(_formatter)
|
||||||
logger.addHandler(_console_handler)
|
logger.addHandler(_console_handler)
|
||||||
|
|
||||||
|
|
||||||
@@ -64,25 +81,42 @@ DECLARE @InputDate DATE = '{target_date}';
|
|||||||
DECLARE @TargetDate DATE = @InputDate;
|
DECLARE @TargetDate DATE = @InputDate;
|
||||||
|
|
||||||
DECLARE @StartDate DATETIME = CAST(@TargetDate AS DATETIME);
|
DECLARE @StartDate DATETIME = CAST(@TargetDate AS DATETIME);
|
||||||
DECLARE @EndDate DATETIME = DATEADD(SECOND, -1, DATEADD(DAY, 1, @StartDate));
|
DECLARE @EndDate DATETIME = {end_datetime_sql};
|
||||||
|
|
||||||
WITH DailyLogs AS (
|
WITH PercoPassages AS (
|
||||||
|
-- Физические факты прохода (Event = 32)
|
||||||
SELECT
|
SELECT
|
||||||
log.HozOrgan AS EmployeeID,
|
log.HozOrgan AS EmployeeID,
|
||||||
log.TimeVal,
|
log.TimeVal,
|
||||||
log.Event,
|
|
||||||
log.Mode,
|
|
||||||
CASE
|
CASE
|
||||||
WHEN log.Mode = 2 OR log.Event IN (29, 27, 33) THEN 'OUT'
|
WHEN log.Mode = 1 THEN 'IN'
|
||||||
WHEN log.Mode = 1 OR log.Event IN (28, 26, 32) THEN 'IN'
|
WHEN log.Mode = 2 THEN 'OUT'
|
||||||
ELSE 'OTHER'
|
ELSE 'OTHER'
|
||||||
END AS Direction,
|
END AS Direction
|
||||||
ROW_NUMBER() OVER (PARTITION BY log.HozOrgan ORDER BY log.TimeVal DESC) AS RowNumDesc
|
|
||||||
FROM pLogData log WITH (NOLOCK)
|
FROM pLogData log WITH (NOLOCK)
|
||||||
|
INNER JOIN pList p WITH (NOLOCK) ON log.HozOrgan = p.ID
|
||||||
|
LEFT JOIN PDivision div WITH (NOLOCK) ON p.Section = div.ID
|
||||||
WHERE log.TimeVal BETWEEN @StartDate AND @EndDate
|
WHERE log.TimeVal BETWEEN @StartDate AND @EndDate
|
||||||
AND log.HozOrgan IS NOT NULL
|
AND log.HozOrgan IS NOT NULL
|
||||||
AND log.HozOrgan > 0
|
AND log.HozOrgan > 0
|
||||||
AND log.Event IN (26, 27, 28, 29, 32, 33, 54, 55, 64, 65)
|
AND log.Event = 32
|
||||||
|
AND log.Mode IN (1, 2)
|
||||||
|
AND (
|
||||||
|
-- Контур 1: Левый турникет открыт для всех
|
||||||
|
log.DoorIndex = 1
|
||||||
|
OR
|
||||||
|
-- Контур 2: Правый турникет разрешен только для реестра двора
|
||||||
|
(
|
||||||
|
log.DoorIndex = 2
|
||||||
|
AND ({turnstile_filter_sql})
|
||||||
|
)
|
||||||
|
OR
|
||||||
|
-- Контур 3: Флигель 1 эт. разрешен только для реестра флигеля
|
||||||
|
(
|
||||||
|
log.DoorIndex = 23
|
||||||
|
AND ({fligel_filter_sql})
|
||||||
|
)
|
||||||
|
)
|
||||||
),
|
),
|
||||||
Passages AS (
|
Passages AS (
|
||||||
SELECT
|
SELECT
|
||||||
@@ -90,10 +124,21 @@ Passages AS (
|
|||||||
MIN(TimeVal) AS FirstRawEvent,
|
MIN(TimeVal) AS FirstRawEvent,
|
||||||
MAX(TimeVal) AS LastRawEvent,
|
MAX(TimeVal) AS LastRawEvent,
|
||||||
MIN(CASE WHEN Direction = 'IN' THEN TimeVal END) AS FirstIn,
|
MIN(CASE WHEN Direction = 'IN' THEN TimeVal END) AS FirstIn,
|
||||||
MAX(CASE WHEN Direction = 'OUT' THEN TimeVal END) AS LastOut,
|
MAX(CASE WHEN Direction = 'OUT' THEN TimeVal END) AS FinalOut
|
||||||
MAX(CASE WHEN RowNumDesc = 1 THEN Direction END) AS LastEventType
|
FROM PercoPassages
|
||||||
FROM DailyLogs
|
|
||||||
GROUP BY EmployeeID
|
GROUP BY EmployeeID
|
||||||
|
),
|
||||||
|
EvaluatedPassages AS (
|
||||||
|
SELECT
|
||||||
|
p.*,
|
||||||
|
CASE
|
||||||
|
WHEN p.FinalOut IS NOT NULL
|
||||||
|
AND p.FirstIn IS NOT NULL
|
||||||
|
AND p.FinalOut > DATEADD(MINUTE, 5, p.FirstIn)
|
||||||
|
THEN p.FinalOut
|
||||||
|
ELSE NULL
|
||||||
|
END AS FilteredLastOut
|
||||||
|
FROM Passages p
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
N'ЛЕНМОРНИИПРОЕКТ' AS [Фирма],
|
N'ЛЕНМОРНИИПРОЕКТ' AS [Фирма],
|
||||||
@@ -115,12 +160,8 @@ SELECT
|
|||||||
ELSE N'—'
|
ELSE N'—'
|
||||||
END AS [Первая_активность],
|
END AS [Первая_активность],
|
||||||
CASE
|
CASE
|
||||||
WHEN @TargetDate = CAST(GETDATE() AS DATE) AND (pass.LastEventType = 'IN' OR pass.LastOut IS NULL OR pass.LastOut <= pass.FirstIn)
|
WHEN pass.FilteredLastOut IS NOT NULL
|
||||||
THEN N'Нет выхода'
|
THEN CAST(CONVERT(VARCHAR(8), pass.FilteredLastOut, 108) AS NVARCHAR(20))
|
||||||
WHEN pass.LastOut IS NOT NULL AND pass.LastOut > pass.FirstIn
|
|
||||||
THEN CAST(CONVERT(VARCHAR(8), pass.LastOut, 108) AS NVARCHAR(20))
|
|
||||||
WHEN @TargetDate < CAST(GETDATE() AS DATE) AND pass.LastRawEvent IS NOT NULL AND pass.LastRawEvent > ISNULL(pass.FirstIn, pass.FirstRawEvent)
|
|
||||||
THEN CAST(CONVERT(VARCHAR(8), pass.LastRawEvent, 108) AS NVARCHAR(20))
|
|
||||||
ELSE N'Нет выхода'
|
ELSE N'Нет выхода'
|
||||||
END AS [Конец_дня],
|
END AS [Конец_дня],
|
||||||
CASE
|
CASE
|
||||||
@@ -128,14 +169,14 @@ SELECT
|
|||||||
RIGHT('0' + CAST(DATEDIFF(MINUTE,
|
RIGHT('0' + CAST(DATEDIFF(MINUTE,
|
||||||
ISNULL(pass.FirstIn, pass.FirstRawEvent),
|
ISNULL(pass.FirstIn, pass.FirstRawEvent),
|
||||||
CASE
|
CASE
|
||||||
WHEN @TargetDate = CAST(GETDATE() AS DATE) AND (pass.LastEventType = 'IN' OR pass.LastOut IS NULL OR pass.LastOut <= pass.FirstIn) THEN GETDATE()
|
WHEN pass.FilteredLastOut IS NOT NULL THEN pass.FilteredLastOut
|
||||||
ELSE ISNULL(pass.LastOut, pass.LastRawEvent)
|
ELSE @EndDate
|
||||||
END) / 60 AS VARCHAR), 2) + ':' +
|
END) / 60 AS VARCHAR), 2) + ':' +
|
||||||
RIGHT('0' + CAST(DATEDIFF(MINUTE,
|
RIGHT('0' + CAST(DATEDIFF(MINUTE,
|
||||||
ISNULL(pass.FirstIn, pass.FirstRawEvent),
|
ISNULL(pass.FirstIn, pass.FirstRawEvent),
|
||||||
CASE
|
CASE
|
||||||
WHEN @TargetDate = CAST(GETDATE() AS DATE) AND (pass.LastEventType = 'IN' OR pass.LastOut IS NULL OR pass.LastOut <= pass.FirstIn) THEN GETDATE()
|
WHEN pass.FilteredLastOut IS NOT NULL THEN pass.FilteredLastOut
|
||||||
ELSE ISNULL(pass.LastOut, pass.LastRawEvent)
|
ELSE @EndDate
|
||||||
END) % 60 AS VARCHAR), 2)
|
END) % 60 AS VARCHAR), 2)
|
||||||
ELSE N'00:00'
|
ELSE N'00:00'
|
||||||
END AS [Находился_в_здании],
|
END AS [Находился_в_здании],
|
||||||
@@ -146,7 +187,7 @@ SELECT
|
|||||||
FROM pList p WITH (NOLOCK)
|
FROM pList p WITH (NOLOCK)
|
||||||
LEFT JOIN PDivision div WITH (NOLOCK) ON p.Section = div.ID
|
LEFT JOIN PDivision div WITH (NOLOCK) ON p.Section = div.ID
|
||||||
LEFT JOIN PPost post WITH (NOLOCK) ON p.Post = post.ID
|
LEFT JOIN PPost post WITH (NOLOCK) ON p.Post = post.ID
|
||||||
LEFT JOIN Passages pass ON p.ID = pass.EmployeeID
|
LEFT JOIN EvaluatedPassages pass ON p.ID = pass.EmployeeID
|
||||||
WHERE
|
WHERE
|
||||||
ISNULL(p.StatusRecord, 0) = 0
|
ISNULL(p.StatusRecord, 0) = 0
|
||||||
AND p.DateTimeInArchive IS NULL
|
AND p.DateTimeInArchive IS NULL
|
||||||
@@ -163,33 +204,113 @@ WHERE
|
|||||||
ORDER BY p.Name ASC;
|
ORDER BY p.Name ASC;
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
SQL_RAW_EVENTS_QUERY = r"""
|
||||||
|
DECLARE @InputDate DATE = '{target_date}';
|
||||||
|
DECLARE @StartDate DATETIME = CAST(@InputDate AS DATETIME);
|
||||||
|
DECLARE @EndDate DATETIME = {end_datetime_sql};
|
||||||
|
|
||||||
def auto_fit_columns(file_path: str, sheet_name: str, padding: float = 2.0, min_width: float = 8.0, max_width: float = 60.0):
|
SELECT
|
||||||
from openpyxl import load_workbook
|
log.TimeVal,
|
||||||
|
log.HozOrgan,
|
||||||
wb = load_workbook(file_path)
|
LTRIM(RTRIM(
|
||||||
ws = wb[sheet_name]
|
ISNULL(CAST(p.Name AS NVARCHAR(255)), N'') +
|
||||||
|
CASE WHEN p.FirstName IS NOT NULL AND CAST(p.FirstName AS NVARCHAR(255)) <> ''
|
||||||
for col_cells in ws.columns:
|
THEN N' ' + CAST(p.FirstName AS NVARCHAR(255)) ELSE N'' END +
|
||||||
max_len = 0
|
CASE WHEN p.MidName IS NOT NULL AND CAST(p.MidName AS NVARCHAR(255)) <> ''
|
||||||
col_letter = get_column_letter(col_cells[0].column)
|
THEN N' ' + CAST(p.MidName AS NVARCHAR(255)) ELSE N'' END
|
||||||
for cell in col_cells:
|
)) AS [Сотрудник],
|
||||||
if cell.value is not None:
|
ISNULL(CAST(div.Name AS NVARCHAR(255)), N'Без подразделения') AS [Подразделение],
|
||||||
cell_len = len(str(cell.value))
|
log.Event,
|
||||||
if cell_len > max_len:
|
log.Mode,
|
||||||
max_len = cell_len
|
log.DoorIndex,
|
||||||
width = max(min_width, min(max_len + padding, max_width))
|
CASE
|
||||||
ws.column_dimensions[col_letter].width = width
|
WHEN log.Mode = 2 THEN 'OUT'
|
||||||
|
WHEN log.Mode = 1 THEN 'IN'
|
||||||
wb.save(file_path)
|
WHEN log.Event IN (2, 27, 29, 33, 55, 65) THEN 'OUT'
|
||||||
|
WHEN log.Event IN (1, 21, 26, 54, 64) THEN 'IN'
|
||||||
|
ELSE 'OTHER'
|
||||||
|
END AS Direction
|
||||||
|
FROM pLogData log WITH (NOLOCK)
|
||||||
|
INNER JOIN pList p WITH (NOLOCK) ON log.HozOrgan = p.ID
|
||||||
|
LEFT JOIN PDivision div WITH (NOLOCK) ON p.Section = div.ID
|
||||||
|
LEFT JOIN PPost post WITH (NOLOCK) ON p.Post = post.ID
|
||||||
|
WHERE log.TimeVal BETWEEN @StartDate AND @EndDate
|
||||||
|
AND log.HozOrgan IS NOT NULL
|
||||||
|
AND log.HozOrgan > 0
|
||||||
|
AND log.Event IN (28, 32)
|
||||||
|
-- ⭐️ Фильтрация сотрудников Ленморниипроект (исключение арендаторов и гостей)
|
||||||
|
AND ISNULL(p.StatusRecord, 0) = 0
|
||||||
|
AND p.DateTimeInArchive IS NULL
|
||||||
|
AND ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') NOT LIKE N'Аренд%'
|
||||||
|
AND ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') NOT IN (N'Без подразделения', N'')
|
||||||
|
AND p.Name NOT LIKE N'бр.%'
|
||||||
|
AND p.Name NOT LIKE N'Гость%'
|
||||||
|
AND ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') NOT IN (N'БГИ', N'КНР')
|
||||||
|
AND ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') NOT LIKE N'Рабоч%'
|
||||||
|
AND ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') NOT LIKE N'Врем%'
|
||||||
|
AND ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') NOT LIKE N'Практика%'
|
||||||
|
AND ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') NOT LIKE N'тест%'
|
||||||
|
AND ISNULL(CAST(post.Name AS NVARCHAR(255)), N'') NOT LIKE N'Практикант%'
|
||||||
|
ORDER BY log.TimeVal ASC;
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
def get_targets(input_date: str | None):
|
def save_df_to_clean_excel(df: pd.DataFrame, file_path: str, sheet_name: str = "Отчет"):
|
||||||
|
workbook = xlsxwriter.Workbook(file_path, {'constant_memory': False})
|
||||||
|
worksheet = workbook.add_worksheet(sheet_name)
|
||||||
|
|
||||||
|
fmt_header = workbook.add_format({
|
||||||
|
'bold': True,
|
||||||
|
'bg_color': '#D9E1F2',
|
||||||
|
'border': 1,
|
||||||
|
'border_color': '#D3D3D3',
|
||||||
|
'align': 'center',
|
||||||
|
'valign': 'vcenter',
|
||||||
|
'font_name': 'Calibri',
|
||||||
|
'font_size': 11
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt_cell = workbook.add_format({
|
||||||
|
'border': 1,
|
||||||
|
'border_color': '#D3D3D3',
|
||||||
|
'valign': 'vcenter',
|
||||||
|
'align': 'left',
|
||||||
|
'font_name': 'Calibri',
|
||||||
|
'font_size': 11
|
||||||
|
})
|
||||||
|
|
||||||
|
headers = list(df.columns)
|
||||||
|
col_widths = [len(str(h)) for h in headers]
|
||||||
|
|
||||||
|
for col_idx, header in enumerate(headers):
|
||||||
|
worksheet.write(0, col_idx, str(header), fmt_header)
|
||||||
|
|
||||||
|
for row_idx, row_values in enumerate(df.values, start=1):
|
||||||
|
for col_idx, val in enumerate(row_values):
|
||||||
|
if pd.isna(val) or val is None:
|
||||||
|
val_str = ""
|
||||||
|
elif isinstance(val, bool):
|
||||||
|
val_str = "Да" if val else "Нет"
|
||||||
|
else:
|
||||||
|
val_str = str(val)
|
||||||
|
|
||||||
|
worksheet.write(row_idx, col_idx, val_str, fmt_cell)
|
||||||
|
|
||||||
|
if len(val_str) > col_widths[col_idx]:
|
||||||
|
col_widths[col_idx] = len(val_str)
|
||||||
|
|
||||||
|
for col_idx, width in enumerate(col_widths):
|
||||||
|
worksheet.set_column(col_idx, col_idx, min(max(width + 3, 10), 45))
|
||||||
|
|
||||||
|
workbook.close()
|
||||||
|
|
||||||
|
|
||||||
|
def get_targets(input_date: str | None, input_time: str | None = None):
|
||||||
targets = []
|
targets = []
|
||||||
if input_date:
|
if input_date:
|
||||||
try:
|
try:
|
||||||
parsed = datetime.strptime(input_date, "%d.%m.%Y").date()
|
parsed = datetime.strptime(input_date.replace('_', '.'), "%d.%m.%Y").date()
|
||||||
targets.append({"name": "Указанная дата", "date": parsed})
|
targets.append({"name": "Указанная дата", "date": parsed, "target_time": input_time})
|
||||||
except ValueError:
|
except ValueError:
|
||||||
log(f"ОШИБКА: Неверный формат даты '{input_date}'. Используйте ДД.ММ.ГГГГ", "ERROR")
|
log(f"ОШИБКА: Неверный формат даты '{input_date}'. Используйте ДД.ММ.ГГГГ", "ERROR")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@@ -198,13 +319,13 @@ def get_targets(input_date: str | None):
|
|||||||
yesterday = (now - timedelta(days=3 if now.weekday() == 0 else 1)).date()
|
yesterday = (now - timedelta(days=3 if now.weekday() == 0 else 1)).date()
|
||||||
today = now.date()
|
today = now.date()
|
||||||
|
|
||||||
targets.append({"name": "Вчера", "date": yesterday})
|
targets.append({"name": "Вчера", "date": yesterday, "target_time": None})
|
||||||
targets.append({"name": "Сегодня", "date": today})
|
targets.append({"name": "Сегодня", "date": today, "target_time": None})
|
||||||
|
|
||||||
return targets
|
return targets
|
||||||
|
|
||||||
|
|
||||||
def run_export(input_date: str | None = None, save_xlsx: bool = True, debug: bool = False):
|
def run_export(input_date: str | None = None, input_time: str | None = None, save_xlsx: bool = True, debug: bool = False):
|
||||||
if debug:
|
if debug:
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
log("=== ВКЛЮЧЕН РЕЖИМ ОТЛАДКИ (DEBUG MODE) ===", "WARNING")
|
log("=== ВКЛЮЧЕН РЕЖИМ ОТЛАДКИ (DEBUG MODE) ===", "WARNING")
|
||||||
@@ -212,7 +333,27 @@ def run_export(input_date: str | None = None, save_xlsx: bool = True, debug: boo
|
|||||||
log("=== [ЭТАП 0] Выгрузка свежих данных СКУД напрямую из БД Орион ===")
|
log("=== [ЭТАП 0] Выгрузка свежих данных СКУД напрямую из БД Орион ===")
|
||||||
os.makedirs(SCUD_DIR, exist_ok=True)
|
os.makedirs(SCUD_DIR, exist_ok=True)
|
||||||
|
|
||||||
targets = get_targets(input_date)
|
# ⭐️ 1. ПОЛУЧАСОВАЯ СИНХРОНИЗАЦИЯ 1С:ЗУП (Файлы с шары + База данных)
|
||||||
|
try:
|
||||||
|
log("[1C] Проверка сетевой шары и синхронизация файлов 1С...")
|
||||||
|
copy_1c_files_from_share()
|
||||||
|
|
||||||
|
target_sync_date = input_date or datetime.now().strftime("%d.%m.%Y")
|
||||||
|
# Прямой опрос свежих отсутствий и сохранение в SQLite
|
||||||
|
df_fresh_abs = load_absent_data(target_sync_date)
|
||||||
|
if df_fresh_abs is not None and not df_fresh_abs.empty:
|
||||||
|
save_absences_to_db(df_fresh_abs, target_sync_date)
|
||||||
|
log(f"[1C] [✓] Актуализировано {len(df_fresh_abs)} отсутствий в SQLite за {target_sync_date}", "SUCCESS")
|
||||||
|
|
||||||
|
# Если на шаре появился свежий штат — также обновляем его в базе
|
||||||
|
df_fresh_staff = load_staff_data(target_sync_date)
|
||||||
|
if df_fresh_staff is not None and not df_fresh_staff.empty:
|
||||||
|
save_staff_to_db(df_fresh_staff, target_sync_date)
|
||||||
|
log(f"[1C] [✓] Актуализирован штат ({len(df_fresh_staff)} чел.) в SQLite за {target_sync_date}", "SUCCESS")
|
||||||
|
except Exception as e:
|
||||||
|
log(f"[1C] ⚠️ Ошибка фоновой синхронизации 1С: {e}", "WARNING")
|
||||||
|
|
||||||
|
targets = get_targets(input_date, input_time)
|
||||||
conn_str = (
|
conn_str = (
|
||||||
f"DRIVER={{{ODBC_DRIVER}}};"
|
f"DRIVER={{{ODBC_DRIVER}}};"
|
||||||
f"SERVER={SERVER_NAME};"
|
f"SERVER={SERVER_NAME};"
|
||||||
@@ -228,21 +369,83 @@ def run_export(input_date: str | None = None, save_xlsx: bool = True, debug: boo
|
|||||||
processing_date = target["date"]
|
processing_date = target["date"]
|
||||||
processing_date_str = processing_date.strftime("%d.%m.%Y")
|
processing_date_str = processing_date.strftime("%d.%m.%Y")
|
||||||
period_label = target["name"]
|
period_label = target["name"]
|
||||||
is_yesterday = (period_label == "Вчера")
|
target_time = target["target_time"]
|
||||||
|
|
||||||
# ⚡️ ПРОВЕРКА НАЛИЧИЯ ВЧЕРАШНЕГО ДНЯ В SQLITE:
|
today_date = datetime.now().date()
|
||||||
# Если это вчерашний день и его снапшот уже зафиксирован в SQLite — пропускаем тяжелый запрос к MS SQL
|
is_past_day = (processing_date < today_date) or (period_label == "Вчера")
|
||||||
if is_yesterday and has_yesterday_final_snapshot(processing_date_str):
|
|
||||||
log(f"[ℹ️] Вчерашний день ({processing_date_str}) уже зафиксирован финишным снапшотом Y. Пропускаем запрос к MS SQL.")
|
if is_past_day and not target_time and has_yesterday_final_snapshot(processing_date_str):
|
||||||
|
log(f"[ℹ️] День ({processing_date_str}) уже зафиксирован финишным снапшотом _FINAL. Пропускаем.")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if is_yesterday:
|
exc_data = load_exceptions()
|
||||||
|
|
||||||
|
# 1. Формируем фильтр правого турникета (DoorIndex = 2)
|
||||||
|
t_fios = [f.replace("'", "''") for f in exc_data.get('turnstile_fio', []) if f]
|
||||||
|
t_depts = [d.replace("'", "''") for d in exc_data.get('turnstile_departments', []) if d]
|
||||||
|
|
||||||
|
full_fio_sql = (
|
||||||
|
"LTRIM(RTRIM("
|
||||||
|
"ISNULL(CAST(p.Name AS NVARCHAR(255)), N'') + "
|
||||||
|
"CASE WHEN p.FirstName IS NOT NULL AND CAST(p.FirstName AS NVARCHAR(255)) <> '' THEN N' ' + CAST(p.FirstName AS NVARCHAR(255)) ELSE N'' END + "
|
||||||
|
"CASE WHEN p.MidName IS NOT NULL AND CAST(p.MidName AS NVARCHAR(255)) <> '' THEN N' ' + CAST(p.MidName AS NVARCHAR(255)) ELSE N'' END"
|
||||||
|
"))"
|
||||||
|
)
|
||||||
|
|
||||||
|
conditions = []
|
||||||
|
if t_fios:
|
||||||
|
fio_in = ", ".join([f"N'{f}'" for f in t_fios])
|
||||||
|
conditions.append(f"{full_fio_sql} IN ({fio_in})")
|
||||||
|
|
||||||
|
if t_depts:
|
||||||
|
dept_in = ", ".join([f"N'{d}'" for d in t_depts])
|
||||||
|
conditions.append(f"ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') IN ({dept_in})")
|
||||||
|
|
||||||
|
turnstile_filter_sql = " OR ".join(conditions) if conditions else "1 = 0"
|
||||||
|
|
||||||
|
# 1.1 Формируем фильтр Флигеля (DoorIndex = 23)
|
||||||
|
fl_fios = [f.replace("'", "''") for f in exc_data.get('fligel_fio', []) if f]
|
||||||
|
fl_depts = [d.replace("'", "''") for d in exc_data.get('fligel_departments', []) if d]
|
||||||
|
|
||||||
|
fl_conditions = []
|
||||||
|
if fl_fios:
|
||||||
|
fl_fio_in = ", ".join([f"N'{f}'" for f in fl_fios])
|
||||||
|
fl_conditions.append(f"{full_fio_sql} IN ({fl_fio_in})")
|
||||||
|
|
||||||
|
if fl_depts:
|
||||||
|
fl_dept_in = ", ".join([f"N'{d}'" for d in fl_depts])
|
||||||
|
fl_conditions.append(f"ISNULL(CAST(div.Name AS NVARCHAR(255)), N'') IN ({fl_dept_in})")
|
||||||
|
|
||||||
|
fligel_filter_sql = " OR ".join(fl_conditions) if fl_conditions else "1 = 0"
|
||||||
|
|
||||||
|
# 2. Безопасное математическое определение @EndDate через DATEADD
|
||||||
|
if target_time:
|
||||||
|
t_clean = target_time.strip()
|
||||||
|
t_parts = t_clean.split(":")
|
||||||
|
h = int(t_parts[0])
|
||||||
|
m = int(t_parts[1]) if len(t_parts) > 1 else 0
|
||||||
|
s = int(t_parts[2]) if len(t_parts) > 2 else 0
|
||||||
|
|
||||||
|
snapshot_time = f"{processing_date.strftime('%Y-%m-%d')} {h:02d}:{m:02d}:{s:02d}"
|
||||||
|
end_datetime_sql = f"DATEADD(SECOND, {s}, DATEADD(MINUTE, {m}, DATEADD(HOUR, {h}, @StartDate)))"
|
||||||
|
is_final = False
|
||||||
|
elif is_past_day:
|
||||||
snapshot_time = f"{processing_date.strftime('%Y-%m-%d')} 23:59:59"
|
snapshot_time = f"{processing_date.strftime('%Y-%m-%d')} 23:59:59"
|
||||||
|
end_datetime_sql = "DATEADD(SECOND, -1, DATEADD(DAY, 1, @StartDate))"
|
||||||
|
is_final = True
|
||||||
else:
|
else:
|
||||||
snapshot_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
snapshot_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
end_datetime_sql = "GETDATE()"
|
||||||
|
is_final = False
|
||||||
|
|
||||||
log(f"--- Обработка периода: {period_label} ({processing_date_str}) --- [Снапшот: {snapshot_time}]")
|
log(f"--- Обработка периода: {period_label} ({processing_date_str}) --- [Срез: {snapshot_time}]")
|
||||||
sql_query = SQL_QUERY_TEMPLATE.format(target_date=processing_date.strftime("%Y-%m-%d"))
|
|
||||||
|
sql_query = SQL_QUERY_TEMPLATE.format(
|
||||||
|
target_date=processing_date.strftime("%Y-%m-%d"),
|
||||||
|
end_datetime_sql=end_datetime_sql,
|
||||||
|
turnstile_filter_sql=turnstile_filter_sql,
|
||||||
|
fligel_filter_sql=fligel_filter_sql
|
||||||
|
)
|
||||||
|
|
||||||
connection = None
|
connection = None
|
||||||
try:
|
try:
|
||||||
@@ -260,7 +463,18 @@ def run_export(input_date: str | None = None, save_xlsx: bool = True, debug: boo
|
|||||||
|
|
||||||
df['Пришел'] = df['Статус'].str.contains('Присутствовал', case=False, na=False) & (~mask_anomaly)
|
df['Пришел'] = df['Статус'].str.contains('Присутствовал', case=False, na=False) & (~mask_anomaly)
|
||||||
|
|
||||||
save_scud_to_db(df, processing_date_str, snapshot_time=snapshot_time, is_yesterday=is_yesterday)
|
save_scud_to_db(df, processing_date_str, snapshot_time=snapshot_time, is_yesterday=is_final)
|
||||||
|
|
||||||
|
raw_sql = SQL_RAW_EVENTS_QUERY.format(
|
||||||
|
target_date=processing_date.strftime("%Y-%m-%d"),
|
||||||
|
end_datetime_sql=end_datetime_sql
|
||||||
|
)
|
||||||
|
df_raw = pd.read_sql(raw_sql, connection)
|
||||||
|
if len(df_raw) > 0:
|
||||||
|
df_raw['fio_clean'] = df_raw['Сотрудник'].apply(clean_scud_fio_light)
|
||||||
|
inserted_count = save_raw_events_to_db(df_raw, processing_date_str)
|
||||||
|
log(f"[✓] В scud_events_raw сохранено {inserted_count} сырых событий за {processing_date_str}!", "SUCCESS")
|
||||||
|
|
||||||
log(f"[✓] Записи за {processing_date_str} успешно сохранены в SQLite!", "SUCCESS")
|
log(f"[✓] Записи за {processing_date_str} успешно сохранены в SQLite!", "SUCCESS")
|
||||||
|
|
||||||
if save_xlsx:
|
if save_xlsx:
|
||||||
@@ -273,8 +487,7 @@ def run_export(input_date: str | None = None, save_xlsx: bool = True, debug: boo
|
|||||||
except OSError as e:
|
except OSError as e:
|
||||||
log(f"ОШИБКА при удалении старого файла {file_name}: {e}", "ERROR")
|
log(f"ОШИБКА при удалении старого файла {file_name}: {e}", "ERROR")
|
||||||
|
|
||||||
df.to_excel(file_path, sheet_name="Отчет", index=False, engine="openpyxl")
|
save_df_to_clean_excel(df, file_path, sheet_name="Отчет")
|
||||||
auto_fit_columns(file_path, sheet_name="Отчет")
|
|
||||||
log(f"[✓] Успешно экспортирован файл: data/scud/{file_name}", "SUCCESS")
|
log(f"[✓] Успешно экспортирован файл: data/scud/{file_name}", "SUCCESS")
|
||||||
else:
|
else:
|
||||||
log(f"Запрос за {processing_date_str} вернул 0 строк.", "WARNING")
|
log(f"Запрос за {processing_date_str} вернул 0 строк.", "WARNING")
|
||||||
@@ -291,22 +504,11 @@ def run_export(input_date: str | None = None, save_xlsx: bool = True, debug: boo
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
help_text = """
|
parser = argparse.ArgumentParser()
|
||||||
Модуль прямого экспорта данных СКУД Орион Pro (MS SQL Server) в SQLite и Excel.
|
parser.add_argument("--date", dest="input_date", default=None, help="Дата среза (ДД.ММ.ГГГГ)")
|
||||||
|
parser.add_argument("--time", dest="input_time", default=None, help="Время среза (ЧЧ:ММ)")
|
||||||
ПРИМЕРЫ ЗАПУСКА:
|
parser.add_argument("-d", "--debug", action="store_true")
|
||||||
python services/scud_export.py -- Автоматическая выгрузка за Сегодня и Вчера
|
parser.add_argument("--no-xlsx", dest="save_xlsx", action="store_false", default=True)
|
||||||
python services/scud_export.py --date 05.08.2026 -- Точечная выгрузка СКУД за конкретную дату
|
|
||||||
python services/scud_export.py --no-xlsx -- Сохранение логов СКУД ТОЛЬКО в SQLite без дублирования в XLSX
|
|
||||||
python services/scud_export.py -d -- Запуск в режиме отладки MS SQL запросов (DEBUG)
|
|
||||||
"""
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
description=help_text,
|
|
||||||
formatter_class=argparse.RawDescriptionHelpFormatter
|
|
||||||
)
|
|
||||||
parser.add_argument("--date", dest="input_date", default=None, help="Дата выгрузки в формате ДД.ММ.ГГГГ (по умолчанию выгружаются Вчера и Сегодня)")
|
|
||||||
parser.add_argument("-d", "--debug", action="store_true", help="Включить подробные логи отладки подключения и запросов к MS SQL")
|
|
||||||
parser.add_argument("--no-xlsx", dest="save_xlsx", action="store_false", default=True, help="Отключить генерирование промежуточных XLSX-файлов в папке data/scud/")
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
run_export(args.input_date, save_xlsx=args.save_xlsx, debug=args.debug)
|
run_export(args.input_date, input_time=args.input_time, save_xlsx=args.save_xlsx, debug=args.debug)
|
||||||
@@ -1,3 +1,10 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: services/share_copier.py
|
||||||
|
ROLE: Синхронизация файлов 1С (Штат и Отсутствия) с сетевой шары в data/1c/.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
from config import DATE_TODAY, DATE_YESTERDAY, ZUP_1C_DIR, SHARE_1C_DIR
|
from config import DATE_TODAY, DATE_YESTERDAY, ZUP_1C_DIR, SHARE_1C_DIR
|
||||||
@@ -52,7 +59,9 @@ def copy_1c_files_from_share():
|
|||||||
local_target_path = os.path.join(ZUP_1C_DIR, filename)
|
local_target_path = os.path.join(ZUP_1C_DIR, filename)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
shutil.copy2(remote_file, local_target_path)
|
# shutil.copyfile копирует только содержимое потока байтов
|
||||||
|
# без попыток изменить POSIX-права/атрибуты (chmod) на CIFS/SMB шаре
|
||||||
|
shutil.copyfile(remote_file, local_target_path)
|
||||||
print(f" [✓] Успешно скопирован с шары: {filename} -> data/1c/")
|
print(f" [✓] Успешно скопирован с шары: {filename} -> data/1c/")
|
||||||
copied_count += 1
|
copied_count += 1
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -0,0 +1,132 @@
|
|||||||
|
"""
|
||||||
|
===============================================================================
|
||||||
|
FILE: services/snapshots/retention.py
|
||||||
|
ROLE: Политика ротации и очистки промежуточных почасовых срезов в SQLite.
|
||||||
|
===============================================================================
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from typing import List, Tuple
|
||||||
|
from core.connection import get_connection
|
||||||
|
|
||||||
|
logger = logging.getLogger("SNAPSHOT_RETENTION")
|
||||||
|
|
||||||
|
|
||||||
|
def cleanup_old_intermediate_snapshots(days_to_keep_all: int = 2) -> int:
|
||||||
|
"""
|
||||||
|
Очищает промежуточные дневные срезы старше days_to_keep_all дней.
|
||||||
|
|
||||||
|
Правило сохранения для архивных дней:
|
||||||
|
- Сохраняется срез Y (23:59:59).
|
||||||
|
- Сохраняется полуденный срез (ближайший к 13:00).
|
||||||
|
- Все остальные промежуточные снапшоты удаляются из scud_logs.
|
||||||
|
|
||||||
|
Возвращает количество удаленных записей.
|
||||||
|
"""
|
||||||
|
cutoff_date = datetime.now() - timedelta(days=days_to_keep_all)
|
||||||
|
cutoff_str = cutoff_date.strftime("%Y-%m-%d")
|
||||||
|
|
||||||
|
deleted_rows_count = 0
|
||||||
|
|
||||||
|
with get_connection() as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
# 1. Получаем список всех уникальных дат старше cutoff_date
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT DISTINCT log_date
|
||||||
|
FROM scud_logs
|
||||||
|
WHERE snapshot_time IS NOT NULL
|
||||||
|
""")
|
||||||
|
all_dates = [row[0] for row in cursor.fetchall()]
|
||||||
|
|
||||||
|
for raw_date in all_dates:
|
||||||
|
try:
|
||||||
|
# Преобразуем формат ДД.ММ.ГГГГ в объект даты
|
||||||
|
d_clean = raw_date.replace('_', '.')
|
||||||
|
dt_obj = datetime.strptime(d_clean, "%d.%m.%Y")
|
||||||
|
if dt_obj >= cutoff_date:
|
||||||
|
continue # Свежие дни не трогаем — там хранятся все срезы
|
||||||
|
|
||||||
|
# 2. Для архивной даты ищем все снапшоты
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT DISTINCT snapshot_id, snapshot_time
|
||||||
|
FROM scud_logs
|
||||||
|
WHERE log_date = ?
|
||||||
|
""", (raw_date,))
|
||||||
|
snapshots = cursor.fetchall()
|
||||||
|
|
||||||
|
if len(snapshots) <= 2:
|
||||||
|
continue # Если и так 1-2 среза, ничего чистить не нужно
|
||||||
|
|
||||||
|
keep_ids = set()
|
||||||
|
|
||||||
|
# Ищем финальный срез Y / 23:59:59 / 22:00:00
|
||||||
|
for s_id, s_time in snapshots:
|
||||||
|
s_id_str = str(s_id)
|
||||||
|
s_time_str = str(s_time)
|
||||||
|
if s_id_str.startswith('Y') or '23:59:59' in s_time_str or '22:00:00' in s_time_str:
|
||||||
|
keep_ids.add(s_id)
|
||||||
|
break
|
||||||
|
|
||||||
|
# Ищем полуденный срез (ближайший к 13:00)
|
||||||
|
noon_target = datetime.strptime(f"{d_clean} 13:00:00", "%d.%m.%Y %H:%M:%S")
|
||||||
|
best_noon_id = None
|
||||||
|
min_noon_diff = float('inf')
|
||||||
|
|
||||||
|
for s_id, s_time in snapshots:
|
||||||
|
if s_id in keep_ids:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
raw_t = str(s_time).strip()
|
||||||
|
if '.' in raw_t.split()[0]:
|
||||||
|
dt_s = datetime.strptime(raw_t, "%d.%m.%Y %H:%M:%S")
|
||||||
|
else:
|
||||||
|
dt_s = datetime.strptime(raw_t, "%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
|
diff = abs((dt_s - noon_target).total_seconds())
|
||||||
|
if diff < min_noon_diff:
|
||||||
|
min_noon_diff = diff
|
||||||
|
best_noon_id = s_id
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if best_noon_id:
|
||||||
|
keep_ids.add(best_noon_id)
|
||||||
|
|
||||||
|
# 3. Все снапшоты, не попавшие в keep_ids, удаляем
|
||||||
|
to_delete_ids = [s_id for s_id, _ in snapshots if s_id not in keep_ids]
|
||||||
|
|
||||||
|
for del_id in to_delete_ids:
|
||||||
|
cursor.execute("DELETE FROM scud_logs WHERE snapshot_id = ?", (del_id,))
|
||||||
|
deleted_rows_count += cursor.rowcount
|
||||||
|
logger.info(f"Удален промежуточный архивный срез {del_id} за {raw_date}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Ошибка при обработке ротации за дату {raw_date}: {e}")
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
if deleted_rows_count > 0:
|
||||||
|
logger.info(f"Ротация завершена: удалено {deleted_rows_count} строк промежуточных срезов.")
|
||||||
|
return deleted_rows_count
|
||||||
|
|
||||||
|
def purge_day_intermediate_snapshots(date_str: str) -> int:
|
||||||
|
"""
|
||||||
|
Удаляет все промежуточные дневные срезы за указанную дату,
|
||||||
|
оставляя нетронутым итоговый вечерний срез Y..._FINAL.
|
||||||
|
"""
|
||||||
|
clean_date = date_str.replace('_', '.')
|
||||||
|
with get_connection() as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("""
|
||||||
|
DELETE FROM scud_logs
|
||||||
|
WHERE log_date = ?
|
||||||
|
AND snapshot_id NOT LIKE 'Y%'
|
||||||
|
AND snapshot_id NOT LIKE '%_FINAL%'
|
||||||
|
""", (clean_date,))
|
||||||
|
deleted = cursor.rowcount
|
||||||
|
conn.commit()
|
||||||
|
if deleted > 0:
|
||||||
|
logger.info(f"[Retention] Удалено {deleted} строк промежуточных срезов за {clean_date}. Сохранен только финишный Y.")
|
||||||
|
return deleted
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
FILE: services/snapshots/service.py
|
FILE: services/snapshots/service.py
|
||||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
PROJECT: SCUD Orion AI (Unified Architecture)
|
||||||
MODULE: services / snapshots
|
MODULE: services / snapshots
|
||||||
ROLE: Бизнес-логика срезов СКУД (выборка, валидация Y-срезов, удаление).
|
ROLE: Бизнес-логика срезов СКУД (выборка, отображение времени, удаление).
|
||||||
|
|
||||||
AI-CONTEXT-ANCHORS:
|
AI-CONTEXT-ANCHORS:
|
||||||
- ANCHOR[SNAPSHOT_GET_REGISTRY]: Выборка срезов с разметкой защищенных Y-снапшотов.
|
- ANCHOR[SNAPSHOT_GET_REGISTRY]: Выборка срезов с разметкой защищенных Y-снапшотов.
|
||||||
@@ -18,20 +18,39 @@ from core.repositories.scud_repo import get_available_snapshots, delete_snapshot
|
|||||||
|
|
||||||
# ANCHOR[SNAPSHOT_GET_REGISTRY]
|
# ANCHOR[SNAPSHOT_GET_REGISTRY]
|
||||||
def get_snapshots_registry(date_str: Optional[str] = None) -> Dict[str, Any]:
|
def get_snapshots_registry(date_str: Optional[str] = None) -> Dict[str, Any]:
|
||||||
"""Возвращает реестр снапшотов за дату или за все доступные дни."""
|
"""
|
||||||
|
Возвращает реестр снапшотов.
|
||||||
|
В поле snapshot_time объединяет время среза и фактическое время создания снапшота.
|
||||||
|
"""
|
||||||
clean_date = date_str.strip() if date_str else ""
|
clean_date = date_str.strip() if date_str else ""
|
||||||
rows = get_available_snapshots(date_str=clean_date if clean_date else None)
|
rows = get_available_snapshots(date_str=clean_date if clean_date else None)
|
||||||
|
|
||||||
snapshots = [
|
snapshots = []
|
||||||
{
|
for r in rows:
|
||||||
"snapshot_id": r[0],
|
snap_id = r[0]
|
||||||
"log_date": r[1],
|
log_date = r[1]
|
||||||
"snapshot_time": r[2],
|
snap_time = r[2]
|
||||||
"record_count": r[3],
|
rec_count = r[3]
|
||||||
"is_final": str(r[0]).startswith("Y")
|
created_at = r[4] if len(r) > 4 else None
|
||||||
}
|
|
||||||
for r in rows
|
slice_time_str = snap_time.split()[1] if snap_time and " " in snap_time else snap_time
|
||||||
]
|
|
||||||
|
created_str = ""
|
||||||
|
if created_at and " " in str(created_at):
|
||||||
|
c_date, c_time = str(created_at).split()[:2]
|
||||||
|
c_parts = c_date.split("-")
|
||||||
|
c_date_fmt = f"{c_parts[2]}.{c_parts[1]}" if len(c_parts) == 3 else c_date
|
||||||
|
created_str = f" · создан {c_date_fmt} {c_time[:5]}"
|
||||||
|
|
||||||
|
display_label = f"Срез {slice_time_str}{created_str}"
|
||||||
|
|
||||||
|
snapshots.append({
|
||||||
|
"snapshot_id": snap_id,
|
||||||
|
"log_date": log_date,
|
||||||
|
"snapshot_time": display_label,
|
||||||
|
"record_count": rec_count,
|
||||||
|
"is_final": str(snap_id).startswith("Y") or "_FINAL" in str(snap_id)
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"query_date": clean_date or "все",
|
"query_date": clean_date or "все",
|
||||||
|
|||||||
+46
-75
@@ -3,99 +3,70 @@
|
|||||||
FILE: services/tasks/exporter.py
|
FILE: services/tasks/exporter.py
|
||||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
PROJECT: SCUD Orion AI (Unified Architecture)
|
||||||
MODULE: services / tasks
|
MODULE: services / tasks
|
||||||
ROLE: Экспорт бэклога задач в форматированный Markdown файл (ROADMAP).
|
ROLE: Экспорт задач в чистый Markdown и генерация прямой ссылки на скачивание.
|
||||||
|
|
||||||
AI-CONTEXT-ANCHORS:
|
|
||||||
- ANCHOR[TASK_EXPORT_MARKDOWN]: Построение структуры Markdown с чекбоксами.
|
|
||||||
===============================================================================
|
===============================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import uuid
|
import uuid
|
||||||
import logging
|
from typing import Dict, Any, Optional
|
||||||
from datetime import datetime
|
from config import BASE_DIR
|
||||||
from typing import Dict, Any, Optional, List
|
|
||||||
from config import OUTPUT_DIR
|
|
||||||
from .repository import repo_get_tasks
|
from .repository import repo_get_tasks
|
||||||
|
|
||||||
logger = logging.getLogger("TASK_EXPORTER")
|
WEB_OUTPUT_DIR = os.path.join(BASE_DIR, "output", "web", "db_export_tasks_markdown")
|
||||||
|
|
||||||
|
|
||||||
# ANCHOR[TASK_EXPORT_MARKDOWN]
|
def export_tasks_to_markdown(
|
||||||
def export_tasks_to_markdown(user_id: int, filename: Optional[str] = None, status_filter: Optional[str] = None) -> Dict[str, Any]:
|
user_id: int,
|
||||||
"""Генерирует Markdown-отчет задач с сохранением в output/web/tasks_export/{uuid}/."""
|
filename: Optional[str] = "ROADMAP.md",
|
||||||
tasks = repo_get_tasks(user_id)
|
status_filter: Optional[str] = None
|
||||||
if not tasks:
|
) -> Dict[str, Any]:
|
||||||
return {"status": "error", "message": "Список задач пуст, экспорт отменен"}
|
tasks = repo_get_tasks(user_id=user_id, status=status_filter)
|
||||||
|
safe_filename = os.path.basename(filename or "ROADMAP.md")
|
||||||
|
if not safe_filename.endswith(".md"):
|
||||||
|
safe_filename += ".md"
|
||||||
|
|
||||||
# 1. Фильтрация задач по статусу
|
session_uuid = str(uuid.uuid4())[:8]
|
||||||
if status_filter and status_filter.upper() != "ALL":
|
target_dir = os.path.join(WEB_OUTPUT_DIR, session_uuid)
|
||||||
tgt = status_filter.upper()
|
os.makedirs(target_dir, exist_ok=True)
|
||||||
if tgt in ["COMPLETED", "DONE", "ВЫПОЛНЕННЫЕ"]:
|
target_filepath = os.path.join(target_dir, safe_filename)
|
||||||
tasks = [t for t in tasks if str(t.get("status", "")).upper() in ["COMPLETED", "DONE"]]
|
|
||||||
elif tgt in ["IN_PROGRESS", "PROGRESS", "В РАБОТЕ"]:
|
|
||||||
tasks = [t for t in tasks if str(t.get("status", "")).upper() in ["IN_PROGRESS", "PROGRESS"]]
|
|
||||||
elif tgt in ["BACKLOG", "PLANNED", "В ПЛАНАХ"]:
|
|
||||||
tasks = [t for t in tasks if str(t.get("status", "")).upper() in ["BACKLOG", "PLANNED"]]
|
|
||||||
|
|
||||||
if not tasks:
|
lines = [
|
||||||
return {"status": "error", "message": f"Нет задач с фильтром '{status_filter}' для экспорта"}
|
f"# 📋 Реестр задач проекта ({safe_filename})",
|
||||||
|
f"**Всего задач:** {len(tasks)} ",
|
||||||
target_filename = filename.strip() if (filename and filename.strip()) else "ROADMAP.md"
|
f"**Пользователь ID:** {user_id} ",
|
||||||
if not target_filename.endswith(".md"):
|
"",
|
||||||
target_filename = f"{target_filename}.md"
|
"| ID | Статус | Приоритет | Модуль | Срок | Задача |",
|
||||||
|
"| :--- | :--- | :--- | :--- | :--- | :--- |"
|
||||||
now_str = datetime.now().strftime("%Y-%m-%d %H:%M")
|
|
||||||
|
|
||||||
# 2. Группировка по модулям
|
|
||||||
modules: Dict[str, List[Dict[str, Any]]] = {}
|
|
||||||
for t in tasks:
|
|
||||||
mod = t.get("module") or "general"
|
|
||||||
modules.setdefault(mod, []).append(t)
|
|
||||||
|
|
||||||
md_lines = [
|
|
||||||
"# 🗺️ Дорожная карта задач проекта (ROADMAP)\n",
|
|
||||||
f"> **Сформировано:** {now_str} | **Всего задач:** {len(tasks)}\n",
|
|
||||||
"---\n"
|
|
||||||
]
|
]
|
||||||
|
|
||||||
for mod_name, mod_tasks in sorted(modules.items()):
|
status_icons = {
|
||||||
md_lines.append(f"## Модуль `{mod_name}`\n")
|
"IN_PROGRESS": "⚙️ В работе",
|
||||||
for t in sorted(mod_tasks, key=lambda x: x.get("id", 0)):
|
"COMPLETED": "✓ Завершено",
|
||||||
status = str(t.get("status", "BACKLOG")).upper()
|
"BACKLOG": "📋 Бэклог"
|
||||||
is_done = status in ["COMPLETED", "DONE"]
|
}
|
||||||
is_progress = status in ["IN_PROGRESS", "PROGRESS"]
|
|
||||||
|
|
||||||
check_box = "[x]" if is_done else "[ ]"
|
for t in tasks:
|
||||||
t_id = t.get("id")
|
t_id = t.get("task_id") or f"#{t.get('id')}"
|
||||||
title = t.get("title", "Без названия")
|
t_status = status_icons.get(t.get("status"), t.get("status", "BACKLOG"))
|
||||||
prio = t.get("priority", "MEDIUM")
|
t_prio = t.get("priority", "MEDIUM")
|
||||||
due = f" *(срок: {t['due_date']})*" if t.get("due_date") else ""
|
t_mod = t.get("module", "general")
|
||||||
status_tag = " `[В РАБОТЕ]`" if is_progress else (" `[ЗАВЕРШЕНО]`" if is_done else "")
|
t_due = t.get("due_date") or "—"
|
||||||
|
t_title = str(t.get("title", "")).replace("|", "\\|").strip()
|
||||||
|
lines.append(f"| `{t_id}` | {t_status} | {t_prio} | `{t_mod}` | {t_due} | {t_title} |")
|
||||||
|
|
||||||
md_lines.append(f"- {check_box} **#{t_id}** [{prio}]{status_tag} {title}{due}")
|
lines.append("")
|
||||||
|
content = "\n".join(lines)
|
||||||
|
|
||||||
md_lines.append("\n---\n")
|
with open(target_filepath, "w", encoding="utf-8") as f:
|
||||||
|
|
||||||
content = "\n".join(md_lines)
|
|
||||||
|
|
||||||
# 3. Сохранение файла в изолированную сессионную папку
|
|
||||||
tool_dir = os.path.join(OUTPUT_DIR, "web", "tasks_export")
|
|
||||||
os.makedirs(tool_dir, exist_ok=True)
|
|
||||||
|
|
||||||
session_token = uuid.uuid4().hex[:8]
|
|
||||||
session_dir = os.path.join(tool_dir, session_token)
|
|
||||||
os.makedirs(session_dir, exist_ok=True)
|
|
||||||
|
|
||||||
filepath = os.path.join(session_dir, target_filename)
|
|
||||||
with open(filepath, "w", encoding="utf-8") as f:
|
|
||||||
f.write(content)
|
f.write(content)
|
||||||
|
|
||||||
|
download_url = f"/api/v1/files/download/db_export_tasks_markdown/{session_uuid}/{safe_filename}"
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"status": "success",
|
"status": "success",
|
||||||
"filename": target_filename,
|
"message": f"Отчет успешно сформирован в файл `{safe_filename}` (всего задач: {len(tasks)}).",
|
||||||
"filepath": filepath,
|
"filename": safe_filename,
|
||||||
"download_url": f"/api/v1/files/download/tasks_export/{session_token}/{target_filename}",
|
"download_url": download_url,
|
||||||
"tasks_count": len(tasks),
|
"tasks_count": len(tasks)
|
||||||
"message": f"Отчет успешно сформирован в файл `{target_filename}` (всего задач: {len(tasks)})."
|
|
||||||
}
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user