Jon Solow
commited on
Commit
•
1bb1936
1
Parent(s):
ae68564
Implement entry point to get stats and stub out live stats with cache
Browse files- src/stats.py +26 -0
src/stats.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from dataclasses import dataclass
|
2 |
import pandas as pd
|
|
|
3 |
|
4 |
from domain.playoffs import PLAYOFF_TEAM_DEF_PLAYER
|
5 |
from queries.nflverse.github_data import get_player_kicking_stats, get_player_stats, get_team_defense_stats
|
@@ -131,6 +132,8 @@ def add_stats_from_team_def_df_to_stat_map(df: pd.DataFrame, stat_map):
|
|
131 |
stat_map[week][player_id].update(player_stats)
|
132 |
|
133 |
|
|
|
|
|
134 |
def assemble_nflverse_stats() -> dict[int, dict[str, dict[str, float]]]:
|
135 |
# map week -> player_id -> stat_key -> stat value
|
136 |
stat_map: dict[int, dict[str, dict[str, float]]] = {w: {} for w in NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK.values()}
|
@@ -144,3 +147,26 @@ def assemble_nflverse_stats() -> dict[int, dict[str, dict[str, float]]]:
|
|
144 |
add_stats_from_team_def_df_to_stat_map(df_def_stats, stat_map)
|
145 |
|
146 |
return stat_map
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from dataclasses import dataclass
|
2 |
import pandas as pd
|
3 |
+
import streamlit as st
|
4 |
|
5 |
from domain.playoffs import PLAYOFF_TEAM_DEF_PLAYER
|
6 |
from queries.nflverse.github_data import get_player_kicking_stats, get_player_stats, get_team_defense_stats
|
|
|
132 |
stat_map[week][player_id].update(player_stats)
|
133 |
|
134 |
|
135 |
+
# 24 hour cache
|
136 |
+
@st.cache_data(ttl=60 * 60 * 24)
|
137 |
def assemble_nflverse_stats() -> dict[int, dict[str, dict[str, float]]]:
|
138 |
# map week -> player_id -> stat_key -> stat value
|
139 |
stat_map: dict[int, dict[str, dict[str, float]]] = {w: {} for w in NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK.values()}
|
|
|
147 |
add_stats_from_team_def_df_to_stat_map(df_def_stats, stat_map)
|
148 |
|
149 |
return stat_map
|
150 |
+
|
151 |
+
|
152 |
+
# 10 minute cache
|
153 |
+
@st.cache_data(ttl=60 * 10)
|
154 |
+
def get_live_stats() -> dict[int, dict[str, dict[str, float]]]:
|
155 |
+
stat_map: dict[int, dict[str, dict[str, float]]] = {w: {} for w in NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK.values()}
|
156 |
+
# TODO - implement live stats
|
157 |
+
return stat_map
|
158 |
+
|
159 |
+
|
160 |
+
def get_stats_map() -> dict[int, dict[str, dict[str, float]]]:
|
161 |
+
# use live stats if available
|
162 |
+
stat_map = get_live_stats()
|
163 |
+
|
164 |
+
# use more permanent nflverse stats over live
|
165 |
+
nflverse_stats = assemble_nflverse_stats()
|
166 |
+
|
167 |
+
# we overwrite the live stats with nflverse stats if they exist for the same player
|
168 |
+
for week, week_stats in nflverse_stats.items():
|
169 |
+
for player_id, player_stats in week_stats.items():
|
170 |
+
stat_map[week][player_id] = player_stats
|
171 |
+
|
172 |
+
return stat_map
|