Spaces:
Running
Running
daily limit counted from the ledger (survives restarts)
Browse files- rt_challenge.py +20 -1
rt_challenge.py
CHANGED
|
@@ -115,11 +115,30 @@ def windows():
|
|
| 115 |
return out
|
| 116 |
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
def submit(user, req):
|
| 119 |
day = time.strftime("%Y-%m-%d", time.gmtime())
|
| 120 |
key = (user.lower(), day)
|
| 121 |
-
|
|
|
|
| 122 |
return 429, {"error": "daily limit reached (%d per day)" % DAILY}
|
|
|
|
| 123 |
cat = str(req.get("category", ""))
|
| 124 |
if cat not in CATS:
|
| 125 |
return 400, {"error": "unknown category"}
|
|
|
|
| 115 |
return out
|
| 116 |
|
| 117 |
|
| 118 |
+
_subs_cache = {"t": 0, "files": []}
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def _used_today(user):
|
| 122 |
+
"""Submissions this account already has in the ledger for the current UTC day (file name = <YYYYmmddHHMMSS>-<user>-<hex>)."""
|
| 123 |
+
if not LEDGER_TOKEN:
|
| 124 |
+
return 0
|
| 125 |
+
try:
|
| 126 |
+
if time.time() - _subs_cache["t"] > 30:
|
| 127 |
+
_subs_cache.update(t=time.time(), files=[f for f in _api().list_repo_files(LEDGER, repo_type="dataset") if f.startswith("submissions/")])
|
| 128 |
+
day = time.strftime("%Y%m%d", time.gmtime())
|
| 129 |
+
tag = "-" + user.lower() + "-"
|
| 130 |
+
return sum(1 for f in _subs_cache["files"] if f[12:20] == day and tag in f.lower())
|
| 131 |
+
except Exception: # noqa: BLE001
|
| 132 |
+
return 0
|
| 133 |
+
|
| 134 |
+
|
| 135 |
def submit(user, req):
|
| 136 |
day = time.strftime("%Y-%m-%d", time.gmtime())
|
| 137 |
key = (user.lower(), day)
|
| 138 |
+
used = max(_daily.get(key, 0), _used_today(user)) # counted from the ledger, so a restart does not reset it
|
| 139 |
+
if used >= DAILY:
|
| 140 |
return 429, {"error": "daily limit reached (%d per day)" % DAILY}
|
| 141 |
+
_daily[key] = used
|
| 142 |
cat = str(req.get("category", ""))
|
| 143 |
if cat not in CATS:
|
| 144 |
return 400, {"error": "unknown category"}
|