o
    Bpj                     @  s   d Z ddlmZ ddlZddlmZmZmZmZm	Z	 ddl
mZ ddlmZ eeZe	eegef  ZdddZdddddZdddddZddddddZdS )zKAuto pause/resume per-BID orchestration when usage allocation is exhausted.    )annotationsN)AnyCallableDictListOptional)evaluate_usage_allocation)maybe_send_usage_validity_alertbidstrreturnboolc              
   C  sL   zddl m} || W S  ty% } ztd| | W Y d }~dS d }~ww )Nr   )	stop_loopz)[%s] Failed to stop orchestrator loop: %sF)orchestrator_supervisorr   	Exceptionloggerwarning)r
   r   exc r   A/home/aiteam/pcaa-dev/dashboard-backend/pipeline_usage_control.py_default_stop_loop   s   
r   )stop_loop_fnr   
StopLoopFnc             	   C  s  t | }|s
dS |   | |pi }t|dpds dS |  ;}| '}t||}|dsC	 W d   W d   dS t	||| W d   n1 sSw   Y  W d   n1 sbw   Y  t
d||d |d  | |dd	d
 |pt}|| dS )z
    Disable pipeline_enabled and stop the orchestrator loop when usage is blocked.
    Returns True if the pipeline was paused by this call.
    Fpipeline_enabledr   blockedNzDUsage limit exhausted for BID %s (%s/%s min); auto-stopping pipelineused_minutesmonthly_minute_limit   r   usage_limit_pausedT)r   strip%ensure_business_pipeline_config_tableget_pipeline_configintgetget_connectioncursorr   r	   r   r   save_pipeline_configr   )
db_handlerr
   r   cfgconnr&   usagestop_fnr   r   r   !pause_pipeline_if_usage_exhausted   sB   




	r-   )start_loop_fnr.   c             	   C  s"  t | }|s
dS |   | |pi }t|dpds dS t|dp'dr3| |ddi dS |  5}| !}t	||}|drV	 W d   W d   dS W d   n1 s`w   Y  W d   n1 sow   Y  t
d||d |d	  | |d
dd |r|| dS )z
    Re-enable pipeline when it was auto-paused for usage and headroom is available again.
    Returns True if the pipeline was resumed by this call.
    Fr   r   r   r   NzFUsage headroom restored for BID %s (%s/%s min); auto-resuming pipeliner   r   r   r   T)r   r    r!   r"   r#   r$   r'   r%   r&   r   r   info)r(   r
   r.   r)   r*   r&   r+   r   r   r   "resume_pipeline_if_usage_availableH   sH   




r0   )r   r.   Dict[str, Any]c             
   C  s  |    g }g }|  }| }|d | pg }W d   n1 s&w   Y  |p.t}|D ]}	t|	dp:d }
|
sAq1t	|	dpHd}t	|	dpQd}|  '}| }t
||
}t||
| W d   n1 srw   Y  W d   n1 sw   Y  |dr|r| |
dd	d
 ||
 ||
 td|
|d |d  q1|r|ds| |
d	dd
 |r||
 ||
 td|
|d |d  q1||dS )zMSupervisor hook: pause exhausted BIDs; resume auto-paused BIDs with headroom.zt
            SELECT bid, pipeline_enabled, usage_limit_paused
            FROM business_pipeline_config
            Nr
    r   r   r   r   r   r   z3Supervisor paused BID %s pipeline (usage %s/%s min)r   r   z4Supervisor resumed BID %s pipeline (usage %s/%s min))pausedresumed)r!   r%   r&   executefetchallr   r   r$   r    r#   r   r	   r'   appendr   r   r/   )r(   r   r.   r3   r4   r*   r&   rowsr,   rowr
   enabledusage_pausedr+   r   r   r    sync_usage_limit_pipeline_statesv   sj   







r<   )r
   r   r   r   )r
   r   r   r   r   r   )r
   r   r.   r   r   r   )r   r   r.   r   r   r1   )__doc__
__future__r   loggingtypingr   r   r   r   r   usage_allocation_utilr   usage_validity_alert_servicer	   	getLogger__name__r   r   r   r   r   r-   r0   r<   r   r   r   r   <module>   s     

21