diff --git a/main.py b/main.py index 0db32326..90db523f 100644 --- a/main.py +++ b/main.py @@ -65,6 +65,7 @@ INVENTORY_SERVICE_URL = os.getenv('INVENTORY_SERVICE_URL', 'http://inventory-ser _cached_live: dict = {"players": []} _cached_trails: dict = {"trails": []} _cached_total_rares: dict = {"all_time": 0, "today": 0, "last_updated": None} +_cached_total_kills: dict = {"total": 0, "last_updated": None} _cache_task: asyncio.Task | None = None _rares_cache_task: asyncio.Task | None = None _cleanup_task: asyncio.Task | None = None @@ -739,14 +740,26 @@ async def _refresh_total_rares_cache() -> None: except Exception as e: logger.debug(f"rare_events table not available or empty: {e}") today_total = 0 - - # Update cache + + # Get total kills from char_stats table (all-time, all characters) + try: + kills_query = "SELECT COALESCE(SUM(total_kills), 0) as total FROM char_stats" + kills_result = await conn.fetch_one(kills_query) + total_kills = kills_result["total"] if kills_result else 0 + except Exception as e: + logger.debug(f"char_stats table not available: {e}") + total_kills = 0 + + # Update caches _cached_total_rares["all_time"] = all_time_total _cached_total_rares["today"] = today_total _cached_total_rares["last_updated"] = datetime.now(timezone.utc) - + + _cached_total_kills["total"] = total_kills + _cached_total_kills["last_updated"] = datetime.now(timezone.utc) + consecutive_failures = 0 - logger.debug(f"Total rares cache updated: All-time: {all_time_total}, Today: {today_total}") + logger.debug(f"Stats cache updated: Rares all-time: {all_time_total}, today: {today_total}, Kills: {total_kills}") except Exception as e: consecutive_failures += 1 @@ -1179,6 +1192,17 @@ async def get_total_rares(): raise HTTPException(status_code=500, detail="Internal server error") +@app.get("/total-kills") +@app.get("/total-kills/") +async def get_total_kills(): + """Return cached total kills statistics (updated every 5 minutes).""" + try: + return JSONResponse(content=jsonable_encoder(_cached_total_kills)) + except Exception as e: + logger.error(f"Failed to get total kills: {e}", exc_info=True) + raise HTTPException(status_code=500, detail="Internal server error") + + # --- GET Spawn Heat Map Endpoint --------------------------------- @app.get("/spawns/heatmap") async def get_spawn_heatmap_data(