Added /games/game-summary
This commit is contained in:
parent
74a037b3fc
commit
a49861bf31
@ -1,3 +1,5 @@
|
||||
import math
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Response
|
||||
from typing import List, Optional, Literal
|
||||
import logging
|
||||
@ -348,7 +350,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).alias('sum_re24'),
|
||||
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(
|
||||
@ -381,7 +383,7 @@ async def get_batting_totals(
|
||||
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).alias('sum_re24'))
|
||||
.where((StratPlay.game << season_games) & (StratPlay.runner.is_null(False)))
|
||||
)
|
||||
|
||||
@ -487,10 +489,12 @@ 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
|
||||
run_re24 = this_run[0].sum_re24
|
||||
else:
|
||||
sum_sb = 0
|
||||
sum_cs = 0
|
||||
run_wpa = 0
|
||||
run_re24 = 0
|
||||
this_wpa = bat_plays.where(
|
||||
(StratPlay.wpa >= min_wpa) & (StratPlay.wpa <= max_wpa) & (StratPlay.batter == x.batter)
|
||||
)
|
||||
@ -538,6 +542,7 @@ async def get_batting_totals(
|
||||
'bp1b': x.sum_bp1b,
|
||||
'bplo': x.sum_bplo,
|
||||
'wpa': sum_wpa + run_wpa,
|
||||
're24': x.sum_re24 + run_re24,
|
||||
'avg': x.sum_hit / tot_ab,
|
||||
'obp': obp,
|
||||
'slg': slg,
|
||||
@ -821,6 +826,123 @@ async def get_pitching_totals(
|
||||
return return_stats
|
||||
|
||||
|
||||
@router.get('/game-summary/{game_id}')
|
||||
async def get_game_summary(
|
||||
game_id: int, csv: Optional[bool] = False, short_output: Optional[bool] = False, tp_max: Optional[int] = 1):
|
||||
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
||||
if this_game is None:
|
||||
db.close()
|
||||
raise HTTPException(status_code=404, detail=f'Game {game_id} not found')
|
||||
|
||||
game_plays = StratPlay.select().where(StratPlay.game_id == game_id)
|
||||
all_hits = game_plays.where(StratPlay.hit == 1)
|
||||
all_errors = game_plays.where(StratPlay.error == 1)
|
||||
all_runs = game_plays.where(StratPlay.run == 1)
|
||||
|
||||
all_dec = Decision.select().where(Decision.game_id == game_id)
|
||||
winner = all_dec.where(Decision.win == 1)
|
||||
loser = all_dec.where(Decision.loss == 1)
|
||||
save_p = all_dec.where(Decision.is_save == 1)
|
||||
save_pitcher = None
|
||||
if save_p.count() > 0:
|
||||
save_pitcher = model_to_dict(save_p.get().pitcher, recurse=not short_output)
|
||||
|
||||
all_holds = all_dec.where(Decision.hold == 1)
|
||||
all_bsaves = all_dec.where(Decision.b_save == 1)
|
||||
|
||||
doubles = all_hits.where(StratPlay.double == 1)
|
||||
triples = all_hits.where(StratPlay.triple == 1)
|
||||
homeruns = all_hits.where((StratPlay.homerun == 1) | (StratPlay.bphr == 1))
|
||||
|
||||
steal_att = game_plays.where(StratPlay.runner.is_null(False))
|
||||
all_sb = steal_att.where(StratPlay.sb == 1)
|
||||
all_cs = steal_att.where(StratPlay.cs == 1)
|
||||
|
||||
top_batters = (
|
||||
StratPlay
|
||||
.select(StratPlay.batter, fn.SUM(StratPlay.re24).alias('sum_re24'), fn.SUM(StratPlay.ab).alias('sum_ab'),
|
||||
fn.SUM(StratPlay.run).alias('sum_run'),
|
||||
fn.SUM(StratPlay.hit).alias('sum_hit'), fn.SUM(StratPlay.rbi).alias('sum_rbi'),
|
||||
fn.SUM(StratPlay.double).alias('sum_double'), fn.SUM(StratPlay.triple).alias('sum_triple'),
|
||||
fn.SUM(StratPlay.homerun).alias('sum_hr'), fn.SUM(StratPlay.bphr).alias('sum_bphr'))
|
||||
.where(StratPlay.game_id == game_id)
|
||||
.group_by(StratPlay.batter)
|
||||
.order_by(SQL('sum_re24').desc())
|
||||
.limit(tp_max)
|
||||
)
|
||||
top_pitchers = (
|
||||
StratPlay
|
||||
.select(StratPlay.pitcher, fn.SUM(StratPlay.re24).alias('sum_re24'), fn.SUM(StratPlay.pa).alias('sum_pa'),
|
||||
fn.SUM(StratPlay.outs).alias('sum_outs'), fn.SUM(StratPlay.e_run).alias('sum_erun'),
|
||||
fn.SUM(StratPlay.run).alias('sum_run'), fn.SUM(StratPlay.so).alias('sum_so'),
|
||||
fn.SUM(StratPlay.hit).alias('sum_hit'))
|
||||
.where(StratPlay.game_id == game_id)
|
||||
.group_by(StratPlay.pitcher)
|
||||
.order_by(SQL('sum_re24').asc())
|
||||
.limit(tp_max)
|
||||
)
|
||||
top_b = [{
|
||||
'player': model_to_dict(x.batter, recurse=not short_output),
|
||||
'ab': x.sum_ab,
|
||||
'run': x.sum_run,
|
||||
'hit': x.sum_hit,
|
||||
'rbi': x.sum_rbi,
|
||||
'double': x.sum_double,
|
||||
'triple': x.sum_triple,
|
||||
'hr': x.sum_hr + x.sum_bphr,
|
||||
're24': x.sum_re24
|
||||
} for x in top_batters]
|
||||
top_p = [{
|
||||
'player': model_to_dict(x.pitcher, recurse=not short_output),
|
||||
'tbf': x.sum_pa,
|
||||
'ip': math.floor(x.sum_outs / 3) + ((x.sum_outs % 3) * .1),
|
||||
'run': x.sum_run,
|
||||
'e_run': x.sum_erun,
|
||||
'hit': x.sum_hit,
|
||||
'so': x.sum_so,
|
||||
're24': x.sum_re24 * -1
|
||||
} for x in top_pitchers]
|
||||
top_players = [*top_b, *top_p]
|
||||
logging.info(f'top_players: {top_players}')
|
||||
|
||||
return {
|
||||
'game': model_to_dict(this_game, recurse=not short_output),
|
||||
'teams': {
|
||||
'away': model_to_dict(this_game.away_team, recurse=not short_output),
|
||||
'home': model_to_dict(this_game.home_team, recurse=not short_output),
|
||||
},
|
||||
'runs': {
|
||||
'away': all_runs.where(StratPlay.batter_team == this_game.away_team).count(),
|
||||
'home': all_runs.where(StratPlay.batter_team == this_game.home_team).count()
|
||||
},
|
||||
'hits': {
|
||||
'away': all_hits.where(StratPlay.batter_team == this_game.away_team).count(),
|
||||
'home': all_hits.where(StratPlay.batter_team == this_game.home_team).count()
|
||||
},
|
||||
'errors': {
|
||||
'away': all_errors.where(StratPlay.defender_team == this_game.away_team).count(),
|
||||
'home': all_errors.where(StratPlay.defender_team == this_game.home_team).count()
|
||||
},
|
||||
'top-players': sorted(top_players, key=lambda x: x['re24'], reverse=True)[:tp_max],
|
||||
'pitchers': {
|
||||
'win': model_to_dict(winner.get().pitcher, recurse=not short_output),
|
||||
'loss': model_to_dict(loser.get().pitcher, recurse=not short_output),
|
||||
'holds': [model_to_dict(x.pitcher, recurse=not short_output) for x in all_holds],
|
||||
'save': save_pitcher,
|
||||
'b_saves': [model_to_dict(x.pitcher, recurse=not short_output) for x in all_bsaves]
|
||||
},
|
||||
'xbh': {
|
||||
'2b': [model_to_dict(x.batter, recurse=not short_output) for x in doubles],
|
||||
'3b': [model_to_dict(x.batter, recurse=not short_output) for x in triples],
|
||||
'hr': [model_to_dict(x.batter, recurse=not short_output) for x in homeruns]
|
||||
},
|
||||
'running': {
|
||||
'sb': [model_to_dict(x.runner, recurse=not short_output) for x in all_sb],
|
||||
'csc': [model_to_dict(x.catcher, recurse=not short_output) for x in all_cs]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@router.get('/{play_id}')
|
||||
async def get_one_play(play_id: int):
|
||||
if StratPlay.get_or_none(StratPlay.id == play_id) is None:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user