
                             S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SSK
r
SrS	rS
 rS rSS jrS rS rS rg)z.Utilities for interacting with streaming logs.    )absolute_import)division)unicode_literalsN)encoding)streamzTvalue(severity,timestamp.date("%Y-%m-%d %H:%M:%S %z",tz="LOCAL"), task_name,message)
   c                 z    [         R                  " [        XS9U[        US9n[	        UR                  5       U5      $ )a  Returns the streaming log of the job by id.

Args:
  name: string id of the entity.
  continue_function: One-arg function that takes in the number of empty polls
    and outputs a boolean to decide if we should keep polling or not. If not
    given, keep polling indefinitely.
  polling_interval: amount of time to sleep between each poll.
  task_name: String name of task.
  allow_multiline: Tells us if logs with multiline messages are okay or not.
)	task_name)filterspolling_intervalcontinue_intervalcontinue_func)r   
LogFetcher_LogFilters_CONTINUE_INTERVAL_SplitMultiline	YieldLogs)namecontinue_functionr   r
   allow_multilinelog_fetchers         -lib/googlecloudsdk/command_lib/ai/log_util.py
StreamLogsr   $   s=     !!$4'*%	'+
 
..0/	BB    c                 z    SSR                  U 5      /nU(       a   UR                  SR                  U5      5        U$ )zReturns filters for log fetcher to use.

Args:
  name: string id of the entity.
  task_name: String name of task.

Returns:
  A list of filters to be passed to the logging API.
zresource.type="ml_job"zresource.labels.job_id="{0}"zresource.labels.task_name="{0}")formatappend)r   r
   r   s      r   r   r   9   s>      > E Ed K' NN4;;IFG	.r   c              #      #    U  H_  n[        U5      nUS   R                  5       nU(       a  Uv   M.  U(       d  S/nU H!  n[        R                  " U5      nXVS'   Uv   M#     Ma     g7f)zSplits the dict output of logs into multiple lines.

Args:
  log_generator: iterator that returns a an ml log in dict format.
  allow_multiline: Tells us if logs with multiline messages are okay or not.

Yields:
  Single-line ml log dictionaries.
message N)_EntryToDict
splitlinescopydeepcopy)log_generatorr   loglog_dictmessagesr   single_line_logs          r   r   r   K   sf      cC H	"--/Hn4'--1%,	"  s   A'A)c                    0 nU R                   (       a  U R                   R                  OSUS'   U R                  US'   [        U 5      US'   / nU R                  b:  [        U R                  5      nSU;   a  US   (       a  UR                  US   5        O<U R                  b/  UR                  [        R                  " U R                  5      5        SR                  U5      US'   U$ )z%Converts a log entry to a dictionary.DEFAULTseverity	timestampr
   r   r    )r,   r   r-   _GetTaskNamejsonPayload_ToDictr   textPayloadsix	text_typejoin)	log_entryoutputr   	json_datas       r   r!   r!   c   s    &/8/A/AI&&++y 	!++&$Y/&'&	--.I I	9	y+,(NN3==!6!678ggg&&	-r   c                     U R                   (       d  0 O[        U R                   R                  5      nUR                  S5      (       d  S$ US   $ )z2Reads the label attributes of the given log entry.r
   unknown_task)resourcer0   labelsget)r5   resource_labelss     r   r.   r.   x   sO    '00Bg7!/.22    5'45r   c                 n    U (       d  0 $ [        U [        5      (       a  U $ [        R                  " U 5      $ )N)
isinstancedictr   MessageToDict)r   s    r   r0   r0      s-    	IN!!'**r   )F)__doc__
__future__r   r   r   r#   apitools.base.pyr   googlecloudsdk.command_lib.logsr   r2   
LOG_FORMATr   r   r   r   r!   r.   r0    r   r   <module>rH      sM    5 &  '  % 2 

  C*$0*5+r   