Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monitoreo y logeo #30

Merged
merged 6 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions app/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

def get_current_user(request: Request, db: Session = Depends(get_db)):
user_id = request.session.get("user_id")
if user_id is None:
raise HTTPException(
status_code=302, detail="Not authenticated", headers={"Location": "/login"}
)

user = db.get(User, user_id)
return user
if user_id:
user = db.get(User, user_id)
return user
return None
108 changes: 68 additions & 40 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import logging
import os
import time
from typing import Dict, List

from fastapi import FastAPI, Form, Request, Depends, HTTPException
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.templating import Jinja2Templates
Expand All @@ -15,23 +18,41 @@
from app.schemas import PlayerCreate
from app.team_optimizer import find_best_combination

#app = FastAPI()
app = FastAPI(docs_url=None, redoc_url=None)

secret_key = os.getenv("SECRET_KEY")

app.add_middleware(SessionMiddleware, secret_key=secret_key)
app.mount("/static", StaticFiles(directory="static"), name="static")


templates = Jinja2Templates(directory="templates")

# Configura el nivel de logging para el módulo específico
logging.getLogger('libsql_client.dbapi2._async_executor').setLevel(logging.WARNING)
logging.getLogger('libsql_client.dbapi2._sync_executor').setLevel(logging.WARNING)
logging.getLogger('libsql_client.dbapi2.types').setLevel(logging.WARNING)

# Configuración general de logging (opcional)
logging.basicConfig(level=logging.INFO)

@app.middleware("http")
async def measure_execution_time(request: Request, call_next):
ignore_paths = ["/static", "/favicon.ico", "/sm/"]

if not any(request.url.path.startswith(prefix) for prefix in ignore_paths):
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
logging.info(f" {process_time:.4f} seconds to process request: {request.method} {request.url.path}")
return response
else:
return await call_next(request)


@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
if exc.status_code in [404, 405]: # Not Found y Method Not Allowed
return RedirectResponse(url="/")
if exc.status_code == 302:
return RedirectResponse(url=exc.headers["Location"], status_code=302)
raise exc


Expand Down Expand Up @@ -86,25 +107,21 @@ async def login(
return RedirectResponse(url="/", status_code=302)


calculated_results: Dict[str, dict] = {}

@app.get("/", response_class=HTMLResponse)
async def get_form(
request: Request,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
if not current_user:
request.session.clear()
return RedirectResponse("/login", status_code=302)

current_user_id = current_user.id
players = db.query(Player).filter(Player.user_id == current_user_id).all()

# Calcular el puntaje total de cada jugador
for player in players:
player.total_score = (
player.velocidad + player.resistencia + player.control + player.pases +
player.tiro + player.defensa + player.habilidad_arquero + player.fuerza_cuerpo + player.vision
)

# Verificar si hay resultados calculados para este usuario
context = {
"request": request,
Expand All @@ -119,18 +136,15 @@ async def get_form(

return templates.TemplateResponse(request=request, name="index.html", context=context)

calculated_results: Dict[str, dict] = {}

@app.post("/submit", response_class=HTMLResponse)
async def submit_form(request: Request, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
start_time_1 = time.time()
current_user_id = current_user.id
form_data = await request.form()
list_players = form_data._list

cant_jug = 0
for tupla in list_players:
if tupla[0] == "names":
cant_jug += 1
cant_jug = sum(1 for tupla in list_players if tupla[0] == "names")

player_data: List[PlayerCreate] = []
for i in range(cant_jug):
Expand All @@ -154,17 +168,26 @@ async def submit_form(request: Request, db: Session = Depends(get_db), current_u
player_data.append(player)

# Guardar o actualizar jugadores en la base de datos
existing_players = db.query(Player).filter(Player.user_id == current_user_id).all()
existing_players_dict = {player.name: player for player in existing_players}

players_to_add = []
for player in player_data:
db_player = db.query(Player).filter(Player.name == player.name, Player.user_id == current_user_id).first()
db_player = existing_players_dict.get(player.name)
if db_player:
for key, value in player.dict().items():
for key, value in player.model_dump().items():
setattr(db_player, key, value)
else:
db_player = Player(**player.dict())
db.add(db_player)
players_to_add.append(Player(**player.model_dump()))

if players_to_add:
db.add_all(players_to_add)

db.commit()

players = db.query(Player).filter(Player.user_id == current_user_id).all()
process_time_1 = time.time() - start_time_1
logging.info(f" {process_time_1:.4f} seconds to save or update players in the database")
start_time_2 = time.time()

# Calcular equipos
player_names = [p.name for p in player_data]
Expand Down Expand Up @@ -192,14 +215,14 @@ async def submit_form(request: Request, db: Session = Depends(get_db), current_u
teams.append([[player_names[i] for i in list(equipos[0])]])
teams.append([[player_names[i] for i in list(equipos[1])]])

# Calcular el puntaje total de cada jugador
for player in players:
player.total_score = (
player.velocidad + player.resistencia + player.control + player.pases +
player.tiro + player.defensa + player.habilidad_arquero + player.fuerza_cuerpo + player.vision
)

# Calcular el total de skills de cada equipo
process_time_2 = time.time() - start_time_2
logging.info(f" {process_time_2:.4f} seconds to calculate the best teams")
start_time_3 = time.time()

# Calculate the total and average skills for each team
players = db.query(Player).filter(Player.user_id == current_user_id).all()
player_data_dict = {player.name: player for player in players}

for team in teams:
team_skills = {
"velocidad": {"total": 0, "avg": 0},
Expand All @@ -212,26 +235,31 @@ async def submit_form(request: Request, db: Session = Depends(get_db), current_u
"fuerza_cuerpo": {"total": 0, "avg": 0},
"vision": {"total": 0, "avg": 0}
}

for player in team[0]:
player_data = db.query(Player).filter(Player.name == player).first()
for key, value in team_skills.items():
team_skills[key]["total"] += getattr(player_data, key)
team_skills[key]["avg"] = str(round(team_skills[key]["total"] / len(team[0]), 2)).replace(".", ",")

team.append([team_skills,
sum([team_skills[key]["total"] for key in team_skills]),
str(round(sum([team_skills[key]["total"] / len(team[0]) for key in team_skills]), 2)).replace(".", ",")])
player_data = player_data_dict[player]
player_attrs = {key: getattr(player_data, key) for key in team_skills}
for key, value in player_attrs.items():
team_skills[key]["total"] += value

num_players = len(team[0])
for key in team_skills:
team_skills[key]["avg"] = str(round(team_skills[key]["total"] / num_players, 2)).replace(".", ",")

total_skills = sum(skill["total"] for skill in team_skills.values())
avg_skills = str(round(sum(skill["total"] / num_players for skill in team_skills.values()), 2)).replace(".", ",")

team.append([team_skills, total_skills, avg_skills])

calculated_results[current_user_id] = {
"players": players,
"teams": teams,
"len_teams": len(teams),
"min_difference": str(min_difference),
"min_difference_total": str(min_difference_total)
}

# Redirigir a la página principal
process_time_3 = time.time() - start_time_3
logging.info(f" {process_time_3:.4f} seconds to calculate the total skills of each team and return the results")
return RedirectResponse(url="/", status_code=303)


Expand Down
Loading