Skip to content

Commit

Permalink
active overwrite exclusion (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
kavorite authored Jun 27, 2024
1 parent 65d808b commit 7fe6132
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
6 changes: 3 additions & 3 deletions masterbase/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ def late_bytes(request: Request, api_key: str, data: LateBytesBody) -> dict[str,
current_time = datetime.now().astimezone(timezone.utc)
steam_id = steam_id_from_api_key(engine, api_key)
converted_late_bytes = bytes.fromhex(data.late_bytes)
added = late_bytes_helper(engine, steam_id, converted_late_bytes, current_time)
if added:
error = late_bytes_helper(engine, steam_id, converted_late_bytes, current_time)
if error is None:
return {"late_bytes": True}
else:
raise HTTPException(detail="late bytes already submitted", status_code=422, extra={"late_bytes": False})
raise HTTPException(detail=error, status_code=422, extra={"late_bytes": False})


@get("/analyst_list_demos", guards=[valid_key_guard, analyst_guard], sync_to_thread=False)
Expand Down
61 changes: 31 additions & 30 deletions masterbase/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from minio import Minio, S3Error
from minio.datatypes import Object as BlobStat
from sqlalchemy import Engine
from sqlalchemy.exc import NoResultFound
from sqlalchemy.ext.asyncio import AsyncEngine

from masterbase.anomaly import DetectionState
Expand Down Expand Up @@ -538,42 +539,42 @@ def late_bytes_helper(
steam_id: str,
late_bytes: bytes,
current_time: datetime,
) -> bool:
) -> str | None:
"""Add late bytes to the database and blob storage.
No-ops and returns False if late bytes are found, True if they were absent.
No-ops and returns an error message if late bytes are found or there are no active sessions.
"""
with engine.connect() as conn:
session_id, old_late_bytes = conn.execute(
sa.text(
"""SELECT session_id, late_bytes FROM demo_sessions
WHERE steam_id = :steam_id
AND updated_at = (
SELECT MAX(updated_at) FROM demo_sessions WHERE steam_id = :steam_id
);
""",
),
{"steam_id": steam_id},
).one()
if session_id and old_late_bytes:
return False
else:
conn.execute(
try:
session_id, old_late_bytes = conn.execute(
sa.text(
"""UPDATE demo_sessions
SET
late_bytes = :late_bytes,
updated_at = :updated_at
WHERE session_id = session_id;"""
"""SELECT session_id, late_bytes FROM demo_sessions
WHERE active = True
AND steam_id = :steam_id;
""",
),
{
"session_id": session_id,
"late_bytes": late_bytes,
"updated_at": current_time.isoformat(),
},
)
conn.commit()
return True
{"steam_id": steam_id},
).one()
except NoResultFound:
return "no active session"
if session_id and old_late_bytes:
return "already submitted"
conn.execute(
sa.text(
"""UPDATE demo_sessions
SET
late_bytes = :late_bytes,
updated_at = :updated_at
WHERE session_id = session_id;"""
),
{
"session_id": session_id,
"late_bytes": late_bytes,
"updated_at": current_time.isoformat(),
},
)
conn.commit()
return None


async def get_demo_size(engine: AsyncEngine, session_id: str) -> str:
Expand Down

0 comments on commit 7fe6132

Please sign in to comment.