Update stratplay.py

Fixed group_by not properly grouping SB/CS and decisions
This commit is contained in:
Cal Corum 2024-10-30 21:38:19 -05:00
parent 4380c26319
commit 4e21e42b6d

View File

@ -1,3 +1,6 @@
from itertools import groupby
from tokenize import group
from fastapi import APIRouter, Depends, HTTPException, Query, Response
from typing import List, Optional, Literal
import logging
@ -468,10 +471,21 @@ async def get_batting_totals(
for x in bat_plays:
this_run = run_plays.order_by(StratPlay.id)
if 'player' in group_by:
if group_by == 'player':
this_run = this_run.where(StratPlay.runner == x.batter)
if 'game' in group_by:
this_run = this_run.where(StratPlay.game == x.game)
elif group_by == 'team':
this_run = this_run.where(StratPlay.batter_team == x.batter_team)
elif group_by == 'playerteam':
this_run = this_run.where((StratPlay.runner == x.batter) & (StratPlay.batter_team == x.batter_team))
elif group_by == 'playergame':
this_run = this_run.where((StratPlay.runner == x.batter) & (StratPlay.game == x.game))
elif group_by == 'teamgame':
this_run = this_run.where((StratPlay.batter_team == x.batter_team) & (StratPlay.game == x.game))
elif group_by == 'playerweek':
this_run = this_run.where((StratPlay.runner == x.batter) & (StratPlay.game.week == x.game.week))
elif group_by == 'teamweek':
this_run = this_run.where((StratPlay.batter_team == x.batter_team) & (StratPlay.game.week == x.game.week))
if this_run.count() > 0:
sum_sb = this_run[0].sum_sb
@ -511,6 +525,10 @@ async def get_batting_totals(
if group_by in ['playerweek', 'teamweek']:
this_week = x.game.week
this_player = 'TOT'
if 'player' in group_by:
this_player = x.batter_id if short_output else model_to_dict(x.batter, recurse=False)
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) / \
@ -518,7 +536,7 @@ async def get_batting_totals(
rbi_rate = (x.sum_rbi - x.sum_hr) / (x.count_runner1 + x.count_runner2 + x.count_runner3)
return_stats['stats'].append({
'player': x.batter_id if short_output else model_to_dict(x.batter, recurse=False),
'player': this_player,
'team': x.batter_team_id if short_output else model_to_dict(x.batter_team, recurse=False),
'pa': x.sum_pa,
'ab': x.sum_ab,
@ -747,12 +765,30 @@ async def get_pitching_totals(
this_game = 'TOT'
if group_by in ['playergame', 'teamgame']:
this_game = x.game_id if short_output else model_to_dict(x.game, recurse=False)
if group_by == 'player':
this_dec = all_dec.where(Decision.pitcher == x.pitcher)
elif group_by == 'team':
this_dec = all_dec.where(Decision.team == x.pitcher_team)
elif group_by == 'playerteam':
this_dec = all_dec.where((Decision.pitcher == x.pitcher) & (Decision.team == x.pitcher_team))
elif group_by == 'playergame':
this_dec = all_dec.where((Decision.pitcher == x.pitcher) & (Decision.game == x.game))
elif group_by == 'teamgame':
this_dec = all_dec.where((Decision.team == x.pitcher_team) & (Decision.game == x.game))
elif group_by == 'playerweek':
this_dec = all_dec.where((Decision.pitcher == x.pitcher) & (Decision.game.week == x.game.week))
elif group_by == 'teamweek':
this_dec = all_dec.where((Decision.team == x.pitcher_team) & (Decision.game.week == x.game.week))
this_week = 'TOT'
if group_by in ['playerweek', 'teamweek']:
if 'week' in group_by:
this_week = x.game.week
this_player = 'TOT'
if 'player' in group_by:
this_player = x.pitcher_id if short_output else model_to_dict(x.pitcher, recurse=False)
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) / \
@ -760,7 +796,7 @@ async def get_pitching_totals(
rbi_rate = (x.sum_rbi - x.sum_hr) / (x.count_runner1 + x.count_runner2 + x.count_runner3)
return_stats['stats'].append({
'player': x.pitcher_id if short_output else model_to_dict(x.pitcher, recurse=False),
'player': this_player,
'team': x.pitcher_team_id if short_output else model_to_dict(x.pitcher_team, recurse=False),
'tbf': x.sum_pa,
'outs': x.sum_outs,