Alvinn-aai commited on
Commit
0672532
·
1 Parent(s): 3a7a1bb

enforce rate limits

Browse files
Files changed (1) hide show
  1. 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
- user_last_submission_date = None
50
- for sid in submitted_ids:
51
- # Extract user ID (last part)
52
- past_user_id = sid.rsplit("_", 1)[-1]
53
- # Extract timestamp string (first two parts)
54
- ts_str = "_".join(sid.split("_", 2)[:2])
55
-
56
- logger.info(f"Comparing past user: {past_user_id} with current user ID: {user_id}")
57
-
58
- ts = datetime.strptime(ts_str, "%Y%m%d_%H%M%S").replace(tzinfo=timezone.utc)
59
- if past_user_id == user_id:
60
- if user_last_submission_date is None:
61
- user_last_submission_date = ts
62
- else:
63
- user_last_submission_date = max(user_last_submission_date, ts)
64
-
65
- if user_last_submission_date is not None:
66
- now = datetime.now(timezone.utc)
67
- elapsed = now - user_last_submission_date
68
- if elapsed < timedelta(hours=MIN_WAIT_TIME_PER_USER_HRS):
69
- remaining_hrs = MIN_WAIT_TIME_PER_USER_HRS - elapsed.total_seconds() / 3600
70
- logger.info(f"{username} must wait {remaining_hrs:.2f} more hours.")
71
- return styled_error(
72
- f"You must wait {MIN_WAIT_TIME_PER_USER_HRS} hours between submissions. "
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)