Spaces:
Build error
Build error
James McCool commited on
Commit ·
50daa3e
1
Parent(s): f4e5fd5
Fixing expired IDs bug
Browse files
global_func/contest_lobby_cache.py
CHANGED
|
@@ -1,11 +1,50 @@
|
|
| 1 |
"""Persist pricing-step contest lists in Mongo (Contest_Information DB)."""
|
| 2 |
-
from datetime import datetime
|
| 3 |
from typing import List, Optional
|
| 4 |
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
|
| 7 |
COLLECTION_NAME = "DFS_PM_pricing_contest_cache"
|
| 8 |
_INDEX_ENSURED = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
def _collection(db):
|
|
@@ -28,32 +67,40 @@ def ensure_contest_cache_indexes(db) -> None:
|
|
| 28 |
|
| 29 |
|
| 30 |
def cache_has_contests(db, site: str, sport: str, game_type: str) -> bool:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
is not None
|
| 35 |
-
)
|
| 36 |
|
| 37 |
|
| 38 |
def get_cached_contests(db, site: str, sport: str, game_type: str) -> Optional[List[dict]]:
|
| 39 |
-
"""Return cached contest list, or None if no document exists."""
|
| 40 |
ensure_contest_cache_indexes(db)
|
| 41 |
-
|
|
|
|
| 42 |
if doc is None:
|
| 43 |
return None
|
| 44 |
contests = doc.get("contests")
|
| 45 |
if contests is None:
|
| 46 |
return []
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
def save_contests_cache(db, site: str, sport: str, game_type: str, contests: List[dict]) -> None:
|
|
|
|
| 51 |
ensure_contest_cache_indexes(db)
|
|
|
|
| 52 |
_collection(db).update_one(
|
| 53 |
{"site": site, "sport": sport, "type": game_type},
|
| 54 |
{
|
| 55 |
"$set": {
|
| 56 |
-
"contests":
|
| 57 |
"updated_at": datetime.utcnow(),
|
| 58 |
}
|
| 59 |
},
|
|
|
|
| 1 |
"""Persist pricing-step contest lists in Mongo (Contest_Information DB)."""
|
| 2 |
+
from datetime import date, datetime
|
| 3 |
from typing import List, Optional
|
| 4 |
|
| 5 |
import pandas as pd
|
| 6 |
+
import pytz
|
| 7 |
|
| 8 |
COLLECTION_NAME = "DFS_PM_pricing_contest_cache"
|
| 9 |
_INDEX_ENSURED = False
|
| 10 |
+
_EASTERN = pytz.timezone("US/Eastern")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def _eastern_today() -> date:
|
| 14 |
+
return datetime.now(_EASTERN).date()
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _parse_contest_slate_date(contest: dict) -> Optional[date]:
|
| 18 |
+
"""Slate date from cached contest dict (DRAFTKINGS YYYYMMDD or YYYY-MM-DD display), or None if unknown."""
|
| 19 |
+
cd = contest.get("contest_date")
|
| 20 |
+
if isinstance(cd, str) and len(cd) == 8 and cd.isdigit():
|
| 21 |
+
try:
|
| 22 |
+
return datetime.strptime(cd, "%Y%m%d").date()
|
| 23 |
+
except ValueError:
|
| 24 |
+
pass
|
| 25 |
+
disp = contest.get("contest_date_display")
|
| 26 |
+
if isinstance(disp, str):
|
| 27 |
+
s = disp.strip()
|
| 28 |
+
if len(s) >= 10:
|
| 29 |
+
try:
|
| 30 |
+
return datetime.strptime(s[:10], "%Y-%m-%d").date()
|
| 31 |
+
except ValueError:
|
| 32 |
+
pass
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def filter_contests_from_today_forward(contests: List[dict]) -> List[dict]:
|
| 37 |
+
"""
|
| 38 |
+
Keep contests whose slate date is today (US/Eastern) or later.
|
| 39 |
+
Entries with no parseable date are kept (e.g. FanDuel CSV uploads without a date column).
|
| 40 |
+
"""
|
| 41 |
+
today = _eastern_today()
|
| 42 |
+
out: List[dict] = []
|
| 43 |
+
for c in contests:
|
| 44 |
+
d = _parse_contest_slate_date(c)
|
| 45 |
+
if d is None or d >= today:
|
| 46 |
+
out.append(c)
|
| 47 |
+
return out
|
| 48 |
|
| 49 |
|
| 50 |
def _collection(db):
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
def cache_has_contests(db, site: str, sport: str, game_type: str) -> bool:
|
| 70 |
+
"""True if cache has at least one non-expired contest (slate date >= today US/Eastern)."""
|
| 71 |
+
cached = get_cached_contests(db, site, sport, game_type)
|
| 72 |
+
return cached is not None and len(cached) > 0
|
|
|
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
def get_cached_contests(db, site: str, sport: str, game_type: str) -> Optional[List[dict]]:
|
| 76 |
+
"""Return cached contest list (today-forward only), or None if no document exists."""
|
| 77 |
ensure_contest_cache_indexes(db)
|
| 78 |
+
key = {"site": site, "sport": sport, "type": game_type}
|
| 79 |
+
doc = _collection(db).find_one(key)
|
| 80 |
if doc is None:
|
| 81 |
return None
|
| 82 |
contests = doc.get("contests")
|
| 83 |
if contests is None:
|
| 84 |
return []
|
| 85 |
+
raw = list(contests)
|
| 86 |
+
filtered = filter_contests_from_today_forward(raw)
|
| 87 |
+
if len(filtered) != len(raw):
|
| 88 |
+
_collection(db).update_one(
|
| 89 |
+
key,
|
| 90 |
+
{"$set": {"contests": filtered, "updated_at": datetime.utcnow()}},
|
| 91 |
+
)
|
| 92 |
+
return filtered
|
| 93 |
|
| 94 |
|
| 95 |
def save_contests_cache(db, site: str, sport: str, game_type: str, contests: List[dict]) -> None:
|
| 96 |
+
"""Replace cached contests with the payload after removing prior-day slates (US/Eastern)."""
|
| 97 |
ensure_contest_cache_indexes(db)
|
| 98 |
+
to_store = filter_contests_from_today_forward(list(contests))
|
| 99 |
_collection(db).update_one(
|
| 100 |
{"site": site, "sport": sport, "type": game_type},
|
| 101 |
{
|
| 102 |
"$set": {
|
| 103 |
+
"contests": to_store,
|
| 104 |
"updated_at": datetime.utcnow(),
|
| 105 |
}
|
| 106 |
},
|