
                             d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
Z
dZd	Zd
 Zd ZddZd Zd Zd Zy)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                     t        j                  t        | |      |t        |      }t	        |j                         |      S )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                 p    ddj                  |       g}|r |j                  dj                  |             |S )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              #      K   | D ]Q  }t        |      }|d   j                         }|r| (|sdg}|D ]   }t        j                  |      }||d<   | " S yw)a  Splits 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   si      cC H	"--/Hn4'--1%,	"  s   AAc                    i }| j                   r| j                   j                  nd|d<   | j                  |d<   t        |       |d<   g }| j                  3t        | j                        }d|v rT|d   rO|j                  |d          n:| j                  .|j                  t        j                  | j                               dj                  |      |d<   |S )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                     | j                   si nt        | j                   j                        }|j                  d      sdS |d   S )z2Reads the label attributes of the given log entry.r
   unknown_task)resourcer0   labelsget)r5   resource_labelss     r   r.   r.   x   sJ    '00Bg7!/.22  5'45r   c                 X    | si S t        | t              r| S t        j                  |       S )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   