o
    i                     @   sH  d dl m Z mZmZ d dlZd dlmZmZ d dlZd dl	m
Z
 d dlmZmZmZ ddlmZmZmZmZmZ d dlmZ d dlZd d	lmZ G d
d de
ZeeZedefddZeedZe ddd Z!e"ddd Z#e$ddefddZ%e"ddd Z&e"dde'fdd Z(e"d!de'fd"d#Z)dS )$    )time	localtimestrftimeN)FastAPIHTTPException)	BaseModel)ListDictAny   )
load_modelpredictupdate_model_dataget_player_datamodel_retarin)asynccontextmanager)setup_loggingc                   @   s   e Zd ZU eed< dS )PredictionRequestgwN)__name__
__module____qualname__int__annotations__ r   r   //var/www/html/fantasy/fantasy_model/app/main.pyr      s   
 r   appc              
   C  sx   t   td zt }|std tdW n ty1 } ztd|  tdd}~ww dV  td dS )zG
    Lifespan event to load the model when the application starts.
    z1Starting FastAPI application and loading model...zModel loading failed.z$Failed to load the model on startup.zError loading model: z6Application startup failed due to model loading error.Nz'FastAPI application is shutting down...)r   loggerinfor   errorRuntimeError	Exception)r   modeler   r   r   lifespan   s    

r$   )r$   httpc                    s   t t }t }tjd|| jt | jt | jdt	dt
|dd || I d H }t | }tjd||j|ddd |S )	NzIncoming Request)methodurlclientz%Y-%m-%d %H:%M:%S)
request_idrequest	timestamp)extrazRequest Finished)status_codeprocess_time)r)   response)struuidZuuid4r   r   r   r&   r'   r(   r   r   r-   )r*   Z	call_nextr)   Z
start_timer/   r.   r   r   r   log_requests)   s(   


r2   /c                   C   s   ddiS )Nmessagez"Welcome to the FPL Prediction API!r   r   r   r   r   	read_rootD   s   r5   z/predictr*   c              
   C   s   t d| j  | jst d tddd| j}t d| d zt|}t d| d	 |jd
dW S  tyS } zt d|  tdt|dd}~ww )z
    Endpoint to make predictions using the pre-trained model.
    
    :param request: PredictionRequest containing the features for prediction.
    :return: Dictionary with predictions.
    z#Get Player XP Predictions for GW : z$No gameweek provided for prediction.i  z$No features provided for prediction.)r-   Zdetailz#Prediction request received for GW .zPrediction for GW z completed successfully.records)ZorientzPrediction error: N)	r   r   r   r   r   r   Zto_dict
ValueErrorr0   )r*   r   Zpredictionsr#   r   r   r   predict_endpointG   s   
r9   z/updatec                   C   s   t d t  t d d S )NzStarting Data Updatez#Data Update completed successfully.)r   r   r   r   r   r   r   update_datag   s   
r:   z/gw_data/{gw}r   c                 C   s0   t d|  d t| }t d|  d |S )NzFetching GW z Players Dataz% Players Data completed successfully.)r   r   r   )r   Zplayers_datar   r   r   gw_datam   s   r;   z/retrain/{gw}c                 C   s    t d t|  t d d S )NzStarting Model Retrainz%Model Retrain completed successfully.)r   r   r   )r   r   r   r   retraint   s   
r<   )*r   r   r   r1   Zfastapir   r   ZpandaspdZpydanticr   typingr   r	   r
   Z
core.modelr   r   r   r   r   
contextlibr   loggingZapp.logging_configr   r   	getLoggerr   r   r$   r   
middlewarer2   getr5   Zpostr9   r:   r   r;   r<   r   r   r   r   <module>   s6   




