Skip to content

Commit

Permalink
refactor: improve error handling and logging in ArkViewer; add get_ov…
Browse files Browse the repository at this point in the history
…er_limit endpoint
  • Loading branch information
vertyco committed Dec 15, 2024
1 parent cd6a52e commit dc989eb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
56 changes: 54 additions & 2 deletions common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import multiprocessing
import os
import sys
from collections import defaultdict
from configparser import ConfigParser
from datetime import datetime, timedelta
from pathlib import Path
Expand Down Expand Up @@ -132,6 +133,14 @@ async def initialize(self) -> bool:
if not cache.map_file:
log.error("Map file path cannot be empty!")
return False
# Make sure cache.config and cache.map_file are on the same physical drive
if cache.config.resolve().drive != Path(cache.map_file).resolve().drive:
log.error(
"Config file and map file must be on the same drive! %s %s",
cache.config,
cache.map_file,
)
return False
if not Path(cache.map_file).exists():
log.error("Map file does not exist! %s", cache.map_file)
return False
Expand Down Expand Up @@ -378,14 +387,57 @@ async def get_data(self, request: Request, datatype: str):
target_data = cache.exports.get(datatype)
if not target_data:
raise HTTPException(
status_code=400,
status_code=404,
detail=f"Datatype {datatype} not cached yet!",
headers=self.info(stringify=True),
)
data = {datatype: target_data}

return JSONResponse(content={**data, **self.info()})

@router.get("/overlimit/{limit}")
async def get_over_limit(self, request: Request, limit: int):
"""Get all players who's tribe has uncryod tames over the limit"""
await self.check_keys(request)
global cache
tamed = cache.exports.get("tamed")
tribes = cache.exports.get("tribes")
if not tamed:
raise HTTPException(
status_code=404,
detail="Tamed data not cached yet!",
headers=self.info(stringify=True),
)
if not tribes:
raise HTTPException(
status_code=404,
detail="Tribes data not cached yet!",
headers=self.info(stringify=True),
)

def _exe():
# First map all tames to tribes
tribe_tames: dict[int, list[dict]] = defaultdict(list)
for tame in tamed["data"]:
if tame.get("uploadedTime") or tame["cryo"]:
continue
tribeid = int(tame["tribeid"])
tribe_tames[tribeid].append(tame)

over_limit: dict[str, list[dict]] = {}
for tribe in tribes["data"]:
if not tribe.get("members"):
continue
uncryod: list[dict] = tribe_tames.get(tribe["tribeid"], [])
if len(uncryod) <= limit:
continue
for member in tribe["members"]:
over_limit[member["steamid"]] = uncryod
return over_limit

over_limit: dict[str, list[dict]] = await asyncio.to_thread(_exe)
return JSONResponse(content={"overlimit": over_limit, **self.info()})

@router.post("/datas")
async def get_datas(self, request: Request, datatypes: Dtypes):
await self.check_keys(request)
Expand All @@ -409,7 +461,7 @@ async def get_datas(self, request: Request, datatypes: Dtypes):
target_data = cache.exports.get(datatype)
if not target_data:
raise HTTPException(
status_code=400,
status_code=404,
detail=f"Datatype {datatype} not cached yet!",
headers=self.info(stringify=True),
)
Expand Down
2 changes: 1 addition & 1 deletion common/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "2.9.13"
VERSION = "2.10.0"

0 comments on commit dc989eb

Please sign in to comment.