Skip to content

Commit

Permalink
added new things so I am going to commit these new things
Browse files Browse the repository at this point in the history
  • Loading branch information
day-mon committed Apr 27, 2024
1 parent d0aa5e7 commit f0ef14a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 14 deletions.
6 changes: 4 additions & 2 deletions api/api/business/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ def accuracy_statement(self) -> str:
return f"""
SELECT
model_name,
(COUNT(winner) FILTER (WHERE (winner = home_team_name AND prediction = home_team_name) OR (winner = away_team_name AND prediction = away_team_name))) * 100.0 / COUNT(winner) AS win_rate
(COUNT(winner) FILTER (WHERE (winner = home_team_name AND prediction = home_team_name) OR (winner = away_team_name AND prediction = away_team_name))) * 100.0 / COUNT(winner) AS win_rate,
COUNT(winner) AS total_games,
COUNT(winner) FILTER (WHERE (winner = home_team_name AND prediction = home_team_name) OR (winner = away_team_name AND prediction = away_team_name)) AS total_correct
FROM
saved_games
WHERE
Expand Down Expand Up @@ -263,7 +265,7 @@ def accuracy_statement(self) -> str:
FROM
saved_games
WHERE
prediction ~ '^\d+$'
prediction ~ '^\\d+$'
AND model_name = $1
GROUP BY
model_name"""
Expand Down
10 changes: 7 additions & 3 deletions api/api/model/model/accuracy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from asyncpg import Record
from pydantic import ConfigDict, BaseModel

Expand All @@ -7,14 +9,16 @@

class AccuracyModel(BaseModel):
model_name: str
accuracy: float
model_config = ConfigDict(protected_namespaces=())
win_rate: float
total_games: Optional[int] = None
total_correct: Optional[int] = None
model_config = ConfigDict(protected_namespaces=(), extra="allow")

@staticmethod
async def from_db(model: PredictionModel, db: Database) -> "AccuracyModel":
query_dates: list[Record] = await db.query(
model.accuracy_statement(), values=[model.model_name]
)
return AccuracyModel(
model_name=query_dates[0]["model_name"], accuracy=query_dates[0]["win_rate"]
**query_dates[0],
)
47 changes: 47 additions & 0 deletions api/api/model/model/saved_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Optional

from pydantic import BaseModel, ConfigDict, field_validator, ValidationInfo, computed_field

from api.business.database import Database


class SavedGame(BaseModel):
game_id: str
date: str
home_team_name: str
home_team_score: int
away_team_name: str
away_team_score: int
model_name: str
prediction: str | int

@computed_field
@property
def prediction_was_correct(self) -> Optional[bool]:
if self.model_name == "ou":
return None

winner = self.home_team_name if self.home_team_score > self.away_team_score else self.away_team_name
return winner == self.prediction

@classmethod
def check_prediction(cls, value, values: ValidationInfo):
if values.data.get("model_name") == "ou":
return int(value)

return value

model_config = ConfigDict(protected_namespaces=(), extra="allow")

@staticmethod
async def from_db(
db: Database,
date: str,
model_name: str
) -> list["SavedGame"]:
query = f"""
SELECT * FROM saved_games
WHERE date = $1
AND model_name = $2"""
response = await db.query(query, values=[date, model_name])
return [SavedGame(**game) for game in response]
51 changes: 42 additions & 9 deletions api/api/routes/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from api.business.model import PredictionModelFactory, PredictionModel, Prediction
from api.model.model.accuracy import AccuracyModel
from api.model.model.date_model import DateModel
from api.model.model.saved_game import SavedGame
from api.utils.daily_game import get_cache_key
from resources.config.application import AppSettings, get_settings
from resources.config.cache import CacheSettings, get_cache_settings
Expand Down Expand Up @@ -41,9 +42,9 @@ async def list_models() -> list[str]:
},
)
async def predict(
name: str,
app_settings: AppSettings = Depends(get_settings),
cache_setting: CacheSettings = Depends(get_cache_settings),
name: str,
app_settings: AppSettings = Depends(get_settings),
cache_setting: CacheSettings = Depends(get_cache_settings),
) -> list[Prediction]:
if name not in PredictionModelFactory.keys():
logger.debug(f"Requested {app_settings.MODEL_DIR}/{name}")
Expand Down Expand Up @@ -98,9 +99,9 @@ async def predict(

@router.get("/accuracy/{name}")
async def history(
name: str,
app_settings: AppSettings = Depends(get_settings),
db_settings: DatabaseSettings = Depends(get_database_settings),
name: str,
app_settings: AppSettings = Depends(get_settings),
db_settings: DatabaseSettings = Depends(get_database_settings),
) -> AccuracyModel:
model_names: list[str] = PredictionModelFactory.keys()
if name not in model_names:
Expand All @@ -124,17 +125,49 @@ async def history(

@router.get("/history/dates")
async def history_dates(
db_settings: DatabaseSettings = Depends(get_database_settings),
db_settings: DatabaseSettings = Depends(get_database_settings),
) -> list[DateModel]:
db: Database = DatabaseFactory.compute_or_get(
name=db_settings.DATABASE_TYPE,
)

model_names: list[str] = PredictionModelFactory.keys()

dates = await DateModel.from_db(
return await DateModel.from_db(
models=model_names,
db=db,
)

return dates

@router.get("/history")
async def history_by_date(
date: str,
model_name: str,
db_settings: DatabaseSettings = Depends(get_database_settings),
) -> list[SavedGame]:
db: Database = DatabaseFactory.compute_or_get(
name=db_settings.DATABASE_TYPE,
)
return await SavedGame.from_db(
db=db,
date=date,
model_name=model_name,
)


@router.get("/history/{date}")
async def history_date(
date: str,
db_settings: DatabaseSettings = Depends(get_database_settings),
) -> list[AccuracyModel]:
db: Database = DatabaseFactory.compute_or_get(
name=db_settings.DATABASE_TYPE,
)

model_names: list[str] = PredictionModelFactory.keys()

return await AccuracyModel.from_db_date(
date=date,
models=model_names,
db=db,
)

0 comments on commit f0ef14a

Please sign in to comment.