
    \iR                         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j>                  e       Z! G d de	      Z"y)zData ingestion agent    )Session)datetime	timedeltaN)Optional)	BaseAgent)BaseDataSource)FlashscoreSource)
MockSource)settings)MatchRepository)TeamRepository)LeagueRepository)MatchMatchStatus)Team)Leaguec                   4     e Zd ZdZdef fdZdefdZ xZS )DataIngestAgentz4Agent for ingesting match data from external sourcesdbc                    t         |   |d       t        |      | _        t	        |      | _        t        |      | _        t        j                  r	 t               | _        y t#               | _        y # t        $ rK}| j                  j                          t        j!                  d|        t#               | _        Y d }~y d }~ww xY w)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__s      $/app/app/agents/data_ingest_agent.pyr   zDataIngestAgent.__init__   s    ./)"-'++B/ &&03C3E   *|D  0  "!NqcRS#-<  0s   A3 3	C<ACCreturnc                 `	   | j                         }d}d}d}	 | j                  j                  d      }t        j	                  dt        |       d       |dd D ]  }| j                  j                  |j                        }|sd}|j                  r/| j                  j                  |j                        j                  }t        |j                  |j                  |d|j                         }| j                  j                  |      }|d	z  }t        j                          }	|	t#        d
      z   }
| j                  j%                  |j                  |	|
      }t        j	                  dt        |       d|j                          |D ]  }| j&                  j)                  |j                   d|j*                   |j*                        }| j&                  j)                  |j                   d|j,                   |j,                        }| j.                  j                  |j                        }|r}|j0                  |_        |j2                  |j2                  |_        |j4                  |j4                  |_        |j6                  |_        | j.                  j9                  |      }|d	z  }nt;        |j                  |j                  |j                  |j6                  |j0                  |j2                  |j4                  |j                  |j<                  	      }| j.                  j                  |      }|d	z  }|d	z  }  | j>                  jA                  |      }|r/||_!        ||_"        ||_#        | jH                  jK                          | jM                  d       t        j	                  d| d| d|        	 tO        | j                  d      r| j                  jQ                          yy# tR        $ rs}tU        |      }t        jW                  d| d       | jM                  d|       Y d}~tO        | j                  d      r| j                  jQ                          yyd}~ww xY w# tO        | j                  d      r| j                  jQ                          w w xY 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   r9   r:   r;   r8   updater   r<   run_repo	get_by_idmatches_processedrecords_createdrecords_updatedr   commit
finish_runhasattrr>   r   strr@   )r"   run_idrR   rS   rT   leagues_dataleague_dataleaguer-   r2   r3   matches_data
match_data	home_team	away_teamexisting_matchmatchrunr#   	error_msgs                       r%   rc   zDataIngestAgent.run)   s7   !^	)++77
CLKK&\!2 38<=+BQ/))::;;P;PQ!%J"**%)%5%5%K%KKL_L_%`%c%c
#"-"7"7(--#-&{'<'<&=>	F "--44V<F#q(O %LLN	#iR&88#//;;))'#  <   fS%6$7}V[[MRS".J $ I I&001:3L3L2MN"11!I !% I I&001:3L3L2MN"11!I &*__%E%EjFZFZ%[N%0:0A0A-%00<8B8M8MN5%00<8B8M8MN54>4I4I1)-)?)?)O'1, !&&,ii)2)2'1'<'<#-#4#4'1'<'<'1'<'<&0&:&:'1'<'<
! !% 6 6u ='1,%*%Q #/9  0N --))&1C(9%&5#&5# OODO)KK)$5#66HHYYcdsctuv t''1  &&( 2  	AILL29+>LNOOEO;t''1  &&( 2	 t''1  &&( 2s*   N0O; ;	Q78Q2<Q: 2Q77Q: :3R-)	__name__
__module____qualname____doc__r   r   boolrc   __classcell__)r$   s   @r%   r   r      s     >,7 ,"e)T e)    r   )#rh   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   	getLoggerre   r    r    rk   r%   <module>r|      sU     " (   % 0 ? 3 $ E C G 7 ( +			8	$y)i y)rk   