Major database enhancement implementing fast-querying season batting stats: Database Schema: - Created seasonbattingstats table with composite primary key (player_id, season) - All batting stats (counting + calculated): pa, ab, avg, obp, slg, ops, woba, etc. - Proper foreign key constraints and performance indexes - Production-ready SQL creation script included Selective Update System: - update_season_batting_stats() function with PostgreSQL upsert logic - Triggers on game PATCH operations to update affected player stats - Recalculates complete season stats from stratplay data - Efficient updates of only players who participated in modified games API Enhancements: - Enhanced SeasonBattingStats.get_top_hitters() with full filtering support - New /api/v3/views/season-stats/batting/refresh endpoint for season rebuilds - Updated views endpoint to use centralized get_top_hitters() method - Support for team, player, min PA, and pagination filtering Infrastructure: - Production database sync Docker service with SSH automation - Comprehensive error handling and logging throughout - Fixed Peewee model to match actual table structure (no auto-id) - Updated CLAUDE.md with dev server info and sync commands 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
108 lines
3.8 KiB
Python
108 lines
3.8 KiB
Python
from fastapi import APIRouter, Response, HTTPException, Query, Depends
|
|
from typing import List, Literal, Optional
|
|
import logging
|
|
import pydantic
|
|
|
|
from ..db_engine import SeasonBattingStats, db, Manager, Team, Current, model_to_dict, fn, query_to_csv, StratPlay, StratGame
|
|
from ..dependencies import oauth2_scheme, valid_token, PRIVATE_IN_SCHEMA, handle_db_errors, update_season_batting_stats
|
|
|
|
logger = logging.getLogger('discord_app')
|
|
|
|
router = APIRouter(
|
|
prefix='/api/v3/views',
|
|
tags=['views']
|
|
)
|
|
|
|
@router.get('/season-stats/batting')
|
|
@handle_db_errors
|
|
async def get_season_batting_stats(
|
|
season: Optional[int] = None,
|
|
team_id: Optional[int] = None,
|
|
player_id: Optional[int] = None,
|
|
sbaplayer_id: Optional[int] = None,
|
|
min_pa: Optional[int] = None, # Minimum plate appearances
|
|
sort_by: str = "woba", # Default sort field
|
|
sort_order: Literal['asc', 'desc'] = 'desc', # asc or desc
|
|
limit: Optional[int] = 200,
|
|
offset: int = 0,
|
|
csv: Optional[bool] = False
|
|
):
|
|
logger.info(f'Getting season {season} batting stats - team_id: {team_id}, player_id: {player_id}, min_pa: {min_pa}, sort_by: {sort_by}, sort_order: {sort_order}, limit: {limit}, offset: {offset}')
|
|
|
|
# Use the enhanced get_top_hitters method
|
|
query = SeasonBattingStats.get_top_hitters(
|
|
season=season,
|
|
stat=sort_by,
|
|
limit=limit if limit != 0 else None,
|
|
desc=(sort_order.lower() == 'desc'),
|
|
team_id=team_id,
|
|
player_id=player_id,
|
|
sbaplayer_id=sbaplayer_id,
|
|
min_pa=min_pa,
|
|
offset=offset
|
|
)
|
|
|
|
# Build applied filters for response
|
|
applied_filters = {}
|
|
if season is not None:
|
|
applied_filters['season'] = season
|
|
if team_id is not None:
|
|
applied_filters['team_id'] = team_id
|
|
if player_id is not None:
|
|
applied_filters['player_id'] = player_id
|
|
if min_pa is not None:
|
|
applied_filters['min_pa'] = min_pa
|
|
|
|
if csv:
|
|
return_val = query_to_csv(query)
|
|
return Response(content=return_val, media_type='text/csv')
|
|
else:
|
|
stat_list = [model_to_dict(stat) for stat in query]
|
|
return {
|
|
'count': len(stat_list),
|
|
'filters': applied_filters,
|
|
'stats': stat_list
|
|
}
|
|
|
|
|
|
@router.post('/season-stats/batting/refresh', include_in_schema=PRIVATE_IN_SCHEMA)
|
|
@handle_db_errors
|
|
async def refresh_season_batting_stats(
|
|
season: int,
|
|
token: str = Depends(oauth2_scheme)
|
|
) -> dict:
|
|
"""
|
|
Refresh batting stats for all players in a specific season.
|
|
Useful for full season updates.
|
|
"""
|
|
if not valid_token(token):
|
|
logger.warning(f'refresh_season_batting_stats - Bad Token: {token}')
|
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
|
|
|
logger.info(f'Refreshing all batting stats for season {season}')
|
|
|
|
try:
|
|
# Get all player IDs who have stratplay records in this season
|
|
batter_ids = [row.batter_id for row in
|
|
StratPlay.select(StratPlay.batter_id.distinct())
|
|
.join(StratGame).where(StratGame.season == season)]
|
|
|
|
if batter_ids:
|
|
update_season_batting_stats(batter_ids, season, db)
|
|
logger.info(f'Successfully refreshed {len(batter_ids)} players for season {season}')
|
|
|
|
return {
|
|
'message': f'Season {season} batting stats refreshed',
|
|
'players_updated': len(batter_ids)
|
|
}
|
|
else:
|
|
logger.warning(f'No batting data found for season {season}')
|
|
return {
|
|
'message': f'No batting data found for season {season}',
|
|
'players_updated': 0
|
|
}
|
|
|
|
except Exception as e:
|
|
logger.error(f'Error refreshing season {season}: {e}')
|
|
raise HTTPException(status_code=500, detail=f'Refresh failed: {str(e)}')
|