Update stratplay.py

Added re24 to /batting and /pitching pulls
This commit is contained in:
Cal Corum 2024-01-22 23:22:51 -06:00
parent fae1439365
commit 1ef4974218

View File

@ -274,7 +274,8 @@ async def get_batting_totals(
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, week_start: Optional[int] = None, week_end: Optional[int] = None):
page_num: Optional[int] = 1, week_start: Optional[int] = None, week_end: Optional[int] = None,
min_repri: Optional[int] = None):
season_games = StratGame.select()
if season is not None:
season_games = season_games.where(StratGame.season << season)
@ -313,7 +314,7 @@ async def get_batting_totals(
fn.SUM(StratPlay.sb).alias('sum_sb'), fn.SUM(StratPlay.cs).alias('sum_cs'),
fn.SUM(StratPlay.bphr).alias('sum_bphr'), fn.SUM(StratPlay.bpfo).alias('sum_bpfo'),
fn.SUM(StratPlay.bp1b).alias('sum_bp1b'), fn.SUM(StratPlay.bplo).alias('sum_bplo'),
fn.SUM(StratPlay.wpa).alias('sum_wpa'),
fn.SUM(StratPlay.wpa).alias('sum_wpa'), fn.SUM(StratPlay.re24_primary).alias('sum_repri'),
fn.COUNT(StratPlay.on_first_final).filter(
StratPlay.on_first_final.is_null(False) & (StratPlay.on_first_final != 4)).alias('count_lo1'),
fn.COUNT(StratPlay.on_second_final).filter(
@ -340,13 +341,15 @@ async def get_batting_totals(
# (StratPlay.starting_outs + StratPlay.outs == 3)).alias('count_runner3_3out')
)
.where((StratPlay.game << season_games) & (StratPlay.batter.is_null(False)))
.having(fn.SUM(StratPlay.pa) >= min_pa)
.having((fn.SUM(StratPlay.pa) >= min_pa))
)
if min_repri is not None:
bat_plays = bat_plays.having(fn.SUM(StratPlay.re24_primary) >= min_repri)
run_plays = (
StratPlay
.select(StratPlay.runner, StratPlay.runner_team, fn.SUM(StratPlay.sb).alias('sum_sb'),
fn.SUM(StratPlay.cs).alias('sum_cs'), fn.SUM(StratPlay.pick_off).alias('sum_pick'),
fn.SUM(StratPlay.wpa).alias('sum_wpa'))
fn.SUM(StratPlay.wpa).alias('sum_wpa'), fn.SUM(StratPlay.re24_running).alias('sum_rerun'))
.where((StratPlay.game << season_games) & (StratPlay.runner.is_null(False)))
)
def_plays = (
@ -425,6 +428,10 @@ async def get_batting_totals(
bat_plays = bat_plays.order_by(SQL('sum_wpa').desc())
elif sort == 'wpa-asc':
bat_plays = bat_plays.order_by(SQL('sum_wpa').asc())
elif sort == 'repri-desc':
bat_plays = bat_plays.order_by(SQL('sum_repri').desc())
elif sort == 'repri-asc':
bat_plays = bat_plays.order_by(SQL('sum_repri').asc())
elif sort == 'pa-desc':
bat_plays = bat_plays.order_by(SQL('sum_pa').desc())
elif sort == 'pa-asc':
@ -459,10 +466,13 @@ async def get_batting_totals(
sum_sb = this_run[0].sum_sb
sum_cs = this_run[0].sum_cs
run_wpa = this_run[0].sum_wpa
sum_rerun = this_run[0].sum_rerun
else:
sum_sb = 0
sum_cs = 0
run_wpa = 0
sum_rerun = 0
this_wpa = bat_plays.where(
(StratPlay.wpa >= min_wpa) & (StratPlay.wpa <= max_wpa) & (StratPlay.batter == x.batter)
)
@ -471,6 +481,12 @@ async def get_batting_totals(
else:
sum_wpa = 0
this_repri = bat_plays.where(StratPlay.batter == x.batter)
if this_wpa.count() > 0:
sum_repri = this_repri[0].sum_repri
else:
sum_repri = 0
tot_ab = x.sum_ab if x.sum_ab > 0 else 1
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 +
@ -525,7 +541,9 @@ async def get_batting_totals(
'lob_all_rate': lob_all_rate,
'lob_2outs': x.count_lo1_3out + x.count_lo2_3out + x.count_lo3_3out,
'rbi%': rbi_rate,
'week': this_week
'week': this_week,
're24_primary': sum_repri,
# 're24_running': sum_rerun
})
db.close()
@ -599,7 +617,8 @@ async def get_pitching_totals(
(StratPlay.starting_outs + StratPlay.outs == 3)).alias('count_lo2_3out'),
fn.COUNT(StratPlay.on_third_final).filter(
StratPlay.on_third_final.is_null(False) & (StratPlay.on_third_final != 4) &
(StratPlay.starting_outs + StratPlay.outs == 3)).alias('count_lo3_3out'))
(StratPlay.starting_outs + StratPlay.outs == 3)).alias('count_lo3_3out'),
fn.SUM(StratPlay.re24_primary).alias('sum_repri'))
.where((StratPlay.game << season_games) & (StratPlay.pitcher.is_null(False)))
.having(fn.SUM(StratPlay.pa) >= min_pa)
)
@ -658,6 +677,10 @@ async def get_pitching_totals(
pit_plays = pit_plays.order_by(SQL('sum_wpa').asc()) # functions seem reversed since pitcher plays negative
elif sort.lower() == 'wpa-asc':
pit_plays = pit_plays.order_by(SQL('sum_wpa').desc())
elif sort.lower() == 'repri-desc':
pit_plays = pit_plays.order_by(SQL('sum_repri').asc()) # functions seem reversed since pitcher plays negative
elif sort.lower() == 'repri-asc':
pit_plays = pit_plays.order_by(SQL('sum_repri').desc())
elif sort.lower() == 'ip-desc':
pit_plays = pit_plays.order_by(SQL('sum_outs').desc())
elif sort.lower() == 'ip-asc':
@ -769,7 +792,8 @@ async def get_pitching_totals(
'game': this_game,
'lob_2outs': x.count_lo1_3out + x.count_lo2_3out + x.count_lo3_3out,
'rbi%': rbi_rate,
'week': this_week
'week': this_week,
're24_primary': x.sum_repri * -1
})
db.close()
if csv: