Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

active overwrite exclusion #87

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -526,42 +527,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