Update main.py
Added pitching stats
This commit is contained in:
parent
cfcd51beb3
commit
4b7f3e5d13
100
main.py
100
main.py
@ -3911,6 +3911,106 @@ async def get_batting_totals(
|
||||
return return_stats
|
||||
|
||||
|
||||
@app.get('/api/v1/plays/pitching')
|
||||
async def get_pitching_totals(
|
||||
player_id: list = Query(default=None), team_id: list = Query(default=None), season: list = Query(default=None),
|
||||
group_by: Literal['team', 'player', 'playerteam', 'playergame', 'teamgame', 'league'] = 'player',
|
||||
min_pa: Optional[int] = 1,
|
||||
sort: Optional[str] = None, limit: Optional[int] = None, short_output: Optional[bool] = False):
|
||||
all_stats = PitchingStat.select(
|
||||
PitchingStat.card, PitchingStat.team, PitchingStat.game_id, PitchingStat.vs_team,
|
||||
PitchingStat.card.player.alias('player'), fn.SUM(PitchingStat.ip).alias('sum_ip'),
|
||||
fn.SUM(PitchingStat.hit).alias('sum_hit'), fn.SUM(PitchingStat.run).alias('sum_run'),
|
||||
fn.SUM(PitchingStat.erun).alias('sum_erun'), fn.SUM(PitchingStat.so).alias('sum_so'),
|
||||
fn.SUM(PitchingStat.bb).alias('sum_bb'), fn.SUM(PitchingStat.hbp).alias('sum_hbp'),
|
||||
fn.SUM(PitchingStat.wp).alias('sum_wp'), fn.SUM(PitchingStat.balk).alias('sum_balk'),
|
||||
fn.SUM(PitchingStat.hr).alias('sum_hr'), fn.SUM(PitchingStat.ir).alias('sum_ir'),
|
||||
fn.SUM(PitchingStat.irs).alias('sum_irs'), fn.SUM(PitchingStat.gs).alias('sum_gs'),
|
||||
fn.SUM(PitchingStat.win).alias('sum_win'), fn.SUM(PitchingStat.loss).alias('sum_loss'),
|
||||
fn.SUM(PitchingStat.hold).alias('sum_hold'), fn.SUM(PitchingStat.sv).alias('sum_sv'),
|
||||
fn.SUM(PitchingStat.bsv).alias('sum_bsv'), fn.COUNT(PitchingStat.game_id).alias('sum_games')
|
||||
).having(
|
||||
fn.SUM(PitchingStat.ip) >= max(min_pa / 3, 1)
|
||||
).join(Card)
|
||||
|
||||
if player_id is not None:
|
||||
all_cards = Card.select().where(Card.player_id << player_id)
|
||||
all_stats = all_stats.where(PitchingStat.card << all_cards)
|
||||
if team_id is not None:
|
||||
all_teams = Team.select().where(Team.id << team_id)
|
||||
all_stats = all_stats.where(PitchingStat.team << all_teams)
|
||||
if season is not None:
|
||||
all_stats = all_stats.where(PitchingStat.season << season)
|
||||
|
||||
if group_by == 'player':
|
||||
all_stats = all_stats.group_by(SQL('player'))
|
||||
elif group_by == 'playerteam':
|
||||
all_stats = all_stats.group_by(SQL('player'), PitchingStat.team)
|
||||
elif group_by == 'playergame':
|
||||
all_stats = all_stats.group_by(SQL('player'), PitchingStat.game_id)
|
||||
elif group_by == 'team':
|
||||
all_stats = all_stats.group_by(PitchingStat.team)
|
||||
elif group_by == 'teamgame':
|
||||
all_stats = all_stats.group_by(PitchingStat.team, PitchingStat.game_id)
|
||||
elif group_by == 'league':
|
||||
all_stats = all_stats.group_by(PitchingStat.season)
|
||||
|
||||
if sort == 'pa-desc':
|
||||
all_stats = all_stats.order_by(SQL('sum_pa').desc())
|
||||
elif sort == 'newest':
|
||||
all_stats = all_stats.order_by(-PitchingStat.game_id)
|
||||
elif sort == 'oldest':
|
||||
all_stats = all_stats.order_by(PitchingStat.game_id)
|
||||
|
||||
if limit is not None:
|
||||
if limit < 1:
|
||||
limit = 1
|
||||
all_stats = all_stats.limit(limit)
|
||||
|
||||
logging.info(f'bat_plays query: {all_stats}')
|
||||
|
||||
return_stats = {
|
||||
'count': all_stats.count(),
|
||||
'stats': [{
|
||||
'player': x.card.player_id if short_output else model_to_dict(x.card.player, recurse=False),
|
||||
'team': x.team_id if short_output else model_to_dict(x.team, recurse=False),
|
||||
'tbf': None,
|
||||
'outs': round(x.sum_ip * 3),
|
||||
'games': x.sum_games,
|
||||
'gs': x.sum_gs,
|
||||
'win': x.sum_win,
|
||||
'loss': x.sum_loss,
|
||||
'hold': x.sum_hold,
|
||||
'save': x.sum_sv,
|
||||
'bsave': x.sum_bsv,
|
||||
'ir': x.sum_ir,
|
||||
'ir_sc': x.sum_irs,
|
||||
'runs': x.sum_run,
|
||||
'e_runs': x.sum_erun,
|
||||
'hits': x.sum_hit,
|
||||
'hr': x.sum_hr,
|
||||
'bb': x.sum_bb,
|
||||
'so': x.sum_so,
|
||||
'hbp': x.sum_hbp,
|
||||
'wp': x.sum_wp,
|
||||
'balk': x.sum_balk,
|
||||
'era': (x.sum_erun * 27) / round(x.sum_ip * 3),
|
||||
'whip': (x.sum_bb + x.sum_hit) / x.sum_ip,
|
||||
'avg': None,
|
||||
'obp': None,
|
||||
'woba': None,
|
||||
'k/9': x.sum_so * 9 / x.sum_ip,
|
||||
'bb/9': x.sum_bb * 9 / x.sum_ip,
|
||||
'k/bb': x.sum_so / max(x.sum_bb, .1),
|
||||
'game': None,
|
||||
'lob_2outs': None,
|
||||
'rbi%': None
|
||||
} for x in all_stats]
|
||||
}
|
||||
db.close()
|
||||
return return_stats
|
||||
|
||||
|
||||
@app.post('/api/v1/batstats')
|
||||
async def v1_batstats_post(stats: BattingStatModel, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user