sghorbal commited on
Commit
fb9b2b8
·
1 Parent(s): d25bf89

expose tournaments names, years and matches

Browse files
Files changed (1) hide show
  1. src/main.py +54 -2
src/main.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  import joblib
3
  import logging
4
  import secrets
5
- from typing import Literal, Optional, Annotated
6
  from datetime import datetime
7
  from fastapi import (
8
  FastAPI,
@@ -203,13 +203,65 @@ async def list_available_models():
203
  return list_registered_models()
204
 
205
 
206
- @app.get("/{circuit}/tournaments", tags=["reference"], description="List the tournaments of the circuit", response_model=list[Tournament])
207
  async def list_tournaments(circuit: Literal["atp", "wta"]):
208
  """
209
  List the tournaments of the circuit
210
  """
211
  return _list_tournaments(circuit)
212
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
  # Get a match
214
  @app.get("/match/{match_id}", tags=["match"], description="Get a match from the database", response_model=MatchApiDetail)
215
  async def get_match(
 
2
  import joblib
3
  import logging
4
  import secrets
5
+ from typing import Literal, Optional, Annotated, List, Dict
6
  from datetime import datetime
7
  from fastapi import (
8
  FastAPI,
 
203
  return list_registered_models()
204
 
205
 
206
+ @app.get("/{circuit}/tournaments", tags=["tournament"], description="List the tournaments of the circuit", response_model=List[Tournament])
207
  async def list_tournaments(circuit: Literal["atp", "wta"]):
208
  """
209
  List the tournaments of the circuit
210
  """
211
  return _list_tournaments(circuit)
212
 
213
+ # List all the tournament names and years
214
+ @app.get("/tournament/names", tags=["tournament"], description="List all the tournament names and years", response_model=List[Dict])
215
+ async def list_tournament_names(
216
+ session: Annotated[Session, Depends(get_session)]
217
+ ):
218
+ """
219
+ List all the tournament names and first and last year of occurrence
220
+ """
221
+ # Get all the tournament names
222
+ # and first and last year of occurrence
223
+ tournaments = session.query(Match.tournament_name).distinct().all()
224
+
225
+ output = []
226
+ # Get the first and last year of occurrence
227
+ for tournament in sorted([tournament[0] for tournament in tournaments], key=str.lower):
228
+ first_year = session.query(Match.date).filter(Match.tournament_name == tournament).order_by(Match.date.asc()).first()
229
+ last_year = session.query(Match.date).filter(Match.tournament_name == tournament).order_by(Match.date.desc()).first()
230
+ if first_year and last_year:
231
+ output.append({
232
+ "name": tournament,
233
+ "first_year": first_year[0].year,
234
+ "last_year": last_year[0].year
235
+ })
236
+ else:
237
+ output.append({
238
+ "name": tournament,
239
+ "first_year": None,
240
+ "last_year": None
241
+ })
242
+
243
+ return output
244
+
245
+ # Get all the matches from a tournament
246
+ @app.get("/tournament/matches", tags=["tournament"], description="Get all the matches from a tournament", response_model=List[MatchApiBase])
247
+ async def search_tournament_matches(
248
+ name: str,
249
+ year: int,
250
+ session: Annotated[Session, Depends(get_session)]
251
+ ):
252
+ """
253
+ Get all the matches from a tournament
254
+ """
255
+ start_date = datetime(year, 1, 1)
256
+ end_date = datetime(year, 12, 31)
257
+ matches = session.query(Match).filter(
258
+ Match.tournament_name == name,
259
+ Match.date.between(start_date, end_date)
260
+ ).all()
261
+
262
+ return sorted(matches, key=lambda x: x.date, reverse=True)
263
+
264
+
265
  # Get a match
266
  @app.get("/match/{match_id}", tags=["match"], description="Get a match from the database", response_model=MatchApiDetail)
267
  async def get_match(