o
    ̿Siy                     @   sf   d dl Z d dlmZ d dlmZmZ d dlZe eZdee	ef fddZ
dee	ef fddZdS )	    N)datetime)AnyDictreturnc                 C   s   zddl m} || }| \}}dd }||}||}W n ty0   td g }g }Y nw dt  ||d}| d j	d	did
|idd zt
|}d|d	< | d | W |S  tyf   Y |S w )zPopulate `db['movers']` with latest gainers/losers.

    This is used by background jobs. Implementation is intentionally
    defensive; on failure it stores empty lists.
    r   )TopMoversFetcherc                 S   sb   | sg S g }| D ]&}t |tr$dD ]}||r"|||  nqqt |tr.|| q|S )N)symbol
nse_symboltradingsymboltickername)
isinstancedictgetappendstr)arroutitemk r   4/var/www/html/Trade-python/app/v1/services/movers.py_norm   s   



z%refresh_movers_service.<locals>._normz<TopMoversFetcher not available or failed; using empty moverslatest)type
fetched_atgainerslosersmoversr   z$setT)upsertsnapshotmovers_history)app.v1.services.zerolive.listr   fetch_top_movers	Exceptionloggerinfor   utcnow	isoformat
update_oner   
insert_one)dbr   fetcherr   r   r   recordhistr   r   r   refresh_movers_service
   s6   

r.   c                 C   s\  | d  ddip
i }t|tr|dng }t|tr!|dng }dtdtt fdd	}||}||}tt	d
dp>d}|dkrP|d| }|d| }tt
g ||}|s{z	| d i  W n	 tyo   Y nw dddt  dS t| d d|idddddd}	i }
|	D ]#}|dpd  }|d}|rt|tr| r| |
|< qt }g }d}d}|D ]}|
|}|s|d7 }q||d||d |d7 }qd}|D ]}|
|}|s|d7 }q||d||d |d7 }qz| d i  |r| d | W n ty"   td Y nw dt||| dS )a  Rebuild `live_movers` from the latest ET movers snapshot (DB-only).

    IMPORTANT:
    - Must NOT call Zerodha, ET scraping, or GPT.
    - Only resolves symbols against the existing `stocks` master list.
    - If a symbol is not found in `stocks`, it is skipped.
    r   r   r   r   r   vr   c                 S   sN   t | tsg S g }| D ]}t |tsq|  }|r$||vr$|| q|S )N)r   listr   stripupperr   )r/   r   itsymr   r   r   
_norm_listM   s   


z:refresh_live_movers_from_latest_movers.<locals>._norm_listLIVE_MOVERS_LIMIT_PER_SIDE200r   Nlive_moversT)okbuiltskipped	timestampstocksz$inNSE)r   exchange   )_idr   stock_idr    rB   GAINER)rB   
mover_typeranklast_updatedLOSERz)Failed to refresh live_movers from movers)find_oner   r   r   r   r0   r   intosgetenvfromkeysdelete_manyr#   r   r&   r'   findr1   r2   r   insert_manyr$   	exceptionlen)r*   docgainers_raw
losers_rawr5   r   r   limit_per_sidesymbolsr=   	by_symbolsr4   sidnow	live_docsr;   rF   r   r   r   &refresh_live_movers_from_latest_movers@   sv   	





r]   )loggingr   typingr   r   rK   	getLogger__name__r$   r   r.   r]   r   r   r   r   <module>   s    
6