Update stratplay.py

Add position grouping to /fielding
Add rate stats to /pitching
This commit is contained in:
Cal Corum 2023-08-07 10:35:30 -05:00
parent 17e9ed100a
commit 08e54e10a1

View File

@ -471,6 +471,7 @@ async def get_pitching_totals(
obp = (x.sum_hit + x.sum_bb + x.sum_hbp + x.sum_ibb) / x.sum_pa obp = (x.sum_hit + x.sum_bb + x.sum_hbp + x.sum_ibb) / x.sum_pa
slg = (x.sum_hr * 4 + x.sum_triple * 3 + x.sum_double * 2 + slg = (x.sum_hr * 4 + x.sum_triple * 3 + x.sum_double * 2 +
(x.sum_hit - x.sum_double - x.sum_triple - x.sum_hr)) / x.sum_ab (x.sum_hit - x.sum_double - x.sum_triple - x.sum_hr)) / x.sum_ab
tot_bb = 0.1 if x.sum_bb == 0 else x.sum_bb
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),
'team': x.pitcher_team_id if short_output else model_to_dict(x.pitcher_team, recurse=False), 'team': x.pitcher_team_id if short_output else model_to_dict(x.pitcher_team, recurse=False),
@ -514,7 +515,10 @@ async def get_pitching_totals(
'slg': slg, 'slg': slg,
'ops': obp + slg, 'ops': obp + slg,
'woba': (.69 * x.sum_bb + .72 * x.sum_hbp + .89 * (x.sum_hit - x.sum_double - x.sum_triple - x.sum_hr) + 'woba': (.69 * x.sum_bb + .72 * x.sum_hbp + .89 * (x.sum_hit - x.sum_double - x.sum_triple - x.sum_hr) +
1.27 * x.sum_double + 1.62 * x.sum_triple + 2.1 * x.sum_hr) / (x.sum_pa - x.sum_ibb) 1.27 * x.sum_double + 1.62 * x.sum_triple + 2.1 * x.sum_hr) / (x.sum_pa - x.sum_ibb),
'k/9': x.sum_so * 9 / (tot_outs / 3),
'bb/9': x.sum_bb * 9 / (tot_outs / 3),
'k/bb': x.sum_so / tot_bb
}) })
db.close() db.close()
return return_stats return return_stats
@ -524,7 +528,8 @@ async def get_pitching_totals(
async def get_fielding_totals( async def get_fielding_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), group_by: Literal['team', 'player', 'playerteam'] = 'player', player_id: list = Query(default=None),
group_by: Literal['team', 'player', 'playerteam', 'playerposition'] = 'player',
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] = 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()
@ -541,7 +546,7 @@ async def get_fielding_totals(
StratPlay StratPlay
.select(StratPlay.defender, StratPlay.defender_team, fn.SUM(StratPlay.error).alias('sum_error'), .select(StratPlay.defender, StratPlay.defender_team, fn.SUM(StratPlay.error).alias('sum_error'),
fn.SUM(StratPlay.hit).alias('sum_hit'), fn.SUM(StratPlay.pa).alias('sum_chances'), fn.SUM(StratPlay.hit).alias('sum_hit'), fn.SUM(StratPlay.pa).alias('sum_chances'),
fn.SUM(StratPlay.wpa).alias('sum_wpa')) fn.SUM(StratPlay.wpa).alias('sum_wpa'), StratPlay.check_pos)
.where((StratPlay.game << season_games) & (StratPlay.defender.is_null(False))) .where((StratPlay.game << season_games) & (StratPlay.defender.is_null(False)))
.having(fn.SUM(StratPlay.pa) >= min_ch) .having(fn.SUM(StratPlay.pa) >= min_ch)
) )
@ -574,6 +579,9 @@ async def get_fielding_totals(
elif group_by == 'playerteam': elif group_by == 'playerteam':
def_plays = def_plays.group_by(StratPlay.defender, StratPlay.defender_team) def_plays = def_plays.group_by(StratPlay.defender, StratPlay.defender_team)
cat_plays = cat_plays.group_by(StratPlay.catcher, StratPlay.catcher_team) cat_plays = cat_plays.group_by(StratPlay.catcher, StratPlay.catcher_team)
elif group_by == 'playerposition':
def_plays = def_plays.group_by(StratPlay.defender, StratPlay.check_pos)
cat_plays = cat_plays.group_by(StratPlay.catcher)
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)
@ -600,6 +608,7 @@ async def get_fielding_totals(
} }
for x in def_plays: for x in def_plays:
logging.info(f'this_play: {x}')
this_cat = cat_plays.where(StratPlay.catcher == x.defender) this_cat = cat_plays.where(StratPlay.catcher == x.defender)
if this_cat.count() > 0: if this_cat.count() > 0:
sum_sb = this_cat[0].sum_sb sum_sb = this_cat[0].sum_sb
@ -611,10 +620,13 @@ async def get_fielding_totals(
sum_cs = 0 sum_cs = 0
sum_wpa = 0 sum_wpa = 0
sum_pb = 0 sum_pb = 0
this_pos = 'TOT'
if group_by == 'playerposition':
this_pos = x.check_pos
return_stats['stats'].append({ return_stats['stats'].append({
'player': x.defender_id if short_output else model_to_dict(x.defender, recurse=False), 'player': x.defender_id if short_output else model_to_dict(x.defender, recurse=False),
'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),
'pos': 'TOT' if position is None or len(position) > 1 else position[0], 'pos': this_pos,
'x-ch': x.sum_chances, 'x-ch': x.sum_chances,
'hit': x.sum_hit, 'hit': x.sum_hit,
'error': x.sum_error, 'error': x.sum_error,