o
    \iR                     @   s   d Z ddlmZ ddlmZmZ ddlZddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZmZ ddlmZ ddlmZ ee Z!G dd de	Z"dS )zData ingestion agent    )Session)datetime	timedeltaN)Optional)	BaseAgent)BaseDataSource)FlashscoreSource)
MockSource)settings)MatchRepository)TeamRepository)LeagueRepository)MatchMatchStatus)Team)Leaguec                       s4   e Zd ZdZdef fddZdefddZ  ZS )DataIngestAgentz4Agent for ingesting match data from external sourcesdbc              
      s   t  |d t|| _t|| _t|| _tj	rEzt
 | _W d S  tyD } z| j  td|  t | _W Y d }~d S d }~ww t | _d S )Nr   z-Failed to initialize Flashscore, using mock: )super__init__r   
match_repor   	team_repor   league_repor
   flashscore_enabledr   data_source	Exceptionr   rollbackloggerwarningr	   )selfr   e	__class__ app/agents/data_ingest_agent.pyr      s   



zDataIngestAgent.__init__returnc                 C   s  |   }d}d}d}zWz| jd}tdt| d |dd D ]}| j|j}|sUd}|j	r=| j
|j	j}t|j|j|d|j d}| j|}|d	7 }t }	|	td
d }
| jj|j|	|
d}tdt| d|j  |D ]r}| j|j d|j |j}| j|j d|j |j}| j|j}|r|j|_|jdur|j|_|jdur|j|_|j|_| j|}|d	7 }n t|j|j|j|j|j|j|j|j|jd	}| j|}|d	7 }|d	7 }qzq%| j |}|r||_!||_"||_#| j$%  | j&dd td| d| d|  W W t'| jdr)| j(  dS dS  t)yb } z+t*|}tj+d| dd | j&d|d W Y d}~W t'| jdr\| j(  dS dS d}~ww t'| jdrp| j(  w w )zRun data ingestionr   footballzFound z leaguesN   league_)	source_idname
country_idcode      )days)	from_dateto_datez matches for _)		league_idhome_team_idaway_team_id
match_datestatus
home_score
away_scorer)   
source_urlT)successz	Ingested z matches, created z
, updated closezData ingestion failed: )exc_infoF)r;   error),	start_runr   get_leaguesr   infolenr   get_by_source_idr)   countryget_or_create_countryidr   r*   creater   nowr   get_matchesr   get_or_create_by_source_idhome_team_nameaway_team_namer   r7   r8   r9   r6   updater   r:   run_repo	get_by_idmatches_processedrecords_createdrecords_updatedr   commit
finish_runhasattrr<   r   strr>   )r   run_idrP   rQ   rR   leagues_dataleague_dataleaguer+   r0   r1   matches_data
match_data	home_team	away_teamexisting_matchmatchrunr    	error_msgr#   r#   r$   ra   )   s   




+
zDataIngestAgent.run)	__name__
__module____qualname____doc__r   r   boolra   __classcell__r#   r#   r!   r$   r      s    r   )#rf   sqlalchemy.ormr   r   r   loggingtypingr   app.agents.baser   app.data_sources.baser   "app.data_sources.flashscore.sourcer   app.data_sources.mock_sourcer	   app.core.configr
   )app.core.db.repositories.match_repositoryr   (app.core.db.repositories.team_repositoryr   *app.core.db.repositories.league_repositoryr   app.core.db.models.matchr   r   app.core.db.models.teamr   app.core.db.models.sportr   	getLoggerrc   r   r   r#   r#   r#   r$   <module>   s$    
