Added periodic fetch

This commit is contained in:
2025-11-29 09:48:50 +01:00
parent 90f0e14f6e
commit 47f4a22bef
4 changed files with 49 additions and 3 deletions

View File

@@ -10,8 +10,7 @@ from models.event_model import normalize_event
router = APIRouter()
@router.get("/fetch-audit-logs")
def fetch_logs(hours: int = 168):
def run_fetch(hours: int = 168):
window = max(1, min(hours, 720)) # cap to 30 days for sanity
logs = []
errors = []
@@ -38,3 +37,11 @@ def fetch_logs(hours: int = 168):
ops.append(UpdateOne({"id": doc.get("id"), "timestamp": doc.get("timestamp")}, {"$set": doc}, upsert=True))
events_collection.bulk_write(ops, ordered=False)
return {"stored_events": len(normalized), "errors": errors}
@router.get("/fetch-audit-logs")
def fetch_logs(hours: int = 168):
try:
return run_fetch(hours=hours)
except Exception as exc:
raise HTTPException(status_code=502, detail=str(exc)) from exc