Enhanced logging
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#Faction data population and status management endpoints.
|
||||
import json
|
||||
from pathlib import Path
|
||||
from fastapi import APIRouter
|
||||
from fastapi import APIRouter, Request, HTTPException
|
||||
|
||||
from models import FactionRequest
|
||||
from services.server_state import STATE
|
||||
@@ -13,25 +13,101 @@ from services.torn_api import (
|
||||
stop_friendly_status_loop,
|
||||
stop_enemy_status_loop
|
||||
)
|
||||
from services.activity_log import activity_logger
|
||||
from utils import load_json_list
|
||||
from utils.auth import get_current_user
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["factions"])
|
||||
|
||||
|
||||
@router.post("/populate_friendly")
|
||||
async def api_populate_friendly(data: FactionRequest):
|
||||
await populate_friendly(data.faction_id)
|
||||
# Return members list for frontend (already in STATE from populate_friendly)
|
||||
members = [m.model_dump() for m in STATE.friendly.values()]
|
||||
return {"status": "friendly populated", "id": data.faction_id, "members": members}
|
||||
async def api_populate_friendly(request: Request, data: FactionRequest):
|
||||
# Get username for logging
|
||||
try:
|
||||
user_info = get_current_user(request)
|
||||
username = user_info.get("username", "Unknown")
|
||||
except:
|
||||
username = "Unknown"
|
||||
|
||||
try:
|
||||
await activity_logger.log_action(
|
||||
username,
|
||||
"Populate Friendly",
|
||||
f"Starting population for faction {data.faction_id}"
|
||||
)
|
||||
|
||||
result = await populate_friendly(data.faction_id)
|
||||
|
||||
if not result:
|
||||
await activity_logger.log_action(
|
||||
username,
|
||||
"Populate Friendly Failed",
|
||||
f"Failed to populate faction {data.faction_id} - API returned False (check API key, faction ID, or network)"
|
||||
)
|
||||
raise HTTPException(status_code=500, detail="Failed to populate friendly faction - check logs")
|
||||
|
||||
# Return members list for frontend (already in STATE from populate_friendly)
|
||||
members = [m.model_dump() for m in STATE.friendly.values()]
|
||||
|
||||
await activity_logger.log_action(
|
||||
username,
|
||||
"Populate Friendly Success",
|
||||
f"Populated {len(members)} members from faction {data.faction_id}"
|
||||
)
|
||||
|
||||
return {"status": "friendly populated", "id": data.faction_id, "members": members}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
error_msg = f"Error populating friendly faction {data.faction_id}: {type(e).__name__}: {str(e)}"
|
||||
await activity_logger.log_action(username, "Populate Friendly Error", error_msg)
|
||||
raise HTTPException(status_code=500, detail=error_msg)
|
||||
|
||||
|
||||
@router.post("/populate_enemy")
|
||||
async def api_populate_enemy(data: FactionRequest):
|
||||
await populate_enemy(data.faction_id)
|
||||
# Return members list for frontend (already in STATE from populate_enemy)
|
||||
members = [m.model_dump() for m in STATE.enemy.values()]
|
||||
return {"status": "enemy populated", "id": data.faction_id, "members": members}
|
||||
async def api_populate_enemy(request: Request, data: FactionRequest):
|
||||
# Get username for logging
|
||||
try:
|
||||
user_info = get_current_user(request)
|
||||
username = user_info.get("username", "Unknown")
|
||||
except:
|
||||
username = "Unknown"
|
||||
|
||||
try:
|
||||
await activity_logger.log_action(
|
||||
username,
|
||||
"Populate Enemy",
|
||||
f"Starting population for faction {data.faction_id}"
|
||||
)
|
||||
|
||||
result = await populate_enemy(data.faction_id)
|
||||
|
||||
if not result:
|
||||
await activity_logger.log_action(
|
||||
username,
|
||||
"Populate Enemy Failed",
|
||||
f"Failed to populate faction {data.faction_id} - API returned False (check API key, faction ID, or network)"
|
||||
)
|
||||
raise HTTPException(status_code=500, detail="Failed to populate enemy faction - check logs")
|
||||
|
||||
# Return members list for frontend (already in STATE from populate_enemy)
|
||||
members = [m.model_dump() for m in STATE.enemy.values()]
|
||||
|
||||
await activity_logger.log_action(
|
||||
username,
|
||||
"Populate Enemy Success",
|
||||
f"Populated {len(members)} members from faction {data.faction_id}"
|
||||
)
|
||||
|
||||
return {"status": "enemy populated", "id": data.faction_id, "members": members}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
error_msg = f"Error populating enemy faction {data.faction_id}: {type(e).__name__}: {str(e)}"
|
||||
await activity_logger.log_action(username, "Populate Enemy Error", error_msg)
|
||||
raise HTTPException(status_code=500, detail=error_msg)
|
||||
|
||||
|
||||
@router.post("/start_friendly_status")
|
||||
|
||||
Reference in New Issue
Block a user