Batting, Pitching, and Fielding stats complete
This commit is contained in:
parent
28e02136dd
commit
d5cfaaa475
@ -1,7 +1,7 @@
|
|||||||
from fastapi import Depends, FastAPI
|
from fastapi import Depends, FastAPI
|
||||||
|
|
||||||
from .routers_v3 import current, players, results, schedules, standings, teams, transactions, battingstats, \
|
from .routers_v3 import current, players, results, schedules, standings, teams, transactions, battingstats, \
|
||||||
pitchingstats
|
pitchingstats, fieldingstats
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
responses={404: {'description': 'Not found'}}
|
responses={404: {'description': 'Not found'}}
|
||||||
@ -17,6 +17,7 @@ app.include_router(transactions.router)
|
|||||||
app.include_router(standings.router)
|
app.include_router(standings.router)
|
||||||
app.include_router(battingstats.router)
|
app.include_router(battingstats.router)
|
||||||
app.include_router(pitchingstats.router)
|
app.include_router(pitchingstats.router)
|
||||||
|
app.include_router(fieldingstats.router)
|
||||||
|
|
||||||
|
|
||||||
# @app.get("/api")
|
# @app.get("/api")
|
||||||
|
|||||||
@ -130,7 +130,7 @@ async def get_batstats(
|
|||||||
async def get_seasonstats(
|
async def get_seasonstats(
|
||||||
season: int, s_type: Literal['regular', 'post', 'total'] = 'regular', team_abbrev: list = Query(default=None),
|
season: int, s_type: Literal['regular', 'post', 'total'] = 'regular', team_abbrev: list = Query(default=None),
|
||||||
team_id: list = Query(default=None), player_name: list = Query(default=None),
|
team_id: list = Query(default=None), player_name: list = Query(default=None),
|
||||||
player_id: list = Query(default=None), full_player: Optional[bool] = False):
|
player_id: list = Query(default=None), short_output: Optional[bool] = False):
|
||||||
weeks = per_season_weeks(season, s_type)
|
weeks = per_season_weeks(season, s_type)
|
||||||
all_stats = (
|
all_stats = (
|
||||||
BattingStat
|
BattingStat
|
||||||
@ -177,9 +177,9 @@ async def get_seasonstats(
|
|||||||
all_stats = all_stats.where(BattingStat.player << all_players)
|
all_stats = all_stats.where(BattingStat.player << all_players)
|
||||||
|
|
||||||
return_stats = {
|
return_stats = {
|
||||||
'count': all_stats.count(),
|
'count': sum(1 for i in all_stats if i.sum_pa > 0),
|
||||||
'stats': [{
|
'stats': [{
|
||||||
'player': model_to_dict(x.player, recurse=False) if full_player else x.player_id,
|
'player': x.player_id if short_output else model_to_dict(x.player, recurse=False),
|
||||||
'pa': x.sum_pa,
|
'pa': x.sum_pa,
|
||||||
'ab': x.sum_ab,
|
'ab': x.sum_ab,
|
||||||
'run': x.sum_run,
|
'run': x.sum_run,
|
||||||
@ -212,7 +212,7 @@ async def get_seasonstats(
|
|||||||
'robs': x.sum_robs,
|
'robs': x.sum_robs,
|
||||||
'raa': x.sum_raa,
|
'raa': x.sum_raa,
|
||||||
'rto': x.sum_rto,
|
'rto': x.sum_rto,
|
||||||
} for x in all_stats]
|
} for x in all_stats if x.sum_pa > 0]
|
||||||
}
|
}
|
||||||
db.close()
|
db.close()
|
||||||
return return_stats
|
return return_stats
|
||||||
@ -263,4 +263,5 @@ async def post_batstats(s_list: BatStatList, token: str = Depends(oauth2_scheme)
|
|||||||
|
|
||||||
# Update career stats
|
# Update career stats
|
||||||
|
|
||||||
|
db.close()
|
||||||
return f'Added {len(all_stats)} batting lines'
|
return f'Added {len(all_stats)} batting lines'
|
||||||
|
|||||||
152
app/routers_v3/fieldingstats.py
Normal file
152
app/routers_v3/fieldingstats.py
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
|
from typing import List, Optional, Literal
|
||||||
|
import logging
|
||||||
|
import pydantic
|
||||||
|
|
||||||
|
from ..db_engine import db, BattingStat, Team, Player, Current, model_to_dict, chunked, fn, per_season_weeks
|
||||||
|
from ..dependencies import oauth2_scheme, valid_token
|
||||||
|
|
||||||
|
router = APIRouter(
|
||||||
|
prefix='/api/v3/fieldingstats',
|
||||||
|
tags=['fieldingstats']
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('')
|
||||||
|
async def get_fieldingstats(
|
||||||
|
season: int, s_type: Optional[str] = 'regular', team_abbrev: list = Query(default=None),
|
||||||
|
player_name: list = Query(default=None), player_id: list = Query(default=None),
|
||||||
|
week_start: Optional[int] = None, week_end: Optional[int] = None, game_num: list = Query(default=None),
|
||||||
|
position: list = Query(default=None), limit: Optional[int] = None, sort: Optional[str] = None,
|
||||||
|
short_output: Optional[bool] = True):
|
||||||
|
if 'post' in s_type.lower():
|
||||||
|
all_stats = BattingStat.post_season(season)
|
||||||
|
if all_stats.count() == 0:
|
||||||
|
db.close()
|
||||||
|
return {'count': 0, 'stats': []}
|
||||||
|
elif s_type.lower() in ['combined', 'total', 'all']:
|
||||||
|
all_stats = BattingStat.combined_season(season)
|
||||||
|
if all_stats.count() == 0:
|
||||||
|
db.close()
|
||||||
|
return {'count': 0, 'stats': []}
|
||||||
|
else:
|
||||||
|
all_stats = BattingStat.regular_season(season)
|
||||||
|
if all_stats.count() == 0:
|
||||||
|
db.close()
|
||||||
|
return {'count': 0, 'stats': []}
|
||||||
|
|
||||||
|
all_stats = all_stats.where(
|
||||||
|
(BattingStat.xch > 0) | (BattingStat.pb > 0) | (BattingStat.sbc > 0)
|
||||||
|
)
|
||||||
|
|
||||||
|
if position is not None:
|
||||||
|
all_stats = all_stats.where(BattingStat.pos << [x.upper() for x in position])
|
||||||
|
if team_abbrev is not None:
|
||||||
|
t_query = Team.select().where(Team.abbrev << [x.upper() for x in team_abbrev])
|
||||||
|
all_stats = all_stats.where(BattingStat.team << t_query)
|
||||||
|
if player_name is not None or player_id is not None:
|
||||||
|
if player_id:
|
||||||
|
all_stats = all_stats.where(BattingStat.player_id << player_id)
|
||||||
|
else:
|
||||||
|
p_query = Player.select_season(season).where(fn.Lower(Player.name) << [x.lower() for x in player_name])
|
||||||
|
all_stats = all_stats.where(BattingStat.player << p_query)
|
||||||
|
if game_num:
|
||||||
|
all_stats = all_stats.where(BattingStat.game == game_num)
|
||||||
|
|
||||||
|
start = 1
|
||||||
|
end = Current.get(Current.season == season).week
|
||||||
|
if week_start is not None:
|
||||||
|
start = week_start
|
||||||
|
if week_end is not None:
|
||||||
|
end = min(week_end, end)
|
||||||
|
if start > end:
|
||||||
|
db.close()
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=f'Start week {start} is after end week {end} - cannot pull stats'
|
||||||
|
)
|
||||||
|
all_stats = all_stats.where(
|
||||||
|
(BattingStat.week >= start) & (BattingStat.week <= end)
|
||||||
|
)
|
||||||
|
|
||||||
|
if limit:
|
||||||
|
all_stats = all_stats.limit(limit)
|
||||||
|
if sort:
|
||||||
|
if sort == 'newest':
|
||||||
|
all_stats = all_stats.order_by(-BattingStat.week, -BattingStat.game)
|
||||||
|
|
||||||
|
return_stats = {
|
||||||
|
'count': all_stats.count(),
|
||||||
|
'stats': [{
|
||||||
|
'player': x.player_id if short_output else model_to_dict(x.player, recurse=False),
|
||||||
|
'team': x.team_id if short_output else model_to_dict(x.team, recurse=False),
|
||||||
|
'pos': x.pos,
|
||||||
|
'xch': x.xch,
|
||||||
|
'xhit': x.xhit,
|
||||||
|
'error': x.error,
|
||||||
|
'pb': x.pb,
|
||||||
|
'sbc': x.sbc,
|
||||||
|
'csc': x.csc,
|
||||||
|
'week': x.week,
|
||||||
|
'game': x.game,
|
||||||
|
'season': x.season
|
||||||
|
} for x in all_stats]
|
||||||
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
return return_stats
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/season/{season}')
|
||||||
|
async def get_stasonstats(
|
||||||
|
season: int, s_type: Literal['regular', 'post', 'total'] = 'regular', team_abbrev: list = Query(default=None),
|
||||||
|
team_id: list = Query(default=None), player_name: list = Query(default=None),
|
||||||
|
player_id: list = Query(default=None), short_output: Optional[bool] = True):
|
||||||
|
weeks = per_season_weeks(season, s_type)
|
||||||
|
all_stats = (
|
||||||
|
BattingStat
|
||||||
|
.select(BattingStat.player, BattingStat.pos, fn.SUM(BattingStat.xch).alias('sum_xch'),
|
||||||
|
fn.SUM(BattingStat.xhit).alias('sum_xhit'), fn.SUM(BattingStat.error).alias('sum_error'),
|
||||||
|
fn.SUM(BattingStat.pb).alias('sum_pb'), fn.SUM(BattingStat.sbc).alias('sum_sbc'),
|
||||||
|
fn.SUM(BattingStat.csc).alias('sum_csc'))
|
||||||
|
.where((BattingStat.week >= weeks['start']) & (BattingStat.week <= weeks['end']) &
|
||||||
|
(BattingStat.season == season))
|
||||||
|
.order_by(BattingStat.player)
|
||||||
|
.group_by(BattingStat.player, BattingStat.pos)
|
||||||
|
)
|
||||||
|
|
||||||
|
if team_abbrev is None and team_id is None and player_name is None and player_id is None:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=400,
|
||||||
|
detail=f'Must include team_id/team_abbrev and/or player_name/player_id'
|
||||||
|
)
|
||||||
|
|
||||||
|
if team_id is not None:
|
||||||
|
all_teams = Team.select().where(Team.id << team_id)
|
||||||
|
all_stats = all_stats.where(BattingStat.team << all_teams)
|
||||||
|
elif team_abbrev is not None:
|
||||||
|
all_teams = Team.select().where(fn.Lower(Team.abbrev) << [x.lower() for x in team_abbrev])
|
||||||
|
all_stats = all_stats.where(BattingStat.team << all_teams)
|
||||||
|
|
||||||
|
if player_name is not None:
|
||||||
|
all_players = Player.select().where(fn.Lower(Player.name) << [x.lower() for x in player_name])
|
||||||
|
all_stats = all_stats.where(BattingStat.player << all_players)
|
||||||
|
elif player_id is not None:
|
||||||
|
all_players = Player.select().where(Player.id << player_id)
|
||||||
|
all_stats = all_stats.where(BattingStat.player << all_players)
|
||||||
|
|
||||||
|
return_stats = {
|
||||||
|
'count': sum(1 for i in all_stats if i.sum_xch + i.sum_sbc > 0),
|
||||||
|
'stats': [{
|
||||||
|
'player': x.player_id if short_output else model_to_dict(x.player, recurse=False),
|
||||||
|
'pos': x.pos,
|
||||||
|
'xch': x.sum_xch,
|
||||||
|
'xhit': x.sum_xhit,
|
||||||
|
'error': x.sum_error,
|
||||||
|
'pb': x.sum_pb,
|
||||||
|
'sbc': x.sum_sbc,
|
||||||
|
'csc': x.sum_csc
|
||||||
|
} for x in all_stats if x.sum_xch + x.sum_sbc > 0]
|
||||||
|
}
|
||||||
|
db.close()
|
||||||
|
return return_stats
|
||||||
@ -106,3 +106,116 @@ async def get_pitstats(
|
|||||||
|
|
||||||
db.close()
|
db.close()
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/season/{season}')
|
||||||
|
async def get_seasonstats(
|
||||||
|
season: int, s_type: Literal['regular', 'post', 'total'] = 'regular', team_abbrev: list = Query(default=None),
|
||||||
|
team_id: list = Query(default=None), player_name: list = Query(default=None),
|
||||||
|
player_id: list = Query(default=None), full_player: Optional[bool] = False):
|
||||||
|
weeks = per_season_weeks(season, s_type)
|
||||||
|
all_stats = (
|
||||||
|
PitchingStat
|
||||||
|
.select(PitchingStat.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.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.SUM(PitchingStat.irs).alias('sum_irs'),
|
||||||
|
fn.SUM(PitchingStat.gs).alias('sum_gs'))
|
||||||
|
.where((PitchingStat.week >= weeks['start']) & (PitchingStat.week <= weeks['end']) &
|
||||||
|
(PitchingStat.season == season))
|
||||||
|
.order_by(PitchingStat.player)
|
||||||
|
.group_by(PitchingStat.player)
|
||||||
|
)
|
||||||
|
|
||||||
|
if team_abbrev is None and team_id is None and player_name is None and player_id is None:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=400,
|
||||||
|
detail=f'Must include team_id/team_abbrev and/or player_name/player_id'
|
||||||
|
)
|
||||||
|
|
||||||
|
if team_id is not None:
|
||||||
|
all_teams = Team.select().where(Team.id << team_id)
|
||||||
|
all_stats = all_stats.where(PitchingStat.team << all_teams)
|
||||||
|
elif team_abbrev is not None:
|
||||||
|
all_teams = Team.select().where(fn.Lower(Team.abbrev) << [x.lower() for x in team_abbrev])
|
||||||
|
all_stats = all_stats.where(PitchingStat.team << all_teams)
|
||||||
|
|
||||||
|
if player_name is not None:
|
||||||
|
all_players = Player.select().where(fn.Lower(Player.name) << [x.lower() for x in player_name])
|
||||||
|
all_stats = all_stats.where(PitchingStat.player << all_players)
|
||||||
|
elif player_id is not None:
|
||||||
|
all_players = Player.select().where(Player.id << player_id)
|
||||||
|
all_stats = all_stats.where(PitchingStat.player << all_players)
|
||||||
|
|
||||||
|
return_stats = {
|
||||||
|
'count': all_stats.count(),
|
||||||
|
'stats': [{
|
||||||
|
'player': model_to_dict(x.player, recurse=False) if full_player else x.player_id,
|
||||||
|
'ip': x.sum_ip,
|
||||||
|
'hit': x.sum_hit,
|
||||||
|
'run': x.sum_run,
|
||||||
|
'erun': x.sum_erun,
|
||||||
|
'so': x.sum_so,
|
||||||
|
'bb': x.sum_bb,
|
||||||
|
'hbp': x.sum_hbp,
|
||||||
|
'wp': x.sum_wp,
|
||||||
|
'balk': x.sum_balk,
|
||||||
|
'hr': x.sum_hr,
|
||||||
|
'ir': x.sum_ir,
|
||||||
|
'irs': x.sum_irs,
|
||||||
|
'gs': x.sum_gs,
|
||||||
|
'win': x.sum_win,
|
||||||
|
'loss': x.sum_loss,
|
||||||
|
'hold': x.sum_hold,
|
||||||
|
'sv': x.sum_sv,
|
||||||
|
'bsv': x.sum_bsv
|
||||||
|
} for x in all_stats]
|
||||||
|
}
|
||||||
|
db.close()
|
||||||
|
return return_stats
|
||||||
|
|
||||||
|
|
||||||
|
@router.patch('/{stat_id}')
|
||||||
|
async def patch_pitstats(stat_id: int, new_stats: PitStatModel, token: str = Depends(oauth2_scheme)):
|
||||||
|
if not valid_token(token):
|
||||||
|
logging.warning(f'patch_pitstats - Bad Token: {token}')
|
||||||
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||||
|
|
||||||
|
if PitchingStat.get_or_none(PitchingStat.id == stat_id) is None:
|
||||||
|
raise HTTPException(status_code=404, detail=f'Stat ID {stat_id} not found')
|
||||||
|
|
||||||
|
PitchingStat.update(**new_stats.dict()).where(PitchingStat.id == stat_id).execute()
|
||||||
|
r_stat = model_to_dict(PitchingStat.get_by_id(stat_id))
|
||||||
|
db.close()
|
||||||
|
return r_stat
|
||||||
|
|
||||||
|
|
||||||
|
@router.post('')
|
||||||
|
async def post_pitstats(s_list: PitStatList, token: str = Depends(oauth2_scheme)):
|
||||||
|
if not valid_token(token):
|
||||||
|
logging.warning(f'post_pitstats - Bad Token: {token}')
|
||||||
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||||
|
|
||||||
|
all_stats = []
|
||||||
|
|
||||||
|
for x in s_list.stats:
|
||||||
|
team = Team.get_or_none(Team.id == x.team_id)
|
||||||
|
this_player = Player.get_or_none(Player.id == x.player_id)
|
||||||
|
if team is None:
|
||||||
|
raise HTTPException(status_code=404, detail=f'Team ID {x.team_id} not found')
|
||||||
|
if this_player is None:
|
||||||
|
raise HTTPException(status_code=404, detail=f'Player ID {x.player_id} not found')
|
||||||
|
|
||||||
|
all_stats.append(PitchingStat(**x.dict()))
|
||||||
|
|
||||||
|
with db.atomic():
|
||||||
|
for batch in chunked(all_stats, 15):
|
||||||
|
PitchingStat.insert_many(batch).on_conflict_replace().execute()
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
return f'Added {len(all_stats)} batting lines'
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user