Batstats and pitchstats
This commit is contained in:
parent
782804424d
commit
3bd4150da5
@ -1,6 +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
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
responses={404: {'description': 'Not found'}}
|
responses={404: {'description': 'Not found'}}
|
||||||
@ -15,6 +16,7 @@ app.include_router(teams.router)
|
|||||||
app.include_router(transactions.router)
|
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.get("/api")
|
# @app.get("/api")
|
||||||
|
|||||||
@ -16,38 +16,38 @@ class BatStatModel(pydantic.BaseModel):
|
|||||||
player_id: int
|
player_id: int
|
||||||
team_id: int
|
team_id: int
|
||||||
pos: str
|
pos: str
|
||||||
pa: int
|
pa: Optional[int] = 0
|
||||||
ab: int
|
ab: Optional[int] = 0
|
||||||
run: int
|
run: Optional[int] = 0
|
||||||
hit: int
|
hit: Optional[int] = 0
|
||||||
rbi: int
|
rbi: Optional[int] = 0
|
||||||
double: int
|
double: Optional[int] = 0
|
||||||
triple: int
|
triple: Optional[int] = 0
|
||||||
hr: int
|
hr: Optional[int] = 0
|
||||||
bb: int
|
bb: Optional[int] = 0
|
||||||
so: int
|
so: Optional[int] = 0
|
||||||
hbp: int
|
hbp: Optional[int] = 0
|
||||||
sac: int
|
sac: Optional[int] = 0
|
||||||
ibb: int
|
ibb: Optional[int] = 0
|
||||||
gidp: int
|
gidp: Optional[int] = 0
|
||||||
sb: int
|
sb: Optional[int] = 0
|
||||||
cs: int
|
cs: Optional[int] = 0
|
||||||
bphr: int
|
bphr: Optional[int] = 0
|
||||||
bpfo: int
|
bpfo: Optional[int] = 0
|
||||||
bp1b: int
|
bp1b: Optional[int] = 0
|
||||||
bplo: int
|
bplo: Optional[int] = 0
|
||||||
xba: int
|
xba: Optional[int] = 0
|
||||||
xbt: int
|
xbt: Optional[int] = 0
|
||||||
xch: int
|
xch: Optional[int] = 0
|
||||||
xhit: int
|
xhit: Optional[int] = 0
|
||||||
error: int
|
error: Optional[int] = 0
|
||||||
pb: int
|
pb: Optional[int] = 0
|
||||||
sbc: int
|
sbc: Optional[int] = 0
|
||||||
csc: int
|
csc: Optional[int] = 0
|
||||||
roba: int
|
roba: Optional[int] = 0
|
||||||
robs: int
|
robs: Optional[int] = 0
|
||||||
raa: int
|
raa: Optional[int] = 0
|
||||||
rto: int
|
rto: Optional[int] = 0
|
||||||
week: int
|
week: int
|
||||||
game: int
|
game: int
|
||||||
season: int
|
season: int
|
||||||
@ -60,7 +60,7 @@ class BatStatList(pydantic.BaseModel):
|
|||||||
|
|
||||||
@router.get('')
|
@router.get('')
|
||||||
async def get_batstats(
|
async def get_batstats(
|
||||||
season: int, s_type: Optional[str] = None, team_abbrev: list = Query(default=None),
|
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),
|
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),
|
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,
|
position: list = Query(default=None), limit: Optional[int] = None, sort: Optional[str] = None,
|
||||||
@ -80,6 +80,7 @@ async def get_batstats(
|
|||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
db.close()
|
db.close()
|
||||||
return {'count': 0, 'stats': []}
|
return {'count': 0, 'stats': []}
|
||||||
|
|
||||||
if position is not None:
|
if position is not None:
|
||||||
all_stats = all_stats.where(BattingStat.pos << [x.upper() for x in position])
|
all_stats = all_stats.where(BattingStat.pos << [x.upper() for x in position])
|
||||||
if team_abbrev is not None:
|
if team_abbrev is not None:
|
||||||
@ -89,7 +90,7 @@ async def get_batstats(
|
|||||||
if player_id:
|
if player_id:
|
||||||
all_stats = all_stats.where(BattingStat.player_id << player_id)
|
all_stats = all_stats.where(BattingStat.player_id << player_id)
|
||||||
else:
|
else:
|
||||||
p_query = Player.select_season(season).where(Player.name << player_name)
|
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)
|
all_stats = all_stats.where(BattingStat.player << p_query)
|
||||||
if game_num:
|
if game_num:
|
||||||
all_stats = all_stats.where(BattingStat.game == game_num)
|
all_stats = all_stats.where(BattingStat.game == game_num)
|
||||||
@ -217,10 +218,10 @@ async def get_seasonstats(
|
|||||||
return return_stats
|
return return_stats
|
||||||
|
|
||||||
|
|
||||||
@router.get('/career/{player_name}')
|
# @router.get('/career/{player_name}')
|
||||||
async def get_careerstats(
|
# async def get_careerstats(
|
||||||
s_type: Literal['regular', 'post', 'total'] = 'regular', player_name: list = Query(default=None)):
|
# s_type: Literal['regular', 'post', 'total'] = 'regular', player_name: list = Query(default=None)):
|
||||||
pass # Keep Career Stats table and recalculate after posting stats
|
# pass # Keep Career Stats table and recalculate after posting stats
|
||||||
|
|
||||||
|
|
||||||
@router.patch('/{stat_id}')
|
@router.patch('/{stat_id}')
|
||||||
@ -257,7 +258,8 @@ async def post_batstats(s_list: BatStatList, token: str = Depends(oauth2_scheme)
|
|||||||
all_stats.append(BattingStat(**x.dict()))
|
all_stats.append(BattingStat(**x.dict()))
|
||||||
|
|
||||||
with db.atomic():
|
with db.atomic():
|
||||||
BattingStat.bulk_create(all_stats, batch_size=15)
|
for batch in chunked(all_stats, 15):
|
||||||
|
BattingStat.insert_many(batch).on_conflict_replace().execute()
|
||||||
|
|
||||||
# Update career stats
|
# Update career stats
|
||||||
|
|
||||||
|
|||||||
108
app/routers_v3/pitchingstats.py
Normal file
108
app/routers_v3/pitchingstats.py
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
|
from typing import List, Optional, Literal
|
||||||
|
import logging
|
||||||
|
import pydantic
|
||||||
|
|
||||||
|
from ..db_engine import db, PitchingStat, Team, Player, Current, model_to_dict, chunked, fn, per_season_weeks
|
||||||
|
from ..dependencies import oauth2_scheme, valid_token
|
||||||
|
|
||||||
|
router = APIRouter(
|
||||||
|
prefix='/api/v3/pitchingstats',
|
||||||
|
tags=['pitchingstats']
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class PitStatModel(pydantic.BaseModel):
|
||||||
|
player_id: int
|
||||||
|
team_id: int
|
||||||
|
ip: Optional[float] = 0.0
|
||||||
|
hit: Optional[int] = 0
|
||||||
|
run: Optional[int] = 0
|
||||||
|
erun: Optional[int] = 0
|
||||||
|
so: Optional[int] = 0
|
||||||
|
bb: Optional[int] = 0
|
||||||
|
hbp: Optional[int] = 0
|
||||||
|
wp: Optional[int] = 0
|
||||||
|
balk: Optional[int] = 0
|
||||||
|
hr: Optional[int] = 0
|
||||||
|
gs: Optional[int] = 0
|
||||||
|
win: Optional[int] = 0
|
||||||
|
loss: Optional[int] = 0
|
||||||
|
hold: Optional[int] = 0
|
||||||
|
sv: Optional[int] = 0
|
||||||
|
bsv: Optional[int] = 0
|
||||||
|
ir: Optional[int] = 0
|
||||||
|
irs: Optional[int] = 0
|
||||||
|
week: int
|
||||||
|
game: int
|
||||||
|
season: int
|
||||||
|
|
||||||
|
|
||||||
|
class PitStatList(pydantic.BaseModel):
|
||||||
|
count: int
|
||||||
|
stats: List[PitStatModel]
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('')
|
||||||
|
async def get_pitstats(
|
||||||
|
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),
|
||||||
|
limit: Optional[int] = None, sort: Optional[str] = None, short_output: Optional[bool] = True):
|
||||||
|
if 'post' in s_type.lower():
|
||||||
|
all_stats = PitchingStat.post_season(season)
|
||||||
|
if all_stats.count() == 0:
|
||||||
|
db.close()
|
||||||
|
return {'count': 0, 'stats': []}
|
||||||
|
elif s_type.lower() in ['combined', 'total', 'all']:
|
||||||
|
all_stats = PitchingStat.combined_season(season)
|
||||||
|
if all_stats.count() == 0:
|
||||||
|
db.close()
|
||||||
|
return {'count': 0, 'stats': []}
|
||||||
|
else:
|
||||||
|
all_stats = PitchingStat.regular_season(season)
|
||||||
|
if all_stats.count() == 0:
|
||||||
|
db.close()
|
||||||
|
return {'count': 0, 'stats': []}
|
||||||
|
|
||||||
|
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(PitchingStat.team << t_query)
|
||||||
|
if player_name is not None or player_id is not None:
|
||||||
|
if player_id:
|
||||||
|
all_stats = all_stats.where(PitchingStat.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(PitchingStat.player << p_query)
|
||||||
|
if game_num:
|
||||||
|
all_stats = all_stats.where(PitchingStat.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(
|
||||||
|
(PitchingStat.week >= start) & (PitchingStat.week <= end)
|
||||||
|
)
|
||||||
|
|
||||||
|
if limit:
|
||||||
|
all_stats = all_stats.limit(limit)
|
||||||
|
if sort:
|
||||||
|
if sort == 'newest':
|
||||||
|
all_stats = all_stats.order_by(-PitchingStat.week, -PitchingStat.game)
|
||||||
|
|
||||||
|
return_stats = {
|
||||||
|
'count': all_stats.count(),
|
||||||
|
'stats': [model_to_dict(x, recurse=not short_output) for x in all_stats]
|
||||||
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
return return_stats
|
||||||
Loading…
Reference in New Issue
Block a user