
    &`iL                     B   d dl Z d dlZd dlZd dlZd dl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mZ d dlmZ d dlmZ  ej        e          ZddZd Zd Zd	 Z ej        d
g d          Zej        dfdZej         e	j	                    ej        z   ddfdefdZ dS )    N)ThreadPoolExecutor)get_or_create_event_looprun_background_task)event_consts)async_loop_foreverc                 |   t          j        |           }i }t          t          j                  }|pt          j        D ]}}||v sJ d|             g }|D ]^}t          j        |d| d          rCt           j                            | |          }	| ||	          sI|                    |	           _|r|||<   ~|S )NzInvalid source type: *z*.log)	oslistdirsetr   EVENT_SOURCE_ALLfnmatchpathjoinappend)
	event_dirsource_typesevent_file_filterevent_log_namessource_filesall_source_typessource_typefilesnfs
             {/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/dashboard/modules/event/event_utils.py_get_source_filesr      s    j++OL<899#D|'D 
. 
.....0U0U0U...  	  	 Aq"8k"8"8"899  GLLA..$09J9J19M9M0Q 	.(-L%    c                     	 | d                              dd                               dd          | d<   n+# t          $ r t                              d|            Y nw xY w| S )Nmessagez\n
z\rz$Restore newline for event failed: %s)replace	Exceptionlogger	exception)
event_dicts    r   _restore_newliner'   %   s    My!))%66>>udKK 	9  M M M?LLLLLMs   36 %AAc                 D    t          t          j        |                     S N)r'   jsonloads)	event_strs    r   _parse_liner-   /   s    DJy11222r   c                     g }| D ]b}|s	 t          |          }|                    |           +# t          $ r+ t                              dt          |                     Y _w xY w|S )NzParse event line failed: %s)r-   r   r#   r$   r%   repr)event_string_listeventsdataevents       r   parse_event_stringsr4   3   s    F! H H 		H%%EMM%     	H 	H 	H:DJJGGGGG	HMs   $02A%$A%ReadFileResult)fidsizemtimepositionlinesTc           
      0   t          | d|          5 }t          j        |                                          }|j        p| }g }t          j        |                                dt
          j                  5 }|}	t          |          D ]}
|                    d|	          }|dk    r n||	z
  t          j
        k    r1|                    ||	|                             d                     nWt          dt          j
                  }t                              d	||	|	|z                                d          ||	z
             |d
z   }	d d d            n# 1 swxY w Y   t!          ||j        |j        |	|          cd d d            S # 1 swxY w Y   d S )Nrbclosefdr   )access   
zutf-8d   z$Ignored long string: %s...(%s chars)   )openr
   statfilenost_inommapACCESS_READrangefindr   EVENT_READ_LINE_LENGTH_LIMITr   decodeminr$   warningr5   st_sizest_mtime)fileposn_linesr>   r   rE   r6   r:   mmstart_septruncated_sizes                r   
_read_filerZ   E   s    
dD'	*	*	* Nawqxxzz""k!TYqxxzz1T-=>>> 	 "E7^^    ggeU++"99E;,"KKKLLE#I!5!5g!>!>????%(l.W%X%XNNN>55>#99:AA'JJe  
 a	  	  	  	  	  	  	  	  	  	  	  	  	  	  	   c4<uMM+N N N N N N N N N N N N N N N N N Ns7   A%F8CEFE	FE	FFFmonitor_thread_pool_executorc                 4   	
 t                      i t                              d dn           t          j        dg d          fd
fd	t          |d	          	
 fd
            }t           |                      S )aB  Monitor events in directory. New events will be read and passed to the
    callback.

    Args:
        event_dir: The event log directory.
        callback (def callback(List[str]): pass): A callback accepts a list of
            event strings.
        monitor_thread_pool_executor: A thread pool exector to monitor/update
            events. None means it will use the default execturo which uses
            num_cpus of the machine * 5 threads (before python 3.8) or
            min(32, num_cpus + 5) (from Python 3.8).
        scan_interval_seconds: An interval seconds between two scans.
        start_mtime: Only the event log files whose last modification
            time is greater than start_mtime are monitored.
        monitor_files (Dict[int, MonitorFile]): The map from event log file id
            to MonitorFile object. Monitor all files start from the beginning
            if the value is None.
        source_types (List[str]): A list of source type name from
            event_pb2.Event.SourceType.keys(). Monitor all source types if the
            value is None.
    NzEMonitor events logs modified after %s on %s, the source types are %s.allMonitorFile)r7   r8   r9   c                 B    t          j        |           }|j        k    S r)   )r
   rE   rQ   )source_filerE   start_mtimes     r   _source_file_filterz+monitor_events.<locals>._source_file_filter   s    w{##}{**r   c                    t          | t                    sJ dt          |            d|  d            t          j        | t          j                  }	 t          j        |          }|j        dk    rg t          j        |           S |j	        p| }
                    |          }|ri|j        |j        k    rQ|j        |j        k    rA|j        |j        k    r1t                              d|            g t          j        |           S |j        }nt                              d|            |}t%          ||d          } 	|j        |j        |j                  |j        <                       
|j                   n%# t,          $ r}t-          d	|            |d }~ww xY w	 t          j        |           d S # t          j        |           w xY w)
NzFile should be a str, but a (z) foundr   z4Skip reading the file because there is no change: %szFound new event log file: %sFr=   zRead event file failed: )
isinstancestrtyper
   rD   O_RDONLYrE   rP   closerG   getr9   r7   r8   rQ   r$   debuginforZ   r6   call_soon_threadsafer:   r#   )rR   rS   fdrE   r6   monitor_filer9   rer^   callbackloopmonitor_filess            r   _read_monitor_filez*monitor_events.<locals>._read_monitor_file   s   #
 
 	E 	ED$t**DDtDDD	E 	E 
 WT2;''	72;;D |q  2 HRLLLL1 +%C(,,S11L  )\->>>$)T\99$*dm;;LLNPT    HRLLLL (0:DAAA2x777A#.;qvqw
#K#KM!% %%h8888 	F 	F 	F=t==>>AE	F 9 HRLLLLLBHRLLLLs8    F 
A,F A6F F> 
F$FF$$F> >GT)cancellablec            	      :  K                        t                     d {V } t          j        t          j                  fdt          j        fdt          t          j	        | 
                                           D               d {V  d S )Nc                    K   4 d {V                       | d           d {V cd d d           d {V  S # 1 d {V swxY w Y   d S )Nr   )run_in_executor)filenameru   rs   r[   	semaphores    r   _concurrent_corozGmonitor_events.<locals>._scan_event_log_files.<locals>._concurrent_coro   s              !1102DhPQ                                    s   =
A
Ac                 &    g | ]} |          S  r~   ).0rz   r|   s     r   
<listcomp>zAmonitor_events.<locals>._scan_event_log_files.<locals>.<listcomp>   s4        ! **  r   )ry   r   asyncio	Semaphorer   CONCURRENT_READ_LIMITgatherlist	itertoolschainvalues)	r   r|   r{   ru   rb   r   rs   r[   r   s	    @@r   _scan_event_log_filesz-monitor_events.<locals>._scan_event_log_files   s      "11(
 
 
 
 
 
 
 
 %l&HII		 	 	 	 	 	 	 	 n    $Y_l6I6I6K6K%L M M  
 	
 	
 	
 	
 	
 	
 	
 	
 	
r   )r   r$   rl   collections
namedtupler   r   )r   rr   r[   scan_interval_secondsra   rt   r   r   r^   ru   rb   rs   s   ``` ``` @@@@r   monitor_eventsr   `   s   < $%%D
KKO%<	   (8U8U8UVVK+ + + + +# # # # # # # #J -4@@@
 
 
 
 
 
 
 
 
 A@
6 4466777r   )NN)!r   r   r   r   r*   logging.handlersloggingrH   r
   timeconcurrent.futuresr   ray._common.utilsr   r   ray.dashboard.modules.eventr   ray.dashboard.utilsr   	getLogger__name__r$   r   r'   r-   r4   r   r5   EVENT_READ_LINE_COUNT_LIMITrZ   SCAN_EVENT_DIR_INTERVAL_SECONDSSCAN_EVENT_START_OFFSET_SECONDSr   r~   r   r   <module>r      s                   				  1 1 1 1 1 1 K K K K K K K K 4 4 4 4 4 4 2 2 2 2 2 2		8	$	$   $  3 3 3
 
 
 ('CCC  $?N N N N> 'F	lJJp8 p8 #5p8 p8 p8 p8 p8 p8r   