o
    ö(j  ã                
   @  s¼   d Z ddlmZ ddlZddlZddlZddlZej dej 	ej 	ej 
e¡¡¡¡ ddlmZ ddlmZ ddlmZmZmZ ejejdd e e¡Zddd„Zedkr\eeƒ ƒ‚dS )zIOne-off cleanup: remove or mark sub-min / unprocessed calls in raw_calls.é    )ÚannotationsN)ÚConfig)ÚDatabaseHandler)Úcall_duration_secondsÚpurge_unprocessed_if_below_minÚshould_skip_at_ingestz%%(asctime)s %(levelname)s %(message)s)ÚlevelÚformatÚreturnÚintc                  C  s  t jtd} | jdddd | jdddd	 | jd
tddd |  ¡ }t|jƒ ¡ }t	t
ƒ ƒ}| |¡p5i }tdt| d¡p?dƒƒ}| d¡pK| |¡}|dkrXt d|¡ dS |› d}| ¡ 3}| ¡ }	| |	|¡s{t d|¡ 	 W d   ƒ dS |	 d|› d|jf¡ |	 ¡ pŒg }
W d   ƒ n1 s—w   Y  d}d}|
D ][}|d7 }t|ƒ}t|||ƒs³q¢t|ƒ}t d|| d¡|| d¡| d¡¡ |jrÒ|d7 }q¢| ¡ }| ¡ }	t|	||d |||ƒrê|d7 }| ¡  W d   ƒ n1 søw   Y  q¢t d||||j|¡ dS )N)Údescriptionz--bidTzBusiness ID)ÚrequiredÚhelpz	--dry-runÚ
store_truezReport only; do not modify rows)Úactionr   z--limitiˆ  zMax rows to scan)ÚtypeÚdefaultr   r   Úmin_call_duration_sÚmin_call_duration_effective_atz>BID %s has no min_call_duration_s configured; nothing to cleanÚ
_raw_callszTable %s does not existé   zŽ
            SELECT callid, call_starttime, call_endtime, duration_seconds,
                   transcription_status, status
            FROM `z”`
            WHERE COALESCE(transcription_status, '') != 'skipped_short'
            ORDER BY call_starttime DESC
            LIMIT %s
            z=BID %s call %s duration=%ss status=%s transcription_status=%sÚcallidÚstatusÚtranscription_statusz8BID %s: scanned=%s acted=%s dry_run=%s min_duration_s=%s)ÚargparseÚArgumentParserÚ__doc__Úadd_argumentr   Ú
parse_argsÚstrÚbidÚstripr   r   Úget_pipeline_configÚmaxÚgetÚ ensure_min_duration_effective_atÚloggerÚinfoÚget_connectionÚcursorÚ_table_existsÚerrorÚexecuteÚlimitÚfetchallÚdictr   r   Údry_runr   Úcommit)ÚparserÚargsr    ÚdbÚcfgÚmin_sÚeffective_atÚ	raw_tableÚconnr)   ÚrowsÚscannedÚactedÚrowÚcall_rowÚduration© r@   ú*scripts/cleanup_short_unprocessed_calls.pyÚmain   s‚   


üý÷ïú
ÿ
ú€úrB   Ú__main__)r
   r   )r   Ú
__future__r   r   ÚloggingÚosÚsysÚpathÚinsertÚdirnameÚabspathÚ__file__Úconfigr   Ú
db_handlerr   Úmin_duration_utilr   r   r   ÚbasicConfigÚINFOÚ	getLoggerÚ__name__r&   rB   Ú
SystemExitr@   r@   r@   rA   Ú<module>   s    &

L
ÿ