Parameter modernization
This commit is contained in:
parent
1168d7abaa
commit
025445bb99
@ -132,7 +132,8 @@ async def get_plays(
|
|||||||
outs: list = Query(default=None), wild_pitch: Optional[int] = None, is_final_out: Optional[bool] = None,
|
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,
|
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),
|
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()
|
all_plays = StratPlay.select()
|
||||||
|
|
||||||
if season is not None:
|
if season is not None:
|
||||||
@ -235,10 +236,9 @@ async def get_plays(
|
|||||||
if play_num is not None:
|
if play_num is not None:
|
||||||
all_plays = all_plays.where(StratPlay.play_num << play_num)
|
all_plays = all_plays.where(StratPlay.play_num << play_num)
|
||||||
|
|
||||||
if limit > 5000:
|
if limit < 1:
|
||||||
limit = 5000
|
|
||||||
elif limit < 1:
|
|
||||||
limit = 1
|
limit = 1
|
||||||
|
bat_plays = all_plays.paginate(page_num, limit)
|
||||||
|
|
||||||
if sort == 'wpa-desc':
|
if sort == 'wpa-desc':
|
||||||
all_plays = all_plays.order_by(-fn.ABS(StratPlay.wpa))
|
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),
|
season: list = Query(default=None), week: list = Query(default=None),
|
||||||
s_type: Literal['regular', 'post', 'total', None] = None, position: 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,
|
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),
|
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),
|
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,
|
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()
|
season_games = StratGame.select()
|
||||||
if season is not None:
|
if season is not None:
|
||||||
season_games = season_games.where(StratGame.season << season)
|
season_games = season_games.where(StratGame.season << season)
|
||||||
if week is not None and s_type is not None:
|
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')
|
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:
|
if week is not None:
|
||||||
season_games = season_games.where(StratGame.week << week)
|
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 is not None:
|
||||||
if s_type == 'regular':
|
if s_type == 'regular':
|
||||||
season_games = season_games.where(StratGame.week <= 18)
|
season_games = season_games.where(StratGame.week <= 18)
|
||||||
@ -384,12 +392,21 @@ async def get_batting_totals(
|
|||||||
elif group_by == 'teamgame':
|
elif group_by == 'teamgame':
|
||||||
bat_plays = bat_plays.group_by(StratPlay.batter_team, StratPlay.game)
|
bat_plays = bat_plays.group_by(StratPlay.batter_team, StratPlay.game)
|
||||||
run_plays = run_plays.group_by(StratPlay.runner_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':
|
elif group_by == 'league':
|
||||||
bat_plays = bat_plays.join(StratGame)
|
bat_plays = bat_plays.join(StratGame)
|
||||||
bat_plays = bat_plays.group_by(StratPlay.game.season)
|
bat_plays = bat_plays.group_by(StratPlay.game.season)
|
||||||
run_plays = run_plays.join(StratGame)
|
run_plays = run_plays.join(StratGame)
|
||||||
run_plays = run_plays.group_by(StratPlay.game.season)
|
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 is not None:
|
||||||
if sort == 'player':
|
if sort == 'player':
|
||||||
bat_plays = bat_plays.order_by(StratPlay.batter)
|
bat_plays = bat_plays.order_by(StratPlay.batter)
|
||||||
@ -413,10 +430,6 @@ async def get_batting_totals(
|
|||||||
elif sort == 'oldest':
|
elif sort == 'oldest':
|
||||||
bat_plays = bat_plays.order_by(StratPlay.game_id, StratPlay.play_num)
|
bat_plays = bat_plays.order_by(StratPlay.game_id, StratPlay.play_num)
|
||||||
run_plays = run_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:
|
if limit < 1:
|
||||||
limit = 1
|
limit = 1
|
||||||
@ -462,6 +475,10 @@ async def get_batting_totals(
|
|||||||
if group_by in ['playergame', 'teamgame']:
|
if group_by in ['playergame', 'teamgame']:
|
||||||
this_game = x.game_id if short_output else model_to_dict(x.game, recurse=False)
|
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
|
lob_all_rate, lob_2outs_rate, rbi_rate = 0, 0, 0
|
||||||
if x.count_runner1 + x.count_runner2 + x.count_runner3 > 0:
|
if x.count_runner1 + x.count_runner2 + x.count_runner3 > 0:
|
||||||
lob_all_rate = (x.count_lo1 + x.count_lo2 + x.count_lo3) / \
|
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': x.count_lo1 + x.count_lo2 + x.count_lo3,
|
||||||
'lob_all_rate': lob_all_rate,
|
'lob_all_rate': lob_all_rate,
|
||||||
'lob_2outs': x.count_lo1_3out + x.count_lo2_3out + x.count_lo3_3out,
|
'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()
|
db.close()
|
||||||
@ -513,18 +531,27 @@ async def get_batting_totals(
|
|||||||
async def get_pitching_totals(
|
async def get_pitching_totals(
|
||||||
season: list = Query(default=None), week: list = Query(default=None),
|
season: list = Query(default=None), week: list = Query(default=None),
|
||||||
s_type: Literal['regular', 'post', 'total', None] = None, player_id: 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),
|
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),
|
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,
|
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()
|
season_games = StratGame.select()
|
||||||
if season is not None:
|
if season is not None:
|
||||||
season_games = season_games.where(StratGame.season << season)
|
season_games = season_games.where(StratGame.season << season)
|
||||||
if week is not None and s_type is not None:
|
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')
|
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:
|
if week is not None:
|
||||||
season_games = season_games.where(StratGame.week << week)
|
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 is not None:
|
||||||
if s_type == 'regular':
|
if s_type == 'regular':
|
||||||
season_games = season_games.where(StratGame.week <= 18)
|
season_games = season_games.where(StratGame.week <= 18)
|
||||||
@ -609,6 +636,12 @@ async def get_pitching_totals(
|
|||||||
elif group_by == 'league':
|
elif group_by == 'league':
|
||||||
pit_plays = pit_plays.join(StratGame)
|
pit_plays = pit_plays.join(StratGame)
|
||||||
pit_plays = pit_plays.group_by(StratPlay.game.season)
|
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 is not None:
|
||||||
if sort.lower() == 'player':
|
if sort.lower() == 'player':
|
||||||
pit_plays = pit_plays.order_by(StratPlay.pitcher)
|
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_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_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
|
lob_all_rate, lob_2outs_rate, rbi_rate = 0, 0, 0
|
||||||
if x.count_runner1 + x.count_runner2 + x.count_runner3 > 0:
|
if x.count_runner1 + x.count_runner2 + x.count_runner3 > 0:
|
||||||
lob_all_rate = (x.count_lo1 + x.count_lo2 + x.count_lo3) / \
|
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,
|
'k/bb': x.sum_so / tot_bb,
|
||||||
'game': this_game,
|
'game': this_game,
|
||||||
'lob_2outs': x.count_lo1_3out + x.count_lo2_3out + x.count_lo3_3out,
|
'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()
|
db.close()
|
||||||
if csv:
|
if csv:
|
||||||
@ -739,7 +777,8 @@ async def get_fielding_totals(
|
|||||||
s_type: Literal['regular', 'post', 'total', None] = None, position: list = Query(default=None),
|
s_type: Literal['regular', 'post', 'total', None] = None, position: list = Query(default=None),
|
||||||
player_id: list = Query(default=None),
|
player_id: list = Query(default=None),
|
||||||
group_by: Literal['team', 'player', 'playerteam', 'playerposition', 'teamposition', 'playerpositiongame',
|
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),
|
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,
|
sort: Optional[str] = None, limit: Optional[int] = 200, short_output: Optional[bool] = False,
|
||||||
page_num: Optional[int] = 1):
|
page_num: Optional[int] = 1):
|
||||||
@ -748,8 +787,15 @@ async def get_fielding_totals(
|
|||||||
season_games = season_games.where(StratGame.season << season)
|
season_games = season_games.where(StratGame.season << season)
|
||||||
if week is not None and s_type is not None:
|
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')
|
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:
|
if week is not None:
|
||||||
season_games = season_games.where(StratGame.week << week)
|
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 is not None:
|
||||||
if s_type == 'regular':
|
if s_type == 'regular':
|
||||||
season_games = season_games.where(StratGame.week <= 18)
|
season_games = season_games.where(StratGame.week <= 18)
|
||||||
@ -810,6 +856,20 @@ async def get_fielding_totals(
|
|||||||
elif group_by == 'playerpositiongame':
|
elif group_by == 'playerpositiongame':
|
||||||
def_plays = def_plays.group_by(StratPlay.defender, StratPlay.check_pos, StratPlay.game)
|
def_plays = def_plays.group_by(StratPlay.defender, StratPlay.check_pos, StratPlay.game)
|
||||||
cat_plays = cat_plays.group_by(StratPlay.catcher, 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 is not None:
|
||||||
if sort == 'player':
|
if sort == 'player':
|
||||||
def_plays = def_plays.order_by(StratPlay.defender)
|
def_plays = def_plays.order_by(StratPlay.defender)
|
||||||
@ -830,8 +890,6 @@ async def get_fielding_totals(
|
|||||||
|
|
||||||
if limit < 1:
|
if limit < 1:
|
||||||
limit = 1
|
limit = 1
|
||||||
elif limit > 500:
|
|
||||||
limit = 500
|
|
||||||
def_plays = def_plays.paginate(page_num, limit)
|
def_plays = def_plays.paginate(page_num, limit)
|
||||||
|
|
||||||
logging.info(f'def_plays query: {def_plays}')
|
logging.info(f'def_plays query: {def_plays}')
|
||||||
@ -888,6 +946,10 @@ async def get_fielding_totals(
|
|||||||
if 'game' in group_by:
|
if 'game' in group_by:
|
||||||
this_game = x.game_id if short_output else model_to_dict(x.game, recurse=False)
|
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({
|
return_stats['stats'].append({
|
||||||
'player': this_player,
|
'player': this_player,
|
||||||
'team': x.defender_team_id if short_output else model_to_dict(x.defender_team, recurse=False),
|
'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,
|
'wpa': (x.sum_wpa + sum_wpa) * -1,
|
||||||
'wf%': (x.sum_chances - (x.sum_error * .5) - (x.sum_hit * .75)) / x.sum_chances,
|
'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,
|
'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()
|
db.close()
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user