o
    Bi'                     @  s   d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ eddd	Ze ZeeZed
dd Zedd-d.ddZedd/ddZedd0d1ddZedd2d3d"d#Zed$d4d5d(d)Zed*d/d+d,ZdS )6    )annotations)FastAPIHTTPException)DatabaseManager)AnalyzerAgent)get_insight)compute_metricszFootball AI Agents APIz1.0.0)titleversionz/healthc                     s
   ddiS )NokT r   r   r   1/var/www/www-root/data/www/77.83.87.30/api/app.pyhealth   s   r   z/matchesNd   status
str | Nonelimitintc                   s$   t j| |dI d H }dd |D S )N)r   r   c                 S  s>   g | ]}|j |j|j|j|j|j|j|j|j|j	|j
d qS )idr   
kickoff_ts	home_team	away_teamminute
home_score
away_score	match_urlcompetition_urllast_update_tsr   .0rr   r   r   
<listcomp>   s    
zmatches.<locals>.<listcomp>)dblist_matches)r   r   rowsr   r   r   matches   s
   r&   z/matches/{match_id}match_idstrc                   sN   t | I d H }|stdd|j|j|j|j|j|j|j	|j
|j|jd
S )N  match not found)
r   r   r   r   r   r   r   r   r   r   )r#   	get_matchr   r   r   r   r   r   r   r   r   r   r   )r'   r!   r   r   r   match!   s   
r,   z/predictions   c                   s"   t j| dI d H }dd |D S )Nr   c                 S  s6   g | ]}|j |j|j|j|j|j|j|j|jd 	qS )	r'   model
p_home_winp_draw
p_away_winexp_home_goalsexp_away_goals
confidence
created_tsr/   r   r   r   r   r"   6   s    

zpredictions.<locals>.<listcomp>)r#   list_predictions)r   r%   r   r   r   predictions3   s
   
r9   z/predictions/{match_id}poisson+formr0   c              
     sN   t j| |dI d H }|stdd|j|j|j|j|j|j|j	|j
|jd	S )Nr0   r)   zprediction not foundr/   )r#   get_prediction_for_matchr   r'   r0   r1   r2   r3   r4   r5   r6   r7   )r'   r0   r!   r   r   r   
predictionB   s   
r=   z/metricsF	recomputeboolc                   s   |r!t t| dI d H }tj| |d |d |d |d dI d H  tj| dI d H }|s4| ddddd	S |j|j|j|j|j|j	d
S )Nr;   	n_matchesaccuracybrierlogloss)r0   naccrB   rC   r   g        )r0   r@   rA   rB   rC   )r0   r@   rA   rB   rC   
updated_ts)
r   r#   save_metricsget_metricsr0   r@   rA   rB   rC   rF   )r0   r>   maggr   r   r   metricsS   s   *rK   z/insights/{match_id}c              	     s   t | I d H }|stddt j| ddI d H }|j|j|j|j|j|j	gd|s-d n|j
|j|j|j|j|jdtj|jddI d H tj|jddI d H tj|j|jd	d
I d H d}t|S )Nr)   r*   r:   r;   )homeawayr   r   score)r1   r2   r3   r4   r5   r6      )rD      r.   )r,   r=   	home_form	away_formhead_to_head)r#   r+   r   r<   r   r   r   r   r   r   r1   r2   r3   r4   r5   r6   analyzer	team_formh2hr   )r'   rI   predctxr   r   r   insightsd   s   

rY   )Nr   )r   r   r   r   )r'   r(   )r-   )r   r   )r:   )r'   r(   r0   r(   )r:   F)r0   r(   r>   r?   )
__future__r   fastapir   r   database.managerr   agents.analyzer_agentr   api.openai_clientr   analytics.metricsr   appr#   rT   getr   r&   r,   r9   r=   rK   rY   r   r   r   r   <module>   s.    
