
    jm              	         d dl mZ d dlZ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
mZmZmZmZmZmZmZ  ed e ej        dd          pd	                    Zd
ZddZddZddZddZddZdddZdddZddZdS )     )annotationsN)datetime)Any)CLIP_VIT_B32_PATHFACE_MODEL_DETFACE_MODEL_RECMOBILECLIP_TS_PATHMOBILECLIP2_TS_PATHMODEL_REGISTRY
OUTPUT_DIRSQLITE_DB_PATH<   HEALTH_STALE_TASK_SECONDS21600i`T     returnsqlite3.Connectionc                     t           j                            t                    } | rt          j        | d           t          j        t          d          }t
          j        |_        |	                    d           |S )NTexist_okr   )timeoutzPRAGMA busy_timeout=5000)
ospathdirnamer   makedirssqlite3connectRowrow_factoryexecute)parentconns     //home/longshao/multi-rider-rag/shared/health.py_connect_sqliter$      sh    W__^,,F +
FT****?>1555D{DLL+,,,K    dict[str, Any]c                 ~   dt           ddd} 	 t                      5 }|                    d                                           d| d<   |                    d           |                                 d| d<   d d d            n# 1 swxY w Y   n.# t
          $ r!}d| d<   t          |          | d	<   Y d }~nd }~ww xY w| S )
NTF)okr   read_okwrite_okzSELECT 1r)   zBEGIN IMMEDIATEr*   r(   error)r   r$   r    fetchonerollback	Exceptionstr)checkr"   excs      r#   _check_sqliter2   $   s   	 E	" 	%$LL$$--///#E)LL*+++MMOOO $E*	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%  " " "dSg" Ls;   B AB7B BB 
BB 
B:B55B:c                    dt           ddd} d}	 t          j        t           d           t          j                            t                     | d<   t          j        ddt           	          \  }}t          j        |d
          5 }|                    d           d d d            n# 1 swxY w Y   d| d<   n.# t          $ r!}d| d<   t          |          | d<   Y d }~nd }~ww xY w|rEt          j                            |          r&	 t          j        |           n^# t          $ r Y nRw xY wnM# |rEt          j                            |          r'	 t          j        |           w # t          $ r Y w w xY ww w xY w| S )NTF)r(   r   existswritable r   r4   z	.healthz_z.tmp)prefixsuffixdirwbs   okr5   r(   r+   )r   r   r   r   isdirtempfilemkstempfdopenwriter.   r/   isfileremoveOSError)r0   
probe_pathfdfhr1   s        r#   _check_output_dirrF   8   s   	 E J
J....'--
33h!)VQ[\\\JYr4   	BHHUOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 j " " "dSg"  	"'..44 		*%%%%     	"'..44 		*%%%%   	 	
 Ls   A6B9 B(B9 (B,,B9 /B,0B9 8D/ 9
C$CD/ C$$D/ 	D 
D+*D+/"E9E'&E9'
E41E93E44E9list[tuple[str, str]]c                     d t          t          j                              D             } |                     dt          fdt
          fdt          fdt          fdt          fg           | S )Nc                "    g | ]\  }}d | |fS )zmodel. ).0keyr   s      r#   
<listcomp>z"_model_entries.<locals>.<listcomp>T   s'    VVV)#t%VVVr%   zmodel.mobileclipzmodel.mobileclip2zmodel.clip_vit_b32zmodel.face_detzmodel.face_rec)	sortedr   itemsextendr	   r
   r   r   r   )entriess    r#   _model_entriesrR   S   ss    VVvn>R>T>T7U7UVVVGNN!34 "56!#45~.~.	
   Nr%   c                 t   g } g }t                      D ]\  }}t          j                            t	          |pd                    }|                    ||d           |rt          j                            |          s|                     ||d           |  t          |          t          |           | dS )Nr6   )rL   r   )r(   checked_countmissing_countmissing)rR   r   r   abspathr/   appendr@   len)rV   checkedlabelr   
normalizeds        r#   _check_modelsr]   a   s    $&G$&G%'' ? ?tW__S__55
uj99::: 	?
!;!; 	?NN5*==>>>kWW	  r%   now_ts
int | Nonec                   t          | pt          j                              } | t          z
  }dt          ddg d}	 t                      5 }|                    d                                          }|                    d|f                                          }|                    d|t          f                                          }d d d            n# 1 swxY w Y   t          ||d         nd          |d<   t          ||d         nd          |d	<   d
 |D             |d<   |d	         dk    |d<   n.# t          $ r!}d|d<   t          |          |d<   Y d }~nd }~ww xY w|S )NTr   )r(   stale_after_secondsrunning_countstale_running_countsample_task_idsz?SELECT COUNT(*) AS total FROM task_queue WHERE status='running'z
                SELECT COUNT(*) AS total
                FROM task_queue
                WHERE status='running'
                  AND (claimed_ts IS NULL OR claimed_ts < ?)
                a  
                SELECT id
                FROM task_queue
                WHERE status='running'
                  AND (claimed_ts IS NULL OR claimed_ts < ?)
                ORDER BY COALESCE(claimed_ts, 0) ASC, id ASC
                LIMIT ?
                totalrb   rc   c                    g | ]
}|d          S )idrJ   )rK   rows     r#   rM   z%_check_task_queue.<locals>.<listcomp>   s    #E#E#E#CI#E#E#Er%   rd   r(   Fr+   )
inttimer   r$   r    r,   HEALTH_SAMPLE_LIMITfetchallr.   r/   )r^   cutoffr0   r"   running_row	stale_rowsample_rowsr1   s           r#   _check_task_queuerq   q   s   &49;;''F//F8  E " 	$,,Q hjj   	  hjj  ,, ,-
 
 hjj 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	2 "%[=T[%9%9Z[!\!\o'*AV9W+=+=\]'^'^#$#E#E#E#E#E 12a7d " " "dSg" Ls=   D7 	B C	D7 CD7 CAD7 7
E"EE"c                   t          | pt          j                              } t                      t                      t	                      t          |           d}t          d |                                D                       | |dS )N)sqlite
output_dirmodels
task_queuec              3  @   K   | ]}|                     d           V  dS )r(   N)get)rK   r0   s     r#   	<genexpr>z$get_health_report.<locals>.<genexpr>   s,      ??e%))D//??????r%   )r(   	timestampchecks)ri   rj   r2   rF   r]   rq   allvalues)r^   r{   s     r#   get_health_reportr~      s    &49;;''F//'))//'//	 F ??v}}?????  r%   dict[str, str]c                     ddl m}  ddlm} d}	  | d           d}n# t          $ r d}Y nw xY w|rdnd|rd	nd
 |            rdndt          j                                                    dS )Nr   )	query_one)is_scheduler_runningFzSELECT 1 AS okTr(   degraded	connecteddisconnectedrunningstopped)statusdb	schedulerrz   )shared.db.kingbaser   shared.schedulerr   r.   r   utcnow	isoformat)r   r   db_oks      r#   get_api_health_reportr      s    ,,,,,,555555E	"###     /$$Z"6kk"6"6"8"8GYYi_&&0022	  s    --)r   r   )r   r&   )r   rG   )N)r^   r_   r   r&   )r   r   )
__future__r   r   r   r<   rj   r   typingr   shared.config.configr   r   r   r	   r
   r   r   r   maxri   getenvr   rk   r$   r2   rF   rR   r]   rq   r~   r   rJ   r%   r#   <module>r      s   " " " " " " 				               	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  CCC		2Mw(W(W(`[`$a$abb        (   6       + + + + +\         r%   