Update stratplay.py
- Added hit param to /plays - Added to /plays/batting and /plays/pitching: -- "league" group_by option -- inning param -- rbi% output
This commit is contained in:
parent
b3d2854210
commit
f9c578187e
@ -125,7 +125,8 @@ async def get_plays(
|
|||||||
batter_pos: list = Query(default=None), catcher_id: list = Query(default=None),
|
batter_pos: list = Query(default=None), catcher_id: list = Query(default=None),
|
||||||
defender_id: list = Query(default=None), runner_id: list = Query(default=None),
|
defender_id: list = Query(default=None), runner_id: list = Query(default=None),
|
||||||
offense_team_id: list = Query(default=None), defense_team_id: list = Query(default=None),
|
offense_team_id: list = Query(default=None), defense_team_id: list = Query(default=None),
|
||||||
double: Optional[int] = None, triple: Optional[int] = None, homerun: Optional[int] = None,
|
hit: Optional[int] = None, double: Optional[int] = None, triple: Optional[int] = None,
|
||||||
|
homerun: Optional[int] = None,
|
||||||
sb: Optional[int] = None, cs: Optional[int] = None, manager_id: list = Query(default=None),
|
sb: Optional[int] = None, cs: Optional[int] = None, manager_id: list = Query(default=None),
|
||||||
run: Optional[int] = None, e_run: Optional[int] = None, rbi: list = Query(default=None),
|
run: Optional[int] = None, e_run: Optional[int] = None, rbi: list = Query(default=None),
|
||||||
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,
|
||||||
@ -180,6 +181,8 @@ async def get_plays(
|
|||||||
all_plays = all_plays.where(
|
all_plays = all_plays.where(
|
||||||
(StratPlay.catcher_team << all_teams) | (StratPlay.defender_team << all_teams)
|
(StratPlay.catcher_team << all_teams) | (StratPlay.defender_team << all_teams)
|
||||||
)
|
)
|
||||||
|
if hit is not None:
|
||||||
|
all_plays = all_plays.where(StratPlay.hit == hit)
|
||||||
if double is not None:
|
if double is not None:
|
||||||
all_plays = all_plays.where(StratPlay.double == double)
|
all_plays = all_plays.where(StratPlay.double == double)
|
||||||
if triple is not None:
|
if triple is not None:
|
||||||
@ -252,9 +255,9 @@ 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'] = 'player',
|
group_by: Literal['team', 'player', 'playerteam', 'playergame', 'teamgame', 'league'] = '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,
|
obc: list = Query(default=None), risp: Optional[bool] = None, inning: list = Query(default=None),
|
||||||
sort: Optional[str] = None, limit: Optional[int] = None, short_output: Optional[bool] = False):
|
sort: Optional[str] = None, limit: Optional[int] = None, short_output: Optional[bool] = False):
|
||||||
season_games = StratGame.select()
|
season_games = StratGame.select()
|
||||||
if season is not None:
|
if season is not None:
|
||||||
@ -340,6 +343,8 @@ async def get_batting_totals(
|
|||||||
bat_plays = bat_plays.where(StratPlay.on_base_code << obc)
|
bat_plays = bat_plays.where(StratPlay.on_base_code << obc)
|
||||||
if risp is not None:
|
if risp is not None:
|
||||||
bat_plays = bat_plays.where(StratPlay.on_base_code << ['100', '101', '110', '111', '010', '011'])
|
bat_plays = bat_plays.where(StratPlay.on_base_code << ['100', '101', '110', '111', '010', '011'])
|
||||||
|
if inning is not None:
|
||||||
|
bat_plays = bat_plays.where(StratPlay.inning_num << inning)
|
||||||
|
|
||||||
if group_by is not None:
|
if group_by is not None:
|
||||||
if group_by == 'player':
|
if group_by == 'player':
|
||||||
@ -362,6 +367,11 @@ async def get_batting_totals(
|
|||||||
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)
|
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)
|
||||||
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)
|
||||||
@ -397,9 +407,6 @@ async def get_batting_totals(
|
|||||||
}
|
}
|
||||||
|
|
||||||
for x in bat_plays:
|
for x in bat_plays:
|
||||||
logging.info(
|
|
||||||
f'batter: {x.batter_id} / left on 1: {x.count_lo1} / left on 2: {x.count_lo2} / left on 3: {x.count_lo3}'
|
|
||||||
)
|
|
||||||
this_run = run_plays.where(StratPlay.runner == x.batter)
|
this_run = run_plays.where(StratPlay.runner == x.batter)
|
||||||
if this_run.count() > 0:
|
if this_run.count() > 0:
|
||||||
sum_sb = this_run[0].sum_sb
|
sum_sb = this_run[0].sum_sb
|
||||||
@ -426,10 +433,11 @@ 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)
|
||||||
|
|
||||||
lob_all_rate, lob_2outs_rate = 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) / \
|
||||||
(x.count_runner1 + x.count_runner2 + x.count_runner3)
|
(x.count_runner1 + x.count_runner2 + x.count_runner3)
|
||||||
|
rbi_rate = (x.sum_rbi - x.sum_hr) / (x.count_runner1 + x.count_runner2 + x.count_runner3)
|
||||||
|
|
||||||
return_stats['stats'].append({
|
return_stats['stats'].append({
|
||||||
'player': x.batter_id if short_output else model_to_dict(x.batter, recurse=False),
|
'player': x.batter_id if short_output else model_to_dict(x.batter, recurse=False),
|
||||||
@ -464,7 +472,8 @@ async def get_batting_totals(
|
|||||||
'game': this_game,
|
'game': this_game,
|
||||||
'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
|
||||||
})
|
})
|
||||||
|
|
||||||
# Get Running Stats
|
# Get Running Stats
|
||||||
@ -479,9 +488,9 @@ 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'] = 'player',
|
group_by: Literal['team', 'player', 'playerteam', 'playergame', 'teamgame', 'league'] = '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,
|
obc: list = Query(default=None), risp: Optional[bool] = None, inning: list = Query(default=None),
|
||||||
sort: Optional[str] = None, limit: Optional[int] = None, short_output: Optional[bool] = False):
|
sort: Optional[str] = None, limit: Optional[int] = None, short_output: Optional[bool] = False):
|
||||||
season_games = StratGame.select()
|
season_games = StratGame.select()
|
||||||
if season is not None:
|
if season is not None:
|
||||||
@ -550,6 +559,8 @@ async def get_pitching_totals(
|
|||||||
pit_plays = pit_plays.where(StratPlay.on_base_code << obc)
|
pit_plays = pit_plays.where(StratPlay.on_base_code << obc)
|
||||||
if risp is not None:
|
if risp is not None:
|
||||||
pit_plays = pit_plays.where(StratPlay.on_base_code << ['100', '101', '110', '111', '010', '011'])
|
pit_plays = pit_plays.where(StratPlay.on_base_code << ['100', '101', '110', '111', '010', '011'])
|
||||||
|
if inning is not None:
|
||||||
|
pit_plays = pit_plays.where(StratPlay.inning_num << inning)
|
||||||
|
|
||||||
if group_by is not None:
|
if group_by is not None:
|
||||||
if group_by == 'player':
|
if group_by == 'player':
|
||||||
@ -562,6 +573,9 @@ async def get_pitching_totals(
|
|||||||
pit_plays = pit_plays.group_by(StratPlay.pitcher, StratPlay.game)
|
pit_plays = pit_plays.group_by(StratPlay.pitcher, StratPlay.game)
|
||||||
elif group_by == 'teamgame':
|
elif group_by == 'teamgame':
|
||||||
pit_plays = pit_plays.group_by(StratPlay.pitcher_team, StratPlay.game)
|
pit_plays = pit_plays.group_by(StratPlay.pitcher_team, StratPlay.game)
|
||||||
|
elif group_by == 'league':
|
||||||
|
pit_plays = pit_plays.join(StratGame)
|
||||||
|
pit_plays = pit_plays.group_by(StratPlay.game.season)
|
||||||
if sort is not None:
|
if sort is not None:
|
||||||
if sort == 'player':
|
if sort == 'player':
|
||||||
pit_plays = pit_plays.order_by(StratPlay.pitcher)
|
pit_plays = pit_plays.order_by(StratPlay.pitcher)
|
||||||
@ -610,6 +624,7 @@ async def get_pitching_totals(
|
|||||||
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) / \
|
||||||
(x.count_runner1 + x.count_runner2 + x.count_runner3)
|
(x.count_runner1 + x.count_runner2 + x.count_runner3)
|
||||||
|
rbi_rate = (x.sum_rbi - x.sum_hr) / (x.count_runner1 + x.count_runner2 + x.count_runner3)
|
||||||
|
|
||||||
return_stats['stats'].append({
|
return_stats['stats'].append({
|
||||||
'player': x.pitcher_id if short_output else model_to_dict(x.pitcher, recurse=False),
|
'player': x.pitcher_id if short_output else model_to_dict(x.pitcher, recurse=False),
|
||||||
@ -659,7 +674,8 @@ async def get_pitching_totals(
|
|||||||
'bb/9': x.sum_bb * 9 / (tot_outs / 3),
|
'bb/9': x.sum_bb * 9 / (tot_outs / 3),
|
||||||
'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
|
||||||
})
|
})
|
||||||
db.close()
|
db.close()
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user