Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Commit
·
0672532
1
Parent(s):
3a7a1bb
enforce rate limits
Browse files- src/submission/submit.py +39 -26
src/submission/submit.py
CHANGED
@@ -46,32 +46,30 @@ def add_new_solutions(
|
|
46 |
|
47 |
logger.info(f"Found {len(submitted_ids)} submissions")
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
f"Remaining wait time: {remaining_hrs:.2f} hours"
|
74 |
-
)
|
75 |
|
76 |
logger.info(
|
77 |
f"Adding new submission: {system_name=}, {org=}, {sys_type=} and {submission_path=}",
|
@@ -172,3 +170,18 @@ def _validate_all_submissions_present(
|
|
172 |
raise ValueError(f"Mismatched problem IDs: {len(missing)} missing, {len(unknown)} unknown")
|
173 |
if len(pd_ds) > len(lbdb.code_problem_ids):
|
174 |
return ValueError("Duplicate problem IDs exist in uploaded file")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
logger.info(f"Found {len(submitted_ids)} submissions")
|
48 |
|
49 |
+
sub_df = pd.DataFrame.from_dict(
|
50 |
+
{
|
51 |
+
"submission_id": submitted_ids,
|
52 |
+
"user_id": map(submission_id_to_user_id, submitted_ids),
|
53 |
+
"timestamp": map(submission_id_to_timestamp, submitted_ids),
|
54 |
+
}
|
55 |
+
)
|
56 |
+
|
57 |
+
cutoff = datetime.now(timezone.utc) - timedelta(hours=RATE_LIMIT_WINDOW_HRS)
|
58 |
+
user_last_sub_date = sub_df[sub_df.user_id == user_id].timestamp.max()
|
59 |
+
|
60 |
+
if pd.notna(user_last_sub_date) and user_last_sub_date > cutoff:
|
61 |
+
remaining_hrs = MIN_WAIT_TIME_PER_USER_HRS - (user_last_sub_date - cutoff).total_seconds() / 3600
|
62 |
+
logger.info(f"{username} must wait {remaining_hrs:.2f} more hours.")
|
63 |
+
return styled_error(
|
64 |
+
f"You must wait {MIN_WAIT_TIME_PER_USER_HRS} hours between submissions. "
|
65 |
+
f"Remaining wait time: {remaining_hrs:.2f} hours"
|
66 |
+
)
|
67 |
+
|
68 |
+
if len(sub_df.timestamp > cutoff) >= MAX_SUBMISSIONS_PER_WINDOW:
|
69 |
+
logger.info(
|
70 |
+
f"Too many submissions in the last {RATE_LIMIT_WINDOW_HRS} hours: {len(sub_df.timestamp > cutoff)}."
|
71 |
+
)
|
72 |
+
return styled_error("The leaderboard has reached its submission capacity for now. Please try again later.")
|
|
|
|
|
73 |
|
74 |
logger.info(
|
75 |
f"Adding new submission: {system_name=}, {org=}, {sys_type=} and {submission_path=}",
|
|
|
170 |
raise ValueError(f"Mismatched problem IDs: {len(missing)} missing, {len(unknown)} unknown")
|
171 |
if len(pd_ds) > len(lbdb.code_problem_ids):
|
172 |
return ValueError("Duplicate problem IDs exist in uploaded file")
|
173 |
+
|
174 |
+
|
175 |
+
def submission_id_to_user_id(submission_id: str) -> str:
|
176 |
+
"""
|
177 |
+
Extracts the user ID from the submission ID: "YYYYMMDD_HHMMSS_username_userid"
|
178 |
+
"""
|
179 |
+
return submission_id.rsplit("_", 1)[-1]
|
180 |
+
|
181 |
+
|
182 |
+
def submission_id_to_timestamp(submission_id: str) -> datetime:
|
183 |
+
"""
|
184 |
+
Extracts the timestamp from the submission ID: "YYYYMMDD_HHMMSS_username_userid"
|
185 |
+
"""
|
186 |
+
ts_str = "_".join(submission_id.split("_", 2)[:2])
|
187 |
+
return datetime.strptime(ts_str, "%Y%m%d_%H%M%S").replace(tzinfo=timezone.utc)
|