From 025445bb99d9c95732c17f4fd3391180792daadb Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Thu, 21 Dec 2023 00:09:44 -0600 Subject: [PATCH] Parameter modernization --- app/routers_v3/stratplay.py | 101 +++++++++++++++++++++++++++++------- 1 file changed, 82 insertions(+), 19 deletions(-) diff --git a/app/routers_v3/stratplay.py b/app/routers_v3/stratplay.py index e17fbff..e718c53 100644 --- a/app/routers_v3/stratplay.py +++ b/app/routers_v3/stratplay.py @@ -132,7 +132,8 @@ async def get_plays( outs: list = Query(default=None), wild_pitch: Optional[int] = None, is_final_out: Optional[bool] = None, is_go_ahead: Optional[bool] = None, is_tied: Optional[bool] = None, is_new_inning: Optional[bool] = None, min_wpa: Optional[float] = None, max_wpa: Optional[float] = None, pitcher_team_id: list = Query(default=None), - short_output: Optional[bool] = False, sort: Optional[str] = None, limit: Optional[int] = 200): + short_output: Optional[bool] = False, sort: Optional[str] = None, limit: Optional[int] = 200, + page_num: Optional[int] = 1): all_plays = StratPlay.select() if season is not None: @@ -235,10 +236,9 @@ async def get_plays( if play_num is not None: all_plays = all_plays.where(StratPlay.play_num << play_num) - if limit > 5000: - limit = 5000 - elif limit < 1: + if limit < 1: limit = 1 + bat_plays = all_plays.paginate(page_num, limit) if sort == 'wpa-desc': all_plays = all_plays.order_by(-fn.ABS(StratPlay.wpa)) @@ -264,18 +264,26 @@ async def get_batting_totals( season: list = Query(default=None), week: list = Query(default=None), s_type: Literal['regular', 'post', 'total', None] = None, position: list = Query(default=None), player_id: list = Query(default=None), min_wpa: Optional[float] = -999, max_wpa: Optional[float] = 999, - group_by: Literal['team', 'player', 'playerteam', 'playergame', 'teamgame', 'league'] = 'player', + group_by: Literal['team', 'player', 'playerteam', 'playergame', 'teamgame', 'league', 'playerweek', + 'teamweek'] = 'player', min_pa: Optional[int] = 1, team_id: list = Query(default=None), manager_id: list = Query(default=None), obc: list = Query(default=None), risp: Optional[bool] = None, inning: list = Query(default=None), sort: Optional[str] = None, limit: Optional[int] = 200, short_output: Optional[bool] = False, - page_num: Optional[int] = 1): + page_num: Optional[int] = 1, week_start: Optional[int] = None, week_end: Optional[int] = None): season_games = StratGame.select() if season is not None: season_games = season_games.where(StratGame.season << season) if week is not None and s_type is not None: raise HTTPException(status_code=400, detail=f'Week and s_type parameters cannot be used in the same query') + if week is not None and (week_start is not None or week_end is not None): + raise HTTPException( + status_code=400, detail=f'Week and week_start/week_end parameters cannot be used in the same query') if week is not None: season_games = season_games.where(StratGame.week << week) + if week_start is not None: + season_games = season_games.where(StratGame.week >= week_start) + if week_end is not None: + season_games = season_games.where(StratGame.week <= week_end) if s_type is not None: if s_type == 'regular': season_games = season_games.where(StratGame.week <= 18) @@ -384,12 +392,21 @@ async def get_batting_totals( elif group_by == 'teamgame': bat_plays = bat_plays.group_by(StratPlay.batter_team, StratPlay.game) run_plays = run_plays.group_by(StratPlay.runner_team, StratPlay.game) - def_plays = def_plays.group_by(StratPlay.defender_team, StratPlay.game) elif group_by == 'league': bat_plays = bat_plays.join(StratGame) bat_plays = bat_plays.group_by(StratPlay.game.season) run_plays = run_plays.join(StratGame) run_plays = run_plays.group_by(StratPlay.game.season) + elif group_by == 'playerweek': + bat_plays = bat_plays.join(StratGame) + bat_plays = bat_plays.group_by(StratPlay.batter, StratPlay.game.week) + run_plays = run_plays.join(StratGame) + run_plays = run_plays.group_by(StratPlay.runner, StratPlay.game.week) + elif group_by == 'teamweek': + bat_plays = bat_plays.join(StratGame) + bat_plays = bat_plays.group_by(StratPlay.batter_team, StratPlay.game.week) + run_plays = run_plays.join(StratGame) + run_plays = run_plays.group_by(StratPlay.runner_team, StratPlay.game.week) if sort is not None: if sort == 'player': bat_plays = bat_plays.order_by(StratPlay.batter) @@ -413,10 +430,6 @@ async def get_batting_totals( elif sort == 'oldest': bat_plays = bat_plays.order_by(StratPlay.game_id, StratPlay.play_num) run_plays = run_plays.order_by(StratPlay.game_id, StratPlay.play_num) - if limit is not None: - if limit < 1: - limit = 1 - bat_plays = bat_plays.limit(limit) if limit < 1: limit = 1 @@ -462,6 +475,10 @@ async def get_batting_totals( if group_by in ['playergame', 'teamgame']: this_game = x.game_id if short_output else model_to_dict(x.game, recurse=False) + this_week = 'TOT' + if group_by in ['playerweek', 'teamweek']: + this_week = x.game.week + lob_all_rate, lob_2outs_rate, rbi_rate = 0, 0, 0 if x.count_runner1 + x.count_runner2 + x.count_runner3 > 0: lob_all_rate = (x.count_lo1 + x.count_lo2 + x.count_lo3) / \ @@ -502,7 +519,8 @@ async def get_batting_totals( 'lob_all': x.count_lo1 + x.count_lo2 + x.count_lo3, 'lob_all_rate': lob_all_rate, 'lob_2outs': x.count_lo1_3out + x.count_lo2_3out + x.count_lo3_3out, - 'rbi%': rbi_rate + 'rbi%': rbi_rate, + 'week': this_week }) db.close() @@ -513,18 +531,27 @@ async def get_batting_totals( async def get_pitching_totals( season: list = Query(default=None), week: list = Query(default=None), s_type: Literal['regular', 'post', 'total', None] = None, player_id: list = Query(default=None), - group_by: Literal['team', 'player', 'playerteam', 'playergame', 'teamgame', 'league'] = 'player', + group_by: Literal['team', 'player', 'playerteam', 'playergame', 'teamgame', 'league', 'playerweek', + 'teamweek'] = 'player', min_pa: Optional[int] = 1, team_id: list = Query(default=None), manager_id: list = Query(default=None), obc: list = Query(default=None), risp: Optional[bool] = None, inning: list = Query(default=None), sort: Optional[str] = None, limit: Optional[int] = 200, short_output: Optional[bool] = False, - csv: Optional[bool] = False, page_num: Optional[int] = 1): + csv: Optional[bool] = False, page_num: Optional[int] = 1, week_start: Optional[int] = None, + week_end: Optional[int] = None): season_games = StratGame.select() if season is not None: season_games = season_games.where(StratGame.season << season) if week is not None and s_type is not None: raise HTTPException(status_code=400, detail=f'Week and s_type parameters cannot be used in the same query') + if week is not None and (week_start is not None or week_end is not None): + raise HTTPException( + status_code=400, detail=f'Week and week_start/week_end parameters cannot be used in the same query') if week is not None: season_games = season_games.where(StratGame.week << week) + if week_start is not None: + season_games = season_games.where(StratGame.week >= week_start) + if week_end is not None: + season_games = season_games.where(StratGame.week <= week_end) if s_type is not None: if s_type == 'regular': season_games = season_games.where(StratGame.week <= 18) @@ -609,6 +636,12 @@ async def get_pitching_totals( elif group_by == 'league': pit_plays = pit_plays.join(StratGame) pit_plays = pit_plays.group_by(StratPlay.game.season) + elif group_by == 'playerweek': + pit_plays = pit_plays.join(StratGame) + pit_plays = pit_plays.group_by(StratPlay.pitcher, StratPlay.game.season) + elif group_by == 'teamweek': + pit_plays = pit_plays.join(StratGame) + pit_plays = pit_plays.group_by(StratPlay.pitcher_team, StratPlay.game.season) if sort is not None: if sort.lower() == 'player': pit_plays = pit_plays.order_by(StratPlay.pitcher) @@ -669,6 +702,10 @@ async def get_pitching_totals( this_game = x.game_id if short_output else model_to_dict(x.game, recurse=False) this_dec = all_dec.where((Decision.pitcher == x.pitcher) & (Decision.game == x.game)) + this_week = 'TOT' + if group_by in ['playerweek', 'teamweek']: + this_week = x.game.week + lob_all_rate, lob_2outs_rate, rbi_rate = 0, 0, 0 if x.count_runner1 + x.count_runner2 + x.count_runner3 > 0: lob_all_rate = (x.count_lo1 + x.count_lo2 + x.count_lo3) / \ @@ -724,7 +761,8 @@ async def get_pitching_totals( 'k/bb': x.sum_so / tot_bb, 'game': this_game, 'lob_2outs': x.count_lo1_3out + x.count_lo2_3out + x.count_lo3_3out, - 'rbi%': rbi_rate + 'rbi%': rbi_rate, + 'week': this_week }) db.close() if csv: @@ -739,7 +777,8 @@ async def get_fielding_totals( s_type: Literal['regular', 'post', 'total', None] = None, position: list = Query(default=None), player_id: list = Query(default=None), group_by: Literal['team', 'player', 'playerteam', 'playerposition', 'teamposition', 'playerpositiongame', - 'playergame'] = 'player', + 'playergame', 'playerteamposition', 'playerweek', 'teamweek'] = 'player', + week_start: Optional[int] = None, week_end: Optional[int] = None, min_ch: Optional[int] = 1, team_id: list = Query(default=None), manager_id: list = Query(default=None), sort: Optional[str] = None, limit: Optional[int] = 200, short_output: Optional[bool] = False, page_num: Optional[int] = 1): @@ -748,8 +787,15 @@ async def get_fielding_totals( season_games = season_games.where(StratGame.season << season) if week is not None and s_type is not None: raise HTTPException(status_code=400, detail=f'Week and s_type parameters cannot be used in the same query') + if week is not None and (week_start is not None or week_end is not None): + raise HTTPException( + status_code=400, detail=f'Week and week_start/week_end parameters cannot be used in the same query') if week is not None: season_games = season_games.where(StratGame.week << week) + if week_start is not None: + season_games = season_games.where(StratGame.week >= week_start) + if week_end is not None: + season_games = season_games.where(StratGame.week <= week_end) if s_type is not None: if s_type == 'regular': season_games = season_games.where(StratGame.week <= 18) @@ -810,6 +856,20 @@ async def get_fielding_totals( elif group_by == 'playerpositiongame': def_plays = def_plays.group_by(StratPlay.defender, StratPlay.check_pos, StratPlay.game) cat_plays = cat_plays.group_by(StratPlay.catcher, StratPlay.game) + elif group_by == 'playerteamposition': + def_plays = def_plays.group_by(StratPlay.defender, StratPlay.defender_team, StratPlay.check_pos) + cat_plays = cat_plays.group_by(StratPlay.catcher, StratPlay.catcher_team) + elif group_by == 'playerweek': + def_plays = def_plays.join(StratGame) + def_plays = def_plays.group_by(StratPlay.defender, StratPlay.game.week) + cat_plays = cat_plays.join(StratGame) + cat_plays = cat_plays.group_by(StratPlay.catcher, StratPlay.game.week) + elif group_by == 'teamweek': + def_plays = def_plays.join(StratGame) + def_plays = def_plays.group_by(StratPlay.defender_team, StratPlay.game.week) + cat_plays = cat_plays.join(StratGame) + cat_plays = cat_plays.group_by(StratPlay.catcher_team, StratPlay.game.week) + if sort is not None: if sort == 'player': def_plays = def_plays.order_by(StratPlay.defender) @@ -830,8 +890,6 @@ async def get_fielding_totals( if limit < 1: limit = 1 - elif limit > 500: - limit = 500 def_plays = def_plays.paginate(page_num, limit) logging.info(f'def_plays query: {def_plays}') @@ -888,6 +946,10 @@ async def get_fielding_totals( if 'game' in group_by: this_game = x.game_id if short_output else model_to_dict(x.game, recurse=False) + this_week = 'TOT' + if group_by in ['playerweek', 'teamweek']: + this_week = x.game.week + return_stats['stats'].append({ 'player': this_player, 'team': x.defender_team_id if short_output else model_to_dict(x.defender_team, recurse=False), @@ -902,7 +964,8 @@ async def get_fielding_totals( 'wpa': (x.sum_wpa + sum_wpa) * -1, 'wf%': (x.sum_chances - (x.sum_error * .5) - (x.sum_hit * .75)) / x.sum_chances, 'cs%': sum_cs / (sum_sb + sum_cs) if (sum_sb + sum_cs) > 0 else None, - 'game': this_game + 'game': this_game, + 'week': this_week }) db.close() return return_stats