Spaces:
Running
Running
File size: 12,717 Bytes
7effb2a ca14792 7effb2a 77c6297 7effb2a 2ae523d 7effb2a 2ae523d 4d68178 2ae523d fe5a14c 7effb2a 4d68178 7effb2a 77c6297 d25bf89 6fe7076 77c6297 55da6e8 2ae523d 1fa825b 77c6297 1fa825b 7effb2a 8b78aa0 696d285 d6bd519 8b78aa0 55da6e8 8b78aa0 ca14792 dbd6b91 f924769 259a596 9316f10 f924769 259a596 f924769 259a596 f924769 8b78aa0 7effb2a ca14792 7effb2a 8b78aa0 7effb2a fb9b2b8 8b78aa0 fb9b2b8 3e66851 fb9b2b8 11e87b3 8b78aa0 11e87b3 8b78aa0 11e87b3 8b78aa0 11e87b3 6fe7076 8b78aa0 6fe7076 9e76472 8b78aa0 9e76472 fb9b2b8 8b78aa0 fb9b2b8 d25bf89 8b78aa0 d25bf89 4d68178 1fa825b 4d68178 1fa825b 4d68178 1fa825b 4d68178 1fa825b 77c6297 1fa825b 77c6297 1fa825b 8b2303c 77c6297 3869c24 1fa825b 8b78aa0 3869c24 77c6297 3869c24 1361de1 3869c24 2ae523d 7effb2a 8b78aa0 7effb2a 2ae523d 7effb2a 2ae523d d6bd519 aace7de 2ae523d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
import os
import logging
import secrets
from typing import Generator, Optional, Annotated, List, Dict, AsyncGenerator
from datetime import datetime
from contextlib import asynccontextmanager
from fastapi import (
FastAPI,
Request,
HTTPException,
Query,
Security,
Depends
)
from fastapi.responses import RedirectResponse, JSONResponse
from fastapi.security.api_key import APIKeyHeader
from starlette.status import (
HTTP_200_OK,
HTTP_403_FORBIDDEN,
HTTP_404_NOT_FOUND,
HTTP_422_UNPROCESSABLE_ENTITY,
HTTP_503_SERVICE_UNAVAILABLE)
from sqlalchemy import text
from sqlalchemy.orm import Session
from pydantic import BaseModel, Field
from dotenv import load_dotenv
from sqlalchemy.exc import IntegrityError
from src.api_factory import create_forward_endpoint, get_remote_params
from src.entity.match import (
Match,
RawMatch,
MatchApiBase,
MatchApiDetail
)
from src.entity.player import (
Player,
PlayerApiDetail,
)
from src.jobs.match import schedule_matches_ingestion
from src.jobs.views import schedule_refresh
from src.repository.common import get_session
from src.service.match import (
insert_new_match,
insert_batch_matches,
)
load_dotenv()
# ------------------------------------------------------------------------------
logging.basicConfig(level=logging.INFO,
handlers=[logging.StreamHandler()])
logger = logging.getLogger(__name__)
def provide_connection() -> Generator[Session, None, None]:
with get_session() as conn:
yield conn
# ------------------------------------------------------------------------------
# Ensure all the necessary jobs are scheduled
if os.getenv("REDIS_URL"):
schedule_refresh()
schedule_matches_ingestion(year=None)
# ------------------------------------------------------------------------------
TENNIS_ML_API = os.getenv("TENNIS_ML_API")
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
if not TENNIS_ML_API:
yield
return
# Create a forward endpoint for each endpoint in the remote API
endpoints = [
"run_experiment",
"predict",
"list_available_models"
]
for endpoint in endpoints:
endpoint_def = await get_remote_params(base_url=TENNIS_ML_API,
endpoint=endpoint,
method='get')
if not endpoint_def["found"]:
logger.warning(f"Endpoint {endpoint} not found in the remote API")
continue
# Create a forward endpoint for the remote API
forward_endpoint = create_forward_endpoint(base_url=TENNIS_ML_API,
_endpoint=endpoint,
param_defs=endpoint_def["params"])
app.add_api_route(
path=f'/{endpoint}',
endpoint=forward_endpoint,
methods=["GET"],
name=f"Forward to remote {forward_endpoint.__name__}",
tags=endpoint_def["general"]["tags"],
description=endpoint_def["general"]["description"],
summary=endpoint_def["general"]["summary"],
)
yield
# ------------------------------------------------------------------------------
FASTAPI_API_KEY = os.getenv("FASTAPI_API_KEY")
safe_clients = ['127.0.0.1']
api_key_header = APIKeyHeader(name='Authorization', auto_error=False)
async def validate_api_key(request: Request, key: str = Security(api_key_header)) -> None:
'''
Check if the API key is valid
Args:
key (str): The API key to check
Raises:
HTTPException: If the API key is invalid
'''
if request.client.host not in safe_clients and not secrets.compare_digest(str(key), str(FASTAPI_API_KEY)):
raise HTTPException(
status_code=HTTP_403_FORBIDDEN, detail="Unauthorized - API Key is wrong"
)
return None
app = FastAPI(dependencies=[Depends(validate_api_key)] if FASTAPI_API_KEY else None,
lifespan=lifespan if TENNIS_ML_API else None,
title="Tennis Insights API")
# ------------------------------------------------------------------------------
@app.get("/", include_in_schema=False)
def redirect_to_docs():
'''
Redirect to the API documentation.
'''
return RedirectResponse(url='/docs')
# List all the tournament names and years
@app.get("/tournament/names", tags=["tournament"], description="List all the tournament names and years", response_model=List[Dict])
async def list_tournament_names(
session: Session = Depends(provide_connection)
):
"""
List all the tournament names and first and last year of occurrence
"""
tournaments = session.execute(text("SELECT t.name, t.first_year, t.last_year FROM data.tournaments_list_m_view AS t")).all()
tournaments = [
{
"name": tournament[0],
"first_year": tournament[1],
"last_year": tournament[2]
}
for tournament in tournaments
]
return tournaments
@app.get("/references/courts", tags=["reference"], description="List all the courts")
async def list_courts(
session: Session = Depends(provide_connection)
):
"""
List all the courts
"""
courts = session.execute(text("SELECT name FROM data.ref_court_m_view")).all()
courts = [court[0] for court in courts]
return courts
@app.get("/references/surfaces", tags=["reference"], description="List all the surfaces")
async def list_surfaces(
session: Session = Depends(provide_connection)
):
"""
List all the surfaces
"""
surfaces = session.execute(text("SELECT name FROM data.ref_surface_m_view")).all()
surfaces = [surface[0] for surface in surfaces]
return surfaces
@app.get("/references/series", tags=["reference"], description="List all the series")
async def list_series(
session: Session = Depends(provide_connection)
):
"""
List all the series
"""
series = session.execute(text("SELECT name FROM data.ref_series_m_view")).all()
series = [serie[0] for serie in series]
return series
class ListPlayersInput(BaseModel):
ids: List[int] = Field(
description="List of player IDs",
)
# Get a list of players
@app.get("/players", tags=["player"], description="Get a list of players from the database", response_model=Dict[str|int, Optional[PlayerApiDetail]])
async def list_players(
params: Annotated[ListPlayersInput, Query()],
session: Session = Depends(provide_connection),
):
"""
Get a list of players from the database
"""
ids = sorted(params.ids)
players = session.query(Player).filter(Player.id.in_(ids)).all()
if not players:
raise HTTPException(
status_code=HTTP_404_NOT_FOUND,
detail=f"Players {ids} not found"
)
players = {player.id: player for player in players}
return {id: players.get(id) for id in ids}
@app.get("/player/{player_id}", tags=["player"], description="Get a player from the database", response_model=PlayerApiDetail)
async def get_player(
player_id: int,
session: Session = Depends(provide_connection)
):
"""
Get a player from the database
"""
player = session.query(Player).filter(Player.id == player_id).first()
if not player:
raise HTTPException(
status_code=HTTP_404_NOT_FOUND,
detail=f"Player {player_id} not found"
)
return player
# Get all the matches from a tournament
@app.get("/tournament/matches", tags=["tournament"], description="Get all the matches from a tournament", response_model=List[MatchApiBase])
async def search_tournament_matches(
name: str,
year: int,
session: Session = Depends(provide_connection)
):
"""
Get all the matches from a tournament
"""
start_date = datetime(year, 1, 1)
end_date = datetime(year, 12, 31)
matches = session.query(Match).filter(
Match.tournament_name == name,
Match.date.between(start_date, end_date)
).all()
return sorted(matches, key=lambda x: x.date, reverse=True)
# Get a match
@app.get("/match/{match_id}", tags=["match"], description="Get a match from the database", response_model=MatchApiDetail)
async def get_match(
match_id: int,
session: Session = Depends(provide_connection)
):
"""
Get a match from the database
"""
match = session.query(Match).filter(Match.id == match_id).first()
if not match:
raise HTTPException(
status_code=HTTP_404_NOT_FOUND,
detail=f"Match {match_id} not found"
)
return match
@app.post("/match/insert", tags=["match"], description="Insert a match into the database")
async def insert_match(
raw_match: RawMatch,
session: Session = Depends(provide_connection),
on_conflict_do_nothing: bool = False,
):
"""
Insert a match into the database
"""
try:
match = insert_new_match(
db=session,
raw_match=raw_match.model_dump(exclude_unset=True),
on_conflict_do_nothing=on_conflict_do_nothing,
)
except IntegrityError as e:
logger.error(f"Error inserting match: {e}")
raise HTTPException(
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
detail="Entity already exists in the database"
)
output = {
"status": "ok",
"match_id": match.id if match else None,
}
return JSONResponse(content=output, status_code=HTTP_200_OK)
@app.post("/match/ingest_year", tags=["match"], description="Ingest matches from tennis-data.co.uk for a given year")
async def ingest_matches(year: Optional[int] = None):
"""
Ingest matches from tennis-data.co.uk for a given year
"""
job = schedule_matches_ingestion(year=year)
return {"job_id": job.get_id(), "job_status": job.get_status()}
@app.post("/batch/match/insert", tags=["match"], description="Insert a batch of matches into the database")
async def insert_batch_match(
raw_matches: list[RawMatch],
on_conflict_do_nothing: bool = False,
session: Session = Depends(provide_connection)
):
"""
Insert a batch of matches into the database
"""
result = insert_batch_matches(db=session,
raw_matches=raw_matches,
on_conflict_do_nothing=on_conflict_do_nothing)
matches = result['matches']
nb_errors = result['nb_errors']
logger.info(f"Number of matches inserted: {len(matches)}")
if nb_errors > 0:
logger.warning(f"Number of errors: {nb_errors}")
return JSONResponse(
content={"status": "ok", "message": f"{len(matches)} matches inserted, {nb_errors} errors"},
status_code=HTTP_422_UNPROCESSABLE_ENTITY
)
else:
output = {
"status": "ok",
"match_ids": [match.id for match in matches],
}
return JSONResponse(content=output, status_code=HTTP_200_OK)
# ------------------------------------------------------------------------------
@app.get("/check_health", tags=["general"], description="Check the health of the API")
async def check_health(session: Session = Depends(provide_connection)):
"""
Check all the services in the infrastructure are working
"""
# Check if the database is alive
try:
session.execute(text("SELECT 1"))
except Exception as e:
logger.error(f"DB check failed: {e}")
return JSONResponse(content={"status": "unhealthy", "detail": "Database not reachable"},
status_code=HTTP_503_SERVICE_UNAVAILABLE)
# Check if the scraper endpoint is reachable
if FLARESOLVERR_API := os.getenv("FLARESOLVERR_API"):
import requests
try:
# Ping the scraper endpoint
response = requests.get(FLARESOLVERR_API + "health", timeout=5)
if response.status_code != HTTP_200_OK:
logger.error(f"Scraper check failed: {response.status_code}")
return JSONResponse(content={"status": "unhealthy", "detail": "Flaresolverr not reachable"},
status_code=HTTP_503_SERVICE_UNAVAILABLE)
except requests.RequestException as e:
logger.error(f"Scraper check failed: {e}")
return JSONResponse(content={"status": "unhealthy", "detail": "Flaresolverr not reachable"},
status_code=HTTP_503_SERVICE_UNAVAILABLE)
return JSONResponse(content={"status": "healthy"}, status_code=HTTP_200_OK)
|