o
    ̿Si                     @   s   d dl Z d dl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mZ d dlmZ eeZe  Zed	Zed
dpLd  dvZddee dee fddZdededefddZdddZddee ddfddZdS )    N)datetime)	timedelta)Optional)ZoneInfo)get_mongo_db)_get_global_zerodha_client_is_market_hours_ist)#compute_market_intelligence_summaryzAsia/Kolkata(MARKET_INTELLIGENCE_ENFORCE_MARKET_HOURS1)0falsenooffnow_utcreturnc                 C   s   ddl m} | pt }z|jtddt}W n ty'   t	t}Y nw | }|j|j
|jddd}||krA|tdd }| dkrT|tdd }| dksG|S )	z0Return the next backend market open time in IST.r   )backend_market_windowUTCtzinfo)hourminutesecondmicrosecond   )days   )app.v1.utils.market_timer   r   utcnowreplacer   
astimezoneIST	Exceptionnowstart_hstart_mr   weekday)r   r   r#   ist_noww	candidate r*   C/var/www/html/Trade-python/app/v1/background/market_intelligence.py_next_backend_market_open_ist   s   r,   dt_fromdt_toc                 C   s6   z
t ||   }W n ty   d}Y nw td|S )Nr   r   )inttotal_secondsr"   max)r-   r.   secr*   r*   r+   _seconds_until-   s   
r3   c                  C   s  t  } t| }t }zzt|s,W W z|   W d S  ty+   tjddd Y d S w t	|}|sRt
d W W z|   W d S  tyQ   tjddd Y d S w t|||d}|d jddid	d|d
 |t didd |d |d
 |t d td|d|d|d W n ty   td Y nw W z|   W d S  ty   tjddd Y d S w z|   W w  ty   tjddd Y w w )Nz/[MarketIntelligence] Error closing DB generatorT)exc_infoz@[MarketIntelligence] Skipping: global Zerodha client unavailable)dbzerodha_clientcaptured_atmarket_intelligence_summarytypelatestz$setr7   )r9   r7   payload
updated_at)upsertmarket_intelligence_history)r7   r;   
created_atz5[MarketIntelligence] Updated | bias=%s vol=%s risk=%smarket_biasvolatility_stateoverall_riskz,[MarketIntelligence] Error computing summary)r   nextr   r   r   closer"   loggerdebugr   errorr	   
update_one
insert_oneinfoget	exception)db_genr5   r7   clientsummaryr*   r*   r+   &_refresh_market_intelligence_once_sync5   st   *
%rP   interval_secondsc              	      sR  t | p	tdd} tdI d H  td|  	 trjtt	
 sjtt	
 }|razt	
 jtddt}W n tyG   t	t}Y nw td|d	|d	 tt||I d H  qt| I d H  qt r|td
 t| I d H  qt4 I d H  ttI d H  W d   I d H  n1 I d H sw   Y  t| I d H  q)N$MARKET_INTELLIGENCE_INTERVAL_SECONDS900   z;[MarketIntelligence] Background loop started (interval=%ss)Tr   r   zP[MarketIntelligence] Skipped: outside backend market hours | now=%s next_open=%sz%Y-%m-%d %H:%Mz:[MarketIntelligence] Previous tick still running; skipping)r/   osgetenvasynciosleeprE   rJ   r
   r   r   r   r,   r   r   r    r!   r"   r#   strftimer3   _MARKET_INTELLIGENCE_LOCKlocked	to_threadrP   )rQ   nxtr'   r*   r*   r+   market_intelligence_loopl   s>   
(r^   )N)r   N)rW   loggingrU   r   r   typingr   zoneinfor   app.db.databaser   !app.v1.background.global_intradayr   r   #app.v1.services.market_intelligencer	   	getLogger__name__rE   LockrZ   r!   rV   striplowerr
   r,   r/   r3   rP   r^   r*   r*   r*   r+   <module>   s*    

7