53 lines
2.3 KiB
Python
53 lines
2.3 KiB
Python
"""
|
|
===============================================================================
|
|
FILE: modules/web_api/routers/snapshots.py
|
|
ROLE: REST API эндпоинты для управления и моментального создания срезов СКУД.
|
|
===============================================================================
|
|
"""
|
|
|
|
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 services.snapshots.service import get_snapshots_registry, delete_snapshots_safely
|
|
from services.scud_export import run_export
|
|
|
|
router = APIRouter(prefix="/api/v1/snapshots", tags=["Snapshots"])
|
|
|
|
|
|
class CreateSnapshotRequest(BaseModel):
|
|
date_str: Optional[str] = None
|
|
|
|
|
|
class DeleteSnapshotsRequest(BaseModel):
|
|
snapshot_ids: List[str]
|
|
|
|
|
|
@router.get("")
|
|
def api_get_snapshots(date_str: Optional[str] = None, current_user = Depends(get_current_user)):
|
|
return get_snapshots_registry(date_str=date_str)
|
|
|
|
|
|
@router.post("/create")
|
|
def api_create_instant_snapshot(req: CreateSnapshotRequest, current_user = Depends(get_current_user)):
|
|
"""Моментальный опрос MS SQL СКУД и запись свежего среза в SQLite."""
|
|
try:
|
|
success = run_export(input_date=req.date_str, save_xlsx=True, debug=False)
|
|
if not success:
|
|
raise HTTPException(status_code=500, detail="Ошибка при обращении к MS SQL Орион")
|
|
|
|
fresh_data = get_snapshots_registry(date_str=req.date_str)
|
|
return {"status": "success", "message": "Срез успешно создан", "data": fresh_data}
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Ошибка создания среза: {str(e)}")
|
|
|
|
|
|
@router.delete("")
|
|
def api_delete_snapshots(req: DeleteSnapshotsRequest, current_user = Depends(get_current_user)):
|
|
safe_ids = [s for s in req.snapshot_ids if not str(s).startswith("Y")]
|
|
if not safe_ids:
|
|
raise HTTPException(status_code=400, detail="Итоговый Y-срез защищен от удаления")
|
|
|
|
res = delete_snapshots_safely(snapshot_ids=safe_ids)
|
|
return {"status": "success", "deleted_count": res.get("deleted_count", len(safe_ids))} |