Compare commits
No commits in common. "main" and "2026.3.4" have entirely different histories.
3
.env
3
.env
@ -6,9 +6,6 @@ SBA_DB_USER_PASSWORD=your_production_password
|
|||||||
# SBa API
|
# SBa API
|
||||||
API_TOKEN=Tp3aO3jhYve5NJF1IqOmJTmk
|
API_TOKEN=Tp3aO3jhYve5NJF1IqOmJTmk
|
||||||
|
|
||||||
# Integrations
|
|
||||||
DISCORD_WEBHOOK_URL=
|
|
||||||
|
|
||||||
# Universal
|
# Universal
|
||||||
TZ=America/Chicago
|
TZ=America/Chicago
|
||||||
LOG_LEVEL=INFO
|
LOG_LEVEL=INFO
|
||||||
@ -1,18 +1,20 @@
|
|||||||
# Gitea Actions: Docker Build, Push, and Notify
|
# Gitea Actions: Docker Build, Push, and Notify
|
||||||
#
|
#
|
||||||
# CI/CD pipeline for Major Domo Database API:
|
# CI/CD pipeline for Major Domo Database API:
|
||||||
# - Triggered by pushing a CalVer tag (e.g., 2026.4.5)
|
# - Builds Docker images on every push/PR
|
||||||
# - Builds Docker image and pushes to Docker Hub with version + latest tags
|
# - Auto-generates CalVer version (YYYY.MM.BUILD) on main branch merges
|
||||||
|
# - Pushes to Docker Hub and creates git tag on main
|
||||||
# - Sends Discord notifications on success/failure
|
# - Sends Discord notifications on success/failure
|
||||||
#
|
|
||||||
# To release: git tag -a 2026.4.5 -m "description" && git push origin 2026.4.5
|
|
||||||
|
|
||||||
name: Build Docker Image
|
name: Build Docker Image
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
tags:
|
branches:
|
||||||
- '20*' # matches CalVer tags like 2026.4.5
|
- main
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
@ -22,16 +24,7 @@ jobs:
|
|||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: https://github.com/actions/checkout@v4
|
uses: https://github.com/actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0 # Full history for tag counting
|
||||||
|
|
||||||
- name: Extract version from tag
|
|
||||||
id: version
|
|
||||||
run: |
|
|
||||||
VERSION=${GITHUB_REF#refs/tags/}
|
|
||||||
SHA_SHORT=$(git rev-parse --short HEAD)
|
|
||||||
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
|
||||||
echo "sha_short=$SHA_SHORT" >> $GITHUB_OUTPUT
|
|
||||||
echo "timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> $GITHUB_OUTPUT
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: https://github.com/docker/setup-buildx-action@v3
|
uses: https://github.com/docker/setup-buildx-action@v3
|
||||||
@ -42,47 +35,80 @@ jobs:
|
|||||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||||
|
|
||||||
- name: Build and push Docker image
|
- name: Generate CalVer version
|
||||||
|
id: calver
|
||||||
|
uses: cal/gitea-actions/calver@main
|
||||||
|
|
||||||
|
# Dev build: push with dev + dev-SHA tags (PR/feature branches)
|
||||||
|
- name: Build Docker image (dev)
|
||||||
|
if: github.ref != 'refs/heads/main'
|
||||||
uses: https://github.com/docker/build-push-action@v5
|
uses: https://github.com/docker/build-push-action@v5
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
push: true
|
push: true
|
||||||
tags: |
|
tags: |
|
||||||
manticorum67/major-domo-database:${{ steps.version.outputs.version }}
|
manticorum67/major-domo-database:dev
|
||||||
manticorum67/major-domo-database:latest
|
manticorum67/major-domo-database:dev-${{ steps.calver.outputs.sha_short }}
|
||||||
cache-from: type=registry,ref=manticorum67/major-domo-database:buildcache
|
cache-from: type=registry,ref=manticorum67/major-domo-database:buildcache
|
||||||
cache-to: type=registry,ref=manticorum67/major-domo-database:buildcache,mode=max
|
cache-to: type=registry,ref=manticorum67/major-domo-database:buildcache,mode=max
|
||||||
|
|
||||||
|
# Production build: push with latest + CalVer tags (main only)
|
||||||
|
- name: Build Docker image (production)
|
||||||
|
if: github.ref == 'refs/heads/main'
|
||||||
|
uses: https://github.com/docker/build-push-action@v5
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
push: true
|
||||||
|
tags: |
|
||||||
|
manticorum67/major-domo-database:latest
|
||||||
|
manticorum67/major-domo-database:${{ steps.calver.outputs.version }}
|
||||||
|
manticorum67/major-domo-database:${{ steps.calver.outputs.version_sha }}
|
||||||
|
cache-from: type=registry,ref=manticorum67/major-domo-database:buildcache
|
||||||
|
cache-to: type=registry,ref=manticorum67/major-domo-database:buildcache,mode=max
|
||||||
|
|
||||||
|
- name: Tag release
|
||||||
|
if: success() && github.ref == 'refs/heads/main'
|
||||||
|
uses: cal/gitea-actions/gitea-tag@main
|
||||||
|
with:
|
||||||
|
version: ${{ steps.calver.outputs.version }}
|
||||||
|
token: ${{ github.token }}
|
||||||
|
|
||||||
- name: Build Summary
|
- name: Build Summary
|
||||||
run: |
|
run: |
|
||||||
echo "## Docker Build Successful" >> $GITHUB_STEP_SUMMARY
|
echo "## Docker Build Successful" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "" >> $GITHUB_STEP_SUMMARY
|
echo "" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "**Version:** \`${{ steps.version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
|
|
||||||
echo "" >> $GITHUB_STEP_SUMMARY
|
|
||||||
echo "**Image Tags:**" >> $GITHUB_STEP_SUMMARY
|
echo "**Image Tags:**" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "- \`manticorum67/major-domo-database:${{ steps.version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
|
|
||||||
echo "- \`manticorum67/major-domo-database:latest\`" >> $GITHUB_STEP_SUMMARY
|
echo "- \`manticorum67/major-domo-database:latest\`" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "- \`manticorum67/major-domo-database:${{ steps.calver.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "- \`manticorum67/major-domo-database:${{ steps.calver.outputs.version_sha }}\`" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "" >> $GITHUB_STEP_SUMMARY
|
echo "" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "**Build Details:**" >> $GITHUB_STEP_SUMMARY
|
echo "**Build Details:**" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "- Commit: \`${{ steps.version.outputs.sha_short }}\`" >> $GITHUB_STEP_SUMMARY
|
echo "- Branch: \`${{ steps.calver.outputs.branch }}\`" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "- Timestamp: \`${{ steps.version.outputs.timestamp }}\`" >> $GITHUB_STEP_SUMMARY
|
echo "- Commit: \`${{ github.sha }}\`" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "- Timestamp: \`${{ steps.calver.outputs.timestamp }}\`" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "" >> $GITHUB_STEP_SUMMARY
|
echo "" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "Pull with: \`docker pull manticorum67/major-domo-database:${{ steps.version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
|
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
|
||||||
|
echo "Pushed to Docker Hub!" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "Pull with: \`docker pull manticorum67/major-domo-database:latest\`" >> $GITHUB_STEP_SUMMARY
|
||||||
|
else
|
||||||
|
echo "_PR build - image not pushed to Docker Hub_" >> $GITHUB_STEP_SUMMARY
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Discord Notification - Success
|
- name: Discord Notification - Success
|
||||||
if: success()
|
if: success() && github.ref == 'refs/heads/main'
|
||||||
uses: cal/gitea-actions/discord-notify@main
|
uses: cal/gitea-actions/discord-notify@main
|
||||||
with:
|
with:
|
||||||
webhook_url: ${{ secrets.DISCORD_WEBHOOK }}
|
webhook_url: ${{ secrets.DISCORD_WEBHOOK }}
|
||||||
title: "Major Domo Database"
|
title: "Major Domo Database"
|
||||||
status: success
|
status: success
|
||||||
version: ${{ steps.version.outputs.version }}
|
version: ${{ steps.calver.outputs.version }}
|
||||||
image_tag: ${{ steps.version.outputs.version }}
|
image_tag: ${{ steps.calver.outputs.version_sha }}
|
||||||
commit_sha: ${{ steps.version.outputs.sha_short }}
|
commit_sha: ${{ steps.calver.outputs.sha_short }}
|
||||||
timestamp: ${{ steps.version.outputs.timestamp }}
|
timestamp: ${{ steps.calver.outputs.timestamp }}
|
||||||
|
|
||||||
- name: Discord Notification - Failure
|
- name: Discord Notification - Failure
|
||||||
if: failure()
|
if: failure() && github.ref == 'refs/heads/main'
|
||||||
uses: cal/gitea-actions/discord-notify@main
|
uses: cal/gitea-actions/discord-notify@main
|
||||||
with:
|
with:
|
||||||
webhook_url: ${{ secrets.DISCORD_WEBHOOK }}
|
webhook_url: ${{ secrets.DISCORD_WEBHOOK }}
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -55,6 +55,7 @@ Include/
|
|||||||
pyvenv.cfg
|
pyvenv.cfg
|
||||||
db_engine.py
|
db_engine.py
|
||||||
main.py
|
main.py
|
||||||
|
migrations.py
|
||||||
db_engine.py
|
db_engine.py
|
||||||
sba_master.db
|
sba_master.db
|
||||||
db_engine.py
|
db_engine.py
|
||||||
|
|||||||
@ -40,7 +40,7 @@ python migrations.py # Run migrations (SQL files in migrat
|
|||||||
- **Bot container**: `dev_sba_postgres` (PostgreSQL) + `dev_sba_db_api` (API) — check with `docker ps`
|
- **Bot container**: `dev_sba_postgres` (PostgreSQL) + `dev_sba_db_api` (API) — check with `docker ps`
|
||||||
- **Image**: `manticorum67/major-domo-database:dev` (Docker Hub)
|
- **Image**: `manticorum67/major-domo-database:dev` (Docker Hub)
|
||||||
|
|
||||||
- **CI/CD**: Gitea Actions — tag-triggered Docker builds. Push a CalVer tag to release: `git tag -a 2026.4.5 -m "description" && git push origin 2026.4.5`
|
- **CI/CD**: Gitea Actions on PR to `main` — builds Docker image, auto-generates CalVer version (`YYYY.MM.BUILD`) on merge
|
||||||
|
|
||||||
## Important
|
## Important
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
# Use specific version for reproducible builds
|
# Use specific version for reproducible builds
|
||||||
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.12
|
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.11
|
||||||
|
|
||||||
# Set Python optimizations
|
# Set Python optimizations
|
||||||
ENV PYTHONUNBUFFERED=1
|
ENV PYTHONUNBUFFERED=1
|
||||||
|
|||||||
1350
app/db_engine.py
1350
app/db_engine.py
File diff suppressed because it is too large
Load Diff
@ -11,8 +11,8 @@ from fastapi import HTTPException, Response
|
|||||||
from fastapi.security import OAuth2PasswordBearer
|
from fastapi.security import OAuth2PasswordBearer
|
||||||
from redis import Redis
|
from redis import Redis
|
||||||
|
|
||||||
date = f"{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}"
|
date = f'{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}'
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger('discord_app')
|
||||||
|
|
||||||
# date = f'{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}'
|
# date = f'{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}'
|
||||||
# log_level = logger.info if os.environ.get('LOG_LEVEL') == 'INFO' else 'WARN'
|
# log_level = logger.info if os.environ.get('LOG_LEVEL') == 'INFO' else 'WARN'
|
||||||
@ -22,14 +22,11 @@ logger = logging.getLogger("discord_app")
|
|||||||
# level=log_level
|
# level=log_level
|
||||||
# )
|
# )
|
||||||
|
|
||||||
# Discord integration
|
|
||||||
DISCORD_WEBHOOK_URL = os.environ.get("DISCORD_WEBHOOK_URL")
|
|
||||||
|
|
||||||
# Redis configuration
|
# Redis configuration
|
||||||
REDIS_HOST = os.environ.get("REDIS_HOST", "localhost")
|
REDIS_HOST = os.environ.get('REDIS_HOST', 'localhost')
|
||||||
REDIS_PORT = int(os.environ.get("REDIS_PORT", "6379"))
|
REDIS_PORT = int(os.environ.get('REDIS_PORT', '6379'))
|
||||||
REDIS_DB = int(os.environ.get("REDIS_DB", "0"))
|
REDIS_DB = int(os.environ.get('REDIS_DB', '0'))
|
||||||
CACHE_ENABLED = os.environ.get("CACHE_ENABLED", "true").lower() == "true"
|
CACHE_ENABLED = os.environ.get('CACHE_ENABLED', 'true').lower() == 'true'
|
||||||
|
|
||||||
# Initialize Redis client with connection error handling
|
# Initialize Redis client with connection error handling
|
||||||
if not CACHE_ENABLED:
|
if not CACHE_ENABLED:
|
||||||
@ -43,7 +40,7 @@ else:
|
|||||||
db=REDIS_DB,
|
db=REDIS_DB,
|
||||||
decode_responses=True,
|
decode_responses=True,
|
||||||
socket_connect_timeout=5,
|
socket_connect_timeout=5,
|
||||||
socket_timeout=5,
|
socket_timeout=5
|
||||||
)
|
)
|
||||||
# Test connection
|
# Test connection
|
||||||
redis_client.ping()
|
redis_client.ping()
|
||||||
@ -53,19 +50,12 @@ else:
|
|||||||
redis_client = None
|
redis_client = None
|
||||||
|
|
||||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
||||||
priv_help = (
|
priv_help = False if not os.environ.get('PRIVATE_IN_SCHEMA') else os.environ.get('PRIVATE_IN_SCHEMA').upper()
|
||||||
False
|
PRIVATE_IN_SCHEMA = True if priv_help == 'TRUE' else False
|
||||||
if not os.environ.get("PRIVATE_IN_SCHEMA")
|
|
||||||
else os.environ.get("PRIVATE_IN_SCHEMA").upper()
|
|
||||||
)
|
|
||||||
PRIVATE_IN_SCHEMA = True if priv_help == "TRUE" else False
|
|
||||||
|
|
||||||
MAX_LIMIT = 500
|
|
||||||
DEFAULT_LIMIT = 200
|
|
||||||
|
|
||||||
|
|
||||||
def valid_token(token):
|
def valid_token(token):
|
||||||
return token == os.environ.get("API_TOKEN")
|
return token == os.environ.get('API_TOKEN')
|
||||||
|
|
||||||
|
|
||||||
def update_season_batting_stats(player_ids, season, db_connection):
|
def update_season_batting_stats(player_ids, season, db_connection):
|
||||||
@ -82,9 +72,7 @@ def update_season_batting_stats(player_ids, season, db_connection):
|
|||||||
if isinstance(player_ids, int):
|
if isinstance(player_ids, int):
|
||||||
player_ids = [player_ids]
|
player_ids = [player_ids]
|
||||||
|
|
||||||
logger.info(
|
logger.info(f"Updating season batting stats for {len(player_ids)} players in season {season}")
|
||||||
f"Updating season batting stats for {len(player_ids)} players in season {season}"
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# SQL query to recalculate and upsert batting stats
|
# SQL query to recalculate and upsert batting stats
|
||||||
@ -233,9 +221,7 @@ def update_season_batting_stats(player_ids, season, db_connection):
|
|||||||
# Execute the query with parameters using the passed database connection
|
# Execute the query with parameters using the passed database connection
|
||||||
db_connection.execute_sql(query, [season, player_ids, season, player_ids])
|
db_connection.execute_sql(query, [season, player_ids, season, player_ids])
|
||||||
|
|
||||||
logger.info(
|
logger.info(f"Successfully updated season batting stats for {len(player_ids)} players in season {season}")
|
||||||
f"Successfully updated season batting stats for {len(player_ids)} players in season {season}"
|
|
||||||
)
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error updating season batting stats: {e}")
|
logger.error(f"Error updating season batting stats: {e}")
|
||||||
@ -256,9 +242,7 @@ def update_season_pitching_stats(player_ids, season, db_connection):
|
|||||||
if isinstance(player_ids, int):
|
if isinstance(player_ids, int):
|
||||||
player_ids = [player_ids]
|
player_ids = [player_ids]
|
||||||
|
|
||||||
logger.info(
|
logger.info(f"Updating season pitching stats for {len(player_ids)} players in season {season}")
|
||||||
f"Updating season pitching stats for {len(player_ids)} players in season {season}"
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# SQL query to recalculate and upsert pitching stats
|
# SQL query to recalculate and upsert pitching stats
|
||||||
@ -373,27 +357,7 @@ def update_season_pitching_stats(player_ids, season, db_connection):
|
|||||||
WHEN SUM(sp.bb) > 0
|
WHEN SUM(sp.bb) > 0
|
||||||
THEN ROUND(SUM(sp.so)::DECIMAL / SUM(sp.bb), 2)
|
THEN ROUND(SUM(sp.so)::DECIMAL / SUM(sp.bb), 2)
|
||||||
ELSE 0.0
|
ELSE 0.0
|
||||||
END AS kperbb,
|
END AS kperbb
|
||||||
|
|
||||||
-- Runners left on base when pitcher recorded the 3rd out
|
|
||||||
SUM(CASE WHEN sp.on_first_final IS NOT NULL AND sp.on_first_final != 4 AND sp.starting_outs + sp.outs = 3 THEN 1 ELSE 0 END) +
|
|
||||||
SUM(CASE WHEN sp.on_second_final IS NOT NULL AND sp.on_second_final != 4 AND sp.starting_outs + sp.outs = 3 THEN 1 ELSE 0 END) +
|
|
||||||
SUM(CASE WHEN sp.on_third_final IS NOT NULL AND sp.on_third_final != 4 AND sp.starting_outs + sp.outs = 3 THEN 1 ELSE 0 END) AS lob_2outs,
|
|
||||||
|
|
||||||
-- RBI allowed (excluding HR) per runner opportunity
|
|
||||||
CASE
|
|
||||||
WHEN (SUM(CASE WHEN sp.on_first_id IS NOT NULL THEN 1 ELSE 0 END) +
|
|
||||||
SUM(CASE WHEN sp.on_second_id IS NOT NULL THEN 1 ELSE 0 END) +
|
|
||||||
SUM(CASE WHEN sp.on_third_id IS NOT NULL THEN 1 ELSE 0 END)) > 0
|
|
||||||
THEN ROUND(
|
|
||||||
(SUM(sp.rbi) - SUM(sp.homerun))::DECIMAL /
|
|
||||||
(SUM(CASE WHEN sp.on_first_id IS NOT NULL THEN 1 ELSE 0 END) +
|
|
||||||
SUM(CASE WHEN sp.on_second_id IS NOT NULL THEN 1 ELSE 0 END) +
|
|
||||||
SUM(CASE WHEN sp.on_third_id IS NOT NULL THEN 1 ELSE 0 END)),
|
|
||||||
3
|
|
||||||
)
|
|
||||||
ELSE 0.000
|
|
||||||
END AS rbipercent
|
|
||||||
|
|
||||||
FROM stratplay sp
|
FROM stratplay sp
|
||||||
JOIN stratgame sg ON sg.id = sp.game_id
|
JOIN stratgame sg ON sg.id = sp.game_id
|
||||||
@ -438,7 +402,7 @@ def update_season_pitching_stats(player_ids, season, db_connection):
|
|||||||
ps.bphr, ps.bpfo, ps.bp1b, ps.bplo, ps.wp, ps.balk,
|
ps.bphr, ps.bpfo, ps.bp1b, ps.bplo, ps.wp, ps.balk,
|
||||||
ps.wpa * -1, ps.era, ps.whip, ps.avg, ps.obp, ps.slg, ps.ops, ps.woba,
|
ps.wpa * -1, ps.era, ps.whip, ps.avg, ps.obp, ps.slg, ps.ops, ps.woba,
|
||||||
ps.hper9, ps.kper9, ps.bbper9, ps.kperbb,
|
ps.hper9, ps.kper9, ps.bbper9, ps.kperbb,
|
||||||
ps.lob_2outs, ps.rbipercent, COALESCE(ps.re24 * -1, 0.0)
|
0.0, 0.0, COALESCE(ps.re24 * -1, 0.0)
|
||||||
FROM pitching_stats ps
|
FROM pitching_stats ps
|
||||||
LEFT JOIN decision_stats ds ON ps.player_id = ds.player_id AND ps.season = ds.season
|
LEFT JOIN decision_stats ds ON ps.player_id = ds.player_id AND ps.season = ds.season
|
||||||
ON CONFLICT (player_id, season)
|
ON CONFLICT (player_id, season)
|
||||||
@ -500,9 +464,7 @@ def update_season_pitching_stats(player_ids, season, db_connection):
|
|||||||
# Execute the query with parameters using the passed database connection
|
# Execute the query with parameters using the passed database connection
|
||||||
db_connection.execute_sql(query, [season, player_ids, season, player_ids])
|
db_connection.execute_sql(query, [season, player_ids, season, player_ids])
|
||||||
|
|
||||||
logger.info(
|
logger.info(f"Successfully updated season pitching stats for {len(player_ids)} players in season {season}")
|
||||||
f"Successfully updated season pitching stats for {len(player_ids)} players in season {season}"
|
|
||||||
)
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error updating season pitching stats: {e}")
|
logger.error(f"Error updating season pitching stats: {e}")
|
||||||
@ -519,15 +481,12 @@ def send_webhook_message(message: str) -> bool:
|
|||||||
Returns:
|
Returns:
|
||||||
bool: True if successful, False otherwise
|
bool: True if successful, False otherwise
|
||||||
"""
|
"""
|
||||||
webhook_url = DISCORD_WEBHOOK_URL
|
webhook_url = "https://discord.com/api/webhooks/1408811717424840876/7RXG_D5IqovA3Jwa9YOobUjVcVMuLc6cQyezABcWuXaHo5Fvz1en10M7J43o3OJ3bzGW"
|
||||||
if not webhook_url:
|
|
||||||
logger.warning(
|
|
||||||
"DISCORD_WEBHOOK_URL env var is not set — skipping webhook message"
|
|
||||||
)
|
|
||||||
return False
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
payload = {"content": message}
|
payload = {
|
||||||
|
"content": message
|
||||||
|
}
|
||||||
|
|
||||||
response = requests.post(webhook_url, json=payload, timeout=10)
|
response = requests.post(webhook_url, json=payload, timeout=10)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
@ -543,9 +502,7 @@ def send_webhook_message(message: str) -> bool:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def cache_result(
|
def cache_result(ttl: int = 300, key_prefix: str = "api", normalize_params: bool = True):
|
||||||
ttl: int = 300, key_prefix: str = "api", normalize_params: bool = True
|
|
||||||
):
|
|
||||||
"""
|
"""
|
||||||
Decorator to cache function results in Redis with parameter normalization.
|
Decorator to cache function results in Redis with parameter normalization.
|
||||||
|
|
||||||
@ -563,7 +520,6 @@ def cache_result(
|
|||||||
# These will use the same cache entry when normalize_params=True:
|
# These will use the same cache entry when normalize_params=True:
|
||||||
# get_player_stats(123, None) and get_player_stats(123)
|
# get_player_stats(123, None) and get_player_stats(123)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
async def wrapper(*args, **kwargs):
|
async def wrapper(*args, **kwargs):
|
||||||
@ -577,16 +533,15 @@ def cache_result(
|
|||||||
if normalize_params:
|
if normalize_params:
|
||||||
# Remove None values and empty collections
|
# Remove None values and empty collections
|
||||||
normalized_kwargs = {
|
normalized_kwargs = {
|
||||||
k: v
|
k: v for k, v in kwargs.items()
|
||||||
for k, v in kwargs.items()
|
|
||||||
if v is not None and v != [] and v != "" and v != {}
|
if v is not None and v != [] and v != "" and v != {}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Generate more readable cache key
|
# Generate more readable cache key
|
||||||
args_str = "_".join(str(arg) for arg in args if arg is not None)
|
args_str = "_".join(str(arg) for arg in args if arg is not None)
|
||||||
kwargs_str = "_".join(
|
kwargs_str = "_".join([
|
||||||
[f"{k}={v}" for k, v in sorted(normalized_kwargs.items())]
|
f"{k}={v}" for k, v in sorted(normalized_kwargs.items())
|
||||||
)
|
])
|
||||||
|
|
||||||
# Combine args and kwargs for cache key
|
# Combine args and kwargs for cache key
|
||||||
key_parts = [key_prefix, func.__name__]
|
key_parts = [key_prefix, func.__name__]
|
||||||
@ -617,12 +572,10 @@ def cache_result(
|
|||||||
redis_client.setex(
|
redis_client.setex(
|
||||||
cache_key,
|
cache_key,
|
||||||
ttl,
|
ttl,
|
||||||
json.dumps(result, default=str, ensure_ascii=False),
|
json.dumps(result, default=str, ensure_ascii=False)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
logger.debug(
|
logger.debug(f"Skipping cache for Response object from {func.__name__}")
|
||||||
f"Skipping cache for Response object from {func.__name__}"
|
|
||||||
)
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -632,7 +585,6 @@ def cache_result(
|
|||||||
return await func(*args, **kwargs)
|
return await func(*args, **kwargs)
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
@ -655,9 +607,7 @@ def invalidate_cache(pattern: str = "*"):
|
|||||||
keys = redis_client.keys(pattern)
|
keys = redis_client.keys(pattern)
|
||||||
if keys:
|
if keys:
|
||||||
deleted = redis_client.delete(*keys)
|
deleted = redis_client.delete(*keys)
|
||||||
logger.info(
|
logger.info(f"Invalidated {deleted} cache entries matching pattern: {pattern}")
|
||||||
f"Invalidated {deleted} cache entries matching pattern: {pattern}"
|
|
||||||
)
|
|
||||||
return deleted
|
return deleted
|
||||||
else:
|
else:
|
||||||
logger.debug(f"No cache entries found matching pattern: {pattern}")
|
logger.debug(f"No cache entries found matching pattern: {pattern}")
|
||||||
@ -684,7 +634,7 @@ def get_cache_stats() -> dict:
|
|||||||
"memory_used": info.get("used_memory_human", "unknown"),
|
"memory_used": info.get("used_memory_human", "unknown"),
|
||||||
"total_keys": redis_client.dbsize(),
|
"total_keys": redis_client.dbsize(),
|
||||||
"connected_clients": info.get("connected_clients", 0),
|
"connected_clients": info.get("connected_clients", 0),
|
||||||
"uptime_seconds": info.get("uptime_in_seconds", 0),
|
"uptime_seconds": info.get("uptime_in_seconds", 0)
|
||||||
}
|
}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting cache stats: {e}")
|
logger.error(f"Error getting cache stats: {e}")
|
||||||
@ -695,7 +645,7 @@ def add_cache_headers(
|
|||||||
max_age: int = 300,
|
max_age: int = 300,
|
||||||
cache_type: str = "public",
|
cache_type: str = "public",
|
||||||
vary: Optional[str] = None,
|
vary: Optional[str] = None,
|
||||||
etag: bool = False,
|
etag: bool = False
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Decorator to add HTTP cache headers to FastAPI responses.
|
Decorator to add HTTP cache headers to FastAPI responses.
|
||||||
@ -715,7 +665,6 @@ def add_cache_headers(
|
|||||||
async def get_user_data():
|
async def get_user_data():
|
||||||
return {"data": "user specific"}
|
return {"data": "user specific"}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
async def wrapper(*args, **kwargs):
|
async def wrapper(*args, **kwargs):
|
||||||
@ -728,7 +677,7 @@ def add_cache_headers(
|
|||||||
# Convert to Response with JSON content
|
# Convert to Response with JSON content
|
||||||
response = Response(
|
response = Response(
|
||||||
content=json.dumps(result, default=str, ensure_ascii=False),
|
content=json.dumps(result, default=str, ensure_ascii=False),
|
||||||
media_type="application/json",
|
media_type="application/json"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# Handle other response types
|
# Handle other response types
|
||||||
@ -746,23 +695,20 @@ def add_cache_headers(
|
|||||||
response.headers["Vary"] = vary
|
response.headers["Vary"] = vary
|
||||||
|
|
||||||
# Add ETag if requested
|
# Add ETag if requested
|
||||||
if etag and (
|
if etag and (hasattr(result, '__dict__') or isinstance(result, (dict, list))):
|
||||||
hasattr(result, "__dict__") or isinstance(result, (dict, list))
|
|
||||||
):
|
|
||||||
content_hash = hashlib.md5(
|
content_hash = hashlib.md5(
|
||||||
json.dumps(result, default=str, sort_keys=True).encode()
|
json.dumps(result, default=str, sort_keys=True).encode()
|
||||||
).hexdigest()
|
).hexdigest()
|
||||||
response.headers["ETag"] = f'"{content_hash}"'
|
response.headers["ETag"] = f'"{content_hash}"'
|
||||||
|
|
||||||
# Add Last-Modified header with current time for dynamic content
|
# Add Last-Modified header with current time for dynamic content
|
||||||
response.headers["Last-Modified"] = datetime.datetime.now(
|
response.headers["Last-Modified"] = datetime.datetime.now(datetime.timezone.utc).strftime(
|
||||||
datetime.timezone.utc
|
"%a, %d %b %Y %H:%M:%S GMT"
|
||||||
).strftime("%a, %d %b %Y %H:%M:%S GMT")
|
)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
@ -772,7 +718,6 @@ def handle_db_errors(func):
|
|||||||
Ensures proper cleanup of database connections and provides consistent error handling.
|
Ensures proper cleanup of database connections and provides consistent error handling.
|
||||||
Includes comprehensive logging with function context, timing, and stack traces.
|
Includes comprehensive logging with function context, timing, and stack traces.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
async def wrapper(*args, **kwargs):
|
async def wrapper(*args, **kwargs):
|
||||||
import time
|
import time
|
||||||
@ -789,24 +734,18 @@ def handle_db_errors(func):
|
|||||||
try:
|
try:
|
||||||
# Log sanitized arguments (avoid logging tokens, passwords, etc.)
|
# Log sanitized arguments (avoid logging tokens, passwords, etc.)
|
||||||
for arg in args:
|
for arg in args:
|
||||||
if hasattr(arg, "__dict__") and hasattr(
|
if hasattr(arg, '__dict__') and hasattr(arg, 'url'): # FastAPI Request object
|
||||||
arg, "url"
|
safe_args.append(f"Request({getattr(arg, 'method', 'UNKNOWN')} {getattr(arg, 'url', 'unknown')})")
|
||||||
): # FastAPI Request object
|
|
||||||
safe_args.append(
|
|
||||||
f"Request({getattr(arg, 'method', 'UNKNOWN')} {getattr(arg, 'url', 'unknown')})"
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
safe_args.append(str(arg)[:100]) # Truncate long values
|
safe_args.append(str(arg)[:100]) # Truncate long values
|
||||||
|
|
||||||
for key, value in kwargs.items():
|
for key, value in kwargs.items():
|
||||||
if key.lower() in ["token", "password", "secret", "key"]:
|
if key.lower() in ['token', 'password', 'secret', 'key']:
|
||||||
safe_kwargs[key] = "[REDACTED]"
|
safe_kwargs[key] = '[REDACTED]'
|
||||||
else:
|
else:
|
||||||
safe_kwargs[key] = str(value)[:100] # Truncate long values
|
safe_kwargs[key] = str(value)[:100] # Truncate long values
|
||||||
|
|
||||||
logger.info(
|
logger.info(f"Starting {func_name} - args: {safe_args}, kwargs: {safe_kwargs}")
|
||||||
f"Starting {func_name} - args: {safe_args}, kwargs: {safe_kwargs}"
|
|
||||||
)
|
|
||||||
|
|
||||||
result = await func(*args, **kwargs)
|
result = await func(*args, **kwargs)
|
||||||
|
|
||||||
@ -815,10 +754,6 @@ def handle_db_errors(func):
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
except HTTPException:
|
|
||||||
# Let intentional HTTP errors (401, 404, etc.) pass through unchanged
|
|
||||||
raise
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
elapsed_time = time.time() - start_time
|
elapsed_time = time.time() - start_time
|
||||||
error_trace = traceback.format_exc()
|
error_trace = traceback.format_exc()
|
||||||
@ -840,12 +775,8 @@ def handle_db_errors(func):
|
|||||||
db.close()
|
db.close()
|
||||||
logger.info(f"Database connection closed for {func_name}")
|
logger.info(f"Database connection closed for {func_name}")
|
||||||
except Exception as close_error:
|
except Exception as close_error:
|
||||||
logger.error(
|
logger.error(f"Error closing database connection in {func_name}: {close_error}")
|
||||||
f"Error closing database connection in {func_name}: {close_error}"
|
|
||||||
)
|
|
||||||
|
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=500, detail=f'Database error in {func_name}: {str(e)}')
|
||||||
status_code=500, detail=f"Database error in {func_name}: {str(e)}"
|
|
||||||
)
|
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|||||||
94
app/main.py
94
app/main.py
@ -2,112 +2,46 @@ import datetime
|
|||||||
import logging
|
import logging
|
||||||
from logging.handlers import RotatingFileHandler
|
from logging.handlers import RotatingFileHandler
|
||||||
import os
|
import os
|
||||||
from urllib.parse import parse_qsl, urlencode
|
|
||||||
|
|
||||||
from fastapi import Depends, FastAPI, Request
|
from fastapi import Depends, FastAPI, Request
|
||||||
from fastapi.openapi.docs import get_swagger_ui_html
|
from fastapi.openapi.docs import get_swagger_ui_html
|
||||||
from fastapi.openapi.utils import get_openapi
|
from fastapi.openapi.utils import get_openapi
|
||||||
|
|
||||||
from .db_engine import db
|
|
||||||
|
|
||||||
# from fastapi.openapi.docs import get_swagger_ui_html
|
# from fastapi.openapi.docs import get_swagger_ui_html
|
||||||
# from fastapi.openapi.utils import get_openapi
|
# from fastapi.openapi.utils import get_openapi
|
||||||
|
|
||||||
from .routers_v3 import (
|
from .routers_v3 import current, players, results, schedules, standings, teams, transactions, battingstats, pitchingstats, fieldingstats, draftpicks, draftlist, managers, awards, draftdata, keepers, stratgame, stratplay, injuries, decisions, divisions, sbaplayers, custom_commands, help_commands, views
|
||||||
current,
|
|
||||||
players,
|
|
||||||
results,
|
|
||||||
schedules,
|
|
||||||
standings,
|
|
||||||
teams,
|
|
||||||
transactions,
|
|
||||||
battingstats,
|
|
||||||
pitchingstats,
|
|
||||||
fieldingstats,
|
|
||||||
draftpicks,
|
|
||||||
draftlist,
|
|
||||||
managers,
|
|
||||||
awards,
|
|
||||||
draftdata,
|
|
||||||
keepers,
|
|
||||||
stratgame,
|
|
||||||
stratplay,
|
|
||||||
injuries,
|
|
||||||
decisions,
|
|
||||||
divisions,
|
|
||||||
sbaplayers,
|
|
||||||
custom_commands,
|
|
||||||
help_commands,
|
|
||||||
views,
|
|
||||||
)
|
|
||||||
|
|
||||||
# date = f'{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}'
|
# date = f'{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}'
|
||||||
log_level = logging.INFO if os.environ.get("LOG_LEVEL") == "INFO" else logging.WARNING
|
log_level = logging.INFO if os.environ.get('LOG_LEVEL') == 'INFO' else logging.WARNING
|
||||||
# logging.basicConfig(
|
# logging.basicConfig(
|
||||||
# filename=f'logs/database/{date}.log',
|
# filename=f'logs/database/{date}.log',
|
||||||
# format='%(asctime)s - sba-database - %(levelname)s - %(message)s',
|
# format='%(asctime)s - sba-database - %(levelname)s - %(message)s',
|
||||||
# level=log_level
|
# level=log_level
|
||||||
# )
|
# )
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger('discord_app')
|
||||||
logger.setLevel(log_level)
|
logger.setLevel(log_level)
|
||||||
|
|
||||||
handler = RotatingFileHandler(
|
handler = RotatingFileHandler(
|
||||||
filename="./logs/sba-database.log",
|
filename='./logs/sba-database.log',
|
||||||
# encoding='utf-8',
|
# encoding='utf-8',
|
||||||
maxBytes=8 * 1024 * 1024, # 8 MiB
|
maxBytes=8 * 1024 * 1024, # 8 MiB
|
||||||
backupCount=5, # Rotate through 5 files
|
backupCount=5, # Rotate through 5 files
|
||||||
)
|
)
|
||||||
|
|
||||||
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||||
handler.setFormatter(formatter)
|
handler.setFormatter(formatter)
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
# root_path='/api',
|
# root_path='/api',
|
||||||
responses={404: {"description": "Not found"}},
|
responses={404: {'description': 'Not found'}},
|
||||||
docs_url="/api/docs",
|
docs_url='/api/docs',
|
||||||
redoc_url="/api/redoc",
|
redoc_url='/api/redoc'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.middleware("http")
|
logger.info(f'Starting up now...')
|
||||||
async def db_connection_middleware(request: Request, call_next):
|
|
||||||
db.connect(reuse_if_open=True)
|
|
||||||
try:
|
|
||||||
response = await call_next(request)
|
|
||||||
finally:
|
|
||||||
if not db.is_closed():
|
|
||||||
db.close()
|
|
||||||
return response
|
|
||||||
|
|
||||||
|
|
||||||
logger.info(f"Starting up now...")
|
|
||||||
|
|
||||||
|
|
||||||
@app.middleware("http")
|
|
||||||
async def db_connection_middleware(request: Request, call_next):
|
|
||||||
from .db_engine import db
|
|
||||||
|
|
||||||
db.connect(reuse_if_open=True)
|
|
||||||
try:
|
|
||||||
response = await call_next(request)
|
|
||||||
return response
|
|
||||||
finally:
|
|
||||||
if not db.is_closed():
|
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@app.middleware("http")
|
|
||||||
async def strip_empty_query_params(request: Request, call_next):
|
|
||||||
qs = request.scope.get("query_string", b"")
|
|
||||||
if qs:
|
|
||||||
pairs = parse_qsl(qs.decode(), keep_blank_values=True)
|
|
||||||
filtered = [(k, v) for k, v in pairs if v != ""]
|
|
||||||
new_qs = urlencode(filtered).encode()
|
|
||||||
request.scope["query_string"] = new_qs
|
|
||||||
if hasattr(request, "_query_params"):
|
|
||||||
del request._query_params
|
|
||||||
return await call_next(request)
|
|
||||||
|
|
||||||
|
|
||||||
app.include_router(current.router)
|
app.include_router(current.router)
|
||||||
@ -136,20 +70,18 @@ app.include_router(custom_commands.router)
|
|||||||
app.include_router(help_commands.router)
|
app.include_router(help_commands.router)
|
||||||
app.include_router(views.router)
|
app.include_router(views.router)
|
||||||
|
|
||||||
logger.info(f"Loaded all routers.")
|
logger.info(f'Loaded all routers.')
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/docs", include_in_schema=False)
|
@app.get("/api/docs", include_in_schema=False)
|
||||||
async def get_docs(req: Request):
|
async def get_docs(req: Request):
|
||||||
logger.debug(req.scope)
|
print(req.scope)
|
||||||
return get_swagger_ui_html(
|
return get_swagger_ui_html(openapi_url=req.scope.get('root_path')+'/openapi.json', title='Swagger')
|
||||||
openapi_url=req.scope.get("root_path") + "/openapi.json", title="Swagger"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/openapi.json", include_in_schema=False)
|
@app.get("/api/openapi.json", include_in_schema=False)
|
||||||
async def openapi():
|
async def openapi():
|
||||||
return get_openapi(title="SBa API Docs", version=f"0.1.1", routes=app.routes)
|
return get_openapi(title='SBa API Docs', version=f'0.1.1', routes=app.routes)
|
||||||
|
|
||||||
|
|
||||||
# @app.get("/api")
|
# @app.get("/api")
|
||||||
|
|||||||
@ -9,8 +9,6 @@ from ..dependencies import (
|
|||||||
valid_token,
|
valid_token,
|
||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -45,8 +43,6 @@ async def get_awards(
|
|||||||
team_id: list = Query(default=None),
|
team_id: list = Query(default=None),
|
||||||
short_output: Optional[bool] = False,
|
short_output: Optional[bool] = False,
|
||||||
player_name: list = Query(default=None),
|
player_name: list = Query(default=None),
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
):
|
):
|
||||||
all_awards = Award.select()
|
all_awards = Award.select()
|
||||||
|
|
||||||
@ -71,13 +67,11 @@ async def get_awards(
|
|||||||
all_players = Player.select().where(fn.Lower(Player.name) << pname_list)
|
all_players = Player.select().where(fn.Lower(Player.name) << pname_list)
|
||||||
all_awards = all_awards.where(Award.player << all_players)
|
all_awards = all_awards.where(Award.player << all_players)
|
||||||
|
|
||||||
total_count = all_awards.count()
|
|
||||||
all_awards = all_awards.offset(offset).limit(limit)
|
|
||||||
|
|
||||||
return_awards = {
|
return_awards = {
|
||||||
"count": total_count,
|
"count": all_awards.count(),
|
||||||
"awards": [model_to_dict(x, recurse=not short_output) for x in all_awards],
|
"awards": [model_to_dict(x, recurse=not short_output) for x in all_awards],
|
||||||
}
|
}
|
||||||
|
db.close()
|
||||||
return return_awards
|
return return_awards
|
||||||
|
|
||||||
|
|
||||||
@ -86,8 +80,10 @@ async def get_awards(
|
|||||||
async def get_one_award(award_id: int, short_output: Optional[bool] = False):
|
async def get_one_award(award_id: int, short_output: Optional[bool] = False):
|
||||||
this_award = Award.get_or_none(Award.id == award_id)
|
this_award = Award.get_or_none(Award.id == award_id)
|
||||||
if this_award is None:
|
if this_award is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"Award ID {award_id} not found")
|
raise HTTPException(status_code=404, detail=f"Award ID {award_id} not found")
|
||||||
|
|
||||||
|
db.close()
|
||||||
return model_to_dict(this_award, recurse=not short_output)
|
return model_to_dict(this_award, recurse=not short_output)
|
||||||
|
|
||||||
|
|
||||||
@ -106,11 +102,12 @@ async def patch_award(
|
|||||||
token: str = Depends(oauth2_scheme),
|
token: str = Depends(oauth2_scheme),
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_player - Bad Token")
|
logger.warning(f"patch_player - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_award = Award.get_or_none(Award.id == award_id)
|
this_award = Award.get_or_none(Award.id == award_id)
|
||||||
if this_award is None:
|
if this_award is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"Award ID {award_id} not found")
|
raise HTTPException(status_code=404, detail=f"Award ID {award_id} not found")
|
||||||
|
|
||||||
if name is not None:
|
if name is not None:
|
||||||
@ -132,8 +129,10 @@ async def patch_award(
|
|||||||
|
|
||||||
if this_award.save() == 1:
|
if this_award.save() == 1:
|
||||||
r_award = model_to_dict(this_award)
|
r_award = model_to_dict(this_award)
|
||||||
|
db.close()
|
||||||
return r_award
|
return r_award
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=500, detail=f"Unable to patch award {award_id}")
|
raise HTTPException(status_code=500, detail=f"Unable to patch award {award_id}")
|
||||||
|
|
||||||
|
|
||||||
@ -141,7 +140,7 @@ async def patch_award(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_award(award_list: AwardList, token: str = Depends(oauth2_scheme)):
|
async def post_award(award_list: AwardList, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_player - Bad Token")
|
logger.warning(f"patch_player - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
new_awards = []
|
new_awards = []
|
||||||
@ -172,11 +171,12 @@ async def post_award(award_list: AwardList, token: str = Depends(oauth2_scheme))
|
|||||||
status_code=404, detail=f"Team ID {x.team_id} not found"
|
status_code=404, detail=f"Team ID {x.team_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
new_awards.append(x.model_dump())
|
new_awards.append(x.dict())
|
||||||
|
|
||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_awards, 15):
|
for batch in chunked(new_awards, 15):
|
||||||
Award.insert_many(batch).on_conflict_ignore().execute()
|
Award.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
db.close()
|
||||||
|
|
||||||
return f"Inserted {len(new_awards)} awards"
|
return f"Inserted {len(new_awards)} awards"
|
||||||
|
|
||||||
@ -185,14 +185,16 @@ async def post_award(award_list: AwardList, token: str = Depends(oauth2_scheme))
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_award(award_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_award(award_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_player - Bad Token")
|
logger.warning(f"patch_player - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_award = Award.get_or_none(Award.id == award_id)
|
this_award = Award.get_or_none(Award.id == award_id)
|
||||||
if this_award is None:
|
if this_award is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"Award ID {award_id} not found")
|
raise HTTPException(status_code=404, detail=f"Award ID {award_id} not found")
|
||||||
|
|
||||||
count = this_award.delete_instance()
|
count = this_award.delete_instance()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Award {award_id} has been deleted"
|
return f"Award {award_id} has been deleted"
|
||||||
|
|||||||
@ -19,8 +19,6 @@ from ..dependencies import (
|
|||||||
valid_token,
|
valid_token,
|
||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -86,21 +84,24 @@ async def get_batstats(
|
|||||||
week_end: Optional[int] = None,
|
week_end: Optional[int] = None,
|
||||||
game_num: list = Query(default=None),
|
game_num: list = Query(default=None),
|
||||||
position: list = Query(default=None),
|
position: list = Query(default=None),
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
limit: Optional[int] = None,
|
||||||
sort: Optional[str] = None,
|
sort: Optional[str] = None,
|
||||||
short_output: Optional[bool] = True,
|
short_output: Optional[bool] = True,
|
||||||
):
|
):
|
||||||
if "post" in s_type.lower():
|
if "post" in s_type.lower():
|
||||||
all_stats = BattingStat.post_season(season)
|
all_stats = BattingStat.post_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
|
db.close()
|
||||||
return {"count": 0, "stats": []}
|
return {"count": 0, "stats": []}
|
||||||
elif s_type.lower() in ["combined", "total", "all"]:
|
elif s_type.lower() in ["combined", "total", "all"]:
|
||||||
all_stats = BattingStat.combined_season(season)
|
all_stats = BattingStat.combined_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
|
db.close()
|
||||||
return {"count": 0, "stats": []}
|
return {"count": 0, "stats": []}
|
||||||
else:
|
else:
|
||||||
all_stats = BattingStat.regular_season(season)
|
all_stats = BattingStat.regular_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
|
db.close()
|
||||||
return {"count": 0, "stats": []}
|
return {"count": 0, "stats": []}
|
||||||
|
|
||||||
if position is not None:
|
if position is not None:
|
||||||
@ -126,12 +127,14 @@ async def get_batstats(
|
|||||||
if week_end is not None:
|
if week_end is not None:
|
||||||
end = min(week_end, end)
|
end = min(week_end, end)
|
||||||
if start > end:
|
if start > end:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404,
|
status_code=404,
|
||||||
detail=f"Start week {start} is after end week {end} - cannot pull stats",
|
detail=f"Start week {start} is after end week {end} - cannot pull stats",
|
||||||
)
|
)
|
||||||
all_stats = all_stats.where((BattingStat.week >= start) & (BattingStat.week <= end))
|
all_stats = all_stats.where((BattingStat.week >= start) & (BattingStat.week <= end))
|
||||||
|
|
||||||
|
if limit:
|
||||||
all_stats = all_stats.limit(limit)
|
all_stats = all_stats.limit(limit)
|
||||||
if sort:
|
if sort:
|
||||||
if sort == "newest":
|
if sort == "newest":
|
||||||
@ -143,6 +146,7 @@ async def get_batstats(
|
|||||||
# 'stats': [{'id': x.id} for x in all_stats]
|
# 'stats': [{'id': x.id} for x in all_stats]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|
||||||
|
|
||||||
@ -164,8 +168,6 @@ async def get_totalstats(
|
|||||||
short_output: Optional[bool] = False,
|
short_output: Optional[bool] = False,
|
||||||
min_pa: Optional[int] = 1,
|
min_pa: Optional[int] = 1,
|
||||||
week: list = Query(default=None),
|
week: list = Query(default=None),
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
):
|
):
|
||||||
if sum(1 for x in [s_type, (week_start or week_end), week] if x is not None) > 1:
|
if sum(1 for x in [s_type, (week_start or week_end), week] if x is not None) > 1:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
@ -299,10 +301,7 @@ async def get_totalstats(
|
|||||||
all_players = Player.select().where(Player.id << player_id)
|
all_players = Player.select().where(Player.id << player_id)
|
||||||
all_stats = all_stats.where(BattingStat.player << all_players)
|
all_stats = all_stats.where(BattingStat.player << all_players)
|
||||||
|
|
||||||
total_count = all_stats.count()
|
return_stats = {"count": all_stats.count(), "stats": []}
|
||||||
all_stats = all_stats.offset(offset).limit(limit)
|
|
||||||
|
|
||||||
return_stats = {"count": total_count, "stats": []}
|
|
||||||
|
|
||||||
for x in all_stats:
|
for x in all_stats:
|
||||||
# Handle player field based on grouping with safe access
|
# Handle player field based on grouping with safe access
|
||||||
@ -345,6 +344,7 @@ async def get_totalstats(
|
|||||||
"bplo": x.sum_bplo,
|
"bplo": x.sum_bplo,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
db.close()
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|
||||||
|
|
||||||
@ -360,16 +360,15 @@ async def patch_batstats(
|
|||||||
stat_id: int, new_stats: BatStatModel, token: str = Depends(oauth2_scheme)
|
stat_id: int, new_stats: BatStatModel, token: str = Depends(oauth2_scheme)
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_batstats - Bad Token")
|
logger.warning(f"patch_batstats - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
if BattingStat.get_or_none(BattingStat.id == stat_id) is None:
|
if BattingStat.get_or_none(BattingStat.id == stat_id) is None:
|
||||||
raise HTTPException(status_code=404, detail=f"Stat ID {stat_id} not found")
|
raise HTTPException(status_code=404, detail=f"Stat ID {stat_id} not found")
|
||||||
|
|
||||||
BattingStat.update(**new_stats.model_dump()).where(
|
BattingStat.update(**new_stats.dict()).where(BattingStat.id == stat_id).execute()
|
||||||
BattingStat.id == stat_id
|
|
||||||
).execute()
|
|
||||||
r_stat = model_to_dict(BattingStat.get_by_id(stat_id))
|
r_stat = model_to_dict(BattingStat.get_by_id(stat_id))
|
||||||
|
db.close()
|
||||||
return r_stat
|
return r_stat
|
||||||
|
|
||||||
|
|
||||||
@ -377,35 +376,24 @@ async def patch_batstats(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_batstats(s_list: BatStatList, token: str = Depends(oauth2_scheme)):
|
async def post_batstats(s_list: BatStatList, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_batstats - Bad Token")
|
logger.warning(f"post_batstats - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
all_stats = []
|
all_stats = []
|
||||||
|
|
||||||
all_team_ids = list(set(x.team_id for x in s_list.stats))
|
|
||||||
all_player_ids = list(set(x.player_id for x in s_list.stats))
|
|
||||||
found_team_ids = (
|
|
||||||
set(t.id for t in Team.select(Team.id).where(Team.id << all_team_ids))
|
|
||||||
if all_team_ids
|
|
||||||
else set()
|
|
||||||
)
|
|
||||||
found_player_ids = (
|
|
||||||
set(p.id for p in Player.select(Player.id).where(Player.id << all_player_ids))
|
|
||||||
if all_player_ids
|
|
||||||
else set()
|
|
||||||
)
|
|
||||||
|
|
||||||
for x in s_list.stats:
|
for x in s_list.stats:
|
||||||
if x.team_id not in found_team_ids:
|
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(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Team ID {x.team_id} not found"
|
status_code=404, detail=f"Team ID {x.team_id} not found"
|
||||||
)
|
)
|
||||||
if x.player_id not in found_player_ids:
|
if this_player is None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Player ID {x.player_id} not found"
|
status_code=404, detail=f"Player ID {x.player_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
all_stats.append(BattingStat(**x.model_dump()))
|
all_stats.append(BattingStat(**x.dict()))
|
||||||
|
|
||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(all_stats, 15):
|
for batch in chunked(all_stats, 15):
|
||||||
@ -413,4 +401,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"
|
||||||
|
|||||||
@ -41,6 +41,7 @@ async def get_current(season: Optional[int] = None):
|
|||||||
|
|
||||||
if current is not None:
|
if current is not None:
|
||||||
r_curr = model_to_dict(current)
|
r_curr = model_to_dict(current)
|
||||||
|
db.close()
|
||||||
return r_curr
|
return r_curr
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
@ -64,7 +65,7 @@ async def patch_current(
|
|||||||
token: str = Depends(oauth2_scheme),
|
token: str = Depends(oauth2_scheme),
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_current - Bad Token")
|
logger.warning(f"patch_current - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -99,8 +100,10 @@ async def patch_current(
|
|||||||
|
|
||||||
if current.save():
|
if current.save():
|
||||||
r_curr = model_to_dict(current)
|
r_curr = model_to_dict(current)
|
||||||
|
db.close()
|
||||||
return r_curr
|
return r_curr
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch current {current_id}"
|
status_code=500, detail=f"Unable to patch current {current_id}"
|
||||||
)
|
)
|
||||||
@ -110,15 +113,17 @@ async def patch_current(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_current(new_current: CurrentModel, token: str = Depends(oauth2_scheme)):
|
async def post_current(new_current: CurrentModel, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_current - Bad Token")
|
logger.warning(f"patch_current - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_current = Current(**new_current.model_dump())
|
this_current = Current(**new_current.dict())
|
||||||
|
|
||||||
if this_current.save():
|
if this_current.save():
|
||||||
r_curr = model_to_dict(this_current)
|
r_curr = model_to_dict(this_current)
|
||||||
|
db.close()
|
||||||
return r_curr
|
return r_curr
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500,
|
status_code=500,
|
||||||
detail=f"Unable to post season {new_current.season} current",
|
detail=f"Unable to post season {new_current.season} current",
|
||||||
@ -129,7 +134,7 @@ async def post_current(new_current: CurrentModel, token: str = Depends(oauth2_sc
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_current(current_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_current(current_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_current - Bad Token")
|
logger.warning(f"patch_current - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
if Current.delete_by_id(current_id) == 1:
|
if Current.delete_by_id(current_id) == 1:
|
||||||
|
|||||||
@ -175,7 +175,7 @@ def delete_custom_command(command_id: int):
|
|||||||
def get_creator_by_discord_id(discord_id: int):
|
def get_creator_by_discord_id(discord_id: int):
|
||||||
"""Get a creator by Discord ID"""
|
"""Get a creator by Discord ID"""
|
||||||
creator = CustomCommandCreator.get_or_none(
|
creator = CustomCommandCreator.get_or_none(
|
||||||
CustomCommandCreator.discord_id == discord_id
|
CustomCommandCreator.discord_id == str(discord_id)
|
||||||
)
|
)
|
||||||
if creator:
|
if creator:
|
||||||
return model_to_dict(creator)
|
return model_to_dict(creator)
|
||||||
@ -296,8 +296,9 @@ async def get_custom_commands(
|
|||||||
if command_dict.get("tags"):
|
if command_dict.get("tags"):
|
||||||
try:
|
try:
|
||||||
command_dict["tags"] = json.loads(command_dict["tags"])
|
command_dict["tags"] = json.loads(command_dict["tags"])
|
||||||
except Exception:
|
except:
|
||||||
command_dict["tags"] = []
|
command_dict["tags"] = []
|
||||||
|
|
||||||
# Get full creator information
|
# Get full creator information
|
||||||
creator_id = command_dict["creator_id"]
|
creator_id = command_dict["creator_id"]
|
||||||
creator_cursor = db.execute_sql(
|
creator_cursor = db.execute_sql(
|
||||||
@ -363,6 +364,8 @@ async def get_custom_commands(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting custom commands: {e}")
|
logger.error(f"Error getting custom commands: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
# Move this route to after the specific string routes
|
# Move this route to after the specific string routes
|
||||||
@ -375,7 +378,7 @@ async def create_custom_command_endpoint(
|
|||||||
):
|
):
|
||||||
"""Create a new custom command"""
|
"""Create a new custom command"""
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("create_custom_command - Bad Token")
|
logger.warning(f"create_custom_command - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -403,7 +406,7 @@ async def create_custom_command_endpoint(
|
|||||||
if command_dict.get("tags"):
|
if command_dict.get("tags"):
|
||||||
try:
|
try:
|
||||||
command_dict["tags"] = json.loads(command_dict["tags"])
|
command_dict["tags"] = json.loads(command_dict["tags"])
|
||||||
except Exception:
|
except:
|
||||||
command_dict["tags"] = []
|
command_dict["tags"] = []
|
||||||
|
|
||||||
creator_created_at = command_dict.pop("creator_created_at")
|
creator_created_at = command_dict.pop("creator_created_at")
|
||||||
@ -427,6 +430,8 @@ async def create_custom_command_endpoint(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error creating custom command: {e}")
|
logger.error(f"Error creating custom command: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.put("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -436,7 +441,7 @@ async def update_custom_command_endpoint(
|
|||||||
):
|
):
|
||||||
"""Update an existing custom command"""
|
"""Update an existing custom command"""
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("update_custom_command - Bad Token")
|
logger.warning(f"update_custom_command - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -462,7 +467,7 @@ async def update_custom_command_endpoint(
|
|||||||
if command_dict.get("tags"):
|
if command_dict.get("tags"):
|
||||||
try:
|
try:
|
||||||
command_dict["tags"] = json.loads(command_dict["tags"])
|
command_dict["tags"] = json.loads(command_dict["tags"])
|
||||||
except Exception:
|
except:
|
||||||
command_dict["tags"] = []
|
command_dict["tags"] = []
|
||||||
|
|
||||||
creator_created_at = command_dict.pop("creator_created_at")
|
creator_created_at = command_dict.pop("creator_created_at")
|
||||||
@ -486,6 +491,8 @@ async def update_custom_command_endpoint(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error updating custom command {command_id}: {e}")
|
logger.error(f"Error updating custom command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.patch("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.patch("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -502,7 +509,7 @@ async def patch_custom_command(
|
|||||||
):
|
):
|
||||||
"""Partially update a custom command"""
|
"""Partially update a custom command"""
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_custom_command - Bad Token")
|
logger.warning(f"patch_custom_command - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -545,7 +552,7 @@ async def patch_custom_command(
|
|||||||
if command_dict.get("tags"):
|
if command_dict.get("tags"):
|
||||||
try:
|
try:
|
||||||
command_dict["tags"] = json.loads(command_dict["tags"])
|
command_dict["tags"] = json.loads(command_dict["tags"])
|
||||||
except Exception:
|
except:
|
||||||
command_dict["tags"] = []
|
command_dict["tags"] = []
|
||||||
|
|
||||||
creator_created_at = command_dict.pop("creator_created_at")
|
creator_created_at = command_dict.pop("creator_created_at")
|
||||||
@ -569,6 +576,8 @@ async def patch_custom_command(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error patching custom command {command_id}: {e}")
|
logger.error(f"Error patching custom command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.delete("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -578,7 +587,7 @@ async def delete_custom_command_endpoint(
|
|||||||
):
|
):
|
||||||
"""Delete a custom command"""
|
"""Delete a custom command"""
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_custom_command - Bad Token")
|
logger.warning(f"delete_custom_command - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -604,6 +613,8 @@ async def delete_custom_command_endpoint(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error deleting custom command {command_id}: {e}")
|
logger.error(f"Error deleting custom command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
# Creator endpoints
|
# Creator endpoints
|
||||||
@ -673,6 +684,8 @@ async def get_creators(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting creators: {e}")
|
logger.error(f"Error getting creators: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.post("/creators", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.post("/creators", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -682,7 +695,7 @@ async def create_creator_endpoint(
|
|||||||
):
|
):
|
||||||
"""Create a new command creator"""
|
"""Create a new command creator"""
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("create_creator - Bad Token")
|
logger.warning(f"create_creator - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -716,6 +729,8 @@ async def create_creator_endpoint(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error creating creator: {e}")
|
logger.error(f"Error creating creator: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/stats")
|
@router.get("/stats")
|
||||||
@ -766,7 +781,7 @@ async def get_custom_command_stats():
|
|||||||
if command_dict.get("tags"):
|
if command_dict.get("tags"):
|
||||||
try:
|
try:
|
||||||
command_dict["tags"] = json.loads(command_dict["tags"])
|
command_dict["tags"] = json.loads(command_dict["tags"])
|
||||||
except Exception:
|
except:
|
||||||
command_dict["tags"] = []
|
command_dict["tags"] = []
|
||||||
command_dict["creator"] = {
|
command_dict["creator"] = {
|
||||||
"discord_id": command_dict.pop("creator_discord_id"),
|
"discord_id": command_dict.pop("creator_discord_id"),
|
||||||
@ -840,6 +855,8 @@ async def get_custom_command_stats():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting custom command stats: {e}")
|
logger.error(f"Error getting custom command stats: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
# Special endpoints for Discord bot integration
|
# Special endpoints for Discord bot integration
|
||||||
@ -864,7 +881,7 @@ async def get_custom_command_by_name_endpoint(command_name: str):
|
|||||||
if command_dict.get("tags"):
|
if command_dict.get("tags"):
|
||||||
try:
|
try:
|
||||||
command_dict["tags"] = json.loads(command_dict["tags"])
|
command_dict["tags"] = json.loads(command_dict["tags"])
|
||||||
except Exception:
|
except:
|
||||||
command_dict["tags"] = []
|
command_dict["tags"] = []
|
||||||
|
|
||||||
# Add creator info - get full creator record
|
# Add creator info - get full creator record
|
||||||
@ -905,6 +922,8 @@ async def get_custom_command_by_name_endpoint(command_name: str):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting custom command by name '{command_name}': {e}")
|
logger.error(f"Error getting custom command by name '{command_name}': {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.patch("/by_name/{command_name}/execute", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.patch("/by_name/{command_name}/execute", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -914,7 +933,7 @@ async def execute_custom_command(
|
|||||||
):
|
):
|
||||||
"""Execute a custom command and update usage statistics"""
|
"""Execute a custom command and update usage statistics"""
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("execute_custom_command - Bad Token")
|
logger.warning(f"execute_custom_command - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -947,7 +966,7 @@ async def execute_custom_command(
|
|||||||
if updated_dict.get("tags"):
|
if updated_dict.get("tags"):
|
||||||
try:
|
try:
|
||||||
updated_dict["tags"] = json.loads(updated_dict["tags"])
|
updated_dict["tags"] = json.loads(updated_dict["tags"])
|
||||||
except Exception:
|
except:
|
||||||
updated_dict["tags"] = []
|
updated_dict["tags"] = []
|
||||||
|
|
||||||
# Build creator object from the fields returned by get_custom_command_by_id
|
# Build creator object from the fields returned by get_custom_command_by_id
|
||||||
@ -972,6 +991,8 @@ async def execute_custom_command(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error executing custom command '{command_name}': {e}")
|
logger.error(f"Error executing custom command '{command_name}': {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/autocomplete")
|
@router.get("/autocomplete")
|
||||||
@ -1007,6 +1028,8 @@ async def get_command_names_for_autocomplete(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting command names for autocomplete: {e}")
|
logger.error(f"Error getting command names for autocomplete: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{command_id}")
|
@router.get("/{command_id}")
|
||||||
@ -1030,7 +1053,7 @@ async def get_custom_command(command_id: int):
|
|||||||
if command_dict.get("tags"):
|
if command_dict.get("tags"):
|
||||||
try:
|
try:
|
||||||
command_dict["tags"] = json.loads(command_dict["tags"])
|
command_dict["tags"] = json.loads(command_dict["tags"])
|
||||||
except Exception:
|
except:
|
||||||
command_dict["tags"] = []
|
command_dict["tags"] = []
|
||||||
|
|
||||||
creator_created_at = command_dict.pop("creator_created_at")
|
creator_created_at = command_dict.pop("creator_created_at")
|
||||||
@ -1055,3 +1078,5 @@ async def get_custom_command(command_id: int):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting custom command {command_id}: {e}")
|
logger.error(f"Error getting custom command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|||||||
@ -19,8 +19,6 @@ from ..dependencies import (
|
|||||||
valid_token,
|
valid_token,
|
||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -75,7 +73,7 @@ async def get_decisions(
|
|||||||
irunners_scored: list = Query(default=None),
|
irunners_scored: list = Query(default=None),
|
||||||
game_id: list = Query(default=None),
|
game_id: list = Query(default=None),
|
||||||
player_id: list = Query(default=None),
|
player_id: list = Query(default=None),
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
limit: Optional[int] = None,
|
||||||
short_output: Optional[bool] = False,
|
short_output: Optional[bool] = False,
|
||||||
):
|
):
|
||||||
all_dec = Decision.select().order_by(
|
all_dec = Decision.select().order_by(
|
||||||
@ -137,12 +135,16 @@ async def get_decisions(
|
|||||||
if irunners_scored is not None:
|
if irunners_scored is not None:
|
||||||
all_dec = all_dec.where(Decision.irunners_scored << irunners_scored)
|
all_dec = all_dec.where(Decision.irunners_scored << irunners_scored)
|
||||||
|
|
||||||
|
if limit is not None:
|
||||||
|
if limit < 1:
|
||||||
|
limit = 1
|
||||||
all_dec = all_dec.limit(limit)
|
all_dec = all_dec.limit(limit)
|
||||||
|
|
||||||
return_dec = {
|
return_dec = {
|
||||||
"count": all_dec.count(),
|
"count": all_dec.count(),
|
||||||
"decisions": [model_to_dict(x, recurse=not short_output) for x in all_dec],
|
"decisions": [model_to_dict(x, recurse=not short_output) for x in all_dec],
|
||||||
}
|
}
|
||||||
|
db.close()
|
||||||
return return_dec
|
return return_dec
|
||||||
|
|
||||||
|
|
||||||
@ -162,11 +164,12 @@ async def patch_decision(
|
|||||||
token: str = Depends(oauth2_scheme),
|
token: str = Depends(oauth2_scheme),
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_decision - Bad Token")
|
logger.warning(f"patch_decision - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_dec = Decision.get_or_none(Decision.id == decision_id)
|
this_dec = Decision.get_or_none(Decision.id == decision_id)
|
||||||
if this_dec is None:
|
if this_dec is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Decision ID {decision_id} not found"
|
status_code=404, detail=f"Decision ID {decision_id} not found"
|
||||||
)
|
)
|
||||||
@ -192,8 +195,10 @@ async def patch_decision(
|
|||||||
|
|
||||||
if this_dec.save() == 1:
|
if this_dec.save() == 1:
|
||||||
d_result = model_to_dict(this_dec)
|
d_result = model_to_dict(this_dec)
|
||||||
|
db.close()
|
||||||
return d_result
|
return d_result
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch decision {decision_id}"
|
status_code=500, detail=f"Unable to patch decision {decision_id}"
|
||||||
)
|
)
|
||||||
@ -203,7 +208,7 @@ async def patch_decision(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_decisions(dec_list: DecisionList, token: str = Depends(oauth2_scheme)):
|
async def post_decisions(dec_list: DecisionList, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_decisions - Bad Token")
|
logger.warning(f"post_decisions - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
new_dec = []
|
new_dec = []
|
||||||
@ -217,11 +222,12 @@ async def post_decisions(dec_list: DecisionList, token: str = Depends(oauth2_sch
|
|||||||
status_code=404, detail=f"Player ID {x.pitcher_id} not found"
|
status_code=404, detail=f"Player ID {x.pitcher_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
new_dec.append(x.model_dump())
|
new_dec.append(x.dict())
|
||||||
|
|
||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_dec, 10):
|
for batch in chunked(new_dec, 10):
|
||||||
Decision.insert_many(batch).on_conflict_ignore().execute()
|
Decision.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
db.close()
|
||||||
|
|
||||||
return f"Inserted {len(new_dec)} decisions"
|
return f"Inserted {len(new_dec)} decisions"
|
||||||
|
|
||||||
@ -230,16 +236,18 @@ async def post_decisions(dec_list: DecisionList, token: str = Depends(oauth2_sch
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_decision(decision_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_decision(decision_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_decision - Bad Token")
|
logger.warning(f"delete_decision - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_dec = Decision.get_or_none(Decision.id == decision_id)
|
this_dec = Decision.get_or_none(Decision.id == decision_id)
|
||||||
if this_dec is None:
|
if this_dec is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Decision ID {decision_id} not found"
|
status_code=404, detail=f"Decision ID {decision_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
count = this_dec.delete_instance()
|
count = this_dec.delete_instance()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Decision {decision_id} has been deleted"
|
return f"Decision {decision_id} has been deleted"
|
||||||
@ -253,14 +261,16 @@ async def delete_decision(decision_id: int, token: str = Depends(oauth2_scheme))
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_decisions_game(game_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_decisions_game(game_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_decisions_game - Bad Token")
|
logger.warning(f"delete_decisions_game - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
||||||
if not this_game:
|
if not this_game:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"Game ID {game_id} not found")
|
raise HTTPException(status_code=404, detail=f"Game ID {game_id} not found")
|
||||||
|
|
||||||
count = Decision.delete().where(Decision.game == this_game).execute()
|
count = Decision.delete().where(Decision.game == this_game).execute()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if count > 0:
|
if count > 0:
|
||||||
return f"Deleted {count} decisions matching Game ID {game_id}"
|
return f"Deleted {count} decisions matching Game ID {game_id}"
|
||||||
|
|||||||
@ -9,8 +9,6 @@ from ..dependencies import (
|
|||||||
valid_token,
|
valid_token,
|
||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -34,8 +32,6 @@ async def get_divisions(
|
|||||||
div_abbrev: Optional[str] = None,
|
div_abbrev: Optional[str] = None,
|
||||||
lg_name: Optional[str] = None,
|
lg_name: Optional[str] = None,
|
||||||
lg_abbrev: Optional[str] = None,
|
lg_abbrev: Optional[str] = None,
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
):
|
):
|
||||||
all_divisions = Division.select().where(Division.season == season)
|
all_divisions = Division.select().where(Division.season == season)
|
||||||
|
|
||||||
@ -48,13 +44,11 @@ async def get_divisions(
|
|||||||
if lg_abbrev is not None:
|
if lg_abbrev is not None:
|
||||||
all_divisions = all_divisions.where(Division.league_abbrev == lg_abbrev)
|
all_divisions = all_divisions.where(Division.league_abbrev == lg_abbrev)
|
||||||
|
|
||||||
total_count = all_divisions.count()
|
|
||||||
all_divisions = all_divisions.offset(offset).limit(limit)
|
|
||||||
|
|
||||||
return_div = {
|
return_div = {
|
||||||
"count": total_count,
|
"count": all_divisions.count(),
|
||||||
"divisions": [model_to_dict(x) for x in all_divisions],
|
"divisions": [model_to_dict(x) for x in all_divisions],
|
||||||
}
|
}
|
||||||
|
db.close()
|
||||||
return return_div
|
return return_div
|
||||||
|
|
||||||
|
|
||||||
@ -63,11 +57,13 @@ async def get_divisions(
|
|||||||
async def get_one_division(division_id: int):
|
async def get_one_division(division_id: int):
|
||||||
this_div = Division.get_or_none(Division.id == division_id)
|
this_div = Division.get_or_none(Division.id == division_id)
|
||||||
if this_div is None:
|
if this_div is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Division ID {division_id} not found"
|
status_code=404, detail=f"Division ID {division_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
r_div = model_to_dict(this_div)
|
r_div = model_to_dict(this_div)
|
||||||
|
db.close()
|
||||||
return r_div
|
return r_div
|
||||||
|
|
||||||
|
|
||||||
@ -82,11 +78,12 @@ async def patch_division(
|
|||||||
token: str = Depends(oauth2_scheme),
|
token: str = Depends(oauth2_scheme),
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_division - Bad Token")
|
logger.warning(f"patch_division - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_div = Division.get_or_none(Division.id == division_id)
|
this_div = Division.get_or_none(Division.id == division_id)
|
||||||
if this_div is None:
|
if this_div is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Division ID {division_id} not found"
|
status_code=404, detail=f"Division ID {division_id} not found"
|
||||||
)
|
)
|
||||||
@ -102,8 +99,10 @@ async def patch_division(
|
|||||||
|
|
||||||
if this_div.save() == 1:
|
if this_div.save() == 1:
|
||||||
r_division = model_to_dict(this_div)
|
r_division = model_to_dict(this_div)
|
||||||
|
db.close()
|
||||||
return r_division
|
return r_division
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch division {division_id}"
|
status_code=500, detail=f"Unable to patch division {division_id}"
|
||||||
)
|
)
|
||||||
@ -115,15 +114,17 @@ async def post_division(
|
|||||||
new_division: DivisionModel, token: str = Depends(oauth2_scheme)
|
new_division: DivisionModel, token: str = Depends(oauth2_scheme)
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_division - Bad Token")
|
logger.warning(f"post_division - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_division = Division(**new_division.model_dump())
|
this_division = Division(**new_division.dict())
|
||||||
|
|
||||||
if this_division.save() == 1:
|
if this_division.save() == 1:
|
||||||
r_division = model_to_dict(this_division)
|
r_division = model_to_dict(this_division)
|
||||||
|
db.close()
|
||||||
return r_division
|
return r_division
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=500, detail=f"Unable to post division")
|
raise HTTPException(status_code=500, detail=f"Unable to post division")
|
||||||
|
|
||||||
|
|
||||||
@ -131,16 +132,18 @@ async def post_division(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_division(division_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_division(division_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_division - Bad Token")
|
logger.warning(f"delete_division - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_div = Division.get_or_none(Division.id == division_id)
|
this_div = Division.get_or_none(Division.id == division_id)
|
||||||
if this_div is None:
|
if this_div is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Division ID {division_id} not found"
|
status_code=404, detail=f"Division ID {division_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
count = this_div.delete_instance()
|
count = this_div.delete_instance()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Division {division_id} has been deleted"
|
return f"Division {division_id} has been deleted"
|
||||||
|
|||||||
@ -32,6 +32,7 @@ async def get_draftdata():
|
|||||||
|
|
||||||
if draft_data is not None:
|
if draft_data is not None:
|
||||||
r_data = model_to_dict(draft_data)
|
r_data = model_to_dict(draft_data)
|
||||||
|
db.close()
|
||||||
return r_data
|
return r_data
|
||||||
|
|
||||||
raise HTTPException(status_code=404, detail=f'No draft data found')
|
raise HTTPException(status_code=404, detail=f'No draft data found')
|
||||||
@ -44,11 +45,12 @@ async def patch_draftdata(
|
|||||||
pick_deadline: Optional[datetime.datetime] = None, result_channel: Optional[int] = None,
|
pick_deadline: Optional[datetime.datetime] = None, result_channel: Optional[int] = None,
|
||||||
ping_channel: Optional[int] = None, pick_minutes: Optional[int] = None, token: str = Depends(oauth2_scheme)):
|
ping_channel: Optional[int] = None, pick_minutes: Optional[int] = None, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning('patch_draftdata - Bad Token')
|
logger.warning(f'patch_draftdata - Bad Token: {token}')
|
||||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||||
|
|
||||||
draft_data = DraftData.get_or_none(DraftData.id == data_id)
|
draft_data = DraftData.get_or_none(DraftData.id == data_id)
|
||||||
if draft_data is None:
|
if draft_data is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f'No draft data found')
|
raise HTTPException(status_code=404, detail=f'No draft data found')
|
||||||
|
|
||||||
if currentpick is not None:
|
if currentpick is not None:
|
||||||
@ -66,6 +68,7 @@ async def patch_draftdata(
|
|||||||
|
|
||||||
saved = draft_data.save()
|
saved = draft_data.save()
|
||||||
r_data = model_to_dict(draft_data)
|
r_data = model_to_dict(draft_data)
|
||||||
|
db.close()
|
||||||
|
|
||||||
if saved == 1:
|
if saved == 1:
|
||||||
return r_data
|
return r_data
|
||||||
|
|||||||
@ -9,8 +9,6 @@ from ..dependencies import (
|
|||||||
valid_token,
|
valid_token,
|
||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -36,11 +34,9 @@ async def get_draftlist(
|
|||||||
season: Optional[int],
|
season: Optional[int],
|
||||||
team_id: list = Query(default=None),
|
team_id: list = Query(default=None),
|
||||||
token: str = Depends(oauth2_scheme),
|
token: str = Depends(oauth2_scheme),
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("get_draftlist - Bad Token")
|
logger.warning(f"get_draftlist - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
all_list = DraftList.select()
|
all_list = DraftList.select()
|
||||||
@ -50,11 +46,9 @@ async def get_draftlist(
|
|||||||
if team_id is not None:
|
if team_id is not None:
|
||||||
all_list = all_list.where(DraftList.team_id << team_id)
|
all_list = all_list.where(DraftList.team_id << team_id)
|
||||||
|
|
||||||
total_count = all_list.count()
|
r_list = {"count": all_list.count(), "picks": [model_to_dict(x) for x in all_list]}
|
||||||
all_list = all_list.offset(offset).limit(limit)
|
|
||||||
|
|
||||||
r_list = {"count": total_count, "picks": [model_to_dict(x) for x in all_list]}
|
|
||||||
|
|
||||||
|
db.close()
|
||||||
return r_list
|
return r_list
|
||||||
|
|
||||||
|
|
||||||
@ -62,7 +56,7 @@ async def get_draftlist(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def get_team_draftlist(team_id: int, token: str = Depends(oauth2_scheme)):
|
async def get_team_draftlist(team_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_draftlist - Bad Token")
|
logger.warning(f"post_draftlist - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_team = Team.get_or_none(Team.id == team_id)
|
this_team = Team.get_or_none(Team.id == team_id)
|
||||||
@ -75,6 +69,7 @@ async def get_team_draftlist(team_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
"picks": [model_to_dict(x) for x in this_list],
|
"picks": [model_to_dict(x) for x in this_list],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
return r_list
|
return r_list
|
||||||
|
|
||||||
|
|
||||||
@ -84,7 +79,7 @@ async def post_draftlist(
|
|||||||
draft_list: DraftListList, token: str = Depends(oauth2_scheme)
|
draft_list: DraftListList, token: str = Depends(oauth2_scheme)
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_draftlist - Bad Token")
|
logger.warning(f"post_draftlist - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
new_list = []
|
new_list = []
|
||||||
@ -98,12 +93,13 @@ async def post_draftlist(
|
|||||||
DraftList.delete().where(DraftList.team == this_team).execute()
|
DraftList.delete().where(DraftList.team == this_team).execute()
|
||||||
|
|
||||||
for x in draft_list.draft_list:
|
for x in draft_list.draft_list:
|
||||||
new_list.append(x.model_dump())
|
new_list.append(x.dict())
|
||||||
|
|
||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_list, 15):
|
for batch in chunked(new_list, 15):
|
||||||
DraftList.insert_many(batch).on_conflict_ignore().execute()
|
DraftList.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
|
||||||
|
db.close()
|
||||||
return f"Inserted {len(new_list)} list values"
|
return f"Inserted {len(new_list)} list values"
|
||||||
|
|
||||||
|
|
||||||
@ -111,8 +107,9 @@ async def post_draftlist(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_draftlist(team_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_draftlist(team_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_draftlist - Bad Token")
|
logger.warning(f"delete_draftlist - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
count = DraftList.delete().where(DraftList.team_id == team_id).execute()
|
count = DraftList.delete().where(DraftList.team_id == team_id).execute()
|
||||||
|
db.close()
|
||||||
return f"Deleted {count} list values"
|
return f"Deleted {count} list values"
|
||||||
|
|||||||
@ -9,8 +9,6 @@ from ..dependencies import (
|
|||||||
valid_token,
|
valid_token,
|
||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -52,7 +50,7 @@ async def get_picks(
|
|||||||
overall_end: Optional[int] = None,
|
overall_end: Optional[int] = None,
|
||||||
short_output: Optional[bool] = False,
|
short_output: Optional[bool] = False,
|
||||||
sort: Optional[str] = None,
|
sort: Optional[str] = None,
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
limit: Optional[int] = None,
|
||||||
player_id: list = Query(default=None),
|
player_id: list = Query(default=None),
|
||||||
player_taken: Optional[bool] = None,
|
player_taken: Optional[bool] = None,
|
||||||
):
|
):
|
||||||
@ -112,6 +110,7 @@ async def get_picks(
|
|||||||
all_picks = all_picks.where(DraftPick.overall <= overall_end)
|
all_picks = all_picks.where(DraftPick.overall <= overall_end)
|
||||||
if player_taken is not None:
|
if player_taken is not None:
|
||||||
all_picks = all_picks.where(DraftPick.player.is_null(not player_taken))
|
all_picks = all_picks.where(DraftPick.player.is_null(not player_taken))
|
||||||
|
if limit is not None:
|
||||||
all_picks = all_picks.limit(limit)
|
all_picks = all_picks.limit(limit)
|
||||||
|
|
||||||
if sort is not None:
|
if sort is not None:
|
||||||
@ -124,6 +123,7 @@ async def get_picks(
|
|||||||
for line in all_picks:
|
for line in all_picks:
|
||||||
return_picks["picks"].append(model_to_dict(line, recurse=not short_output))
|
return_picks["picks"].append(model_to_dict(line, recurse=not short_output))
|
||||||
|
|
||||||
|
db.close()
|
||||||
return return_picks
|
return return_picks
|
||||||
|
|
||||||
|
|
||||||
@ -135,6 +135,7 @@ async def get_one_pick(pick_id: int, short_output: Optional[bool] = False):
|
|||||||
r_pick = model_to_dict(this_pick, recurse=not short_output)
|
r_pick = model_to_dict(this_pick, recurse=not short_output)
|
||||||
else:
|
else:
|
||||||
raise HTTPException(status_code=404, detail=f"Pick ID {pick_id} not found")
|
raise HTTPException(status_code=404, detail=f"Pick ID {pick_id} not found")
|
||||||
|
db.close()
|
||||||
return r_pick
|
return r_pick
|
||||||
|
|
||||||
|
|
||||||
@ -144,14 +145,15 @@ async def patch_pick(
|
|||||||
pick_id: int, new_pick: DraftPickModel, token: str = Depends(oauth2_scheme)
|
pick_id: int, new_pick: DraftPickModel, token: str = Depends(oauth2_scheme)
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_pick - Bad Token")
|
logger.warning(f"patch_pick - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
if DraftPick.get_or_none(DraftPick.id == pick_id) is None:
|
if DraftPick.get_or_none(DraftPick.id == pick_id) is None:
|
||||||
raise HTTPException(status_code=404, detail=f"Pick ID {pick_id} not found")
|
raise HTTPException(status_code=404, detail=f"Pick ID {pick_id} not found")
|
||||||
|
|
||||||
DraftPick.update(**new_pick.model_dump()).where(DraftPick.id == pick_id).execute()
|
DraftPick.update(**new_pick.dict()).where(DraftPick.id == pick_id).execute()
|
||||||
r_pick = model_to_dict(DraftPick.get_by_id(pick_id))
|
r_pick = model_to_dict(DraftPick.get_by_id(pick_id))
|
||||||
|
db.close()
|
||||||
return r_pick
|
return r_pick
|
||||||
|
|
||||||
|
|
||||||
@ -159,7 +161,7 @@ async def patch_pick(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_picks(p_list: DraftPickList, token: str = Depends(oauth2_scheme)):
|
async def post_picks(p_list: DraftPickList, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_picks - Bad Token")
|
logger.warning(f"post_picks - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
new_picks = []
|
new_picks = []
|
||||||
@ -168,16 +170,18 @@ async def post_picks(p_list: DraftPickList, token: str = Depends(oauth2_scheme))
|
|||||||
DraftPick.season == pick.season, DraftPick.overall == pick.overall
|
DraftPick.season == pick.season, DraftPick.overall == pick.overall
|
||||||
)
|
)
|
||||||
if dupe:
|
if dupe:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500,
|
status_code=500,
|
||||||
detail=f"Pick # {pick.overall} already exists for season {pick.season}",
|
detail=f"Pick # {pick.overall} already exists for season {pick.season}",
|
||||||
)
|
)
|
||||||
|
|
||||||
new_picks.append(pick.model_dump())
|
new_picks.append(pick.dict())
|
||||||
|
|
||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_picks, 15):
|
for batch in chunked(new_picks, 15):
|
||||||
DraftPick.insert_many(batch).on_conflict_ignore().execute()
|
DraftPick.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
db.close()
|
||||||
|
|
||||||
return f"Inserted {len(new_picks)} picks"
|
return f"Inserted {len(new_picks)} picks"
|
||||||
|
|
||||||
@ -186,7 +190,7 @@ async def post_picks(p_list: DraftPickList, token: str = Depends(oauth2_scheme))
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_pick(pick_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_pick(pick_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_pick - Bad Token")
|
logger.warning(f"delete_pick - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_pick = DraftPick.get_or_none(DraftPick.id == pick_id)
|
this_pick = DraftPick.get_or_none(DraftPick.id == pick_id)
|
||||||
@ -194,6 +198,7 @@ async def delete_pick(pick_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
raise HTTPException(status_code=404, detail=f"Pick ID {pick_id} not found")
|
raise HTTPException(status_code=404, detail=f"Pick ID {pick_id} not found")
|
||||||
|
|
||||||
count = this_pick.delete_instance()
|
count = this_pick.delete_instance()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Draft pick {pick_id} has been deleted"
|
return f"Draft pick {pick_id} has been deleted"
|
||||||
|
|||||||
@ -3,58 +3,40 @@ from typing import List, Optional, Literal
|
|||||||
import logging
|
import logging
|
||||||
import pydantic
|
import pydantic
|
||||||
|
|
||||||
from ..db_engine import (
|
from ..db_engine import db, BattingStat, Team, Player, Current, model_to_dict, chunked, fn, per_season_weeks
|
||||||
db,
|
from ..dependencies import oauth2_scheme, valid_token, handle_db_errors
|
||||||
BattingStat,
|
|
||||||
Team,
|
logger = logging.getLogger('discord_app')
|
||||||
Player,
|
|
||||||
Current,
|
router = APIRouter(
|
||||||
model_to_dict,
|
prefix='/api/v3/fieldingstats',
|
||||||
chunked,
|
tags=['fieldingstats']
|
||||||
fn,
|
|
||||||
per_season_weeks,
|
|
||||||
)
|
|
||||||
from ..dependencies import (
|
|
||||||
oauth2_scheme,
|
|
||||||
valid_token,
|
|
||||||
handle_db_errors,
|
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/v3/fieldingstats", tags=["fieldingstats"])
|
@router.get('')
|
||||||
|
|
||||||
|
|
||||||
@router.get("")
|
|
||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def get_fieldingstats(
|
async def get_fieldingstats(
|
||||||
season: int,
|
season: int, s_type: Optional[str] = 'regular', team_abbrev: list = Query(default=None),
|
||||||
s_type: Optional[str] = "regular",
|
player_name: list = Query(default=None), player_id: list = Query(default=None),
|
||||||
team_abbrev: list = Query(default=None),
|
week_start: Optional[int] = None, week_end: Optional[int] = None, game_num: list = Query(default=None),
|
||||||
player_name: list = Query(default=None),
|
position: list = Query(default=None), limit: Optional[int] = None, sort: Optional[str] = None,
|
||||||
player_id: list = Query(default=None),
|
short_output: Optional[bool] = True):
|
||||||
week_start: Optional[int] = None,
|
if 'post' in s_type.lower():
|
||||||
week_end: Optional[int] = None,
|
|
||||||
game_num: list = Query(default=None),
|
|
||||||
position: list = Query(default=None),
|
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
sort: Optional[str] = None,
|
|
||||||
short_output: Optional[bool] = True,
|
|
||||||
):
|
|
||||||
if "post" in s_type.lower():
|
|
||||||
all_stats = BattingStat.post_season(season)
|
all_stats = BattingStat.post_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
return {"count": 0, "stats": []}
|
db.close()
|
||||||
elif s_type.lower() in ["combined", "total", "all"]:
|
return {'count': 0, 'stats': []}
|
||||||
|
elif s_type.lower() in ['combined', 'total', 'all']:
|
||||||
all_stats = BattingStat.combined_season(season)
|
all_stats = BattingStat.combined_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
return {"count": 0, "stats": []}
|
db.close()
|
||||||
|
return {'count': 0, 'stats': []}
|
||||||
else:
|
else:
|
||||||
all_stats = BattingStat.regular_season(season)
|
all_stats = BattingStat.regular_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
return {"count": 0, "stats": []}
|
db.close()
|
||||||
|
return {'count': 0, 'stats': []}
|
||||||
|
|
||||||
all_stats = all_stats.where(
|
all_stats = all_stats.where(
|
||||||
(BattingStat.xch > 0) | (BattingStat.pb > 0) | (BattingStat.sbc > 0)
|
(BattingStat.xch > 0) | (BattingStat.pb > 0) | (BattingStat.sbc > 0)
|
||||||
@ -69,9 +51,7 @@ async def get_fieldingstats(
|
|||||||
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(
|
p_query = Player.select_season(season).where(fn.Lower(Player.name) << [x.lower() for x in player_name])
|
||||||
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)
|
||||||
@ -83,91 +63,73 @@ async def get_fieldingstats(
|
|||||||
if week_end is not None:
|
if week_end is not None:
|
||||||
end = min(week_end, end)
|
end = min(week_end, end)
|
||||||
if start > end:
|
if start > end:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404,
|
status_code=404,
|
||||||
detail=f"Start week {start} is after end week {end} - cannot pull stats",
|
detail=f'Start week {start} is after end week {end} - cannot pull stats'
|
||||||
|
)
|
||||||
|
all_stats = all_stats.where(
|
||||||
|
(BattingStat.week >= start) & (BattingStat.week <= end)
|
||||||
)
|
)
|
||||||
all_stats = all_stats.where((BattingStat.week >= start) & (BattingStat.week <= end))
|
|
||||||
|
|
||||||
total_count = all_stats.count()
|
if limit:
|
||||||
all_stats = all_stats.limit(limit)
|
all_stats = all_stats.limit(limit)
|
||||||
if sort:
|
if sort:
|
||||||
if sort == "newest":
|
if sort == 'newest':
|
||||||
all_stats = all_stats.order_by(-BattingStat.week, -BattingStat.game)
|
all_stats = all_stats.order_by(-BattingStat.week, -BattingStat.game)
|
||||||
|
|
||||||
return_stats = {
|
return_stats = {
|
||||||
"count": total_count,
|
'count': all_stats.count(),
|
||||||
"stats": [
|
'stats': [{
|
||||||
{
|
'player': x.player_id if short_output else model_to_dict(x.player, recurse=False),
|
||||||
"player": x.player_id
|
'team': x.team_id if short_output else model_to_dict(x.team, recurse=False),
|
||||||
if short_output
|
'pos': x.pos,
|
||||||
else model_to_dict(x.player, recurse=False),
|
'xch': x.xch,
|
||||||
"team": x.team_id
|
'xhit': x.xhit,
|
||||||
if short_output
|
'error': x.error,
|
||||||
else model_to_dict(x.team, recurse=False),
|
'pb': x.pb,
|
||||||
"pos": x.pos,
|
'sbc': x.sbc,
|
||||||
"xch": x.xch,
|
'csc': x.csc,
|
||||||
"xhit": x.xhit,
|
'week': x.week,
|
||||||
"error": x.error,
|
'game': x.game,
|
||||||
"pb": x.pb,
|
'season': x.season
|
||||||
"sbc": x.sbc,
|
} for x in all_stats]
|
||||||
"csc": x.csc,
|
|
||||||
"week": x.week,
|
|
||||||
"game": x.game,
|
|
||||||
"season": x.season,
|
|
||||||
}
|
|
||||||
for x in all_stats
|
|
||||||
],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|
||||||
|
|
||||||
@router.get("/totals")
|
@router.get('/totals')
|
||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def get_totalstats(
|
async def get_totalstats(
|
||||||
season: int,
|
season: int, s_type: Literal['regular', 'post', 'total', None] = None, team_abbrev: list = Query(default=None),
|
||||||
s_type: Literal["regular", "post", "total", None] = None,
|
team_id: list = Query(default=None), player_name: list = Query(default=None),
|
||||||
team_abbrev: list = Query(default=None),
|
week_start: Optional[int] = None, week_end: Optional[int] = None, game_num: list = Query(default=None),
|
||||||
team_id: list = Query(default=None),
|
position: list = Query(default=None), sort: Optional[str] = None, player_id: list = Query(default=None),
|
||||||
player_name: list = Query(default=None),
|
group_by: Literal['team', 'player', 'playerteam'] = 'player', short_output: Optional[bool] = False,
|
||||||
week_start: Optional[int] = None,
|
min_ch: Optional[int] = 1, week: list = Query(default=None)):
|
||||||
week_end: Optional[int] = None,
|
|
||||||
game_num: list = Query(default=None),
|
|
||||||
position: list = Query(default=None),
|
|
||||||
sort: Optional[str] = None,
|
|
||||||
player_id: list = Query(default=None),
|
|
||||||
group_by: Literal["team", "player", "playerteam"] = "player",
|
|
||||||
short_output: Optional[bool] = False,
|
|
||||||
min_ch: Optional[int] = 1,
|
|
||||||
week: list = Query(default=None),
|
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
):
|
|
||||||
|
|
||||||
# Build SELECT fields conditionally based on group_by to match GROUP BY exactly
|
# Build SELECT fields conditionally based on group_by to match GROUP BY exactly
|
||||||
select_fields = []
|
select_fields = []
|
||||||
|
|
||||||
if group_by == "player":
|
if group_by == 'player':
|
||||||
select_fields = [BattingStat.player, BattingStat.pos]
|
select_fields = [BattingStat.player, BattingStat.pos]
|
||||||
elif group_by == "team":
|
elif group_by == 'team':
|
||||||
select_fields = [BattingStat.team, BattingStat.pos]
|
select_fields = [BattingStat.team, BattingStat.pos]
|
||||||
elif group_by == "playerteam":
|
elif group_by == 'playerteam':
|
||||||
select_fields = [BattingStat.player, BattingStat.team, BattingStat.pos]
|
select_fields = [BattingStat.player, BattingStat.team, BattingStat.pos]
|
||||||
else:
|
else:
|
||||||
# Default case
|
# Default case
|
||||||
select_fields = [BattingStat.player, BattingStat.pos]
|
select_fields = [BattingStat.player, BattingStat.pos]
|
||||||
|
|
||||||
all_stats = (
|
all_stats = (
|
||||||
BattingStat.select(
|
BattingStat
|
||||||
*select_fields,
|
.select(*select_fields,
|
||||||
fn.SUM(BattingStat.xch).alias("sum_xch"),
|
fn.SUM(BattingStat.xch).alias('sum_xch'),
|
||||||
fn.SUM(BattingStat.xhit).alias("sum_xhit"),
|
fn.SUM(BattingStat.xhit).alias('sum_xhit'), fn.SUM(BattingStat.error).alias('sum_error'),
|
||||||
fn.SUM(BattingStat.error).alias("sum_error"),
|
fn.SUM(BattingStat.pb).alias('sum_pb'), fn.SUM(BattingStat.sbc).alias('sum_sbc'),
|
||||||
fn.SUM(BattingStat.pb).alias("sum_pb"),
|
fn.SUM(BattingStat.csc).alias('sum_csc'))
|
||||||
fn.SUM(BattingStat.sbc).alias("sum_sbc"),
|
|
||||||
fn.SUM(BattingStat.csc).alias("sum_csc"),
|
|
||||||
)
|
|
||||||
.where(BattingStat.season == season)
|
.where(BattingStat.season == season)
|
||||||
.having(fn.SUM(BattingStat.xch) >= min_ch)
|
.having(fn.SUM(BattingStat.xch) >= min_ch)
|
||||||
)
|
)
|
||||||
@ -179,20 +141,16 @@ async def get_totalstats(
|
|||||||
elif week_start is not None or week_end is not None:
|
elif week_start is not None or week_end is not None:
|
||||||
if week_start is None or week_end is None:
|
if week_start is None or week_end is None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400, detail='Both week_start and week_end must be included if either is used.'
|
||||||
detail="Both week_start and week_end must be included if either is used.",
|
|
||||||
)
|
|
||||||
weeks["start"] = week_start
|
|
||||||
if week_end < weeks["start"]:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=400,
|
|
||||||
detail="week_end must be greater than or equal to week_start",
|
|
||||||
)
|
)
|
||||||
|
weeks['start'] = week_start
|
||||||
|
if week_end < weeks['start']:
|
||||||
|
raise HTTPException(status_code=400, detail='week_end must be greater than or equal to week_start')
|
||||||
else:
|
else:
|
||||||
weeks["end"] = week_end
|
weeks['end'] = week_end
|
||||||
|
|
||||||
all_stats = all_stats.where(
|
all_stats = all_stats.where(
|
||||||
(BattingStat.week >= weeks["start"]) & (BattingStat.week <= weeks["end"])
|
(BattingStat.week >= weeks['start']) & (BattingStat.week <= weeks['end'])
|
||||||
)
|
)
|
||||||
|
|
||||||
elif week is not None:
|
elif week is not None:
|
||||||
@ -203,20 +161,14 @@ async def get_totalstats(
|
|||||||
if position is not None:
|
if position is not None:
|
||||||
p_list = [x.upper() for x in position]
|
p_list = [x.upper() for x in position]
|
||||||
all_players = Player.select().where(
|
all_players = Player.select().where(
|
||||||
(Player.pos_1 << p_list)
|
(Player.pos_1 << p_list) | (Player.pos_2 << p_list) | (Player.pos_3 << p_list) | (Player.pos_4 << p_list) |
|
||||||
| (Player.pos_2 << p_list)
|
(Player.pos_5 << p_list) | (Player.pos_6 << p_list) | (Player.pos_7 << p_list) | (Player.pos_8 << p_list)
|
||||||
| (Player.pos_3 << p_list)
|
|
||||||
| (Player.pos_4 << p_list)
|
|
||||||
| (Player.pos_5 << p_list)
|
|
||||||
| (Player.pos_6 << p_list)
|
|
||||||
| (Player.pos_7 << p_list)
|
|
||||||
| (Player.pos_8 << p_list)
|
|
||||||
)
|
)
|
||||||
all_stats = all_stats.where(BattingStat.player << all_players)
|
all_stats = all_stats.where(BattingStat.player << all_players)
|
||||||
if sort is not None:
|
if sort is not None:
|
||||||
if sort == "player":
|
if sort == 'player':
|
||||||
all_stats = all_stats.order_by(BattingStat.player)
|
all_stats = all_stats.order_by(BattingStat.player)
|
||||||
elif sort == "team":
|
elif sort == 'team':
|
||||||
all_stats = all_stats.order_by(BattingStat.team)
|
all_stats = all_stats.order_by(BattingStat.team)
|
||||||
if group_by is not None:
|
if group_by is not None:
|
||||||
# Use the same fields for GROUP BY as we used for SELECT
|
# Use the same fields for GROUP BY as we used for SELECT
|
||||||
@ -225,55 +177,47 @@ async def get_totalstats(
|
|||||||
all_teams = Team.select().where(Team.id << team_id)
|
all_teams = Team.select().where(Team.id << team_id)
|
||||||
all_stats = all_stats.where(BattingStat.team << all_teams)
|
all_stats = all_stats.where(BattingStat.team << all_teams)
|
||||||
elif team_abbrev is not None:
|
elif team_abbrev is not None:
|
||||||
all_teams = Team.select().where(
|
all_teams = Team.select().where(fn.Lower(Team.abbrev) << [x.lower() for x in team_abbrev])
|
||||||
fn.Lower(Team.abbrev) << [x.lower() for x in team_abbrev]
|
|
||||||
)
|
|
||||||
all_stats = all_stats.where(BattingStat.team << all_teams)
|
all_stats = all_stats.where(BattingStat.team << all_teams)
|
||||||
|
|
||||||
if player_name is not None:
|
if player_name is not None:
|
||||||
all_players = Player.select().where(
|
all_players = Player.select().where(fn.Lower(Player.name) << [x.lower() for x in player_name])
|
||||||
fn.Lower(Player.name) << [x.lower() for x in player_name]
|
|
||||||
)
|
|
||||||
all_stats = all_stats.where(BattingStat.player << all_players)
|
all_stats = all_stats.where(BattingStat.player << all_players)
|
||||||
elif player_id is not None:
|
elif player_id is not None:
|
||||||
all_players = Player.select().where(Player.id << player_id)
|
all_players = Player.select().where(Player.id << player_id)
|
||||||
all_stats = all_stats.where(BattingStat.player << all_players)
|
all_stats = all_stats.where(BattingStat.player << all_players)
|
||||||
|
|
||||||
total_count = all_stats.count()
|
return_stats = {
|
||||||
all_stats = all_stats.offset(offset).limit(limit)
|
'count': 0,
|
||||||
|
'stats': []
|
||||||
return_stats = {"count": total_count, "stats": []}
|
}
|
||||||
|
|
||||||
for x in all_stats:
|
for x in all_stats:
|
||||||
if x.sum_xch + x.sum_sbc <= 0:
|
if x.sum_xch + x.sum_sbc <= 0:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Handle player field based on grouping with safe access
|
# Handle player field based on grouping with safe access
|
||||||
this_player = "TOT"
|
this_player = 'TOT'
|
||||||
if "player" in group_by and hasattr(x, "player"):
|
if 'player' in group_by and hasattr(x, 'player'):
|
||||||
this_player = (
|
this_player = x.player_id if short_output else model_to_dict(x.player, recurse=False)
|
||||||
x.player_id if short_output else model_to_dict(x.player, recurse=False)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Handle team field based on grouping with safe access
|
# Handle team field based on grouping with safe access
|
||||||
this_team = "TOT"
|
this_team = 'TOT'
|
||||||
if "team" in group_by and hasattr(x, "team"):
|
if 'team' in group_by and hasattr(x, 'team'):
|
||||||
this_team = (
|
this_team = x.team_id if short_output else model_to_dict(x.team, recurse=False)
|
||||||
x.team_id if short_output else model_to_dict(x.team, recurse=False)
|
|
||||||
)
|
|
||||||
|
|
||||||
return_stats["stats"].append(
|
return_stats['stats'].append({
|
||||||
{
|
'player': this_player,
|
||||||
"player": this_player,
|
'team': this_team,
|
||||||
"team": this_team,
|
'pos': x.pos,
|
||||||
"pos": x.pos,
|
'xch': x.sum_xch,
|
||||||
"xch": x.sum_xch,
|
'xhit': x.sum_xhit,
|
||||||
"xhit": x.sum_xhit,
|
'error': x.sum_error,
|
||||||
"error": x.sum_error,
|
'pb': x.sum_pb,
|
||||||
"pb": x.sum_pb,
|
'sbc': x.sum_sbc,
|
||||||
"sbc": x.sum_sbc,
|
'csc': x.sum_csc
|
||||||
"csc": x.sum_csc,
|
})
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
return_stats['count'] = len(return_stats['stats'])
|
||||||
|
db.close()
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|||||||
@ -138,6 +138,8 @@ async def get_help_commands(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting help commands: {e}")
|
logger.error(f"Error getting help commands: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.post("/", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.post("/", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -147,7 +149,7 @@ async def create_help_command_endpoint(
|
|||||||
):
|
):
|
||||||
"""Create a new help command"""
|
"""Create a new help command"""
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("create_help_command - Bad Token")
|
logger.warning(f"create_help_command - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -185,6 +187,8 @@ async def create_help_command_endpoint(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error creating help command: {e}")
|
logger.error(f"Error creating help command: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.put("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -194,7 +198,7 @@ async def update_help_command_endpoint(
|
|||||||
):
|
):
|
||||||
"""Update an existing help command"""
|
"""Update an existing help command"""
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("update_help_command - Bad Token")
|
logger.warning(f"update_help_command - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -234,6 +238,8 @@ async def update_help_command_endpoint(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error updating help command {command_id}: {e}")
|
logger.error(f"Error updating help command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.patch("/{command_id}/restore", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.patch("/{command_id}/restore", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -243,7 +249,7 @@ async def restore_help_command_endpoint(
|
|||||||
):
|
):
|
||||||
"""Restore a soft-deleted help command"""
|
"""Restore a soft-deleted help command"""
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("restore_help_command - Bad Token")
|
logger.warning(f"restore_help_command - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -271,6 +277,8 @@ async def restore_help_command_endpoint(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error restoring help command {command_id}: {e}")
|
logger.error(f"Error restoring help command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.delete("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -280,7 +288,7 @@ async def delete_help_command_endpoint(
|
|||||||
):
|
):
|
||||||
"""Soft delete a help command"""
|
"""Soft delete a help command"""
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_help_command - Bad Token")
|
logger.warning(f"delete_help_command - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -301,6 +309,8 @@ async def delete_help_command_endpoint(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error deleting help command {command_id}: {e}")
|
logger.error(f"Error deleting help command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/stats")
|
@router.get("/stats")
|
||||||
@ -358,6 +368,8 @@ async def get_help_command_stats():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting help command stats: {e}")
|
logger.error(f"Error getting help command stats: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
# Special endpoints for Discord bot integration
|
# Special endpoints for Discord bot integration
|
||||||
@ -390,6 +402,8 @@ async def get_help_command_by_name_endpoint(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting help command by name '{command_name}': {e}")
|
logger.error(f"Error getting help command by name '{command_name}': {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.patch("/by_name/{command_name}/view", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.patch("/by_name/{command_name}/view", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -397,7 +411,7 @@ async def get_help_command_by_name_endpoint(
|
|||||||
async def increment_view_count(command_name: str, token: str = Depends(oauth2_scheme)):
|
async def increment_view_count(command_name: str, token: str = Depends(oauth2_scheme)):
|
||||||
"""Increment view count for a help command"""
|
"""Increment view count for a help command"""
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("increment_view_count - Bad Token")
|
logger.warning(f"increment_view_count - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -425,6 +439,8 @@ async def increment_view_count(command_name: str, token: str = Depends(oauth2_sc
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error incrementing view count for '{command_name}': {e}")
|
logger.error(f"Error incrementing view count for '{command_name}': {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/autocomplete")
|
@router.get("/autocomplete")
|
||||||
@ -454,6 +470,8 @@ async def get_help_names_for_autocomplete(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting help names for autocomplete: {e}")
|
logger.error(f"Error getting help names for autocomplete: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{command_id}")
|
@router.get("/{command_id}")
|
||||||
@ -481,3 +499,5 @@ async def get_help_command(command_id: int):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting help command {command_id}: {e}")
|
logger.error(f"Error getting help command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|||||||
@ -9,8 +9,6 @@ from ..dependencies import (
|
|||||||
valid_token,
|
valid_token,
|
||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -40,8 +38,6 @@ async def get_injuries(
|
|||||||
is_active: bool = None,
|
is_active: bool = None,
|
||||||
short_output: bool = False,
|
short_output: bool = False,
|
||||||
sort: Optional[str] = "start-asc",
|
sort: Optional[str] = "start-asc",
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
):
|
):
|
||||||
all_injuries = Injury.select()
|
all_injuries = Injury.select()
|
||||||
|
|
||||||
@ -68,13 +64,11 @@ async def get_injuries(
|
|||||||
elif sort == "start-desc":
|
elif sort == "start-desc":
|
||||||
all_injuries = all_injuries.order_by(-Injury.start_week, -Injury.start_game)
|
all_injuries = all_injuries.order_by(-Injury.start_week, -Injury.start_game)
|
||||||
|
|
||||||
total_count = all_injuries.count()
|
|
||||||
all_injuries = all_injuries.offset(offset).limit(limit)
|
|
||||||
|
|
||||||
return_injuries = {
|
return_injuries = {
|
||||||
"count": total_count,
|
"count": all_injuries.count(),
|
||||||
"injuries": [model_to_dict(x, recurse=not short_output) for x in all_injuries],
|
"injuries": [model_to_dict(x, recurse=not short_output) for x in all_injuries],
|
||||||
}
|
}
|
||||||
|
db.close()
|
||||||
return return_injuries
|
return return_injuries
|
||||||
|
|
||||||
|
|
||||||
@ -86,11 +80,12 @@ async def patch_injury(
|
|||||||
token: str = Depends(oauth2_scheme),
|
token: str = Depends(oauth2_scheme),
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_injury - Bad Token")
|
logger.warning(f"patch_injury - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_injury = Injury.get_or_none(Injury.id == injury_id)
|
this_injury = Injury.get_or_none(Injury.id == injury_id)
|
||||||
if this_injury is None:
|
if this_injury is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"Injury ID {injury_id} not found")
|
raise HTTPException(status_code=404, detail=f"Injury ID {injury_id} not found")
|
||||||
|
|
||||||
if is_active is not None:
|
if is_active is not None:
|
||||||
@ -98,8 +93,10 @@ async def patch_injury(
|
|||||||
|
|
||||||
if this_injury.save() == 1:
|
if this_injury.save() == 1:
|
||||||
r_injury = model_to_dict(this_injury)
|
r_injury = model_to_dict(this_injury)
|
||||||
|
db.close()
|
||||||
return r_injury
|
return r_injury
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch injury {injury_id}"
|
status_code=500, detail=f"Unable to patch injury {injury_id}"
|
||||||
)
|
)
|
||||||
@ -109,15 +106,17 @@ async def patch_injury(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_injury(new_injury: InjuryModel, token: str = Depends(oauth2_scheme)):
|
async def post_injury(new_injury: InjuryModel, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_injury - Bad Token")
|
logger.warning(f"post_injury - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_injury = Injury(**new_injury.model_dump())
|
this_injury = Injury(**new_injury.dict())
|
||||||
|
|
||||||
if this_injury.save():
|
if this_injury.save():
|
||||||
r_injury = model_to_dict(this_injury)
|
r_injury = model_to_dict(this_injury)
|
||||||
|
db.close()
|
||||||
return r_injury
|
return r_injury
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=500, detail=f"Unable to post injury")
|
raise HTTPException(status_code=500, detail=f"Unable to post injury")
|
||||||
|
|
||||||
|
|
||||||
@ -125,14 +124,16 @@ async def post_injury(new_injury: InjuryModel, token: str = Depends(oauth2_schem
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_injury(injury_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_injury(injury_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_injury - Bad Token")
|
logger.warning(f"delete_injury - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_injury = Injury.get_or_none(Injury.id == injury_id)
|
this_injury = Injury.get_or_none(Injury.id == injury_id)
|
||||||
if this_injury is None:
|
if this_injury is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"Injury ID {injury_id} not found")
|
raise HTTPException(status_code=404, detail=f"Injury ID {injury_id} not found")
|
||||||
|
|
||||||
count = this_injury.delete_instance()
|
count = this_injury.delete_instance()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Injury {injury_id} has been deleted"
|
return f"Injury {injury_id} has been deleted"
|
||||||
|
|||||||
@ -9,8 +9,6 @@ from ..dependencies import (
|
|||||||
valid_token,
|
valid_token,
|
||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -36,8 +34,6 @@ async def get_keepers(
|
|||||||
team_id: list = Query(default=None),
|
team_id: list = Query(default=None),
|
||||||
player_id: list = Query(default=None),
|
player_id: list = Query(default=None),
|
||||||
short_output: bool = False,
|
short_output: bool = False,
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
):
|
):
|
||||||
all_keepers = Keeper.select()
|
all_keepers = Keeper.select()
|
||||||
|
|
||||||
@ -48,13 +44,11 @@ async def get_keepers(
|
|||||||
if player_id is not None:
|
if player_id is not None:
|
||||||
all_keepers = all_keepers.where(Keeper.player_id << player_id)
|
all_keepers = all_keepers.where(Keeper.player_id << player_id)
|
||||||
|
|
||||||
total_count = all_keepers.count()
|
|
||||||
all_keepers = all_keepers.offset(offset).limit(limit)
|
|
||||||
|
|
||||||
return_keepers = {
|
return_keepers = {
|
||||||
"count": total_count,
|
"count": all_keepers.count(),
|
||||||
"keepers": [model_to_dict(x, recurse=not short_output) for x in all_keepers],
|
"keepers": [model_to_dict(x, recurse=not short_output) for x in all_keepers],
|
||||||
}
|
}
|
||||||
|
db.close()
|
||||||
return return_keepers
|
return return_keepers
|
||||||
|
|
||||||
|
|
||||||
@ -68,7 +62,7 @@ async def patch_keeper(
|
|||||||
token: str = Depends(oauth2_scheme),
|
token: str = Depends(oauth2_scheme),
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_keeper - Bad Token")
|
logger.warning(f"patch_keeper - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_keeper = Keeper.get_or_none(Keeper.id == keeper_id)
|
this_keeper = Keeper.get_or_none(Keeper.id == keeper_id)
|
||||||
@ -84,8 +78,10 @@ async def patch_keeper(
|
|||||||
|
|
||||||
if this_keeper.save():
|
if this_keeper.save():
|
||||||
r_keeper = model_to_dict(this_keeper)
|
r_keeper = model_to_dict(this_keeper)
|
||||||
|
db.close()
|
||||||
return r_keeper
|
return r_keeper
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch keeper {keeper_id}"
|
status_code=500, detail=f"Unable to patch keeper {keeper_id}"
|
||||||
)
|
)
|
||||||
@ -95,16 +91,17 @@ async def patch_keeper(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_keepers(k_list: KeeperList, token: str = Depends(oauth2_scheme)):
|
async def post_keepers(k_list: KeeperList, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_keepers - Bad Token")
|
logger.warning(f"post_keepers - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
new_keepers = []
|
new_keepers = []
|
||||||
for keeper in k_list.keepers:
|
for keeper in k_list.keepers:
|
||||||
new_keepers.append(keeper.model_dump())
|
new_keepers.append(keeper.dict())
|
||||||
|
|
||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_keepers, 14):
|
for batch in chunked(new_keepers, 14):
|
||||||
Keeper.insert_many(batch).on_conflict_ignore().execute()
|
Keeper.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
db.close()
|
||||||
|
|
||||||
return f"Inserted {len(new_keepers)} keepers"
|
return f"Inserted {len(new_keepers)} keepers"
|
||||||
|
|
||||||
@ -113,7 +110,7 @@ async def post_keepers(k_list: KeeperList, token: str = Depends(oauth2_scheme)):
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_keeper(keeper_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_keeper(keeper_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_keeper - Bad Token")
|
logger.warning(f"delete_keeper - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_keeper = Keeper.get_or_none(Keeper.id == keeper_id)
|
this_keeper = Keeper.get_or_none(Keeper.id == keeper_id)
|
||||||
@ -121,6 +118,7 @@ async def delete_keeper(keeper_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
raise HTTPException(status_code=404, detail=f"Keeper ID {keeper_id} not found")
|
raise HTTPException(status_code=404, detail=f"Keeper ID {keeper_id} not found")
|
||||||
|
|
||||||
count = this_keeper.delete_instance()
|
count = this_keeper.delete_instance()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Keeper ID {keeper_id} has been deleted"
|
return f"Keeper ID {keeper_id} has been deleted"
|
||||||
|
|||||||
@ -9,8 +9,6 @@ from ..dependencies import (
|
|||||||
valid_token,
|
valid_token,
|
||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -31,8 +29,6 @@ async def get_managers(
|
|||||||
name: list = Query(default=None),
|
name: list = Query(default=None),
|
||||||
active: Optional[bool] = None,
|
active: Optional[bool] = None,
|
||||||
short_output: Optional[bool] = False,
|
short_output: Optional[bool] = False,
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
):
|
):
|
||||||
if active is not None:
|
if active is not None:
|
||||||
current = Current.latest()
|
current = Current.latest()
|
||||||
@ -65,9 +61,7 @@ async def get_managers(
|
|||||||
i_mgr.append(z)
|
i_mgr.append(z)
|
||||||
final_mgrs = [model_to_dict(y, recurse=not short_output) for y in i_mgr]
|
final_mgrs = [model_to_dict(y, recurse=not short_output) for y in i_mgr]
|
||||||
|
|
||||||
total_count = len(final_mgrs)
|
return_managers = {"count": len(final_mgrs), "managers": final_mgrs}
|
||||||
final_mgrs = final_mgrs[offset : offset + limit]
|
|
||||||
return_managers = {"count": total_count, "managers": final_mgrs}
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
all_managers = Manager.select()
|
all_managers = Manager.select()
|
||||||
@ -75,15 +69,14 @@ async def get_managers(
|
|||||||
name_list = [x.lower() for x in name]
|
name_list = [x.lower() for x in name]
|
||||||
all_managers = all_managers.where(fn.Lower(Manager.name) << name_list)
|
all_managers = all_managers.where(fn.Lower(Manager.name) << name_list)
|
||||||
|
|
||||||
total_count = all_managers.count()
|
|
||||||
all_managers = all_managers.offset(offset).limit(limit)
|
|
||||||
return_managers = {
|
return_managers = {
|
||||||
"count": total_count,
|
"count": all_managers.count(),
|
||||||
"managers": [
|
"managers": [
|
||||||
model_to_dict(x, recurse=not short_output) for x in all_managers
|
model_to_dict(x, recurse=not short_output) for x in all_managers
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
return return_managers
|
return return_managers
|
||||||
|
|
||||||
|
|
||||||
@ -93,6 +86,7 @@ async def get_one_manager(manager_id: int, short_output: Optional[bool] = False)
|
|||||||
this_manager = Manager.get_or_none(Manager.id == manager_id)
|
this_manager = Manager.get_or_none(Manager.id == manager_id)
|
||||||
if this_manager is not None:
|
if this_manager is not None:
|
||||||
r_manager = model_to_dict(this_manager, recurse=not short_output)
|
r_manager = model_to_dict(this_manager, recurse=not short_output)
|
||||||
|
db.close()
|
||||||
return r_manager
|
return r_manager
|
||||||
else:
|
else:
|
||||||
raise HTTPException(status_code=404, detail=f"Manager {manager_id} not found")
|
raise HTTPException(status_code=404, detail=f"Manager {manager_id} not found")
|
||||||
@ -109,11 +103,12 @@ async def patch_manager(
|
|||||||
token: str = Depends(oauth2_scheme),
|
token: str = Depends(oauth2_scheme),
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_manager - Bad Token")
|
logger.warning(f"patch_manager - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_manager = Manager.get_or_none(Manager.id == manager_id)
|
this_manager = Manager.get_or_none(Manager.id == manager_id)
|
||||||
if this_manager is None:
|
if this_manager is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Manager ID {manager_id} not found"
|
status_code=404, detail=f"Manager ID {manager_id} not found"
|
||||||
)
|
)
|
||||||
@ -129,8 +124,10 @@ async def patch_manager(
|
|||||||
|
|
||||||
if this_manager.save() == 1:
|
if this_manager.save() == 1:
|
||||||
r_manager = model_to_dict(this_manager)
|
r_manager = model_to_dict(this_manager)
|
||||||
|
db.close()
|
||||||
return r_manager
|
return r_manager
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch manager {this_manager}"
|
status_code=500, detail=f"Unable to patch manager {this_manager}"
|
||||||
)
|
)
|
||||||
@ -140,15 +137,17 @@ async def patch_manager(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_manager(new_manager: ManagerModel, token: str = Depends(oauth2_scheme)):
|
async def post_manager(new_manager: ManagerModel, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_manager - Bad Token")
|
logger.warning(f"post_manager - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_manager = Manager(**new_manager.model_dump())
|
this_manager = Manager(**new_manager.dict())
|
||||||
|
|
||||||
if this_manager.save():
|
if this_manager.save():
|
||||||
r_manager = model_to_dict(this_manager)
|
r_manager = model_to_dict(this_manager)
|
||||||
|
db.close()
|
||||||
return r_manager
|
return r_manager
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to post manager {this_manager.name}"
|
status_code=500, detail=f"Unable to post manager {this_manager.name}"
|
||||||
)
|
)
|
||||||
@ -158,16 +157,18 @@ async def post_manager(new_manager: ManagerModel, token: str = Depends(oauth2_sc
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_manager(manager_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_manager(manager_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_manager - Bad Token")
|
logger.warning(f"delete_manager - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_manager = Manager.get_or_none(Manager.id == manager_id)
|
this_manager = Manager.get_or_none(Manager.id == manager_id)
|
||||||
if this_manager is None:
|
if this_manager is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Manager ID {manager_id} not found"
|
status_code=404, detail=f"Manager ID {manager_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
count = this_manager.delete_instance()
|
count = this_manager.delete_instance()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Manager {manager_id} has been deleted"
|
return f"Manager {manager_id} has been deleted"
|
||||||
|
|||||||
@ -1,3 +1,6 @@
|
|||||||
|
import datetime
|
||||||
|
import os
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
from typing import List, Optional, Literal
|
from typing import List, Optional, Literal
|
||||||
import logging
|
import logging
|
||||||
@ -19,8 +22,6 @@ from ..dependencies import (
|
|||||||
valid_token,
|
valid_token,
|
||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -70,7 +71,7 @@ async def get_pitstats(
|
|||||||
week_start: Optional[int] = None,
|
week_start: Optional[int] = None,
|
||||||
week_end: Optional[int] = None,
|
week_end: Optional[int] = None,
|
||||||
game_num: list = Query(default=None),
|
game_num: list = Query(default=None),
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
limit: Optional[int] = None,
|
||||||
ip_min: Optional[float] = None,
|
ip_min: Optional[float] = None,
|
||||||
sort: Optional[str] = None,
|
sort: Optional[str] = None,
|
||||||
short_output: Optional[bool] = True,
|
short_output: Optional[bool] = True,
|
||||||
@ -78,14 +79,17 @@ async def get_pitstats(
|
|||||||
if "post" in s_type.lower():
|
if "post" in s_type.lower():
|
||||||
all_stats = PitchingStat.post_season(season)
|
all_stats = PitchingStat.post_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
|
db.close()
|
||||||
return {"count": 0, "stats": []}
|
return {"count": 0, "stats": []}
|
||||||
elif s_type.lower() in ["combined", "total", "all"]:
|
elif s_type.lower() in ["combined", "total", "all"]:
|
||||||
all_stats = PitchingStat.combined_season(season)
|
all_stats = PitchingStat.combined_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
|
db.close()
|
||||||
return {"count": 0, "stats": []}
|
return {"count": 0, "stats": []}
|
||||||
else:
|
else:
|
||||||
all_stats = PitchingStat.regular_season(season)
|
all_stats = PitchingStat.regular_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
|
db.close()
|
||||||
return {"count": 0, "stats": []}
|
return {"count": 0, "stats": []}
|
||||||
|
|
||||||
if team_abbrev is not None:
|
if team_abbrev is not None:
|
||||||
@ -111,6 +115,7 @@ async def get_pitstats(
|
|||||||
if week_end is not None:
|
if week_end is not None:
|
||||||
end = min(week_end, end)
|
end = min(week_end, end)
|
||||||
if start > end:
|
if start > end:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404,
|
status_code=404,
|
||||||
detail=f"Start week {start} is after end week {end} - cannot pull stats",
|
detail=f"Start week {start} is after end week {end} - cannot pull stats",
|
||||||
@ -119,6 +124,7 @@ async def get_pitstats(
|
|||||||
(PitchingStat.week >= start) & (PitchingStat.week <= end)
|
(PitchingStat.week >= start) & (PitchingStat.week <= end)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if limit:
|
||||||
all_stats = all_stats.limit(limit)
|
all_stats = all_stats.limit(limit)
|
||||||
if sort:
|
if sort:
|
||||||
if sort == "newest":
|
if sort == "newest":
|
||||||
@ -129,6 +135,7 @@ async def get_pitstats(
|
|||||||
"stats": [model_to_dict(x, recurse=not short_output) for x in all_stats],
|
"stats": [model_to_dict(x, recurse=not short_output) for x in all_stats],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|
||||||
|
|
||||||
@ -150,8 +157,6 @@ async def get_totalstats(
|
|||||||
short_output: Optional[bool] = False,
|
short_output: Optional[bool] = False,
|
||||||
group_by: Literal["team", "player", "playerteam"] = "player",
|
group_by: Literal["team", "player", "playerteam"] = "player",
|
||||||
week: list = Query(default=None),
|
week: list = Query(default=None),
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
):
|
):
|
||||||
if sum(1 for x in [s_type, (week_start or week_end), week] if x is not None) > 1:
|
if sum(1 for x in [s_type, (week_start or week_end), week] if x is not None) > 1:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
@ -257,10 +262,7 @@ async def get_totalstats(
|
|||||||
all_players = Player.select().where(Player.id << player_id)
|
all_players = Player.select().where(Player.id << player_id)
|
||||||
all_stats = all_stats.where(PitchingStat.player << all_players)
|
all_stats = all_stats.where(PitchingStat.player << all_players)
|
||||||
|
|
||||||
total_count = all_stats.count()
|
return_stats = {"count": all_stats.count(), "stats": []}
|
||||||
all_stats = all_stats.offset(offset).limit(limit)
|
|
||||||
|
|
||||||
return_stats = {"count": total_count, "stats": []}
|
|
||||||
|
|
||||||
for x in all_stats:
|
for x in all_stats:
|
||||||
# Handle player field based on grouping with safe access
|
# Handle player field based on grouping with safe access
|
||||||
@ -302,6 +304,7 @@ async def get_totalstats(
|
|||||||
"bsv": x.sum_bsv,
|
"bsv": x.sum_bsv,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
db.close()
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|
||||||
|
|
||||||
@ -311,16 +314,15 @@ async def patch_pitstats(
|
|||||||
stat_id: int, new_stats: PitStatModel, token: str = Depends(oauth2_scheme)
|
stat_id: int, new_stats: PitStatModel, token: str = Depends(oauth2_scheme)
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_pitstats - Bad Token")
|
logger.warning(f"patch_pitstats - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
if PitchingStat.get_or_none(PitchingStat.id == stat_id) is None:
|
if PitchingStat.get_or_none(PitchingStat.id == stat_id) is None:
|
||||||
raise HTTPException(status_code=404, detail=f"Stat ID {stat_id} not found")
|
raise HTTPException(status_code=404, detail=f"Stat ID {stat_id} not found")
|
||||||
|
|
||||||
PitchingStat.update(**new_stats.model_dump()).where(
|
PitchingStat.update(**new_stats.dict()).where(PitchingStat.id == stat_id).execute()
|
||||||
PitchingStat.id == stat_id
|
|
||||||
).execute()
|
|
||||||
r_stat = model_to_dict(PitchingStat.get_by_id(stat_id))
|
r_stat = model_to_dict(PitchingStat.get_by_id(stat_id))
|
||||||
|
db.close()
|
||||||
return r_stat
|
return r_stat
|
||||||
|
|
||||||
|
|
||||||
@ -328,7 +330,7 @@ async def patch_pitstats(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_pitstats(s_list: PitStatList, token: str = Depends(oauth2_scheme)):
|
async def post_pitstats(s_list: PitStatList, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_pitstats - Bad Token")
|
logger.warning(f"post_pitstats - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
all_stats = []
|
all_stats = []
|
||||||
@ -345,10 +347,11 @@ async def post_pitstats(s_list: PitStatList, token: str = Depends(oauth2_scheme)
|
|||||||
status_code=404, detail=f"Player ID {x.player_id} not found"
|
status_code=404, detail=f"Player ID {x.player_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
all_stats.append(PitchingStat(**x.model_dump()))
|
all_stats.append(PitchingStat(**x.dict()))
|
||||||
|
|
||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(all_stats, 15):
|
for batch in chunked(all_stats, 15):
|
||||||
PitchingStat.insert_many(batch).on_conflict_ignore().execute()
|
PitchingStat.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
|
||||||
|
db.close()
|
||||||
return f"Added {len(all_stats)} batting lines"
|
return f"Added {len(all_stats)} batting lines"
|
||||||
|
|||||||
@ -4,13 +4,9 @@ Thin HTTP layer using PlayerService for business logic.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from fastapi import APIRouter, Query, Response, Depends
|
from fastapi import APIRouter, Query, Response, Depends
|
||||||
from typing import Literal, Optional, List
|
from typing import Optional, List
|
||||||
|
|
||||||
from ..dependencies import (
|
from ..dependencies import oauth2_scheme, cache_result, handle_db_errors
|
||||||
oauth2_scheme,
|
|
||||||
cache_result,
|
|
||||||
handle_db_errors,
|
|
||||||
)
|
|
||||||
from ..services.base import BaseService
|
from ..services.base import BaseService
|
||||||
from ..services.player_service import PlayerService
|
from ..services.player_service import PlayerService
|
||||||
|
|
||||||
@ -27,7 +23,7 @@ async def get_players(
|
|||||||
pos: list = Query(default=None),
|
pos: list = Query(default=None),
|
||||||
strat_code: list = Query(default=None),
|
strat_code: list = Query(default=None),
|
||||||
is_injured: Optional[bool] = None,
|
is_injured: Optional[bool] = None,
|
||||||
sort: Optional[Literal["cost-asc", "cost-desc", "name-asc", "name-desc"]] = None,
|
sort: Optional[str] = None,
|
||||||
limit: Optional[int] = Query(
|
limit: Optional[int] = Query(
|
||||||
default=None, ge=1, description="Maximum number of results to return"
|
default=None, ge=1, description="Maximum number of results to return"
|
||||||
),
|
),
|
||||||
|
|||||||
@ -9,8 +9,6 @@ from ..dependencies import (
|
|||||||
valid_token,
|
valid_token,
|
||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -44,8 +42,6 @@ async def get_results(
|
|||||||
away_abbrev: list = Query(default=None),
|
away_abbrev: list = Query(default=None),
|
||||||
home_abbrev: list = Query(default=None),
|
home_abbrev: list = Query(default=None),
|
||||||
short_output: Optional[bool] = False,
|
short_output: Optional[bool] = False,
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
):
|
):
|
||||||
all_results = Result.select_season(season)
|
all_results = Result.select_season(season)
|
||||||
|
|
||||||
@ -78,13 +74,11 @@ async def get_results(
|
|||||||
if week_end is not None:
|
if week_end is not None:
|
||||||
all_results = all_results.where(Result.week <= week_end)
|
all_results = all_results.where(Result.week <= week_end)
|
||||||
|
|
||||||
total_count = all_results.count()
|
|
||||||
all_results = all_results.offset(offset).limit(limit)
|
|
||||||
|
|
||||||
return_results = {
|
return_results = {
|
||||||
"count": total_count,
|
"count": all_results.count(),
|
||||||
"results": [model_to_dict(x, recurse=not short_output) for x in all_results],
|
"results": [model_to_dict(x, recurse=not short_output) for x in all_results],
|
||||||
}
|
}
|
||||||
|
db.close()
|
||||||
return return_results
|
return return_results
|
||||||
|
|
||||||
|
|
||||||
@ -96,6 +90,7 @@ async def get_one_result(result_id: int, short_output: Optional[bool] = False):
|
|||||||
r_result = model_to_dict(this_result, recurse=not short_output)
|
r_result = model_to_dict(this_result, recurse=not short_output)
|
||||||
else:
|
else:
|
||||||
r_result = None
|
r_result = None
|
||||||
|
db.close()
|
||||||
return r_result
|
return r_result
|
||||||
|
|
||||||
|
|
||||||
@ -114,7 +109,7 @@ async def patch_result(
|
|||||||
token: str = Depends(oauth2_scheme),
|
token: str = Depends(oauth2_scheme),
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_player - Bad Token")
|
logger.warning(f"patch_player - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_result = Result.get_or_none(Result.id == result_id)
|
this_result = Result.get_or_none(Result.id == result_id)
|
||||||
@ -147,8 +142,10 @@ async def patch_result(
|
|||||||
|
|
||||||
if this_result.save() == 1:
|
if this_result.save() == 1:
|
||||||
r_result = model_to_dict(this_result)
|
r_result = model_to_dict(this_result)
|
||||||
|
db.close()
|
||||||
return r_result
|
return r_result
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch result {result_id}"
|
status_code=500, detail=f"Unable to patch result {result_id}"
|
||||||
)
|
)
|
||||||
@ -158,36 +155,26 @@ async def patch_result(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_results(result_list: ResultList, token: str = Depends(oauth2_scheme)):
|
async def post_results(result_list: ResultList, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_player - Bad Token")
|
logger.warning(f"patch_player - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
new_results = []
|
new_results = []
|
||||||
|
|
||||||
all_team_ids = list(
|
|
||||||
set(x.awayteam_id for x in result_list.results)
|
|
||||||
| set(x.hometeam_id for x in result_list.results)
|
|
||||||
)
|
|
||||||
found_team_ids = (
|
|
||||||
set(t.id for t in Team.select(Team.id).where(Team.id << all_team_ids))
|
|
||||||
if all_team_ids
|
|
||||||
else set()
|
|
||||||
)
|
|
||||||
|
|
||||||
for x in result_list.results:
|
for x in result_list.results:
|
||||||
if x.awayteam_id not in found_team_ids:
|
if Team.get_or_none(Team.id == x.awayteam_id) is None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Team ID {x.awayteam_id} not found"
|
status_code=404, detail=f"Team ID {x.awayteam_id} not found"
|
||||||
)
|
)
|
||||||
if x.hometeam_id not in found_team_ids:
|
if Team.get_or_none(Team.id == x.hometeam_id) is None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Team ID {x.hometeam_id} not found"
|
status_code=404, detail=f"Team ID {x.hometeam_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
new_results.append(x.model_dump())
|
new_results.append(x.dict())
|
||||||
|
|
||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_results, 15):
|
for batch in chunked(new_results, 15):
|
||||||
Result.insert_many(batch).on_conflict_ignore().execute()
|
Result.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
db.close()
|
||||||
|
|
||||||
return f"Inserted {len(new_results)} results"
|
return f"Inserted {len(new_results)} results"
|
||||||
|
|
||||||
@ -196,14 +183,16 @@ async def post_results(result_list: ResultList, token: str = Depends(oauth2_sche
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_result(result_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_result(result_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_result - Bad Token")
|
logger.warning(f"delete_result - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_result = Result.get_or_none(Result.id == result_id)
|
this_result = Result.get_or_none(Result.id == result_id)
|
||||||
if not this_result:
|
if not this_result:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"Result ID {result_id} not found")
|
raise HTTPException(status_code=404, detail=f"Result ID {result_id} not found")
|
||||||
|
|
||||||
count = this_result.delete_instance()
|
count = this_result.delete_instance()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Result {result_id} has been deleted"
|
return f"Result {result_id} has been deleted"
|
||||||
|
|||||||
@ -12,8 +12,6 @@ from ..dependencies import (
|
|||||||
valid_token,
|
valid_token,
|
||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -46,8 +44,6 @@ async def get_players(
|
|||||||
key_mlbam: list = Query(default=None),
|
key_mlbam: list = Query(default=None),
|
||||||
sort: Optional[str] = None,
|
sort: Optional[str] = None,
|
||||||
csv: Optional[bool] = False,
|
csv: Optional[bool] = False,
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
):
|
):
|
||||||
all_players = SbaPlayer.select()
|
all_players = SbaPlayer.select()
|
||||||
|
|
||||||
@ -102,15 +98,14 @@ async def get_players(
|
|||||||
|
|
||||||
if csv:
|
if csv:
|
||||||
return_val = query_to_csv(all_players)
|
return_val = query_to_csv(all_players)
|
||||||
|
db.close()
|
||||||
return Response(content=return_val, media_type="text/csv")
|
return Response(content=return_val, media_type="text/csv")
|
||||||
|
|
||||||
total_count = all_players.count()
|
|
||||||
all_players = all_players.offset(offset).limit(limit)
|
|
||||||
|
|
||||||
return_val = {
|
return_val = {
|
||||||
"count": total_count,
|
"count": all_players.count(),
|
||||||
"players": [model_to_dict(x) for x in all_players],
|
"players": [model_to_dict(x) for x in all_players],
|
||||||
}
|
}
|
||||||
|
db.close()
|
||||||
return return_val
|
return return_val
|
||||||
|
|
||||||
|
|
||||||
@ -119,11 +114,13 @@ async def get_players(
|
|||||||
async def get_one_player(player_id: int):
|
async def get_one_player(player_id: int):
|
||||||
this_player = SbaPlayer.get_or_none(SbaPlayer.id == player_id)
|
this_player = SbaPlayer.get_or_none(SbaPlayer.id == player_id)
|
||||||
if this_player is None:
|
if this_player is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"SbaPlayer id {player_id} not found"
|
status_code=404, detail=f"SbaPlayer id {player_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
r_data = model_to_dict(this_player)
|
r_data = model_to_dict(this_player)
|
||||||
|
db.close()
|
||||||
return r_data
|
return r_data
|
||||||
|
|
||||||
|
|
||||||
@ -140,7 +137,8 @@ async def patch_player(
|
|||||||
token: str = Depends(oauth2_scheme),
|
token: str = Depends(oauth2_scheme),
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logging.warning("Bad Token")
|
logging.warning(f"Bad Token: {token}")
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=401,
|
status_code=401,
|
||||||
detail="You are not authorized to patch mlb players. This event has been logged.",
|
detail="You are not authorized to patch mlb players. This event has been logged.",
|
||||||
@ -148,6 +146,7 @@ async def patch_player(
|
|||||||
|
|
||||||
this_player = SbaPlayer.get_or_none(SbaPlayer.id == player_id)
|
this_player = SbaPlayer.get_or_none(SbaPlayer.id == player_id)
|
||||||
if this_player is None:
|
if this_player is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"SbaPlayer id {player_id} not found"
|
status_code=404, detail=f"SbaPlayer id {player_id} not found"
|
||||||
)
|
)
|
||||||
@ -167,8 +166,10 @@ async def patch_player(
|
|||||||
|
|
||||||
if this_player.save() == 1:
|
if this_player.save() == 1:
|
||||||
return_val = model_to_dict(this_player)
|
return_val = model_to_dict(this_player)
|
||||||
|
db.close()
|
||||||
return return_val
|
return return_val
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=418,
|
status_code=418,
|
||||||
detail="Well slap my ass and call me a teapot; I could not save that player",
|
detail="Well slap my ass and call me a teapot; I could not save that player",
|
||||||
@ -179,7 +180,8 @@ async def patch_player(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_players(players: PlayerList, token: str = Depends(oauth2_scheme)):
|
async def post_players(players: PlayerList, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logging.warning("Bad Token")
|
logging.warning(f"Bad Token: {token}")
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=401,
|
status_code=401,
|
||||||
detail="You are not authorized to post mlb players. This event has been logged.",
|
detail="You are not authorized to post mlb players. This event has been logged.",
|
||||||
@ -198,6 +200,7 @@ async def post_players(players: PlayerList, token: str = Depends(oauth2_scheme))
|
|||||||
)
|
)
|
||||||
if dupes.count() > 0:
|
if dupes.count() > 0:
|
||||||
logger.error(f"Found a dupe for {x}")
|
logger.error(f"Found a dupe for {x}")
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail=f"{x.first_name} {x.last_name} has a key already in the database",
|
detail=f"{x.first_name} {x.last_name} has a key already in the database",
|
||||||
@ -208,6 +211,7 @@ async def post_players(players: PlayerList, token: str = Depends(oauth2_scheme))
|
|||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_players, 15):
|
for batch in chunked(new_players, 15):
|
||||||
SbaPlayer.insert_many(batch).on_conflict_ignore().execute()
|
SbaPlayer.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
db.close()
|
||||||
|
|
||||||
return f"Inserted {len(new_players)} new MLB players"
|
return f"Inserted {len(new_players)} new MLB players"
|
||||||
|
|
||||||
@ -216,7 +220,8 @@ async def post_players(players: PlayerList, token: str = Depends(oauth2_scheme))
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_one_player(player: SbaPlayerModel, token: str = Depends(oauth2_scheme)):
|
async def post_one_player(player: SbaPlayerModel, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logging.warning("Bad Token")
|
logging.warning(f"Bad Token: {token}")
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=401,
|
status_code=401,
|
||||||
detail="You are not authorized to post mlb players. This event has been logged.",
|
detail="You are not authorized to post mlb players. This event has been logged.",
|
||||||
@ -231,17 +236,20 @@ async def post_one_player(player: SbaPlayerModel, token: str = Depends(oauth2_sc
|
|||||||
logging.info(f"POST /SbaPlayers/one - dupes found:")
|
logging.info(f"POST /SbaPlayers/one - dupes found:")
|
||||||
for x in dupes:
|
for x in dupes:
|
||||||
logging.info(f"{x}")
|
logging.info(f"{x}")
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail=f"{player.first_name} {player.last_name} has a key already in the database",
|
detail=f"{player.first_name} {player.last_name} has a key already in the database",
|
||||||
)
|
)
|
||||||
|
|
||||||
new_player = SbaPlayer(**player.model_dump())
|
new_player = SbaPlayer(**player.dict())
|
||||||
saved = new_player.save()
|
saved = new_player.save()
|
||||||
if saved == 1:
|
if saved == 1:
|
||||||
return_val = model_to_dict(new_player)
|
return_val = model_to_dict(new_player)
|
||||||
|
db.close()
|
||||||
return return_val
|
return return_val
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=418,
|
status_code=418,
|
||||||
detail="Well slap my ass and call me a teapot; I could not save that player",
|
detail="Well slap my ass and call me a teapot; I could not save that player",
|
||||||
@ -252,7 +260,8 @@ async def post_one_player(player: SbaPlayerModel, token: str = Depends(oauth2_sc
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_player(player_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_player(player_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logging.warning("Bad Token")
|
logging.warning(f"Bad Token: {token}")
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=401,
|
status_code=401,
|
||||||
detail="You are not authorized to delete mlb players. This event has been logged.",
|
detail="You are not authorized to delete mlb players. This event has been logged.",
|
||||||
@ -260,11 +269,13 @@ async def delete_player(player_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
|
|
||||||
this_player = SbaPlayer.get_or_none(SbaPlayer.id == player_id)
|
this_player = SbaPlayer.get_or_none(SbaPlayer.id == player_id)
|
||||||
if this_player is None:
|
if this_player is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"SbaPlayer id {player_id} not found"
|
status_code=404, detail=f"SbaPlayer id {player_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
count = this_player.delete_instance()
|
count = this_player.delete_instance()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Player {player_id} has been deleted"
|
return f"Player {player_id} has been deleted"
|
||||||
|
|||||||
@ -9,8 +9,6 @@ from ..dependencies import (
|
|||||||
valid_token,
|
valid_token,
|
||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -40,8 +38,6 @@ async def get_schedules(
|
|||||||
week_start: Optional[int] = None,
|
week_start: Optional[int] = None,
|
||||||
week_end: Optional[int] = None,
|
week_end: Optional[int] = None,
|
||||||
short_output: Optional[bool] = True,
|
short_output: Optional[bool] = True,
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
):
|
):
|
||||||
all_sched = Schedule.select_season(season)
|
all_sched = Schedule.select_season(season)
|
||||||
|
|
||||||
@ -73,13 +69,11 @@ async def get_schedules(
|
|||||||
|
|
||||||
all_sched = all_sched.order_by(Schedule.id)
|
all_sched = all_sched.order_by(Schedule.id)
|
||||||
|
|
||||||
total_count = all_sched.count()
|
|
||||||
all_sched = all_sched.offset(offset).limit(limit)
|
|
||||||
|
|
||||||
return_sched = {
|
return_sched = {
|
||||||
"count": total_count,
|
"count": all_sched.count(),
|
||||||
"schedules": [model_to_dict(x, recurse=not short_output) for x in all_sched],
|
"schedules": [model_to_dict(x, recurse=not short_output) for x in all_sched],
|
||||||
}
|
}
|
||||||
|
db.close()
|
||||||
return return_sched
|
return return_sched
|
||||||
|
|
||||||
|
|
||||||
@ -91,6 +85,7 @@ async def get_one_schedule(schedule_id: int):
|
|||||||
r_sched = model_to_dict(this_sched)
|
r_sched = model_to_dict(this_sched)
|
||||||
else:
|
else:
|
||||||
r_sched = None
|
r_sched = None
|
||||||
|
db.close()
|
||||||
return r_sched
|
return r_sched
|
||||||
|
|
||||||
|
|
||||||
@ -106,7 +101,7 @@ async def patch_schedule(
|
|||||||
token: str = Depends(oauth2_scheme),
|
token: str = Depends(oauth2_scheme),
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_schedule - Bad Token")
|
logger.warning(f"patch_schedule - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_sched = Schedule.get_or_none(Schedule.id == schedule_id)
|
this_sched = Schedule.get_or_none(Schedule.id == schedule_id)
|
||||||
@ -132,8 +127,10 @@ async def patch_schedule(
|
|||||||
|
|
||||||
if this_sched.save() == 1:
|
if this_sched.save() == 1:
|
||||||
r_sched = model_to_dict(this_sched)
|
r_sched = model_to_dict(this_sched)
|
||||||
|
db.close()
|
||||||
return r_sched
|
return r_sched
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch schedule {schedule_id}"
|
status_code=500, detail=f"Unable to patch schedule {schedule_id}"
|
||||||
)
|
)
|
||||||
@ -143,36 +140,26 @@ async def patch_schedule(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_schedules(sched_list: ScheduleList, token: str = Depends(oauth2_scheme)):
|
async def post_schedules(sched_list: ScheduleList, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_schedules - Bad Token")
|
logger.warning(f"post_schedules - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
new_sched = []
|
new_sched = []
|
||||||
|
|
||||||
all_team_ids = list(
|
|
||||||
set(x.awayteam_id for x in sched_list.schedules)
|
|
||||||
| set(x.hometeam_id for x in sched_list.schedules)
|
|
||||||
)
|
|
||||||
found_team_ids = (
|
|
||||||
set(t.id for t in Team.select(Team.id).where(Team.id << all_team_ids))
|
|
||||||
if all_team_ids
|
|
||||||
else set()
|
|
||||||
)
|
|
||||||
|
|
||||||
for x in sched_list.schedules:
|
for x in sched_list.schedules:
|
||||||
if x.awayteam_id not in found_team_ids:
|
if Team.get_or_none(Team.id == x.awayteam_id) is None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Team ID {x.awayteam_id} not found"
|
status_code=404, detail=f"Team ID {x.awayteam_id} not found"
|
||||||
)
|
)
|
||||||
if x.hometeam_id not in found_team_ids:
|
if Team.get_or_none(Team.id == x.hometeam_id) is None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Team ID {x.hometeam_id} not found"
|
status_code=404, detail=f"Team ID {x.hometeam_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
new_sched.append(x.model_dump())
|
new_sched.append(x.dict())
|
||||||
|
|
||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_sched, 15):
|
for batch in chunked(new_sched, 15):
|
||||||
Schedule.insert_many(batch).on_conflict_ignore().execute()
|
Schedule.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
db.close()
|
||||||
|
|
||||||
return f"Inserted {len(new_sched)} schedules"
|
return f"Inserted {len(new_sched)} schedules"
|
||||||
|
|
||||||
@ -181,7 +168,7 @@ async def post_schedules(sched_list: ScheduleList, token: str = Depends(oauth2_s
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_schedule(schedule_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_schedule(schedule_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_schedule - Bad Token")
|
logger.warning(f"delete_schedule - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_sched = Schedule.get_or_none(Schedule.id == schedule_id)
|
this_sched = Schedule.get_or_none(Schedule.id == schedule_id)
|
||||||
@ -191,6 +178,7 @@ async def delete_schedule(schedule_id: int, token: str = Depends(oauth2_scheme))
|
|||||||
)
|
)
|
||||||
|
|
||||||
count = this_sched.delete_instance()
|
count = this_sched.delete_instance()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Schedule {this_sched} has been deleted"
|
return f"Schedule {this_sched} has been deleted"
|
||||||
|
|||||||
@ -1,33 +1,24 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
import logging
|
import logging
|
||||||
|
import pydantic
|
||||||
|
|
||||||
from ..db_engine import db, Standings, Team, Division, model_to_dict, chunked, fn
|
from ..db_engine import db, Standings, Team, Division, model_to_dict, chunked, fn
|
||||||
from ..dependencies import (
|
from ..dependencies import oauth2_scheme, valid_token, PRIVATE_IN_SCHEMA, handle_db_errors
|
||||||
oauth2_scheme,
|
|
||||||
valid_token,
|
logger = logging.getLogger('discord_app')
|
||||||
PRIVATE_IN_SCHEMA,
|
|
||||||
handle_db_errors,
|
router = APIRouter(
|
||||||
MAX_LIMIT,
|
prefix='/api/v3/standings',
|
||||||
DEFAULT_LIMIT,
|
tags=['standings']
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/v3/standings", tags=["standings"])
|
@router.get('')
|
||||||
|
|
||||||
|
|
||||||
@router.get("")
|
|
||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def get_standings(
|
async def get_standings(
|
||||||
season: int,
|
season: int, team_id: list = Query(default=None), league_abbrev: Optional[str] = None,
|
||||||
team_id: list = Query(default=None),
|
division_abbrev: Optional[str] = None, short_output: Optional[bool] = False):
|
||||||
league_abbrev: Optional[str] = None,
|
|
||||||
division_abbrev: Optional[str] = None,
|
|
||||||
short_output: Optional[bool] = False,
|
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
):
|
|
||||||
standings = Standings.select_season(season)
|
standings = Standings.select_season(season)
|
||||||
|
|
||||||
# if standings.count() == 0:
|
# if standings.count() == 0:
|
||||||
@ -39,67 +30,55 @@ async def get_standings(
|
|||||||
standings = standings.where(Standings.team << t_query)
|
standings = standings.where(Standings.team << t_query)
|
||||||
|
|
||||||
if league_abbrev is not None:
|
if league_abbrev is not None:
|
||||||
l_query = Division.select().where(
|
l_query = Division.select().where(fn.Lower(Division.league_abbrev) == league_abbrev.lower())
|
||||||
fn.Lower(Division.league_abbrev) == league_abbrev.lower()
|
|
||||||
)
|
|
||||||
standings = standings.where(Standings.team.division << l_query)
|
standings = standings.where(Standings.team.division << l_query)
|
||||||
|
|
||||||
if division_abbrev is not None:
|
if division_abbrev is not None:
|
||||||
d_query = Division.select().where(
|
d_query = Division.select().where(fn.Lower(Division.division_abbrev) == division_abbrev.lower())
|
||||||
fn.Lower(Division.division_abbrev) == division_abbrev.lower()
|
|
||||||
)
|
|
||||||
standings = standings.where(Standings.team.division << d_query)
|
standings = standings.where(Standings.team.division << d_query)
|
||||||
|
|
||||||
def win_pct(this_team_stan):
|
def win_pct(this_team_stan):
|
||||||
if this_team_stan.wins + this_team_stan.losses == 0:
|
if this_team_stan.wins + this_team_stan.losses == 0:
|
||||||
return 0
|
return 0
|
||||||
else:
|
else:
|
||||||
return (
|
return (this_team_stan.wins / (this_team_stan.wins + this_team_stan.losses)) + \
|
||||||
this_team_stan.wins / (this_team_stan.wins + this_team_stan.losses)
|
(this_team_stan.run_diff * .000001)
|
||||||
) + (this_team_stan.run_diff * 0.000001)
|
|
||||||
|
|
||||||
div_teams = [x for x in standings]
|
div_teams = [x for x in standings]
|
||||||
div_teams.sort(key=lambda team: win_pct(team), reverse=True)
|
div_teams.sort(key=lambda team: win_pct(team), reverse=True)
|
||||||
|
|
||||||
total_count = len(div_teams)
|
|
||||||
div_teams = div_teams[offset : offset + limit]
|
|
||||||
|
|
||||||
return_standings = {
|
return_standings = {
|
||||||
"count": total_count,
|
'count': len(div_teams),
|
||||||
"standings": [model_to_dict(x, recurse=not short_output) for x in div_teams],
|
'standings': [model_to_dict(x, recurse=not short_output) for x in div_teams]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
return return_standings
|
return return_standings
|
||||||
|
|
||||||
|
|
||||||
@router.get("/team/{team_id}")
|
@router.get('/team/{team_id}')
|
||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def get_team_standings(team_id: int):
|
async def get_team_standings(team_id: int):
|
||||||
this_stan = Standings.get_or_none(Standings.team_id == team_id)
|
this_stan = Standings.get_or_none(Standings.team_id == team_id)
|
||||||
if this_stan is None:
|
if this_stan is None:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=404, detail=f'No standings found for team id {team_id}')
|
||||||
status_code=404, detail=f"No standings found for team id {team_id}"
|
|
||||||
)
|
|
||||||
|
|
||||||
return model_to_dict(this_stan)
|
return model_to_dict(this_stan)
|
||||||
|
|
||||||
|
|
||||||
@router.patch("/{stan_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.patch('/{stan_id}', include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def patch_standings(
|
async def patch_standings(
|
||||||
stan_id: int,
|
stan_id, wins: Optional[int] = None, losses: Optional[int] = None, token: str = Depends(oauth2_scheme)):
|
||||||
wins: Optional[int] = None,
|
|
||||||
losses: Optional[int] = None,
|
|
||||||
token: str = Depends(oauth2_scheme),
|
|
||||||
):
|
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_standings - Bad Token")
|
logger.warning(f'patch_standings - Bad Token: {token}')
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
this_stan = Standings.get_by_id(stan_id)
|
this_stan = Standings.get_by_id(stan_id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=404, detail=f"No team found with id {stan_id}")
|
db.close()
|
||||||
|
raise HTTPException(status_code=404, detail=f'No team found with id {stan_id}')
|
||||||
|
|
||||||
if wins:
|
if wins:
|
||||||
this_stan.wins = wins
|
this_stan.wins = wins
|
||||||
@ -107,37 +86,40 @@ async def patch_standings(
|
|||||||
this_stan.losses = losses
|
this_stan.losses = losses
|
||||||
|
|
||||||
this_stan.save()
|
this_stan.save()
|
||||||
|
db.close()
|
||||||
|
|
||||||
return model_to_dict(this_stan)
|
return model_to_dict(this_stan)
|
||||||
|
|
||||||
|
|
||||||
@router.post("/s{season}/new", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.post('/s{season}/new', include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_standings(season: int, token: str = Depends(oauth2_scheme)):
|
async def post_standings(season: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_standings - Bad Token")
|
logger.warning(f'post_standings - Bad Token: {token}')
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||||
|
|
||||||
new_teams = []
|
new_teams = []
|
||||||
all_teams = Team.select().where(Team.season == season)
|
all_teams = Team.select().where(Team.season == season)
|
||||||
for x in all_teams:
|
for x in all_teams:
|
||||||
new_teams.append(Standings({"team_id": x.id}))
|
new_teams.append(Standings({'team_id': x.id}))
|
||||||
|
|
||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_teams, 16):
|
for batch in chunked(new_teams, 16):
|
||||||
Standings.insert_many(batch).on_conflict_ignore().execute()
|
Standings.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
db.close()
|
||||||
|
|
||||||
return f"Inserted {len(new_teams)} standings"
|
return f'Inserted {len(new_teams)} standings'
|
||||||
|
|
||||||
|
|
||||||
@router.post("/s{season}/recalculate", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.post('/s{season}/recalculate', include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def recalculate_standings(season: int, token: str = Depends(oauth2_scheme)):
|
async def recalculate_standings(season: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("recalculate_standings - Bad Token")
|
logger.warning(f'recalculate_standings - Bad Token: {token}')
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||||
|
|
||||||
code = Standings.recalculate(season)
|
code = Standings.recalculate(season)
|
||||||
|
db.close()
|
||||||
if code == 69:
|
if code == 69:
|
||||||
raise HTTPException(status_code=500, detail=f"Error recreating Standings rows")
|
raise HTTPException(status_code=500, detail=f'Error recreating Standings rows')
|
||||||
return f"Just recalculated standings for season {season}"
|
return f'Just recalculated standings for season {season}'
|
||||||
|
|||||||
@ -13,7 +13,6 @@ from ..dependencies import (
|
|||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
update_season_batting_stats,
|
update_season_batting_stats,
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -60,8 +59,6 @@ async def get_games(
|
|||||||
division_id: Optional[int] = None,
|
division_id: Optional[int] = None,
|
||||||
short_output: Optional[bool] = False,
|
short_output: Optional[bool] = False,
|
||||||
sort: Optional[str] = None,
|
sort: Optional[str] = None,
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=1000),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
) -> Any:
|
) -> Any:
|
||||||
all_games = StratGame.select()
|
all_games = StratGame.select()
|
||||||
|
|
||||||
@ -122,13 +119,11 @@ async def get_games(
|
|||||||
StratGame.season, StratGame.week, StratGame.game_num
|
StratGame.season, StratGame.week, StratGame.game_num
|
||||||
)
|
)
|
||||||
|
|
||||||
total_count = all_games.count()
|
|
||||||
all_games = all_games.offset(offset).limit(limit)
|
|
||||||
|
|
||||||
return_games = {
|
return_games = {
|
||||||
"count": total_count,
|
"count": all_games.count(),
|
||||||
"games": [model_to_dict(x, recurse=not short_output) for x in all_games],
|
"games": [model_to_dict(x, recurse=not short_output) for x in all_games],
|
||||||
}
|
}
|
||||||
|
db.close()
|
||||||
return return_games
|
return return_games
|
||||||
|
|
||||||
|
|
||||||
@ -137,9 +132,11 @@ async def get_games(
|
|||||||
async def get_one_game(game_id: int) -> Any:
|
async def get_one_game(game_id: int) -> Any:
|
||||||
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
||||||
if not this_game:
|
if not this_game:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
||||||
|
|
||||||
g_result = model_to_dict(this_game)
|
g_result = model_to_dict(this_game)
|
||||||
|
db.close()
|
||||||
return g_result
|
return g_result
|
||||||
|
|
||||||
|
|
||||||
@ -156,11 +153,12 @@ async def patch_game(
|
|||||||
scorecard_url: Optional[str] = None,
|
scorecard_url: Optional[str] = None,
|
||||||
) -> Any:
|
) -> Any:
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_game - Bad Token")
|
logger.warning(f"patch_game - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
||||||
if not this_game:
|
if not this_game:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
||||||
|
|
||||||
if game_num is not None:
|
if game_num is not None:
|
||||||
@ -236,7 +234,7 @@ async def patch_game(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_games(game_list: GameList, token: str = Depends(oauth2_scheme)) -> Any:
|
async def post_games(game_list: GameList, token: str = Depends(oauth2_scheme)) -> Any:
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_games - Bad Token")
|
logger.warning(f"post_games - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
new_games = []
|
new_games = []
|
||||||
@ -250,11 +248,12 @@ async def post_games(game_list: GameList, token: str = Depends(oauth2_scheme)) -
|
|||||||
status_code=404, detail=f"Team ID {x.home_team_id} not found"
|
status_code=404, detail=f"Team ID {x.home_team_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
new_games.append(x.model_dump())
|
new_games.append(x.dict())
|
||||||
|
|
||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_games, 16):
|
for batch in chunked(new_games, 16):
|
||||||
StratGame.insert_many(batch).on_conflict_ignore().execute()
|
StratGame.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
db.close()
|
||||||
|
|
||||||
return f"Inserted {len(new_games)} games"
|
return f"Inserted {len(new_games)} games"
|
||||||
|
|
||||||
@ -263,11 +262,12 @@ async def post_games(game_list: GameList, token: str = Depends(oauth2_scheme)) -
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def wipe_game(game_id: int, token: str = Depends(oauth2_scheme)) -> Any:
|
async def wipe_game(game_id: int, token: str = Depends(oauth2_scheme)) -> Any:
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("wipe_game - Bad Token")
|
logger.warning(f"wipe_game - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
||||||
if not this_game:
|
if not this_game:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
||||||
|
|
||||||
this_game.away_score = None
|
this_game.away_score = None
|
||||||
@ -278,8 +278,10 @@ async def wipe_game(game_id: int, token: str = Depends(oauth2_scheme)) -> Any:
|
|||||||
|
|
||||||
if this_game.save() == 1:
|
if this_game.save() == 1:
|
||||||
g_result = model_to_dict(this_game)
|
g_result = model_to_dict(this_game)
|
||||||
|
db.close()
|
||||||
return g_result
|
return g_result
|
||||||
else:
|
else:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=500, detail=f"Unable to wipe game {game_id}")
|
raise HTTPException(status_code=500, detail=f"Unable to wipe game {game_id}")
|
||||||
|
|
||||||
|
|
||||||
@ -287,14 +289,16 @@ async def wipe_game(game_id: int, token: str = Depends(oauth2_scheme)) -> Any:
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_game(game_id: int, token: str = Depends(oauth2_scheme)) -> Any:
|
async def delete_game(game_id: int, token: str = Depends(oauth2_scheme)) -> Any:
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_game - Bad Token")
|
logger.warning(f"delete_game - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
||||||
if not this_game:
|
if not this_game:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
||||||
|
|
||||||
count = this_game.delete_instance()
|
count = this_game.delete_instance()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"StratGame {game_id} has been deleted"
|
return f"StratGame {game_id} has been deleted"
|
||||||
|
|||||||
@ -13,13 +13,7 @@ from ...db_engine import (
|
|||||||
fn,
|
fn,
|
||||||
model_to_dict,
|
model_to_dict,
|
||||||
)
|
)
|
||||||
from ...dependencies import (
|
from ...dependencies import add_cache_headers, cache_result, handle_db_errors
|
||||||
add_cache_headers,
|
|
||||||
cache_result,
|
|
||||||
handle_db_errors,
|
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
|
||||||
from .common import build_season_games
|
from .common import build_season_games
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
@ -58,7 +52,7 @@ async def get_batting_totals(
|
|||||||
risp: Optional[bool] = None,
|
risp: Optional[bool] = None,
|
||||||
inning: list = Query(default=None),
|
inning: list = Query(default=None),
|
||||||
sort: Optional[str] = None,
|
sort: Optional[str] = None,
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
limit: Optional[int] = 200,
|
||||||
short_output: Optional[bool] = False,
|
short_output: Optional[bool] = False,
|
||||||
page_num: Optional[int] = 1,
|
page_num: Optional[int] = 1,
|
||||||
week_start: Optional[int] = None,
|
week_start: Optional[int] = None,
|
||||||
@ -429,6 +423,8 @@ async def get_batting_totals(
|
|||||||
run_plays = run_plays.order_by(StratPlay.game.asc())
|
run_plays = run_plays.order_by(StratPlay.game.asc())
|
||||||
# For other group_by values, skip game_id/play_num sorting since they're not in GROUP BY
|
# For other group_by values, skip game_id/play_num sorting since they're not in GROUP BY
|
||||||
|
|
||||||
|
if limit < 1:
|
||||||
|
limit = 1
|
||||||
bat_plays = bat_plays.paginate(page_num, limit)
|
bat_plays = bat_plays.paginate(page_num, limit)
|
||||||
|
|
||||||
logger.info(f"bat_plays query: {bat_plays}")
|
logger.info(f"bat_plays query: {bat_plays}")
|
||||||
@ -598,4 +594,5 @@ async def get_batting_totals(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
db.close()
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|||||||
@ -20,8 +20,10 @@ logger = logging.getLogger("discord_app")
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def get_one_play(play_id: int):
|
async def get_one_play(play_id: int):
|
||||||
if StratPlay.get_or_none(StratPlay.id == play_id) is None:
|
if StratPlay.get_or_none(StratPlay.id == play_id) is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"Play ID {play_id} not found")
|
raise HTTPException(status_code=404, detail=f"Play ID {play_id} not found")
|
||||||
r_play = model_to_dict(StratPlay.get_by_id(play_id))
|
r_play = model_to_dict(StratPlay.get_by_id(play_id))
|
||||||
|
db.close()
|
||||||
return r_play
|
return r_play
|
||||||
|
|
||||||
|
|
||||||
@ -31,14 +33,16 @@ async def patch_play(
|
|||||||
play_id: int, new_play: PlayModel, token: str = Depends(oauth2_scheme)
|
play_id: int, new_play: PlayModel, token: str = Depends(oauth2_scheme)
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_play - Bad Token")
|
logger.warning(f"patch_play - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
if StratPlay.get_or_none(StratPlay.id == play_id) is None:
|
if StratPlay.get_or_none(StratPlay.id == play_id) is None:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"Play ID {play_id} not found")
|
raise HTTPException(status_code=404, detail=f"Play ID {play_id} not found")
|
||||||
|
|
||||||
StratPlay.update(**new_play.model_dump()).where(StratPlay.id == play_id).execute()
|
StratPlay.update(**new_play.dict()).where(StratPlay.id == play_id).execute()
|
||||||
r_play = model_to_dict(StratPlay.get_by_id(play_id))
|
r_play = model_to_dict(StratPlay.get_by_id(play_id))
|
||||||
|
db.close()
|
||||||
return r_play
|
return r_play
|
||||||
|
|
||||||
|
|
||||||
@ -46,7 +50,7 @@ async def patch_play(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_plays(p_list: PlayList, token: str = Depends(oauth2_scheme)):
|
async def post_plays(p_list: PlayList, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_plays - Bad Token")
|
logger.warning(f"post_plays - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
new_plays = []
|
new_plays = []
|
||||||
@ -84,11 +88,12 @@ async def post_plays(p_list: PlayList, token: str = Depends(oauth2_scheme)):
|
|||||||
if this_play.pa == 0:
|
if this_play.pa == 0:
|
||||||
this_play.batter_final = None
|
this_play.batter_final = None
|
||||||
|
|
||||||
new_plays.append(this_play.model_dump())
|
new_plays.append(this_play.dict())
|
||||||
|
|
||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_plays, 20):
|
for batch in chunked(new_plays, 20):
|
||||||
StratPlay.insert_many(batch).on_conflict_ignore().execute()
|
StratPlay.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
db.close()
|
||||||
|
|
||||||
return f"Inserted {len(new_plays)} plays"
|
return f"Inserted {len(new_plays)} plays"
|
||||||
|
|
||||||
@ -97,14 +102,16 @@ async def post_plays(p_list: PlayList, token: str = Depends(oauth2_scheme)):
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_play(play_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_play(play_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_play - Bad Token")
|
logger.warning(f"delete_play - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_play = StratPlay.get_or_none(StratPlay.id == play_id)
|
this_play = StratPlay.get_or_none(StratPlay.id == play_id)
|
||||||
if not this_play:
|
if not this_play:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"Play ID {play_id} not found")
|
raise HTTPException(status_code=404, detail=f"Play ID {play_id} not found")
|
||||||
|
|
||||||
count = this_play.delete_instance()
|
count = this_play.delete_instance()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Play {play_id} has been deleted"
|
return f"Play {play_id} has been deleted"
|
||||||
@ -118,14 +125,16 @@ async def delete_play(play_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_plays_game(game_id: int, token: str = Depends(oauth2_scheme)):
|
async def delete_plays_game(game_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_plays_game - Bad Token")
|
logger.warning(f"delete_plays_game - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
||||||
if not this_game:
|
if not this_game:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"Game ID {game_id} not found")
|
raise HTTPException(status_code=404, detail=f"Game ID {game_id} not found")
|
||||||
|
|
||||||
count = StratPlay.delete().where(StratPlay.game == this_game).execute()
|
count = StratPlay.delete().where(StratPlay.game == this_game).execute()
|
||||||
|
db.close()
|
||||||
|
|
||||||
if count > 0:
|
if count > 0:
|
||||||
return f"Deleted {count} plays matching Game ID {game_id}"
|
return f"Deleted {count} plays matching Game ID {game_id}"
|
||||||
@ -139,11 +148,12 @@ async def delete_plays_game(game_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def post_erun_check(token: str = Depends(oauth2_scheme)):
|
async def post_erun_check(token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_erun_check - Bad Token")
|
logger.warning(f"post_erun_check - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
all_plays = StratPlay.update(run=1).where(
|
all_plays = StratPlay.update(run=1).where(
|
||||||
(StratPlay.e_run == 1) & (StratPlay.run == 0)
|
(StratPlay.e_run == 1) & (StratPlay.run == 0)
|
||||||
)
|
)
|
||||||
count = all_plays.execute()
|
count = all_plays.execute()
|
||||||
|
db.close()
|
||||||
return count
|
return count
|
||||||
|
|||||||
@ -13,13 +13,7 @@ from ...db_engine import (
|
|||||||
fn,
|
fn,
|
||||||
SQL,
|
SQL,
|
||||||
)
|
)
|
||||||
from ...dependencies import (
|
from ...dependencies import handle_db_errors, add_cache_headers, cache_result
|
||||||
handle_db_errors,
|
|
||||||
add_cache_headers,
|
|
||||||
cache_result,
|
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
|
||||||
from .common import build_season_games
|
from .common import build_season_games
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -57,7 +51,7 @@ async def get_fielding_totals(
|
|||||||
team_id: list = Query(default=None),
|
team_id: list = Query(default=None),
|
||||||
manager_id: list = Query(default=None),
|
manager_id: list = Query(default=None),
|
||||||
sort: Optional[str] = None,
|
sort: Optional[str] = None,
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
limit: Optional[int] = 200,
|
||||||
short_output: Optional[bool] = False,
|
short_output: Optional[bool] = False,
|
||||||
page_num: Optional[int] = 1,
|
page_num: Optional[int] = 1,
|
||||||
):
|
):
|
||||||
@ -243,6 +237,8 @@ async def get_fielding_totals(
|
|||||||
def_plays = def_plays.order_by(StratPlay.game.asc())
|
def_plays = def_plays.order_by(StratPlay.game.asc())
|
||||||
# For other group_by values, skip game_id/play_num sorting since they're not in GROUP BY
|
# For other group_by values, skip game_id/play_num sorting since they're not in GROUP BY
|
||||||
|
|
||||||
|
if limit < 1:
|
||||||
|
limit = 1
|
||||||
def_plays = def_plays.paginate(page_num, limit)
|
def_plays = def_plays.paginate(page_num, limit)
|
||||||
|
|
||||||
logger.info(f"def_plays query: {def_plays}")
|
logger.info(f"def_plays query: {def_plays}")
|
||||||
@ -365,4 +361,5 @@ async def get_fielding_totals(
|
|||||||
"week": this_week,
|
"week": this_week,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
db.close()
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|||||||
@ -16,13 +16,7 @@ from ...db_engine import (
|
|||||||
SQL,
|
SQL,
|
||||||
complex_data_to_csv,
|
complex_data_to_csv,
|
||||||
)
|
)
|
||||||
from ...dependencies import (
|
from ...dependencies import handle_db_errors, add_cache_headers, cache_result
|
||||||
handle_db_errors,
|
|
||||||
add_cache_headers,
|
|
||||||
cache_result,
|
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
|
||||||
from .common import build_season_games
|
from .common import build_season_games
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
@ -57,7 +51,7 @@ async def get_pitching_totals(
|
|||||||
risp: Optional[bool] = None,
|
risp: Optional[bool] = None,
|
||||||
inning: list = Query(default=None),
|
inning: list = Query(default=None),
|
||||||
sort: Optional[str] = None,
|
sort: Optional[str] = None,
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
limit: Optional[int] = 200,
|
||||||
short_output: Optional[bool] = False,
|
short_output: Optional[bool] = False,
|
||||||
csv: Optional[bool] = False,
|
csv: Optional[bool] = False,
|
||||||
page_num: Optional[int] = 1,
|
page_num: Optional[int] = 1,
|
||||||
@ -170,6 +164,8 @@ async def get_pitching_totals(
|
|||||||
if group_by in ["playergame", "teamgame"]:
|
if group_by in ["playergame", "teamgame"]:
|
||||||
pitch_plays = pitch_plays.order_by(StratPlay.game.asc())
|
pitch_plays = pitch_plays.order_by(StratPlay.game.asc())
|
||||||
|
|
||||||
|
if limit < 1:
|
||||||
|
limit = 1
|
||||||
pitch_plays = pitch_plays.paginate(page_num, limit)
|
pitch_plays = pitch_plays.paginate(page_num, limit)
|
||||||
|
|
||||||
# Execute the Peewee query
|
# Execute the Peewee query
|
||||||
@ -352,6 +348,7 @@ async def get_pitching_totals(
|
|||||||
)
|
)
|
||||||
|
|
||||||
return_stats["count"] = len(return_stats["stats"])
|
return_stats["count"] = len(return_stats["stats"])
|
||||||
|
db.close()
|
||||||
if csv:
|
if csv:
|
||||||
return Response(
|
return Response(
|
||||||
content=complex_data_to_csv(return_stats["stats"]), media_type="text/csv"
|
content=complex_data_to_csv(return_stats["stats"]), media_type="text/csv"
|
||||||
|
|||||||
@ -16,8 +16,6 @@ from ...dependencies import (
|
|||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
add_cache_headers,
|
add_cache_headers,
|
||||||
cache_result,
|
cache_result,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -72,7 +70,7 @@ async def get_plays(
|
|||||||
pitcher_team_id: list = Query(default=None),
|
pitcher_team_id: list = Query(default=None),
|
||||||
short_output: Optional[bool] = False,
|
short_output: Optional[bool] = False,
|
||||||
sort: Optional[str] = None,
|
sort: Optional[str] = None,
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
limit: Optional[int] = 200,
|
||||||
page_num: Optional[int] = 1,
|
page_num: Optional[int] = 1,
|
||||||
s_type: Literal["regular", "post", "total", None] = None,
|
s_type: Literal["regular", "post", "total", None] = None,
|
||||||
):
|
):
|
||||||
@ -187,6 +185,8 @@ async def get_plays(
|
|||||||
season_games = season_games.where(StratGame.week > 18)
|
season_games = season_games.where(StratGame.week > 18)
|
||||||
all_plays = all_plays.where(StratPlay.game << season_games)
|
all_plays = all_plays.where(StratPlay.game << season_games)
|
||||||
|
|
||||||
|
if limit < 1:
|
||||||
|
limit = 1
|
||||||
bat_plays = all_plays.paginate(page_num, limit)
|
bat_plays = all_plays.paginate(page_num, limit)
|
||||||
|
|
||||||
if sort == "wpa-desc":
|
if sort == "wpa-desc":
|
||||||
@ -210,4 +210,5 @@ async def get_plays(
|
|||||||
"count": all_plays.count(),
|
"count": all_plays.count(),
|
||||||
"plays": [model_to_dict(x, recurse=not short_output) for x in all_plays],
|
"plays": [model_to_dict(x, recurse=not short_output) for x in all_plays],
|
||||||
}
|
}
|
||||||
|
db.close()
|
||||||
return return_plays
|
return return_plays
|
||||||
|
|||||||
@ -11,8 +11,6 @@ from ..dependencies import (
|
|||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
cache_result,
|
cache_result,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
from ..services.base import BaseService
|
from ..services.base import BaseService
|
||||||
from ..services.team_service import TeamService
|
from ..services.team_service import TeamService
|
||||||
|
|||||||
@ -10,8 +10,6 @@ from ..dependencies import (
|
|||||||
valid_token,
|
valid_token,
|
||||||
PRIVATE_IN_SCHEMA,
|
PRIVATE_IN_SCHEMA,
|
||||||
handle_db_errors,
|
handle_db_errors,
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
logger = logging.getLogger("discord_app")
|
||||||
@ -38,7 +36,7 @@ class TransactionList(pydantic.BaseModel):
|
|||||||
@router.get("")
|
@router.get("")
|
||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def get_transactions(
|
async def get_transactions(
|
||||||
season: int,
|
season,
|
||||||
team_abbrev: list = Query(default=None),
|
team_abbrev: list = Query(default=None),
|
||||||
week_start: Optional[int] = 0,
|
week_start: Optional[int] = 0,
|
||||||
week_end: Optional[int] = None,
|
week_end: Optional[int] = None,
|
||||||
@ -47,9 +45,8 @@ async def get_transactions(
|
|||||||
player_name: list = Query(default=None),
|
player_name: list = Query(default=None),
|
||||||
player_id: list = Query(default=None),
|
player_id: list = Query(default=None),
|
||||||
move_id: Optional[str] = None,
|
move_id: Optional[str] = None,
|
||||||
|
is_trade: Optional[bool] = None,
|
||||||
short_output: Optional[bool] = False,
|
short_output: Optional[bool] = False,
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = Query(default=0, ge=0),
|
|
||||||
):
|
):
|
||||||
if season:
|
if season:
|
||||||
transactions = Transaction.select_season(season)
|
transactions = Transaction.select_season(season)
|
||||||
@ -78,29 +75,30 @@ async def get_transactions(
|
|||||||
transactions = transactions.where(Transaction.player << these_players)
|
transactions = transactions.where(Transaction.player << these_players)
|
||||||
|
|
||||||
if cancelled:
|
if cancelled:
|
||||||
transactions = transactions.where(Transaction.cancelled == True)
|
transactions = transactions.where(Transaction.cancelled == 1)
|
||||||
else:
|
else:
|
||||||
transactions = transactions.where(Transaction.cancelled == False)
|
transactions = transactions.where(Transaction.cancelled == 0)
|
||||||
|
|
||||||
if frozen:
|
if frozen:
|
||||||
transactions = transactions.where(Transaction.frozen == True)
|
transactions = transactions.where(Transaction.frozen == 1)
|
||||||
else:
|
else:
|
||||||
transactions = transactions.where(Transaction.frozen == False)
|
transactions = transactions.where(Transaction.frozen == 0)
|
||||||
|
|
||||||
|
if is_trade is not None:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=501, detail="The is_trade parameter is not implemented, yet"
|
||||||
|
)
|
||||||
|
|
||||||
transactions = transactions.order_by(-Transaction.week, Transaction.moveid)
|
transactions = transactions.order_by(-Transaction.week, Transaction.moveid)
|
||||||
|
|
||||||
total_count = transactions.count()
|
|
||||||
transactions = transactions.offset(offset).limit(limit)
|
|
||||||
|
|
||||||
return_trans = {
|
return_trans = {
|
||||||
"count": total_count,
|
"count": transactions.count(),
|
||||||
"limit": limit,
|
|
||||||
"offset": offset,
|
|
||||||
"transactions": [
|
"transactions": [
|
||||||
model_to_dict(x, recurse=not short_output) for x in transactions
|
model_to_dict(x, recurse=not short_output) for x in transactions
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
return return_trans
|
return return_trans
|
||||||
|
|
||||||
|
|
||||||
@ -113,11 +111,12 @@ async def patch_transactions(
|
|||||||
cancelled: Optional[bool] = None,
|
cancelled: Optional[bool] = None,
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("patch_transactions - Bad Token")
|
logger.warning(f"patch_transactions - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
these_moves = Transaction.select().where(Transaction.moveid == move_id)
|
these_moves = Transaction.select().where(Transaction.moveid == move_id)
|
||||||
if these_moves.count() == 0:
|
if these_moves.count() == 0:
|
||||||
|
db.close()
|
||||||
raise HTTPException(status_code=404, detail=f"Move ID {move_id} not found")
|
raise HTTPException(status_code=404, detail=f"Move ID {move_id} not found")
|
||||||
|
|
||||||
if frozen is not None:
|
if frozen is not None:
|
||||||
@ -129,6 +128,7 @@ async def patch_transactions(
|
|||||||
x.cancelled = cancelled
|
x.cancelled = cancelled
|
||||||
x.save()
|
x.save()
|
||||||
|
|
||||||
|
db.close()
|
||||||
return f"Updated {these_moves.count()} transactions"
|
return f"Updated {these_moves.count()} transactions"
|
||||||
|
|
||||||
|
|
||||||
@ -138,46 +138,32 @@ async def post_transactions(
|
|||||||
moves: TransactionList, token: str = Depends(oauth2_scheme)
|
moves: TransactionList, token: str = Depends(oauth2_scheme)
|
||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("post_transactions - Bad Token")
|
logger.warning(f"post_transactions - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
all_moves = []
|
all_moves = []
|
||||||
|
|
||||||
all_team_ids = list(
|
|
||||||
set(x.oldteam_id for x in moves.moves) | set(x.newteam_id for x in moves.moves)
|
|
||||||
)
|
|
||||||
all_player_ids = list(set(x.player_id for x in moves.moves))
|
|
||||||
found_team_ids = (
|
|
||||||
set(t.id for t in Team.select(Team.id).where(Team.id << all_team_ids))
|
|
||||||
if all_team_ids
|
|
||||||
else set()
|
|
||||||
)
|
|
||||||
found_player_ids = (
|
|
||||||
set(p.id for p in Player.select(Player.id).where(Player.id << all_player_ids))
|
|
||||||
if all_player_ids
|
|
||||||
else set()
|
|
||||||
)
|
|
||||||
|
|
||||||
for x in moves.moves:
|
for x in moves.moves:
|
||||||
if x.oldteam_id not in found_team_ids:
|
if Team.get_or_none(Team.id == x.oldteam_id) is None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Team ID {x.oldteam_id} not found"
|
status_code=404, detail=f"Team ID {x.oldteam_id} not found"
|
||||||
)
|
)
|
||||||
if x.newteam_id not in found_team_ids:
|
if Team.get_or_none(Team.id == x.newteam_id) is None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Team ID {x.newteam_id} not found"
|
status_code=404, detail=f"Team ID {x.newteam_id} not found"
|
||||||
)
|
)
|
||||||
if x.player_id not in found_player_ids:
|
if Player.get_or_none(Player.id == x.player_id) is None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Player ID {x.player_id} not found"
|
status_code=404, detail=f"Player ID {x.player_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
all_moves.append(x.model_dump())
|
all_moves.append(x.dict())
|
||||||
|
|
||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(all_moves, 15):
|
for batch in chunked(all_moves, 15):
|
||||||
Transaction.insert_many(batch).on_conflict_ignore().execute()
|
Transaction.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
|
||||||
|
db.close()
|
||||||
return f"{len(all_moves)} transactions have been added"
|
return f"{len(all_moves)} transactions have been added"
|
||||||
|
|
||||||
|
|
||||||
@ -185,12 +171,13 @@ async def post_transactions(
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def delete_transactions(move_id, token: str = Depends(oauth2_scheme)):
|
async def delete_transactions(move_id, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("delete_transactions - Bad Token")
|
logger.warning(f"delete_transactions - Bad Token: {token}")
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
delete_query = Transaction.delete().where(Transaction.moveid == move_id)
|
delete_query = Transaction.delete().where(Transaction.moveid == move_id)
|
||||||
|
|
||||||
count = delete_query.execute()
|
count = delete_query.execute()
|
||||||
|
db.close()
|
||||||
if count > 0:
|
if count > 0:
|
||||||
return f"Removed {count} transactions"
|
return f"Removed {count} transactions"
|
||||||
else:
|
else:
|
||||||
|
|||||||
@ -3,282 +3,187 @@ from typing import List, Literal, Optional
|
|||||||
import logging
|
import logging
|
||||||
import pydantic
|
import pydantic
|
||||||
|
|
||||||
from ..db_engine import (
|
from ..db_engine import SeasonBattingStats, SeasonPitchingStats, db, Manager, Team, Current, model_to_dict, fn, query_to_csv, StratPlay, StratGame
|
||||||
SeasonBattingStats,
|
from ..dependencies import add_cache_headers, cache_result, oauth2_scheme, valid_token, PRIVATE_IN_SCHEMA, handle_db_errors, update_season_batting_stats, update_season_pitching_stats, get_cache_stats
|
||||||
SeasonPitchingStats,
|
|
||||||
db,
|
logger = logging.getLogger('discord_app')
|
||||||
Manager,
|
|
||||||
Team,
|
router = APIRouter(
|
||||||
Current,
|
prefix='/api/v3/views',
|
||||||
model_to_dict,
|
tags=['views']
|
||||||
fn,
|
|
||||||
query_to_csv,
|
|
||||||
StratPlay,
|
|
||||||
StratGame,
|
|
||||||
)
|
|
||||||
from ..dependencies import (
|
|
||||||
add_cache_headers,
|
|
||||||
cache_result,
|
|
||||||
oauth2_scheme,
|
|
||||||
valid_token,
|
|
||||||
PRIVATE_IN_SCHEMA,
|
|
||||||
handle_db_errors,
|
|
||||||
update_season_batting_stats,
|
|
||||||
update_season_pitching_stats,
|
|
||||||
get_cache_stats,
|
|
||||||
MAX_LIMIT,
|
|
||||||
DEFAULT_LIMIT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger("discord_app")
|
@router.get('/season-stats/batting')
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/v3/views", tags=["views"])
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/season-stats/batting")
|
|
||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
@add_cache_headers(max_age=10 * 60)
|
@add_cache_headers(max_age=10*60)
|
||||||
@cache_result(ttl=5 * 60, key_prefix="season-batting")
|
@cache_result(ttl=5*60, key_prefix='season-batting')
|
||||||
async def get_season_batting_stats(
|
async def get_season_batting_stats(
|
||||||
season: Optional[int] = None,
|
season: Optional[int] = None,
|
||||||
team_id: Optional[int] = None,
|
team_id: Optional[int] = None,
|
||||||
player_id: Optional[int] = None,
|
player_id: Optional[int] = None,
|
||||||
sbaplayer_id: Optional[int] = None,
|
sbaplayer_id: Optional[int] = None,
|
||||||
min_pa: Optional[int] = None, # Minimum plate appearances
|
min_pa: Optional[int] = None, # Minimum plate appearances
|
||||||
sort_by: Literal[
|
sort_by: str = "woba", # Default sort field
|
||||||
"pa",
|
sort_order: Literal['asc', 'desc'] = 'desc', # asc or desc
|
||||||
"ab",
|
limit: Optional[int] = 200,
|
||||||
"run",
|
|
||||||
"hit",
|
|
||||||
"double",
|
|
||||||
"triple",
|
|
||||||
"homerun",
|
|
||||||
"rbi",
|
|
||||||
"bb",
|
|
||||||
"so",
|
|
||||||
"bphr",
|
|
||||||
"bpfo",
|
|
||||||
"bp1b",
|
|
||||||
"bplo",
|
|
||||||
"gidp",
|
|
||||||
"hbp",
|
|
||||||
"sac",
|
|
||||||
"ibb",
|
|
||||||
"avg",
|
|
||||||
"obp",
|
|
||||||
"slg",
|
|
||||||
"ops",
|
|
||||||
"woba",
|
|
||||||
"k_pct",
|
|
||||||
"sb",
|
|
||||||
"cs",
|
|
||||||
] = "woba", # Sort field
|
|
||||||
sort_order: Literal["asc", "desc"] = "desc", # asc or desc
|
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = 0,
|
offset: int = 0,
|
||||||
csv: Optional[bool] = False,
|
csv: Optional[bool] = False
|
||||||
):
|
):
|
||||||
logger.info(
|
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}')
|
||||||
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
|
# Use the enhanced get_top_hitters method
|
||||||
query = SeasonBattingStats.get_top_hitters(
|
query = SeasonBattingStats.get_top_hitters(
|
||||||
season=season,
|
season=season,
|
||||||
stat=sort_by,
|
stat=sort_by,
|
||||||
limit=limit if limit != 0 else None,
|
limit=limit if limit != 0 else None,
|
||||||
desc=(sort_order.lower() == "desc"),
|
desc=(sort_order.lower() == 'desc'),
|
||||||
team_id=team_id,
|
team_id=team_id,
|
||||||
player_id=player_id,
|
player_id=player_id,
|
||||||
sbaplayer_id=sbaplayer_id,
|
sbaplayer_id=sbaplayer_id,
|
||||||
min_pa=min_pa,
|
min_pa=min_pa,
|
||||||
offset=offset,
|
offset=offset
|
||||||
)
|
)
|
||||||
|
|
||||||
# Build applied filters for response
|
# Build applied filters for response
|
||||||
applied_filters = {}
|
applied_filters = {}
|
||||||
if season is not None:
|
if season is not None:
|
||||||
applied_filters["season"] = season
|
applied_filters['season'] = season
|
||||||
if team_id is not None:
|
if team_id is not None:
|
||||||
applied_filters["team_id"] = team_id
|
applied_filters['team_id'] = team_id
|
||||||
if player_id is not None:
|
if player_id is not None:
|
||||||
applied_filters["player_id"] = player_id
|
applied_filters['player_id'] = player_id
|
||||||
if min_pa is not None:
|
if min_pa is not None:
|
||||||
applied_filters["min_pa"] = min_pa
|
applied_filters['min_pa'] = min_pa
|
||||||
|
|
||||||
if csv:
|
if csv:
|
||||||
return_val = query_to_csv(query)
|
return_val = query_to_csv(query)
|
||||||
return Response(content=return_val, media_type="text/csv")
|
return Response(content=return_val, media_type='text/csv')
|
||||||
else:
|
else:
|
||||||
stat_list = [model_to_dict(stat) for stat in query]
|
stat_list = [model_to_dict(stat) for stat in query]
|
||||||
return {"count": len(stat_list), "filters": applied_filters, "stats": stat_list}
|
return {
|
||||||
|
'count': len(stat_list),
|
||||||
|
'filters': applied_filters,
|
||||||
|
'stats': stat_list
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.post("/season-stats/batting/refresh", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.post('/season-stats/batting/refresh', include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def refresh_season_batting_stats(
|
async def refresh_season_batting_stats(
|
||||||
season: int, token: str = Depends(oauth2_scheme)
|
season: int,
|
||||||
|
token: str = Depends(oauth2_scheme)
|
||||||
) -> dict:
|
) -> dict:
|
||||||
"""
|
"""
|
||||||
Refresh batting stats for all players in a specific season.
|
Refresh batting stats for all players in a specific season.
|
||||||
Useful for full season updates.
|
Useful for full season updates.
|
||||||
"""
|
"""
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("refresh_season_batting_stats - Bad Token")
|
logger.warning(f'refresh_season_batting_stats - Bad Token: {token}')
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||||
|
|
||||||
logger.info(f"Refreshing all batting stats for season {season}")
|
logger.info(f'Refreshing all batting stats for season {season}')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get all player IDs who have stratplay records in this season
|
# Get all player IDs who have stratplay records in this season
|
||||||
batter_ids = [
|
batter_ids = [row.batter_id for row in
|
||||||
row.batter_id
|
StratPlay.select(StratPlay.batter_id.distinct())
|
||||||
for row in StratPlay.select(StratPlay.batter_id.distinct())
|
.join(StratGame).where(StratGame.season == season)]
|
||||||
.join(StratGame)
|
|
||||||
.where(StratGame.season == season)
|
|
||||||
]
|
|
||||||
|
|
||||||
if batter_ids:
|
if batter_ids:
|
||||||
update_season_batting_stats(batter_ids, season, db)
|
update_season_batting_stats(batter_ids, season, db)
|
||||||
logger.info(
|
logger.info(f'Successfully refreshed {len(batter_ids)} players for season {season}')
|
||||||
f"Successfully refreshed {len(batter_ids)} players for season {season}"
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"message": f"Season {season} batting stats refreshed",
|
'message': f'Season {season} batting stats refreshed',
|
||||||
"players_updated": len(batter_ids),
|
'players_updated': len(batter_ids)
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
logger.warning(f"No batting data found for season {season}")
|
logger.warning(f'No batting data found for season {season}')
|
||||||
return {
|
return {
|
||||||
"message": f"No batting data found for season {season}",
|
'message': f'No batting data found for season {season}',
|
||||||
"players_updated": 0,
|
'players_updated': 0
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error refreshing season {season}: {e}")
|
logger.error(f'Error refreshing season {season}: {e}')
|
||||||
raise HTTPException(status_code=500, detail=f"Refresh failed: {str(e)}")
|
raise HTTPException(status_code=500, detail=f'Refresh failed: {str(e)}')
|
||||||
|
|
||||||
|
|
||||||
@router.get("/season-stats/pitching")
|
@router.get('/season-stats/pitching')
|
||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
@add_cache_headers(max_age=10 * 60)
|
@add_cache_headers(max_age=10*60)
|
||||||
@cache_result(ttl=5 * 60, key_prefix="season-pitching")
|
@cache_result(ttl=5*60, key_prefix='season-pitching')
|
||||||
async def get_season_pitching_stats(
|
async def get_season_pitching_stats(
|
||||||
season: Optional[int] = None,
|
season: Optional[int] = None,
|
||||||
team_id: Optional[int] = None,
|
team_id: Optional[int] = None,
|
||||||
player_id: Optional[int] = None,
|
player_id: Optional[int] = None,
|
||||||
sbaplayer_id: Optional[int] = None,
|
sbaplayer_id: Optional[int] = None,
|
||||||
min_outs: Optional[int] = None, # Minimum outs pitched
|
min_outs: Optional[int] = None, # Minimum outs pitched
|
||||||
sort_by: Literal[
|
sort_by: str = "era", # Default sort field
|
||||||
"tbf",
|
sort_order: Literal['asc', 'desc'] = 'asc', # asc or desc (asc default for ERA)
|
||||||
"outs",
|
limit: Optional[int] = 200,
|
||||||
"games",
|
|
||||||
"gs",
|
|
||||||
"win",
|
|
||||||
"loss",
|
|
||||||
"hold",
|
|
||||||
"saves",
|
|
||||||
"bsave",
|
|
||||||
"ir",
|
|
||||||
"irs",
|
|
||||||
"ab",
|
|
||||||
"run",
|
|
||||||
"e_run",
|
|
||||||
"hits",
|
|
||||||
"double",
|
|
||||||
"triple",
|
|
||||||
"homerun",
|
|
||||||
"bb",
|
|
||||||
"so",
|
|
||||||
"hbp",
|
|
||||||
"sac",
|
|
||||||
"ibb",
|
|
||||||
"gidp",
|
|
||||||
"sb",
|
|
||||||
"cs",
|
|
||||||
"bphr",
|
|
||||||
"bpfo",
|
|
||||||
"bp1b",
|
|
||||||
"bplo",
|
|
||||||
"wp",
|
|
||||||
"balk",
|
|
||||||
"wpa",
|
|
||||||
"era",
|
|
||||||
"whip",
|
|
||||||
"avg",
|
|
||||||
"obp",
|
|
||||||
"slg",
|
|
||||||
"ops",
|
|
||||||
"woba",
|
|
||||||
"hper9",
|
|
||||||
"kper9",
|
|
||||||
"bbper9",
|
|
||||||
"kperbb",
|
|
||||||
"lob_2outs",
|
|
||||||
"rbipercent",
|
|
||||||
"re24",
|
|
||||||
] = "era", # Sort field
|
|
||||||
sort_order: Literal["asc", "desc"] = "asc", # asc or desc (asc default for ERA)
|
|
||||||
limit: int = Query(default=DEFAULT_LIMIT, ge=1, le=MAX_LIMIT),
|
|
||||||
offset: int = 0,
|
offset: int = 0,
|
||||||
csv: Optional[bool] = False,
|
csv: Optional[bool] = False
|
||||||
):
|
):
|
||||||
logger.info(
|
logger.info(f'Getting season {season} pitching stats - team_id: {team_id}, player_id: {player_id}, min_outs: {min_outs}, sort_by: {sort_by}, sort_order: {sort_order}, limit: {limit}, offset: {offset}')
|
||||||
f"Getting season {season} pitching stats - team_id: {team_id}, player_id: {player_id}, min_outs: {min_outs}, sort_by: {sort_by}, sort_order: {sort_order}, limit: {limit}, offset: {offset}"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Use the get_top_pitchers method
|
# Use the get_top_pitchers method
|
||||||
query = SeasonPitchingStats.get_top_pitchers(
|
query = SeasonPitchingStats.get_top_pitchers(
|
||||||
season=season,
|
season=season,
|
||||||
stat=sort_by,
|
stat=sort_by,
|
||||||
limit=limit if limit != 0 else None,
|
limit=limit if limit != 0 else None,
|
||||||
desc=(sort_order.lower() == "desc"),
|
desc=(sort_order.lower() == 'desc'),
|
||||||
team_id=team_id,
|
team_id=team_id,
|
||||||
player_id=player_id,
|
player_id=player_id,
|
||||||
sbaplayer_id=sbaplayer_id,
|
sbaplayer_id=sbaplayer_id,
|
||||||
min_outs=min_outs,
|
min_outs=min_outs,
|
||||||
offset=offset,
|
offset=offset
|
||||||
)
|
)
|
||||||
|
|
||||||
# Build applied filters for response
|
# Build applied filters for response
|
||||||
applied_filters = {}
|
applied_filters = {}
|
||||||
if season is not None:
|
if season is not None:
|
||||||
applied_filters["season"] = season
|
applied_filters['season'] = season
|
||||||
if team_id is not None:
|
if team_id is not None:
|
||||||
applied_filters["team_id"] = team_id
|
applied_filters['team_id'] = team_id
|
||||||
if player_id is not None:
|
if player_id is not None:
|
||||||
applied_filters["player_id"] = player_id
|
applied_filters['player_id'] = player_id
|
||||||
if min_outs is not None:
|
if min_outs is not None:
|
||||||
applied_filters["min_outs"] = min_outs
|
applied_filters['min_outs'] = min_outs
|
||||||
|
|
||||||
if csv:
|
if csv:
|
||||||
return_val = query_to_csv(query)
|
return_val = query_to_csv(query)
|
||||||
return Response(content=return_val, media_type="text/csv")
|
return Response(content=return_val, media_type='text/csv')
|
||||||
else:
|
else:
|
||||||
stat_list = [model_to_dict(stat) for stat in query]
|
stat_list = [model_to_dict(stat) for stat in query]
|
||||||
return {"count": len(stat_list), "filters": applied_filters, "stats": stat_list}
|
return {
|
||||||
|
'count': len(stat_list),
|
||||||
|
'filters': applied_filters,
|
||||||
|
'stats': stat_list
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.post("/season-stats/pitching/refresh", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.post('/season-stats/pitching/refresh', include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def refresh_season_pitching_stats(
|
async def refresh_season_pitching_stats(
|
||||||
season: int, token: str = Depends(oauth2_scheme)
|
season: int,
|
||||||
|
token: str = Depends(oauth2_scheme)
|
||||||
) -> dict:
|
) -> dict:
|
||||||
"""
|
"""
|
||||||
Refresh pitching statistics for a specific season by aggregating from individual games.
|
Refresh pitching statistics for a specific season by aggregating from individual games.
|
||||||
Private endpoint - not included in public API documentation.
|
Private endpoint - not included in public API documentation.
|
||||||
"""
|
"""
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("refresh_season_batting_stats - Bad Token")
|
logger.warning(f'refresh_season_batting_stats - Bad Token: {token}')
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||||
|
|
||||||
logger.info(f"Refreshing season {season} pitching stats")
|
logger.info(f'Refreshing season {season} pitching stats')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get all pitcher IDs for this season
|
# Get all pitcher IDs for this season
|
||||||
pitcher_query = (
|
pitcher_query = (
|
||||||
StratPlay.select(StratPlay.pitcher_id)
|
StratPlay
|
||||||
|
.select(StratPlay.pitcher_id)
|
||||||
.join(StratGame, on=(StratPlay.game_id == StratGame.id))
|
.join(StratGame, on=(StratPlay.game_id == StratGame.id))
|
||||||
.where((StratGame.season == season) & (StratPlay.pitcher_id.is_null(False)))
|
.where((StratGame.season == season) & (StratPlay.pitcher_id.is_null(False)))
|
||||||
.distinct()
|
.distinct()
|
||||||
@ -286,50 +191,51 @@ async def refresh_season_pitching_stats(
|
|||||||
pitcher_ids = [row.pitcher_id for row in pitcher_query]
|
pitcher_ids = [row.pitcher_id for row in pitcher_query]
|
||||||
|
|
||||||
if not pitcher_ids:
|
if not pitcher_ids:
|
||||||
logger.warning(f"No pitchers found for season {season}")
|
logger.warning(f'No pitchers found for season {season}')
|
||||||
return {
|
return {
|
||||||
"status": "success",
|
'status': 'success',
|
||||||
"message": f"No pitchers found for season {season}",
|
'message': f'No pitchers found for season {season}',
|
||||||
"players_updated": 0,
|
'players_updated': 0
|
||||||
}
|
}
|
||||||
|
|
||||||
# Use the dependency function to update pitching stats
|
# Use the dependency function to update pitching stats
|
||||||
update_season_pitching_stats(pitcher_ids, season, db)
|
update_season_pitching_stats(pitcher_ids, season, db)
|
||||||
|
|
||||||
logger.info(
|
logger.info(f'Season {season} pitching stats refreshed successfully - {len(pitcher_ids)} players updated')
|
||||||
f"Season {season} pitching stats refreshed successfully - {len(pitcher_ids)} players updated"
|
|
||||||
)
|
|
||||||
return {
|
return {
|
||||||
"status": "success",
|
'status': 'success',
|
||||||
"message": f"Season {season} pitching stats refreshed",
|
'message': f'Season {season} pitching stats refreshed',
|
||||||
"players_updated": len(pitcher_ids),
|
'players_updated': len(pitcher_ids)
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error refreshing season {season} pitching stats: {e}")
|
logger.error(f'Error refreshing season {season} pitching stats: {e}')
|
||||||
raise HTTPException(status_code=500, detail=f"Refresh failed: {str(e)}")
|
raise HTTPException(status_code=500, detail=f'Refresh failed: {str(e)}')
|
||||||
|
|
||||||
|
|
||||||
@router.get("/admin/cache", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.get('/admin/cache', include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def get_admin_cache_stats(token: str = Depends(oauth2_scheme)) -> dict:
|
async def get_admin_cache_stats(
|
||||||
|
token: str = Depends(oauth2_scheme)
|
||||||
|
) -> dict:
|
||||||
"""
|
"""
|
||||||
Get Redis cache statistics and status.
|
Get Redis cache statistics and status.
|
||||||
Private endpoint - requires authentication.
|
Private endpoint - requires authentication.
|
||||||
"""
|
"""
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logger.warning("get_admin_cache_stats - Bad Token")
|
logger.warning(f'get_admin_cache_stats - Bad Token: {token}')
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||||
|
|
||||||
logger.info("Getting cache statistics")
|
logger.info('Getting cache statistics')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cache_stats = get_cache_stats()
|
cache_stats = get_cache_stats()
|
||||||
logger.info(f"Cache stats retrieved: {cache_stats}")
|
logger.info(f'Cache stats retrieved: {cache_stats}')
|
||||||
return {"status": "success", "cache_info": cache_stats}
|
return {
|
||||||
|
'status': 'success',
|
||||||
|
'cache_info': cache_stats
|
||||||
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting cache stats: {e}")
|
logger.error(f'Error getting cache stats: {e}')
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=500, detail=f'Failed to get cache stats: {str(e)}')
|
||||||
status_code=500, detail=f"Failed to get cache stats: {str(e)}"
|
|
||||||
)
|
|
||||||
|
|||||||
@ -39,7 +39,7 @@ class PlayerService(BaseService):
|
|||||||
cache_patterns = ["players*", "players-search*", "player*", "team-roster*"]
|
cache_patterns = ["players*", "players-search*", "player*", "team-roster*"]
|
||||||
|
|
||||||
# Deprecated fields to exclude from player responses
|
# Deprecated fields to exclude from player responses
|
||||||
EXCLUDED_FIELDS = ["pitcher_injury"]
|
EXCLUDED_FIELDS = ['pitcher_injury']
|
||||||
|
|
||||||
# Class-level repository for dependency injection
|
# Class-level repository for dependency injection
|
||||||
_injected_repo: Optional[AbstractPlayerRepository] = None
|
_injected_repo: Optional[AbstractPlayerRepository] = None
|
||||||
@ -135,21 +135,17 @@ class PlayerService(BaseService):
|
|||||||
# Apply sorting
|
# Apply sorting
|
||||||
query = cls._apply_player_sort(query, sort)
|
query = cls._apply_player_sort(query, sort)
|
||||||
|
|
||||||
# Apply pagination at DB level for real queries, Python level for mocks
|
# Convert to list of dicts
|
||||||
if isinstance(query, InMemoryQueryResult):
|
|
||||||
total_count = len(query)
|
|
||||||
players_data = cls._query_to_player_dicts(query, short_output)
|
players_data = cls._query_to_player_dicts(query, short_output)
|
||||||
|
|
||||||
|
# Store total count before pagination
|
||||||
|
total_count = len(players_data)
|
||||||
|
|
||||||
|
# Apply pagination (offset and limit)
|
||||||
if offset is not None:
|
if offset is not None:
|
||||||
players_data = players_data[offset:]
|
players_data = players_data[offset:]
|
||||||
if limit is not None:
|
if limit is not None:
|
||||||
players_data = players_data[:limit]
|
players_data = players_data[:limit]
|
||||||
else:
|
|
||||||
total_count = query.count()
|
|
||||||
if offset is not None:
|
|
||||||
query = query.offset(offset)
|
|
||||||
if limit is not None:
|
|
||||||
query = query.limit(limit)
|
|
||||||
players_data = cls._query_to_player_dicts(query, short_output)
|
|
||||||
|
|
||||||
# Return format
|
# Return format
|
||||||
if as_csv:
|
if as_csv:
|
||||||
@ -158,7 +154,7 @@ class PlayerService(BaseService):
|
|||||||
return {
|
return {
|
||||||
"count": len(players_data),
|
"count": len(players_data),
|
||||||
"total": total_count,
|
"total": total_count,
|
||||||
"players": players_data,
|
"players": players_data
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -208,9 +204,9 @@ class PlayerService(BaseService):
|
|||||||
p_list = [x.upper() for x in pos]
|
p_list = [x.upper() for x in pos]
|
||||||
|
|
||||||
# Expand generic "P" to match all pitcher positions
|
# Expand generic "P" to match all pitcher positions
|
||||||
pitcher_positions = ["SP", "RP", "CP"]
|
pitcher_positions = ['SP', 'RP', 'CP']
|
||||||
if "P" in p_list:
|
if 'P' in p_list:
|
||||||
p_list.remove("P")
|
p_list.remove('P')
|
||||||
p_list.extend(pitcher_positions)
|
p_list.extend(pitcher_positions)
|
||||||
|
|
||||||
pos_conditions = (
|
pos_conditions = (
|
||||||
@ -249,9 +245,9 @@ class PlayerService(BaseService):
|
|||||||
p_list = [p.upper() for p in pos]
|
p_list = [p.upper() for p in pos]
|
||||||
|
|
||||||
# Expand generic "P" to match all pitcher positions
|
# Expand generic "P" to match all pitcher positions
|
||||||
pitcher_positions = ["SP", "RP", "CP"]
|
pitcher_positions = ['SP', 'RP', 'CP']
|
||||||
if "P" in p_list:
|
if 'P' in p_list:
|
||||||
p_list.remove("P")
|
p_list.remove('P')
|
||||||
p_list.extend(pitcher_positions)
|
p_list.extend(pitcher_positions)
|
||||||
|
|
||||||
player_pos = [
|
player_pos = [
|
||||||
@ -389,23 +385,19 @@ class PlayerService(BaseService):
|
|||||||
# This filters at the database level instead of loading all players
|
# This filters at the database level instead of loading all players
|
||||||
if search_all_seasons:
|
if search_all_seasons:
|
||||||
# Search all seasons, order by season DESC (newest first)
|
# Search all seasons, order by season DESC (newest first)
|
||||||
query = (
|
query = (Player.select()
|
||||||
Player.select()
|
|
||||||
.where(fn.Lower(Player.name).contains(query_lower))
|
.where(fn.Lower(Player.name).contains(query_lower))
|
||||||
.order_by(Player.season.desc(), Player.name)
|
.order_by(Player.season.desc(), Player.name)
|
||||||
.limit(limit * 2)
|
.limit(limit * 2)) # Get extra for exact match sorting
|
||||||
) # Get extra for exact match sorting
|
|
||||||
else:
|
else:
|
||||||
# Search specific season
|
# Search specific season
|
||||||
query = (
|
query = (Player.select()
|
||||||
Player.select()
|
|
||||||
.where(
|
.where(
|
||||||
(Player.season == season)
|
(Player.season == season) &
|
||||||
& (fn.Lower(Player.name).contains(query_lower))
|
(fn.Lower(Player.name).contains(query_lower))
|
||||||
)
|
)
|
||||||
.order_by(Player.name)
|
.order_by(Player.name)
|
||||||
.limit(limit * 2)
|
.limit(limit * 2)) # Get extra for exact match sorting
|
||||||
) # Get extra for exact match sorting
|
|
||||||
|
|
||||||
# Execute query and convert limited results to dicts
|
# Execute query and convert limited results to dicts
|
||||||
players = list(query)
|
players = list(query)
|
||||||
@ -476,29 +468,19 @@ class PlayerService(BaseService):
|
|||||||
# Use backrefs=False to avoid circular reference issues
|
# Use backrefs=False to avoid circular reference issues
|
||||||
player_dict = model_to_dict(player, recurse=recurse, backrefs=False)
|
player_dict = model_to_dict(player, recurse=recurse, backrefs=False)
|
||||||
# Filter out excluded fields
|
# Filter out excluded fields
|
||||||
return {
|
return {k: v for k, v in player_dict.items() if k not in cls.EXCLUDED_FIELDS}
|
||||||
k: v for k, v in player_dict.items() if k not in cls.EXCLUDED_FIELDS
|
|
||||||
}
|
|
||||||
except (ImportError, AttributeError, TypeError) as e:
|
except (ImportError, AttributeError, TypeError) as e:
|
||||||
# Log the error and fall back to non-recursive serialization
|
# Log the error and fall back to non-recursive serialization
|
||||||
logger.warning(
|
logger.warning(f"Error in recursive player serialization: {e}, falling back to non-recursive")
|
||||||
f"Error in recursive player serialization: {e}, falling back to non-recursive"
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
# Fallback to non-recursive serialization
|
# Fallback to non-recursive serialization
|
||||||
player_dict = model_to_dict(player, recurse=False)
|
player_dict = model_to_dict(player, recurse=False)
|
||||||
return {
|
return {k: v for k, v in player_dict.items() if k not in cls.EXCLUDED_FIELDS}
|
||||||
k: v for k, v in player_dict.items() if k not in cls.EXCLUDED_FIELDS
|
|
||||||
}
|
|
||||||
except Exception as fallback_error:
|
except Exception as fallback_error:
|
||||||
# Final fallback to basic dict conversion
|
# Final fallback to basic dict conversion
|
||||||
logger.error(
|
logger.error(f"Error in non-recursive serialization: {fallback_error}, using basic dict")
|
||||||
f"Error in non-recursive serialization: {fallback_error}, using basic dict"
|
|
||||||
)
|
|
||||||
player_dict = dict(player)
|
player_dict = dict(player)
|
||||||
return {
|
return {k: v for k, v in player_dict.items() if k not in cls.EXCLUDED_FIELDS}
|
||||||
k: v for k, v in player_dict.items() if k not in cls.EXCLUDED_FIELDS
|
|
||||||
}
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def update_player(
|
def update_player(
|
||||||
@ -526,8 +508,6 @@ class PlayerService(BaseService):
|
|||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Error updating player {player_id}: {str(e)}"
|
status_code=500, detail=f"Error updating player {player_id}: {str(e)}"
|
||||||
)
|
)
|
||||||
finally:
|
|
||||||
temp_service.invalidate_related_cache(cls.cache_patterns)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def patch_player(
|
def patch_player(
|
||||||
@ -555,8 +535,6 @@ class PlayerService(BaseService):
|
|||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Error patching player {player_id}: {str(e)}"
|
status_code=500, detail=f"Error patching player {player_id}: {str(e)}"
|
||||||
)
|
)
|
||||||
finally:
|
|
||||||
temp_service.invalidate_related_cache(cls.cache_patterns)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_players(
|
def create_players(
|
||||||
@ -589,8 +567,6 @@ class PlayerService(BaseService):
|
|||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Error creating players: {str(e)}"
|
status_code=500, detail=f"Error creating players: {str(e)}"
|
||||||
)
|
)
|
||||||
finally:
|
|
||||||
temp_service.invalidate_related_cache(cls.cache_patterns)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def delete_player(cls, player_id: int, token: str) -> Dict[str, str]:
|
def delete_player(cls, player_id: int, token: str) -> Dict[str, str]:
|
||||||
@ -614,8 +590,6 @@ class PlayerService(BaseService):
|
|||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Error deleting player {player_id}: {str(e)}"
|
status_code=500, detail=f"Error deleting player {player_id}: {str(e)}"
|
||||||
)
|
)
|
||||||
finally:
|
|
||||||
temp_service.invalidate_related_cache(cls.cache_patterns)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _format_player_csv(cls, players: List[Dict]) -> str:
|
def _format_player_csv(cls, players: List[Dict]) -> str:
|
||||||
@ -629,12 +603,12 @@ class PlayerService(BaseService):
|
|||||||
flat_player = player.copy()
|
flat_player = player.copy()
|
||||||
|
|
||||||
# Flatten team object to just abbreviation
|
# Flatten team object to just abbreviation
|
||||||
if isinstance(flat_player.get("team"), dict):
|
if isinstance(flat_player.get('team'), dict):
|
||||||
flat_player["team"] = flat_player["team"].get("abbrev", "")
|
flat_player['team'] = flat_player['team'].get('abbrev', '')
|
||||||
|
|
||||||
# Flatten sbaplayer object to just ID
|
# Flatten sbaplayer object to just ID
|
||||||
if isinstance(flat_player.get("sbaplayer"), dict):
|
if isinstance(flat_player.get('sbaplayer'), dict):
|
||||||
flat_player["sbaplayer"] = flat_player["sbaplayer"].get("id", "")
|
flat_player['sbaplayer'] = flat_player['sbaplayer'].get('id', '')
|
||||||
|
|
||||||
flattened_players.append(flat_player)
|
flattened_players.append(flat_player)
|
||||||
|
|
||||||
|
|||||||
@ -34,7 +34,6 @@ services:
|
|||||||
- REDIS_HOST=sba_redis
|
- REDIS_HOST=sba_redis
|
||||||
- REDIS_PORT=6379
|
- REDIS_PORT=6379
|
||||||
- REDIS_DB=0
|
- REDIS_DB=0
|
||||||
- DISCORD_WEBHOOK_URL=${DISCORD_WEBHOOK_URL}
|
|
||||||
depends_on:
|
depends_on:
|
||||||
- postgres
|
- postgres
|
||||||
- redis
|
- redis
|
||||||
|
|||||||
@ -1,88 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
"""Apply pending SQL migrations and record them in schema_versions.
|
|
||||||
|
|
||||||
Usage:
|
|
||||||
python migrations.py
|
|
||||||
|
|
||||||
Connects to PostgreSQL using the same environment variables as the API:
|
|
||||||
POSTGRES_DB (default: sba_master)
|
|
||||||
POSTGRES_USER (default: sba_admin)
|
|
||||||
POSTGRES_PASSWORD (required)
|
|
||||||
POSTGRES_HOST (default: sba_postgres)
|
|
||||||
POSTGRES_PORT (default: 5432)
|
|
||||||
|
|
||||||
On first run against an existing database, all migrations will be applied.
|
|
||||||
All migration files use IF NOT EXISTS guards so re-applying is safe.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
import psycopg2
|
|
||||||
|
|
||||||
|
|
||||||
MIGRATIONS_DIR = Path(__file__).parent / "migrations"
|
|
||||||
|
|
||||||
_CREATE_SCHEMA_VERSIONS = """
|
|
||||||
CREATE TABLE IF NOT EXISTS schema_versions (
|
|
||||||
filename VARCHAR(255) PRIMARY KEY,
|
|
||||||
applied_at TIMESTAMP NOT NULL DEFAULT NOW()
|
|
||||||
);
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def _get_connection():
|
|
||||||
password = os.environ.get("POSTGRES_PASSWORD")
|
|
||||||
if password is None:
|
|
||||||
raise RuntimeError("POSTGRES_PASSWORD environment variable is not set")
|
|
||||||
return psycopg2.connect(
|
|
||||||
dbname=os.environ.get("POSTGRES_DB", "sba_master"),
|
|
||||||
user=os.environ.get("POSTGRES_USER", "sba_admin"),
|
|
||||||
password=password,
|
|
||||||
host=os.environ.get("POSTGRES_HOST", "sba_postgres"),
|
|
||||||
port=int(os.environ.get("POSTGRES_PORT", "5432")),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
conn = _get_connection()
|
|
||||||
try:
|
|
||||||
with conn:
|
|
||||||
with conn.cursor() as cur:
|
|
||||||
cur.execute(_CREATE_SCHEMA_VERSIONS)
|
|
||||||
|
|
||||||
with conn.cursor() as cur:
|
|
||||||
cur.execute("SELECT filename FROM schema_versions")
|
|
||||||
applied = {row[0] for row in cur.fetchall()}
|
|
||||||
|
|
||||||
migration_files = sorted(MIGRATIONS_DIR.glob("*.sql"))
|
|
||||||
pending = [f for f in migration_files if f.name not in applied]
|
|
||||||
|
|
||||||
if not pending:
|
|
||||||
print("No pending migrations.")
|
|
||||||
return
|
|
||||||
|
|
||||||
for migration_file in pending:
|
|
||||||
print(f"Applying {migration_file.name} ...", end=" ", flush=True)
|
|
||||||
sql = migration_file.read_text()
|
|
||||||
with conn:
|
|
||||||
with conn.cursor() as cur:
|
|
||||||
cur.execute(sql)
|
|
||||||
cur.execute(
|
|
||||||
"INSERT INTO schema_versions (filename) VALUES (%s)",
|
|
||||||
(migration_file.name,),
|
|
||||||
)
|
|
||||||
print("done")
|
|
||||||
|
|
||||||
print(f"\nApplied {len(pending)} migration(s).")
|
|
||||||
finally:
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
try:
|
|
||||||
main()
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error: {e}", file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
-- Migration: Add schema_versions table for migration tracking
|
|
||||||
-- Date: 2026-03-27
|
|
||||||
-- Description: Creates a table to record which SQL migrations have been applied,
|
|
||||||
-- preventing double-application and missed migrations across environments.
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS schema_versions (
|
|
||||||
filename VARCHAR(255) PRIMARY KEY,
|
|
||||||
applied_at TIMESTAMP NOT NULL DEFAULT NOW()
|
|
||||||
);
|
|
||||||
@ -1,24 +0,0 @@
|
|||||||
-- Migration: Add missing indexes on foreign key columns in stratplay and stratgame
|
|
||||||
-- Created: 2026-03-27
|
|
||||||
--
|
|
||||||
-- PostgreSQL does not auto-index foreign key columns. These tables are the
|
|
||||||
-- highest-volume tables in the schema and are filtered/joined on these columns
|
|
||||||
-- in batting, pitching, and running stats aggregation and standings recalculation.
|
|
||||||
|
|
||||||
-- stratplay: FK join column
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_stratplay_game_id ON stratplay(game_id);
|
|
||||||
|
|
||||||
-- stratplay: filtered in batting stats aggregation
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_stratplay_batter_id ON stratplay(batter_id);
|
|
||||||
|
|
||||||
-- stratplay: filtered in pitching stats aggregation
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_stratplay_pitcher_id ON stratplay(pitcher_id);
|
|
||||||
|
|
||||||
-- stratplay: filtered in running stats
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_stratplay_runner_id ON stratplay(runner_id);
|
|
||||||
|
|
||||||
-- stratgame: heavily filtered by season
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_stratgame_season ON stratgame(season);
|
|
||||||
|
|
||||||
-- stratgame: standings recalculation query ordering
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_stratgame_season_week_game_num ON stratgame(season, week, game_num);
|
|
||||||
@ -1,2 +0,0 @@
|
|||||||
pytest==9.0.2
|
|
||||||
pytest-asyncio==1.3.0
|
|
||||||
@ -1,10 +1,11 @@
|
|||||||
fastapi==0.133.0
|
fastapi
|
||||||
uvicorn==0.41.0
|
uvicorn
|
||||||
starlette==0.52.1
|
|
||||||
peewee==3.13.3
|
peewee==3.13.3
|
||||||
python-multipart==0.0.22
|
python-multipart
|
||||||
numpy==1.26.4
|
numpy<2.0.0
|
||||||
pandas==3.0.1
|
pandas
|
||||||
psycopg2-binary==2.9.11
|
psycopg2-binary>=2.9.0
|
||||||
requests==2.32.5
|
requests
|
||||||
redis==7.3.0
|
redis>=4.5.0
|
||||||
|
pytest>=7.0.0
|
||||||
|
pytest-asyncio>=0.21.0
|
||||||
|
|||||||
@ -81,9 +81,9 @@ class TestRouteRegistration:
|
|||||||
for route, methods in EXPECTED_PLAY_ROUTES.items():
|
for route, methods in EXPECTED_PLAY_ROUTES.items():
|
||||||
assert route in paths, f"Route {route} missing from OpenAPI schema"
|
assert route in paths, f"Route {route} missing from OpenAPI schema"
|
||||||
for method in methods:
|
for method in methods:
|
||||||
assert method in paths[route], (
|
assert (
|
||||||
f"Method {method.upper()} missing for {route}"
|
method in paths[route]
|
||||||
)
|
), f"Method {method.upper()} missing for {route}"
|
||||||
|
|
||||||
def test_play_routes_have_plays_tag(self, api):
|
def test_play_routes_have_plays_tag(self, api):
|
||||||
"""All play routes should be tagged with 'plays'."""
|
"""All play routes should be tagged with 'plays'."""
|
||||||
@ -96,9 +96,9 @@ class TestRouteRegistration:
|
|||||||
for method, spec in paths[route].items():
|
for method, spec in paths[route].items():
|
||||||
if method in ("get", "post", "patch", "delete"):
|
if method in ("get", "post", "patch", "delete"):
|
||||||
tags = spec.get("tags", [])
|
tags = spec.get("tags", [])
|
||||||
assert "plays" in tags, (
|
assert (
|
||||||
f"{method.upper()} {route} missing 'plays' tag, has {tags}"
|
"plays" in tags
|
||||||
)
|
), f"{method.upper()} {route} missing 'plays' tag, has {tags}"
|
||||||
|
|
||||||
@pytest.mark.post_deploy
|
@pytest.mark.post_deploy
|
||||||
@pytest.mark.skip(
|
@pytest.mark.skip(
|
||||||
@ -124,9 +124,9 @@ class TestRouteRegistration:
|
|||||||
]:
|
]:
|
||||||
params = paths[route]["get"].get("parameters", [])
|
params = paths[route]["get"].get("parameters", [])
|
||||||
param_names = [p["name"] for p in params]
|
param_names = [p["name"] for p in params]
|
||||||
assert "sbaplayer_id" in param_names, (
|
assert (
|
||||||
f"sbaplayer_id parameter missing from {route}"
|
"sbaplayer_id" in param_names
|
||||||
)
|
), f"sbaplayer_id parameter missing from {route}"
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@ -493,9 +493,10 @@ class TestPlayCrud:
|
|||||||
assert result["id"] == play_id
|
assert result["id"] == play_id
|
||||||
|
|
||||||
def test_get_nonexistent_play(self, api):
|
def test_get_nonexistent_play(self, api):
|
||||||
"""GET /plays/999999999 returns 404 Not Found."""
|
"""GET /plays/999999999 returns an error (wrapped by handle_db_errors)."""
|
||||||
r = requests.get(f"{api}/api/v3/plays/999999999", timeout=10)
|
r = requests.get(f"{api}/api/v3/plays/999999999", timeout=10)
|
||||||
assert r.status_code == 404
|
# handle_db_errors wraps HTTPException as 500 with detail message
|
||||||
|
assert r.status_code == 500
|
||||||
assert "not found" in r.json().get("detail", "").lower()
|
assert "not found" in r.json().get("detail", "").lower()
|
||||||
|
|
||||||
|
|
||||||
@ -574,9 +575,9 @@ class TestGroupBySbaPlayer:
|
|||||||
)
|
)
|
||||||
assert r_seasons.status_code == 200
|
assert r_seasons.status_code == 200
|
||||||
season_pas = [s["pa"] for s in r_seasons.json()["stats"]]
|
season_pas = [s["pa"] for s in r_seasons.json()["stats"]]
|
||||||
assert career_pa >= max(season_pas), (
|
assert career_pa >= max(
|
||||||
f"Career PA ({career_pa}) should be >= max season PA ({max(season_pas)})"
|
season_pas
|
||||||
)
|
), f"Career PA ({career_pa}) should be >= max season PA ({max(season_pas)})"
|
||||||
|
|
||||||
@pytest.mark.post_deploy
|
@pytest.mark.post_deploy
|
||||||
def test_batting_sbaplayer_short_output(self, api):
|
def test_batting_sbaplayer_short_output(self, api):
|
||||||
|
|||||||
@ -7,18 +7,21 @@ import pytest
|
|||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
|
|
||||||
from app.services.player_service import PlayerService
|
from app.services.player_service import PlayerService
|
||||||
from app.services.base import ServiceConfig
|
from app.services.base import ServiceConfig
|
||||||
from app.services.mocks import MockPlayerRepository, MockCacheService, EnhancedMockCache
|
from app.services.mocks import (
|
||||||
|
MockPlayerRepository,
|
||||||
|
MockCacheService,
|
||||||
|
EnhancedMockCache
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
# FIXTURES
|
# FIXTURES
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def cache():
|
def cache():
|
||||||
"""Create fresh cache for each test."""
|
"""Create fresh cache for each test."""
|
||||||
@ -32,65 +35,12 @@ def repo(cache):
|
|||||||
|
|
||||||
# Add test players
|
# Add test players
|
||||||
players = [
|
players = [
|
||||||
{
|
{'id': 1, 'name': 'Mike Trout', 'wara': 5.2, 'team_id': 1, 'season': 10, 'pos_1': 'CF', 'pos_2': 'LF', 'strat_code': 'Elite', 'injury_rating': 'A'},
|
||||||
"id": 1,
|
{'id': 2, 'name': 'Aaron Judge', 'wara': 4.8, 'team_id': 2, 'season': 10, 'pos_1': 'RF', 'strat_code': 'Power', 'injury_rating': 'B'},
|
||||||
"name": "Mike Trout",
|
{'id': 3, 'name': 'Mookie Betts', 'wara': 5.5, 'team_id': 3, 'season': 10, 'pos_1': 'RF', 'pos_2': '2B', 'strat_code': 'Elite', 'injury_rating': 'A'},
|
||||||
"wara": 5.2,
|
{'id': 4, 'name': 'Injured Player', 'wara': 2.0, 'team_id': 1, 'season': 10, 'pos_1': 'P', 'il_return': 'Week 5', 'injury_rating': 'C'},
|
||||||
"team_id": 1,
|
{'id': 5, 'name': 'Old Player', 'wara': 1.0, 'team_id': 1, 'season': 5, 'pos_1': '1B'},
|
||||||
"season": 10,
|
{'id': 6, 'name': 'Juan Soto', 'wara': 4.5, 'team_id': 2, 'season': 10, 'pos_1': '1B', 'strat_code': 'Contact'},
|
||||||
"pos_1": "CF",
|
|
||||||
"pos_2": "LF",
|
|
||||||
"strat_code": "Elite",
|
|
||||||
"injury_rating": "A",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 2,
|
|
||||||
"name": "Aaron Judge",
|
|
||||||
"wara": 4.8,
|
|
||||||
"team_id": 2,
|
|
||||||
"season": 10,
|
|
||||||
"pos_1": "RF",
|
|
||||||
"strat_code": "Power",
|
|
||||||
"injury_rating": "B",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 3,
|
|
||||||
"name": "Mookie Betts",
|
|
||||||
"wara": 5.5,
|
|
||||||
"team_id": 3,
|
|
||||||
"season": 10,
|
|
||||||
"pos_1": "RF",
|
|
||||||
"pos_2": "2B",
|
|
||||||
"strat_code": "Elite",
|
|
||||||
"injury_rating": "A",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 4,
|
|
||||||
"name": "Injured Player",
|
|
||||||
"wara": 2.0,
|
|
||||||
"team_id": 1,
|
|
||||||
"season": 10,
|
|
||||||
"pos_1": "P",
|
|
||||||
"il_return": "Week 5",
|
|
||||||
"injury_rating": "C",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 5,
|
|
||||||
"name": "Old Player",
|
|
||||||
"wara": 1.0,
|
|
||||||
"team_id": 1,
|
|
||||||
"season": 5,
|
|
||||||
"pos_1": "1B",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 6,
|
|
||||||
"name": "Juan Soto",
|
|
||||||
"wara": 4.5,
|
|
||||||
"team_id": 2,
|
|
||||||
"season": 10,
|
|
||||||
"pos_1": "1B",
|
|
||||||
"strat_code": "Contact",
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
|
|
||||||
for player in players:
|
for player in players:
|
||||||
@ -110,7 +60,6 @@ def service(repo, cache):
|
|||||||
# TEST CLASSES
|
# TEST CLASSES
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
|
|
||||||
|
|
||||||
class TestPlayerServiceGetPlayers:
|
class TestPlayerServiceGetPlayers:
|
||||||
"""Tests for get_players method - 50+ lines covered."""
|
"""Tests for get_players method - 50+ lines covered."""
|
||||||
|
|
||||||
@ -118,73 +67,71 @@ class TestPlayerServiceGetPlayers:
|
|||||||
"""Get all players for a season."""
|
"""Get all players for a season."""
|
||||||
result = service.get_players(season=10)
|
result = service.get_players(season=10)
|
||||||
|
|
||||||
assert result["count"] >= 5 # We have 5 season 10 players
|
assert result['count'] >= 5 # We have 5 season 10 players
|
||||||
assert len(result["players"]) >= 5
|
assert len(result['players']) >= 5
|
||||||
assert all(p.get("season") == 10 for p in result["players"])
|
assert all(p.get('season') == 10 for p in result['players'])
|
||||||
|
|
||||||
def test_filter_by_single_team(self, service):
|
def test_filter_by_single_team(self, service):
|
||||||
"""Filter by single team ID."""
|
"""Filter by single team ID."""
|
||||||
result = service.get_players(season=10, team_id=[1])
|
result = service.get_players(season=10, team_id=[1])
|
||||||
|
|
||||||
assert result["count"] >= 1
|
assert result['count'] >= 1
|
||||||
assert all(p.get("team_id") == 1 for p in result["players"])
|
assert all(p.get('team_id') == 1 for p in result['players'])
|
||||||
|
|
||||||
def test_filter_by_multiple_teams(self, service):
|
def test_filter_by_multiple_teams(self, service):
|
||||||
"""Filter by multiple team IDs."""
|
"""Filter by multiple team IDs."""
|
||||||
result = service.get_players(season=10, team_id=[1, 2])
|
result = service.get_players(season=10, team_id=[1, 2])
|
||||||
|
|
||||||
assert result["count"] >= 2
|
assert result['count'] >= 2
|
||||||
assert all(p.get("team_id") in [1, 2] for p in result["players"])
|
assert all(p.get('team_id') in [1, 2] for p in result['players'])
|
||||||
|
|
||||||
def test_filter_by_position(self, service):
|
def test_filter_by_position(self, service):
|
||||||
"""Filter by position."""
|
"""Filter by position."""
|
||||||
result = service.get_players(season=10, pos=["CF"])
|
result = service.get_players(season=10, pos=['CF'])
|
||||||
|
|
||||||
assert result["count"] >= 1
|
assert result['count'] >= 1
|
||||||
assert any(
|
assert any(p.get('pos_1') == 'CF' or p.get('pos_2') == 'CF' for p in result['players'])
|
||||||
p.get("pos_1") == "CF" or p.get("pos_2") == "CF" for p in result["players"]
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_filter_by_strat_code(self, service):
|
def test_filter_by_strat_code(self, service):
|
||||||
"""Filter by strat code."""
|
"""Filter by strat code."""
|
||||||
result = service.get_players(season=10, strat_code=["Elite"])
|
result = service.get_players(season=10, strat_code=['Elite'])
|
||||||
|
|
||||||
assert result["count"] >= 2 # Trout and Betts
|
assert result['count'] >= 2 # Trout and Betts
|
||||||
assert all("Elite" in str(p.get("strat_code", "")) for p in result["players"])
|
assert all('Elite' in str(p.get('strat_code', '')) for p in result['players'])
|
||||||
|
|
||||||
def test_filter_injured_only(self, service):
|
def test_filter_injured_only(self, service):
|
||||||
"""Filter injured players only."""
|
"""Filter injured players only."""
|
||||||
result = service.get_players(season=10, is_injured=True)
|
result = service.get_players(season=10, is_injured=True)
|
||||||
|
|
||||||
assert result["count"] >= 1
|
assert result['count'] >= 1
|
||||||
assert all(p.get("il_return") is not None for p in result["players"])
|
assert all(p.get('il_return') is not None for p in result['players'])
|
||||||
|
|
||||||
def test_sort_cost_ascending(self, service):
|
def test_sort_cost_ascending(self, service):
|
||||||
"""Sort by WARA ascending."""
|
"""Sort by WARA ascending."""
|
||||||
result = service.get_players(season=10, sort="cost-asc")
|
result = service.get_players(season=10, sort='cost-asc')
|
||||||
|
|
||||||
wara = [p.get("wara", 0) for p in result["players"]]
|
wara = [p.get('wara', 0) for p in result['players']]
|
||||||
assert wara == sorted(wara)
|
assert wara == sorted(wara)
|
||||||
|
|
||||||
def test_sort_cost_descending(self, service):
|
def test_sort_cost_descending(self, service):
|
||||||
"""Sort by WARA descending."""
|
"""Sort by WARA descending."""
|
||||||
result = service.get_players(season=10, sort="cost-desc")
|
result = service.get_players(season=10, sort='cost-desc')
|
||||||
|
|
||||||
wara = [p.get("wara", 0) for p in result["players"]]
|
wara = [p.get('wara', 0) for p in result['players']]
|
||||||
assert wara == sorted(wara, reverse=True)
|
assert wara == sorted(wara, reverse=True)
|
||||||
|
|
||||||
def test_sort_name_ascending(self, service):
|
def test_sort_name_ascending(self, service):
|
||||||
"""Sort by name ascending."""
|
"""Sort by name ascending."""
|
||||||
result = service.get_players(season=10, sort="name-asc")
|
result = service.get_players(season=10, sort='name-asc')
|
||||||
|
|
||||||
names = [p.get("name", "") for p in result["players"]]
|
names = [p.get('name', '') for p in result['players']]
|
||||||
assert names == sorted(names)
|
assert names == sorted(names)
|
||||||
|
|
||||||
def test_sort_name_descending(self, service):
|
def test_sort_name_descending(self, service):
|
||||||
"""Sort by name descending."""
|
"""Sort by name descending."""
|
||||||
result = service.get_players(season=10, sort="name-desc")
|
result = service.get_players(season=10, sort='name-desc')
|
||||||
|
|
||||||
names = [p.get("name", "") for p in result["players"]]
|
names = [p.get('name', '') for p in result['players']]
|
||||||
assert names == sorted(names, reverse=True)
|
assert names == sorted(names, reverse=True)
|
||||||
|
|
||||||
|
|
||||||
@ -193,46 +140,46 @@ class TestPlayerServiceSearch:
|
|||||||
|
|
||||||
def test_exact_name_match(self, service):
|
def test_exact_name_match(self, service):
|
||||||
"""Search with exact name match."""
|
"""Search with exact name match."""
|
||||||
result = service.search_players("Mike Trout", season=10)
|
result = service.search_players('Mike Trout', season=10)
|
||||||
|
|
||||||
assert result["count"] >= 1
|
assert result['count'] >= 1
|
||||||
names = [p.get("name") for p in result["players"]]
|
names = [p.get('name') for p in result['players']]
|
||||||
assert "Mike Trout" in names
|
assert 'Mike Trout' in names
|
||||||
|
|
||||||
def test_partial_name_match(self, service):
|
def test_partial_name_match(self, service):
|
||||||
"""Search with partial name match."""
|
"""Search with partial name match."""
|
||||||
result = service.search_players("Trout", season=10)
|
result = service.search_players('Trout', season=10)
|
||||||
|
|
||||||
assert result["count"] >= 1
|
assert result['count'] >= 1
|
||||||
assert any("Trout" in p.get("name", "") for p in result["players"])
|
assert any('Trout' in p.get('name', '') for p in result['players'])
|
||||||
|
|
||||||
def test_case_insensitive_search(self, service):
|
def test_case_insensitive_search(self, service):
|
||||||
"""Search is case insensitive."""
|
"""Search is case insensitive."""
|
||||||
result1 = service.search_players("MIKE", season=10)
|
result1 = service.search_players('MIKE', season=10)
|
||||||
result2 = service.search_players("mike", season=10)
|
result2 = service.search_players('mike', season=10)
|
||||||
|
|
||||||
assert result1["count"] == result2["count"]
|
assert result1['count'] == result2['count']
|
||||||
|
|
||||||
def test_search_all_seasons(self, service):
|
def test_search_all_seasons(self, service):
|
||||||
"""Search across all seasons."""
|
"""Search across all seasons."""
|
||||||
result = service.search_players("Player", season=None)
|
result = service.search_players('Player', season=None)
|
||||||
|
|
||||||
# Should find both current and old players
|
# Should find both current and old players
|
||||||
assert result["all_seasons"] == True
|
assert result['all_seasons'] == True
|
||||||
assert result["count"] >= 2
|
assert result['count'] >= 2
|
||||||
|
|
||||||
def test_search_limit(self, service):
|
def test_search_limit(self, service):
|
||||||
"""Limit search results."""
|
"""Limit search results."""
|
||||||
result = service.search_players("a", season=10, limit=2)
|
result = service.search_players('a', season=10, limit=2)
|
||||||
|
|
||||||
assert result["count"] <= 2
|
assert result['count'] <= 2
|
||||||
|
|
||||||
def test_search_no_results(self, service):
|
def test_search_no_results(self, service):
|
||||||
"""Search returns empty when no matches."""
|
"""Search returns empty when no matches."""
|
||||||
result = service.search_players("XYZ123NotExist", season=10)
|
result = service.search_players('XYZ123NotExist', season=10)
|
||||||
|
|
||||||
assert result["count"] == 0
|
assert result['count'] == 0
|
||||||
assert result["players"] == []
|
assert result['players'] == []
|
||||||
|
|
||||||
|
|
||||||
class TestPlayerServiceGetPlayer:
|
class TestPlayerServiceGetPlayer:
|
||||||
@ -243,8 +190,8 @@ class TestPlayerServiceGetPlayer:
|
|||||||
result = service.get_player(1)
|
result = service.get_player(1)
|
||||||
|
|
||||||
assert result is not None
|
assert result is not None
|
||||||
assert result.get("id") == 1
|
assert result.get('id') == 1
|
||||||
assert result.get("name") == "Mike Trout"
|
assert result.get('name') == 'Mike Trout'
|
||||||
|
|
||||||
def test_get_nonexistent_player(self, service):
|
def test_get_nonexistent_player(self, service):
|
||||||
"""Get player that doesn't exist."""
|
"""Get player that doesn't exist."""
|
||||||
@ -257,8 +204,8 @@ class TestPlayerServiceGetPlayer:
|
|||||||
result = service.get_player(1, short_output=True)
|
result = service.get_player(1, short_output=True)
|
||||||
|
|
||||||
# Should still have basic fields
|
# Should still have basic fields
|
||||||
assert result.get("id") == 1
|
assert result.get('id') == 1
|
||||||
assert result.get("name") == "Mike Trout"
|
assert result.get('name') == 'Mike Trout'
|
||||||
|
|
||||||
|
|
||||||
class TestPlayerServiceCreate:
|
class TestPlayerServiceCreate:
|
||||||
@ -269,26 +216,24 @@ class TestPlayerServiceCreate:
|
|||||||
config = ServiceConfig(player_repo=repo, cache=cache)
|
config = ServiceConfig(player_repo=repo, cache=cache)
|
||||||
service = PlayerService(config=config)
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
new_player = [
|
new_player = [{
|
||||||
{
|
'name': 'New Player',
|
||||||
"name": "New Player",
|
'wara': 3.0,
|
||||||
"wara": 3.0,
|
'team_id': 1,
|
||||||
"team_id": 1,
|
'season': 10,
|
||||||
"season": 10,
|
'pos_1': 'SS'
|
||||||
"pos_1": "SS",
|
}]
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
# Mock auth
|
# Mock auth
|
||||||
with patch.object(service, "require_auth", return_value=True):
|
with patch.object(service, 'require_auth', return_value=True):
|
||||||
result = service.create_players(new_player, "valid_token")
|
result = service.create_players(new_player, 'valid_token')
|
||||||
|
|
||||||
assert "Inserted" in str(result)
|
assert 'Inserted' in str(result)
|
||||||
|
|
||||||
# Verify player was added (ID 7 since fixture has players 1-6)
|
# Verify player was added (ID 7 since fixture has players 1-6)
|
||||||
player = repo.get_by_id(7) # Next ID after fixture data
|
player = repo.get_by_id(7) # Next ID after fixture data
|
||||||
assert player is not None
|
assert player is not None
|
||||||
assert player["name"] == "New Player"
|
assert player['name'] == 'New Player'
|
||||||
|
|
||||||
def test_create_multiple_players(self, repo, cache):
|
def test_create_multiple_players(self, repo, cache):
|
||||||
"""Create multiple new players."""
|
"""Create multiple new players."""
|
||||||
@ -296,59 +241,37 @@ class TestPlayerServiceCreate:
|
|||||||
service = PlayerService(config=config)
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
new_players = [
|
new_players = [
|
||||||
{
|
{'name': 'Player A', 'wara': 2.0, 'team_id': 1, 'season': 10, 'pos_1': '2B'},
|
||||||
"name": "Player A",
|
{'name': 'Player B', 'wara': 2.5, 'team_id': 2, 'season': 10, 'pos_1': '3B'},
|
||||||
"wara": 2.0,
|
|
||||||
"team_id": 1,
|
|
||||||
"season": 10,
|
|
||||||
"pos_1": "2B",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "Player B",
|
|
||||||
"wara": 2.5,
|
|
||||||
"team_id": 2,
|
|
||||||
"season": 10,
|
|
||||||
"pos_1": "3B",
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
|
|
||||||
with patch.object(service, "require_auth", return_value=True):
|
with patch.object(service, 'require_auth', return_value=True):
|
||||||
result = service.create_players(new_players, "valid_token")
|
result = service.create_players(new_players, 'valid_token')
|
||||||
|
|
||||||
assert "Inserted 2 players" in str(result)
|
assert 'Inserted 2 players' in str(result)
|
||||||
|
|
||||||
def test_create_duplicate_fails(self, repo, cache):
|
def test_create_duplicate_fails(self, repo, cache):
|
||||||
"""Creating duplicate player should fail."""
|
"""Creating duplicate player should fail."""
|
||||||
config = ServiceConfig(player_repo=repo, cache=cache)
|
config = ServiceConfig(player_repo=repo, cache=cache)
|
||||||
service = PlayerService(config=config)
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
duplicate = [
|
duplicate = [{'name': 'Mike Trout', 'wara': 5.0, 'team_id': 1, 'season': 10, 'pos_1': 'CF'}]
|
||||||
{
|
|
||||||
"name": "Mike Trout",
|
|
||||||
"wara": 5.0,
|
|
||||||
"team_id": 1,
|
|
||||||
"season": 10,
|
|
||||||
"pos_1": "CF",
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
with patch.object(service, "require_auth", return_value=True):
|
with patch.object(service, 'require_auth', return_value=True):
|
||||||
with pytest.raises(Exception) as exc_info:
|
with pytest.raises(Exception) as exc_info:
|
||||||
service.create_players(duplicate, "valid_token")
|
service.create_players(duplicate, 'valid_token')
|
||||||
|
|
||||||
assert "already exists" in str(exc_info.value)
|
assert 'already exists' in str(exc_info.value)
|
||||||
|
|
||||||
def test_create_requires_auth(self, repo, cache):
|
def test_create_requires_auth(self, repo, cache):
|
||||||
"""Creating players requires authentication."""
|
"""Creating players requires authentication."""
|
||||||
config = ServiceConfig(player_repo=repo, cache=cache)
|
config = ServiceConfig(player_repo=repo, cache=cache)
|
||||||
service = PlayerService(config=config)
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
new_player = [
|
new_player = [{'name': 'Test', 'wara': 1.0, 'team_id': 1, 'season': 10, 'pos_1': 'P'}]
|
||||||
{"name": "Test", "wara": 1.0, "team_id": 1, "season": 10, "pos_1": "P"}
|
|
||||||
]
|
|
||||||
|
|
||||||
with pytest.raises(Exception) as exc_info:
|
with pytest.raises(Exception) as exc_info:
|
||||||
service.create_players(new_player, "bad_token")
|
service.create_players(new_player, 'bad_token')
|
||||||
|
|
||||||
assert exc_info.value.status_code == 401
|
assert exc_info.value.status_code == 401
|
||||||
|
|
||||||
@ -361,46 +284,50 @@ class TestPlayerServiceUpdate:
|
|||||||
config = ServiceConfig(player_repo=repo, cache=cache)
|
config = ServiceConfig(player_repo=repo, cache=cache)
|
||||||
service = PlayerService(config=config)
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
with patch.object(service, "require_auth", return_value=True):
|
with patch.object(service, 'require_auth', return_value=True):
|
||||||
result = service.patch_player(1, {"name": "New Name"}, "valid_token")
|
result = service.patch_player(1, {'name': 'New Name'}, 'valid_token')
|
||||||
|
|
||||||
assert result is not None
|
assert result is not None
|
||||||
assert result.get("name") == "New Name"
|
assert result.get('name') == 'New Name'
|
||||||
|
|
||||||
def test_patch_player_wara(self, repo, cache):
|
def test_patch_player_wara(self, repo, cache):
|
||||||
"""Patch player's WARA."""
|
"""Patch player's WARA."""
|
||||||
config = ServiceConfig(player_repo=repo, cache=cache)
|
config = ServiceConfig(player_repo=repo, cache=cache)
|
||||||
service = PlayerService(config=config)
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
with patch.object(service, "require_auth", return_value=True):
|
with patch.object(service, 'require_auth', return_value=True):
|
||||||
result = service.patch_player(1, {"wara": 6.0}, "valid_token")
|
result = service.patch_player(1, {'wara': 6.0}, 'valid_token')
|
||||||
|
|
||||||
assert result.get("wara") == 6.0
|
assert result.get('wara') == 6.0
|
||||||
|
|
||||||
def test_patch_multiple_fields(self, repo, cache):
|
def test_patch_multiple_fields(self, repo, cache):
|
||||||
"""Patch multiple fields at once."""
|
"""Patch multiple fields at once."""
|
||||||
config = ServiceConfig(player_repo=repo, cache=cache)
|
config = ServiceConfig(player_repo=repo, cache=cache)
|
||||||
service = PlayerService(config=config)
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
updates = {"name": "Updated Name", "wara": 7.0, "strat_code": "Super Elite"}
|
updates = {
|
||||||
|
'name': 'Updated Name',
|
||||||
|
'wara': 7.0,
|
||||||
|
'strat_code': 'Super Elite'
|
||||||
|
}
|
||||||
|
|
||||||
with patch.object(service, "require_auth", return_value=True):
|
with patch.object(service, 'require_auth', return_value=True):
|
||||||
result = service.patch_player(1, updates, "valid_token")
|
result = service.patch_player(1, updates, 'valid_token')
|
||||||
|
|
||||||
assert result.get("name") == "Updated Name"
|
assert result.get('name') == 'Updated Name'
|
||||||
assert result.get("wara") == 7.0
|
assert result.get('wara') == 7.0
|
||||||
assert result.get("strat_code") == "Super Elite"
|
assert result.get('strat_code') == 'Super Elite'
|
||||||
|
|
||||||
def test_patch_nonexistent_player(self, repo, cache):
|
def test_patch_nonexistent_player(self, repo, cache):
|
||||||
"""Patch fails for non-existent player."""
|
"""Patch fails for non-existent player."""
|
||||||
config = ServiceConfig(player_repo=repo, cache=cache)
|
config = ServiceConfig(player_repo=repo, cache=cache)
|
||||||
service = PlayerService(config=config)
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
with patch.object(service, "require_auth", return_value=True):
|
with patch.object(service, 'require_auth', return_value=True):
|
||||||
with pytest.raises(Exception) as exc_info:
|
with pytest.raises(Exception) as exc_info:
|
||||||
service.patch_player(99999, {"name": "Test"}, "valid_token")
|
service.patch_player(99999, {'name': 'Test'}, 'valid_token')
|
||||||
|
|
||||||
assert "not found" in str(exc_info.value)
|
assert 'not found' in str(exc_info.value)
|
||||||
|
|
||||||
def test_patch_requires_auth(self, repo, cache):
|
def test_patch_requires_auth(self, repo, cache):
|
||||||
"""Patching requires authentication."""
|
"""Patching requires authentication."""
|
||||||
@ -408,7 +335,7 @@ class TestPlayerServiceUpdate:
|
|||||||
service = PlayerService(config=config)
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
with pytest.raises(Exception) as exc_info:
|
with pytest.raises(Exception) as exc_info:
|
||||||
service.patch_player(1, {"name": "Test"}, "bad_token")
|
service.patch_player(1, {'name': 'Test'}, 'bad_token')
|
||||||
|
|
||||||
assert exc_info.value.status_code == 401
|
assert exc_info.value.status_code == 401
|
||||||
|
|
||||||
@ -424,10 +351,10 @@ class TestPlayerServiceDelete:
|
|||||||
# Verify player exists
|
# Verify player exists
|
||||||
assert repo.get_by_id(1) is not None
|
assert repo.get_by_id(1) is not None
|
||||||
|
|
||||||
with patch.object(service, "require_auth", return_value=True):
|
with patch.object(service, 'require_auth', return_value=True):
|
||||||
result = service.delete_player(1, "valid_token")
|
result = service.delete_player(1, 'valid_token')
|
||||||
|
|
||||||
assert "deleted" in str(result)
|
assert 'deleted' in str(result)
|
||||||
|
|
||||||
# Verify player is gone
|
# Verify player is gone
|
||||||
assert repo.get_by_id(1) is None
|
assert repo.get_by_id(1) is None
|
||||||
@ -437,11 +364,11 @@ class TestPlayerServiceDelete:
|
|||||||
config = ServiceConfig(player_repo=repo, cache=cache)
|
config = ServiceConfig(player_repo=repo, cache=cache)
|
||||||
service = PlayerService(config=config)
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
with patch.object(service, "require_auth", return_value=True):
|
with patch.object(service, 'require_auth', return_value=True):
|
||||||
with pytest.raises(Exception) as exc_info:
|
with pytest.raises(Exception) as exc_info:
|
||||||
service.delete_player(99999, "valid_token")
|
service.delete_player(99999, 'valid_token')
|
||||||
|
|
||||||
assert "not found" in str(exc_info.value)
|
assert 'not found' in str(exc_info.value)
|
||||||
|
|
||||||
def test_delete_requires_auth(self, repo, cache):
|
def test_delete_requires_auth(self, repo, cache):
|
||||||
"""Deleting requires authentication."""
|
"""Deleting requires authentication."""
|
||||||
@ -449,11 +376,56 @@ class TestPlayerServiceDelete:
|
|||||||
service = PlayerService(config=config)
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
with pytest.raises(Exception) as exc_info:
|
with pytest.raises(Exception) as exc_info:
|
||||||
service.delete_player(1, "bad_token")
|
service.delete_player(1, 'bad_token')
|
||||||
|
|
||||||
assert exc_info.value.status_code == 401
|
assert exc_info.value.status_code == 401
|
||||||
|
|
||||||
|
|
||||||
|
class TestPlayerServiceCache:
|
||||||
|
"""Tests for cache functionality."""
|
||||||
|
|
||||||
|
@pytest.mark.skip(reason="Caching not yet implemented in service methods")
|
||||||
|
def test_cache_set_on_read(self, service, cache):
|
||||||
|
"""Cache is set on player read."""
|
||||||
|
service.get_players(season=10)
|
||||||
|
|
||||||
|
assert cache.was_called('set')
|
||||||
|
|
||||||
|
@pytest.mark.skip(reason="Caching not yet implemented in service methods")
|
||||||
|
def test_cache_invalidation_on_update(self, repo, cache):
|
||||||
|
"""Cache is invalidated on player update."""
|
||||||
|
config = ServiceConfig(player_repo=repo, cache=cache)
|
||||||
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
|
# Read to set cache
|
||||||
|
service.get_players(season=10)
|
||||||
|
initial_calls = len(cache.get_calls('set'))
|
||||||
|
|
||||||
|
# Update should invalidate cache
|
||||||
|
with patch.object(service, 'require_auth', return_value=True):
|
||||||
|
service.patch_player(1, {'name': 'Test'}, 'valid_token')
|
||||||
|
|
||||||
|
# Should have more delete calls after update
|
||||||
|
delete_calls = [c for c in cache.get_calls() if c.get('method') == 'delete']
|
||||||
|
assert len(delete_calls) > 0
|
||||||
|
|
||||||
|
@pytest.mark.skip(reason="Caching not yet implemented in service methods")
|
||||||
|
def test_cache_hit_rate(self, repo, cache):
|
||||||
|
"""Test cache hit rate tracking."""
|
||||||
|
config = ServiceConfig(player_repo=repo, cache=cache)
|
||||||
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
|
# First call - cache miss
|
||||||
|
service.get_players(season=10)
|
||||||
|
miss_count = cache._miss_count
|
||||||
|
|
||||||
|
# Second call - cache hit
|
||||||
|
service.get_players(season=10)
|
||||||
|
|
||||||
|
# Hit rate should have improved
|
||||||
|
assert cache.hit_rate > 0
|
||||||
|
|
||||||
|
|
||||||
class TestPlayerServiceValidation:
|
class TestPlayerServiceValidation:
|
||||||
"""Tests for input validation and edge cases."""
|
"""Tests for input validation and edge cases."""
|
||||||
|
|
||||||
@ -461,19 +433,19 @@ class TestPlayerServiceValidation:
|
|||||||
"""Invalid season returns empty result."""
|
"""Invalid season returns empty result."""
|
||||||
result = service.get_players(season=999)
|
result = service.get_players(season=999)
|
||||||
|
|
||||||
assert result["count"] == 0 or result["players"] == []
|
assert result['count'] == 0 or result['players'] == []
|
||||||
|
|
||||||
def test_empty_search_returns_all(self, service):
|
def test_empty_search_returns_all(self, service):
|
||||||
"""Empty search query returns all players."""
|
"""Empty search query returns all players."""
|
||||||
result = service.search_players("", season=10)
|
result = service.search_players('', season=10)
|
||||||
|
|
||||||
assert result["count"] >= 1
|
assert result['count'] >= 1
|
||||||
|
|
||||||
def test_sort_with_no_results(self, service):
|
def test_sort_with_no_results(self, service):
|
||||||
"""Sorting with no results doesn't error."""
|
"""Sorting with no results doesn't error."""
|
||||||
result = service.get_players(season=999, sort="cost-desc")
|
result = service.get_players(season=999, sort='cost-desc')
|
||||||
|
|
||||||
assert result["count"] == 0 or result["players"] == []
|
assert result['count'] == 0 or result['players'] == []
|
||||||
|
|
||||||
def test_cache_clear_on_create(self, repo, cache):
|
def test_cache_clear_on_create(self, repo, cache):
|
||||||
"""Cache is cleared when new players are created."""
|
"""Cache is cleared when new players are created."""
|
||||||
@ -481,21 +453,16 @@ class TestPlayerServiceValidation:
|
|||||||
service = PlayerService(config=config)
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
# Set up some cache data
|
# Set up some cache data
|
||||||
cache.set("test:key", "value", 300)
|
cache.set('test:key', 'value', 300)
|
||||||
|
|
||||||
with patch.object(service, "require_auth", return_value=True):
|
with patch.object(service, 'require_auth', return_value=True):
|
||||||
service.create_players(
|
service.create_players([{
|
||||||
[
|
'name': 'New',
|
||||||
{
|
'wara': 1.0,
|
||||||
"name": "New",
|
'team_id': 1,
|
||||||
"wara": 1.0,
|
'season': 10,
|
||||||
"team_id": 1,
|
'pos_1': 'P'
|
||||||
"season": 10,
|
}], 'valid_token')
|
||||||
"pos_1": "P",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"valid_token",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Should have invalidate calls
|
# Should have invalidate calls
|
||||||
assert len(cache.get_calls()) > 0
|
assert len(cache.get_calls()) > 0
|
||||||
@ -510,37 +477,30 @@ class TestPlayerServiceIntegration:
|
|||||||
service = PlayerService(config=config)
|
service = PlayerService(config=config)
|
||||||
|
|
||||||
# CREATE
|
# CREATE
|
||||||
with patch.object(service, "require_auth", return_value=True):
|
with patch.object(service, 'require_auth', return_value=True):
|
||||||
create_result = service.create_players(
|
create_result = service.create_players([{
|
||||||
[
|
'name': 'CRUD Test',
|
||||||
{
|
'wara': 3.0,
|
||||||
"name": "CRUD Test",
|
'team_id': 1,
|
||||||
"wara": 3.0,
|
'season': 10,
|
||||||
"team_id": 1,
|
'pos_1': 'DH'
|
||||||
"season": 10,
|
}], 'valid_token')
|
||||||
"pos_1": "DH",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"valid_token",
|
|
||||||
)
|
|
||||||
|
|
||||||
# READ
|
# READ
|
||||||
search_result = service.search_players("CRUD", season=10)
|
search_result = service.search_players('CRUD', season=10)
|
||||||
assert search_result["count"] >= 1
|
assert search_result['count'] >= 1
|
||||||
|
|
||||||
player_id = search_result["players"][0].get("id")
|
player_id = search_result['players'][0].get('id')
|
||||||
|
|
||||||
# UPDATE
|
# UPDATE
|
||||||
with patch.object(service, "require_auth", return_value=True):
|
with patch.object(service, 'require_auth', return_value=True):
|
||||||
update_result = service.patch_player(
|
update_result = service.patch_player(player_id, {'wara': 4.0}, 'valid_token')
|
||||||
player_id, {"wara": 4.0}, "valid_token"
|
assert update_result.get('wara') == 4.0
|
||||||
)
|
|
||||||
assert update_result.get("wara") == 4.0
|
|
||||||
|
|
||||||
# DELETE
|
# DELETE
|
||||||
with patch.object(service, "require_auth", return_value=True):
|
with patch.object(service, 'require_auth', return_value=True):
|
||||||
delete_result = service.delete_player(player_id, "valid_token")
|
delete_result = service.delete_player(player_id, 'valid_token')
|
||||||
assert "deleted" in str(delete_result)
|
assert 'deleted' in str(delete_result)
|
||||||
|
|
||||||
# VERIFY DELETED
|
# VERIFY DELETED
|
||||||
get_result = service.get_player(player_id)
|
get_result = service.get_player(player_id)
|
||||||
@ -550,13 +510,13 @@ class TestPlayerServiceIntegration:
|
|||||||
"""Search and then filter operations."""
|
"""Search and then filter operations."""
|
||||||
# First get all players
|
# First get all players
|
||||||
all_result = service.get_players(season=10)
|
all_result = service.get_players(season=10)
|
||||||
initial_count = all_result["count"]
|
initial_count = all_result['count']
|
||||||
|
|
||||||
# Then filter by team
|
# Then filter by team
|
||||||
filtered = service.get_players(season=10, team_id=[1])
|
filtered = service.get_players(season=10, team_id=[1])
|
||||||
|
|
||||||
# Filtered should be <= all
|
# Filtered should be <= all
|
||||||
assert filtered["count"] <= initial_count
|
assert filtered['count'] <= initial_count
|
||||||
|
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
|
|||||||
@ -1,154 +0,0 @@
|
|||||||
"""
|
|
||||||
Tests for query limit/offset parameter validation and middleware behavior.
|
|
||||||
|
|
||||||
Verifies that:
|
|
||||||
- FastAPI enforces MAX_LIMIT cap (returns 422 for limit > 500)
|
|
||||||
- FastAPI enforces ge=1 on limit (returns 422 for limit=0 or limit=-1)
|
|
||||||
- Transactions endpoint returns limit/offset keys in the response
|
|
||||||
- strip_empty_query_params middleware treats ?param= as absent
|
|
||||||
|
|
||||||
These tests exercise FastAPI parameter validation which fires before any
|
|
||||||
handler code runs, so most tests don't require a live DB connection.
|
|
||||||
|
|
||||||
The app imports redis and psycopg2 at module level, so we mock those
|
|
||||||
system-level packages before importing app.main.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import pytest
|
|
||||||
from unittest.mock import MagicMock, patch
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Stub out C-extension / system packages that aren't installed in the test
|
|
||||||
# environment before any app code is imported.
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
_redis_stub = MagicMock()
|
|
||||||
_redis_stub.Redis = MagicMock(return_value=MagicMock(ping=MagicMock(return_value=True)))
|
|
||||||
sys.modules.setdefault("redis", _redis_stub)
|
|
||||||
|
|
||||||
_psycopg2_stub = MagicMock()
|
|
||||||
sys.modules.setdefault("psycopg2", _psycopg2_stub)
|
|
||||||
|
|
||||||
_playhouse_pool_stub = MagicMock()
|
|
||||||
sys.modules.setdefault("playhouse.pool", _playhouse_pool_stub)
|
|
||||||
_playhouse_pool_stub.PooledPostgresqlDatabase = MagicMock()
|
|
||||||
|
|
||||||
_pandas_stub = MagicMock()
|
|
||||||
sys.modules.setdefault("pandas", _pandas_stub)
|
|
||||||
_pandas_stub.DataFrame = MagicMock()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
|
||||||
def client():
|
|
||||||
"""
|
|
||||||
TestClient with the Peewee db object mocked so the app can be imported
|
|
||||||
without a running PostgreSQL instance. FastAPI validates query params
|
|
||||||
before calling handler code, so 422 responses don't need a real DB.
|
|
||||||
"""
|
|
||||||
mock_db = MagicMock()
|
|
||||||
mock_db.is_closed.return_value = False
|
|
||||||
mock_db.connect.return_value = None
|
|
||||||
mock_db.close.return_value = None
|
|
||||||
|
|
||||||
with patch("app.db_engine.db", mock_db):
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
from app.main import app
|
|
||||||
|
|
||||||
with TestClient(app, raise_server_exceptions=False) as c:
|
|
||||||
yield c
|
|
||||||
|
|
||||||
|
|
||||||
def test_limit_exceeds_max_returns_422(client):
|
|
||||||
"""
|
|
||||||
GET /api/v3/decisions with limit=1000 should return 422.
|
|
||||||
|
|
||||||
MAX_LIMIT is 500; the decisions endpoint declares
|
|
||||||
limit: int = Query(ge=1, le=MAX_LIMIT), so FastAPI rejects values > 500
|
|
||||||
before any handler code runs.
|
|
||||||
"""
|
|
||||||
response = client.get("/api/v3/decisions?limit=1000")
|
|
||||||
assert response.status_code == 422
|
|
||||||
|
|
||||||
|
|
||||||
def test_limit_zero_returns_422(client):
|
|
||||||
"""
|
|
||||||
GET /api/v3/decisions with limit=0 should return 422.
|
|
||||||
|
|
||||||
Query(ge=1) rejects zero values.
|
|
||||||
"""
|
|
||||||
response = client.get("/api/v3/decisions?limit=0")
|
|
||||||
assert response.status_code == 422
|
|
||||||
|
|
||||||
|
|
||||||
def test_limit_negative_returns_422(client):
|
|
||||||
"""
|
|
||||||
GET /api/v3/decisions with limit=-1 should return 422.
|
|
||||||
|
|
||||||
Query(ge=1) rejects negative values.
|
|
||||||
"""
|
|
||||||
response = client.get("/api/v3/decisions?limit=-1")
|
|
||||||
assert response.status_code == 422
|
|
||||||
|
|
||||||
|
|
||||||
def test_transactions_has_limit_in_response(client):
|
|
||||||
"""
|
|
||||||
GET /api/v3/transactions?season=12 should include 'limit' and 'offset'
|
|
||||||
keys in the JSON response body.
|
|
||||||
|
|
||||||
The transactions endpoint was updated to return pagination metadata
|
|
||||||
alongside results so callers know the applied page size.
|
|
||||||
"""
|
|
||||||
mock_qs = MagicMock()
|
|
||||||
mock_qs.count.return_value = 0
|
|
||||||
mock_qs.where.return_value = mock_qs
|
|
||||||
mock_qs.order_by.return_value = mock_qs
|
|
||||||
mock_qs.offset.return_value = mock_qs
|
|
||||||
mock_qs.limit.return_value = mock_qs
|
|
||||||
mock_qs.__iter__ = MagicMock(return_value=iter([]))
|
|
||||||
|
|
||||||
with (
|
|
||||||
patch("app.routers_v3.transactions.Transaction") as mock_txn,
|
|
||||||
patch("app.routers_v3.transactions.Team") as mock_team,
|
|
||||||
patch("app.routers_v3.transactions.Player") as mock_player,
|
|
||||||
):
|
|
||||||
mock_txn.select_season.return_value = mock_qs
|
|
||||||
mock_txn.select.return_value = mock_qs
|
|
||||||
mock_team.select.return_value = mock_qs
|
|
||||||
mock_player.select.return_value = mock_qs
|
|
||||||
|
|
||||||
response = client.get("/api/v3/transactions?season=12")
|
|
||||||
|
|
||||||
# If the mock is sufficient the response is 200 with pagination keys;
|
|
||||||
# if some DB path still fires we at least confirm limit param is accepted.
|
|
||||||
assert response.status_code != 422
|
|
||||||
if response.status_code == 200:
|
|
||||||
data = response.json()
|
|
||||||
assert "limit" in data, "Response missing 'limit' key"
|
|
||||||
assert "offset" in data, "Response missing 'offset' key"
|
|
||||||
|
|
||||||
|
|
||||||
def test_empty_string_param_stripped(client):
|
|
||||||
"""
|
|
||||||
Query params with an empty string value should be treated as absent.
|
|
||||||
|
|
||||||
The strip_empty_query_params middleware rewrites the query string before
|
|
||||||
FastAPI parses it, so ?league_abbrev= is removed entirely rather than
|
|
||||||
forwarded as an empty string to the handler.
|
|
||||||
|
|
||||||
Expected: the request is accepted (not 422) and the empty param is ignored.
|
|
||||||
"""
|
|
||||||
mock_qs = MagicMock()
|
|
||||||
mock_qs.count.return_value = 0
|
|
||||||
mock_qs.where.return_value = mock_qs
|
|
||||||
mock_qs.__iter__ = MagicMock(return_value=iter([]))
|
|
||||||
|
|
||||||
with patch("app.routers_v3.standings.Standings") as mock_standings:
|
|
||||||
mock_standings.select_season.return_value = mock_qs
|
|
||||||
|
|
||||||
# ?league_abbrev= should be stripped → treated as absent (None), not ""
|
|
||||||
response = client.get("/api/v3/standings?season=12&league_abbrev=")
|
|
||||||
|
|
||||||
assert response.status_code != 422, (
|
|
||||||
"Empty string query param caused a 422 — middleware may not be stripping it"
|
|
||||||
)
|
|
||||||
Loading…
Reference in New Issue
Block a user