Standings updates

Added /standings/team/{id} endpoint
Fixed pythag calculation
This commit is contained in:
Cal Corum 2023-08-24 14:29:07 -05:00
parent 5110d8b497
commit f6e9e6c7fd
2 changed files with 25 additions and 5 deletions

View File

@ -1,4 +1,6 @@
import copy import copy
import datetime
import logging
import math import math
from typing import Literal from typing import Literal
@ -14,6 +16,13 @@ db = SqliteDatabase(
} }
) )
date = f'{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}'
logging.basicConfig(
filename=f'logs/database/{date}.log',
format='%(asctime)s - database - %(levelname)s - %(message)s',
level=logging.INFO
)
""" """
Per season updates: Per season updates:
@ -529,16 +538,18 @@ class Team(BaseModel):
runs_scored, runs_allowed = 0, 0 runs_scored, runs_allowed = 0, 0
away_games = StratGame.select( away_games = StratGame.select(
fn.SUM(StratGame.away_score).alias('r_us'), fn.SUM(StratGame.home_score).alias('r_them') StratGame.away_team, fn.SUM(StratGame.away_score).alias('r_us'),
).where(StratGame.away_team == self & StratGame.game_num.is_null(False)) fn.SUM(StratGame.home_score).alias('r_them')
).where((StratGame.away_team == self) & StratGame.game_num.is_null(False))
if away_games.count() > 0: if away_games.count() > 0:
runs_scored += away_games[0].r_us runs_scored += away_games[0].r_us
runs_allowed += away_games[0].r_them runs_allowed += away_games[0].r_them
home_games = StratGame.select( home_games = StratGame.select(
fn.SUM(StratGame.home_score).alias('r_us'), fn.SUM(StratGame.away_score).alias('r_them') StratGame.home_team, fn.SUM(StratGame.home_score).alias('r_us'),
).where(StratGame.home_team == self & StratGame.game_num.is_null(False)) fn.SUM(StratGame.away_score).alias('r_them')
if away_games.count() > 0: ).where((StratGame.home_team == self) & StratGame.game_num.is_null(False))
if home_games.count() > 0:
runs_scored += home_games[0].r_them runs_scored += home_games[0].r_them
runs_allowed += home_games[0].r_us runs_allowed += home_games[0].r_us

View File

@ -59,6 +59,15 @@ async def get_standings(
return return_standings return return_standings
@router.get('/team/{team_id}')
async def get_team_standings(team_id: int):
this_stan = Standings.get_or_none(Standings.team_id == team_id)
if this_stan is None:
raise HTTPException(status_code=404, detail=f'No standings found for team id {team_id}')
return model_to_dict(this_stan)
@router.patch('/{stan_id}') @router.patch('/{stan_id}')
async def patch_standings( async def patch_standings(
stan_id, wins: Optional[int] = None, losses: Optional[int] = None, token: str = Depends(oauth2_scheme)): stan_id, wins: Optional[int] = None, losses: Optional[int] = None, token: str = Depends(oauth2_scheme)):